-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_transform.py
59 lines (41 loc) · 2.16 KB
/
test_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Unit tests for the input-transforms in Annif"""
import pytest
import annif.transform
from annif.exception import ConfigurationException
from annif.transform import parse_specs
def test_parse_specs():
parsed = parse_specs("foo, bar(42,43,key=abc)")
assert parsed == [("foo", [], {}), ("bar", ["42", "43"], {"key": "abc"})]
def test_get_transform_nonexistent():
with pytest.raises(ConfigurationException):
annif.transform.get_transform("nonexistent", project=None)
def test_get_transform_badspec(project):
with pytest.raises(ConfigurationException):
annif.transform.get_transform("pass(invalid_argument)", project)
def test_input_limiter():
transf = annif.transform.get_transform("limit(3)", project=None)
assert transf.transform_text("running") == "run"
def test_input_limiter_with_negative_value(project):
with pytest.raises(ConfigurationException):
annif.transform.get_transform("limit(-2)", project)
def test_chained_transforms_text():
transf = annif.transform.get_transform("limit(5),pass,limit(3),", project=None)
assert transf.transform_text("abcdefghij") == "abc"
# Check with a more arbitrary transform function
reverser = annif.transform.transform.IdentityTransform(None)
reverser.transform_fn = lambda x: x[::-1]
transf.transforms.append(reverser)
assert transf.transform_text("abcdefghij") == "cba"
def test_chained_transforms_corpus(document_corpus):
transf = annif.transform.get_transform("limit(5),pass,limit(3),", project=None)
transformed_corpus = transf.transform_corpus(document_corpus)
for transf_doc, doc in zip(transformed_corpus.documents, document_corpus.documents):
assert transf_doc.text == doc.text[:3]
assert transf_doc.subject_set == doc.subject_set
# Check with a more arbitrary transform function
reverser = annif.transform.transform.IdentityTransform(None)
reverser.transform_fn = lambda x: x[::-1]
transf.transforms.append(reverser)
for transf_doc, doc in zip(transformed_corpus.documents, document_corpus.documents):
assert transf_doc.text == doc.text[:3][::-1]
assert transf_doc.subject_set == doc.subject_set