From 9a11779168d06bb972ece7ed99d0ec6cd75d5096 Mon Sep 17 00:00:00 2001
From: Grzegorz Kostkowski <grzegorz.kostkowski@pwr.edu.pl>
Date: Wed, 9 Mar 2022 10:08:44 +0100
Subject: [PATCH] Add test script with data and verify that implemented feature
 works

Test script contains function `remove_attribute` which can be
placed in cclutils package to provide wrapper for this new
functionality.

Test script has been implemented, but it is not automated (CI/CD).
It was not automated because it looks like corpus2 tests are not
maintained (not included in CI/CD and make test informs about
failed tests) so not sure where new tests should be "attached".
---
 src/tests/python/data/ccl03.xml      | 16 ++++++++++++
 src/tests/python/test_remove_attr.py | 39 ++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 src/tests/python/data/ccl03.xml
 create mode 100644 src/tests/python/test_remove_attr.py

diff --git a/src/tests/python/data/ccl03.xml b/src/tests/python/data/ccl03.xml
new file mode 100644
index 0000000..d9e41b1
--- /dev/null
+++ b/src/tests/python/data/ccl03.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE chunkList SYSTEM "ccl.dtd">
+<chunkList>
+ <chunk id="ch1">
+  <sentence id="s1">
+   <tok>
+    <orth>Kofeina</orth>
+    <lex disamb="1"><base>kofeina</base><ctag>subst:sg:nom:f</ctag></lex>
+    <prop key="e01">http://plwordnet.pwr.wroc.pl/wordnet/synset/15470</prop>
+    <prop key="sense:ukb:syns_id">15470</prop>
+    <prop key="sense:ukb:syns_rank">15470/30.0403943450</prop>
+    <prop key="sense:ukb:unitsstr">kofeina.1(24:sbst)</prop>
+   </tok>
+  </sentence>
+ </chunk>
+</chunkList>
diff --git a/src/tests/python/test_remove_attr.py b/src/tests/python/test_remove_attr.py
new file mode 100644
index 0000000..fdc79d0
--- /dev/null
+++ b/src/tests/python/test_remove_attr.py
@@ -0,0 +1,39 @@
+import cclutils as ccl
+
+
+def remove_attribute(token, key):
+    """
+    Remove attribute of a token.
+
+    Args:
+        token (Corpus2.token)
+        key (object): Attribute name, automatically casted to string.
+
+    Returns:
+        bool value informing about the status of the removal operation: `True` if
+        attribute has been removed. If token has no specified key or any
+        metadata at all, `False` will be returned.
+    """
+    if token.has_metadata():
+        metadata = token.get_metadata()
+        return metadata.remove_attribute(key)
+    return False
+
+
+test_doc = "data/ccl03.xml"
+rm_prop = "e01"
+doc = ccl.read(test_doc)
+
+tok = [
+    t for p in doc.paragraphs() for s in p.sentences() for t in s.tokens()
+].pop()
+
+# initial check
+assert ccl.get_attribute(tok, rm_prop) == "http://plwordnet.pwr.wroc.pl/wordnet/synset/15470"
+
+# test
+assert remove_attribute(tok, rm_prop) == True
+assert remove_attribute(tok, "not-existing-prop") == False
+assert ccl.get_attribute(tok, rm_prop, None) == None
+
+print("All tests passed!")
-- 
GitLab