1
|
|
|
"""Unit test module for Annif CLI commands""" |
2
|
|
|
|
3
|
|
|
import random |
4
|
|
|
import re |
5
|
|
|
from click.testing import CliRunner |
6
|
|
|
import annif.cli |
7
|
|
|
|
8
|
|
|
runner = CliRunner() |
9
|
|
|
|
10
|
|
|
# Generate a random project name to use in tests |
11
|
|
|
TEMP_PROJECT = ''.join( |
12
|
|
|
random.choice('abcdefghiklmnopqrstuvwxyz') for _ in range(8)) |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def test_list_projects(): |
16
|
|
|
result = runner.invoke(annif.cli.run_list_projects) |
17
|
|
|
assert not result.exception |
18
|
|
|
assert result.exit_code == 0 |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def test_list_projects_bad_arguments(): |
22
|
|
|
# The listprojects function does not accept any arguments, it should fail |
23
|
|
|
# if such are provided. |
24
|
|
|
assert runner.invoke(annif.cli.run_list_projects, ['moi']).exit_code != 0 |
25
|
|
|
assert runner.invoke( |
26
|
|
|
annif.cli.run_list_projects, ['moi', '--debug', 'y']).exit_code != 0 |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_show_project(): |
30
|
|
|
assert runner.invoke( |
31
|
|
|
annif.cli.run_show_project, |
32
|
|
|
[TEMP_PROJECT]).exit_code != 0 |
33
|
|
|
# Test should not fail even if the user queries for a non-existent project. |
34
|
|
|
failed_result = runner.invoke(annif.cli.run_show_project, ['nonexistent']) |
35
|
|
|
assert failed_result.exception |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def test_list_subjects(): |
39
|
|
|
pass |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_show_subject(): |
43
|
|
|
pass |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_load(): |
47
|
|
|
pass |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def test_drop_subject(): |
51
|
|
|
pass |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def test_analyze(): |
55
|
|
|
result = runner.invoke( |
56
|
|
|
annif.cli.run_analyze, |
57
|
|
|
['myproject-fi'], |
58
|
|
|
input='kissa') |
59
|
|
|
assert not result.exception |
60
|
|
|
assert result.output == "1.0\t<http://example.org/dummy>\tdummy\n" |
61
|
|
|
assert result.exit_code == 0 |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_analyze_nonexistent(): |
65
|
|
|
result = runner.invoke( |
66
|
|
|
annif.cli.run_analyze, |
67
|
|
|
[TEMP_PROJECT], |
68
|
|
|
input='kissa') |
69
|
|
|
assert result.exception |
70
|
|
|
assert result.output == "No projects found with id '{}'.\n".format( |
71
|
|
|
TEMP_PROJECT) |
72
|
|
|
assert result.exit_code != 0 |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_eval_label(tmpdir): |
76
|
|
|
keyfile = tmpdir.join('dummy.key') |
77
|
|
|
keyfile.write("dummy\nanother\n") |
78
|
|
|
|
79
|
|
|
result = runner.invoke( |
80
|
|
|
annif.cli.run_eval, [ |
81
|
|
|
'myproject-en', str(keyfile)], input='nothing special') |
82
|
|
|
assert not result.exception |
83
|
|
|
assert result.exit_code == 0 |
84
|
|
|
|
85
|
|
|
precision = re.search('Precision:\s+(\d.\d+)', result.output) |
86
|
|
|
print(precision.group(1)) |
87
|
|
|
assert float(precision.group(1)) == 1.0 |
88
|
|
|
recall = re.search('Recall:\s+(\d.\d+)', result.output) |
89
|
|
|
assert float(recall.group(1)) == 0.5 |
90
|
|
|
f_measure = re.search('F-measure:\s+(\d.\d+)', result.output) |
91
|
|
|
assert float(f_measure.group(1)) > 0.66 |
92
|
|
|
assert float(f_measure.group(1)) < 0.67 |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_eval_uri(tmpdir): |
96
|
|
|
keyfile = tmpdir.join('dummy.key') |
97
|
|
|
keyfile.write( |
98
|
|
|
"<http://example.org/one>\tone\n<http://example.org/dummy>\tdummy\n") |
99
|
|
|
|
100
|
|
|
result = runner.invoke( |
101
|
|
|
annif.cli.run_eval, [ |
102
|
|
|
'myproject-en', str(keyfile)], input='nothing special') |
103
|
|
|
assert not result.exception |
104
|
|
|
assert result.exit_code == 0 |
105
|
|
|
|
106
|
|
|
precision = re.search('Precision:\s+(\d.\d+)', result.output) |
107
|
|
|
assert float(precision.group(1)) == 1.0 |
108
|
|
|
recall = re.search('Recall:\s+(\d.\d+)', result.output) |
109
|
|
|
assert float(recall.group(1)) == 0.5 |
110
|
|
|
f_measure = re.search('F-measure:\s+(\d.\d+)', result.output) |
111
|
|
|
assert float(f_measure.group(1)) > 0.66 |
112
|
|
|
assert float(f_measure.group(1)) < 0.67 |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
def test_evaldir(tmpdir): |
116
|
|
|
tmpdir.join('doc1.txt').write('doc1') |
117
|
|
|
tmpdir.join('doc1.key').write('dummy') |
118
|
|
|
tmpdir.join('doc2.txt').write('doc2') |
119
|
|
|
tmpdir.join('doc2.key').write('none') |
120
|
|
|
tmpdir.join('doc3.txt').write('doc3') |
121
|
|
|
|
122
|
|
|
result = runner.invoke( |
123
|
|
|
annif.cli.run_evaldir, [ |
124
|
|
|
'myproject-en', str(tmpdir)]) |
125
|
|
|
assert not result.exception |
126
|
|
|
assert result.exit_code == 0 |
127
|
|
|
|
128
|
|
|
precision = re.search('Precision:\s+(\d.\d+)', result.output) |
129
|
|
|
assert float(precision.group(1)) == 0.5 |
130
|
|
|
recall = re.search('Recall:\s+(\d.\d+)', result.output) |
131
|
|
|
assert float(recall.group(1)) == 0.5 |
132
|
|
|
f_measure = re.search('F-measure:\s+(\d.\d+)', result.output) |
133
|
|
|
assert float(f_measure.group(1)) == 0.5 |
134
|
|
|
|