-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathconftest.py
160 lines (121 loc) · 4.38 KB
/
conftest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""common fixtures for use by all test classes"""
import os
import shutil
import unittest.mock
import py.path
import pytest
import annif
import annif.analyzer
import annif.corpus
import annif.project
import annif.registry
@pytest.fixture(scope="module")
def cxapp():
# make sure the dummy vocab is in place because many tests depend on it
subjfile = os.path.join(os.path.dirname(__file__), "corpora", "dummy-subjects.csv")
cxapp = annif.create_app(config_name="annif.default_config.TestingConfig")
with cxapp.app.app_context():
project = annif.registry.get_project("dummy-en")
# the vocab is needed for both English and Finnish language projects
vocab = annif.corpus.SubjectFileCSV(subjfile)
project.vocab.load_vocabulary(vocab)
return cxapp
@pytest.fixture(scope="module")
def app(cxapp):
return cxapp.app
@pytest.fixture(scope="module")
def app_with_initialize():
cxapp = annif.create_app(config_name="annif.default_config.TestingInitializeConfig")
return cxapp.app
@pytest.fixture(scope="module")
@unittest.mock.patch.dict(os.environ, {"ANNIF_PROJECTS_INIT": ".*-fi"})
def app_with_initialize_fi_projects():
cxapp = annif.create_app(config_name="annif.default_config.TestingInitializeConfig")
return cxapp.app
@pytest.fixture
def app_client(cxapp):
with cxapp.test_client() as app_client:
yield app_client
@pytest.fixture(scope="module")
def registry(app):
with app.app_context():
return app.annif_registry
@pytest.fixture(scope="module")
def datadir(tmpdir_factory):
return tmpdir_factory.mktemp("data")
@pytest.fixture(scope="module")
def testdatadir(app):
"""a fixture to access the tests/data directory as a py.path.local
object"""
with app.app_context():
dir = py.path.local(app.config["DATADIR"])
# clean up previous state of datadir
shutil.rmtree(os.path.join(str(dir), "projects"), ignore_errors=True)
return dir
@pytest.fixture(scope="module")
def subject_file():
docfile = os.path.join(
os.path.dirname(__file__), "corpora", "archaeology", "subjects.tsv"
)
return annif.corpus.SubjectFileTSV(docfile, "fi")
@pytest.fixture(scope="module")
def dummy_subject_index(testdatadir):
"""a fixture to access the subject index of the dummy vocabulary"""
vocab = annif.vocab.AnnifVocabulary("dummy", testdatadir)
return vocab.subjects
@pytest.fixture(scope="module")
def vocabulary(datadir):
vocab = annif.vocab.AnnifVocabulary("my-vocab", datadir)
subjfile = os.path.join(
os.path.dirname(__file__), "corpora", "archaeology", "yso-archaeology.ttl"
)
subjects = annif.corpus.SubjectFileSKOS(subjfile)
vocab.load_vocabulary(subjects)
return vocab
@pytest.fixture(scope="module")
def subject_index(vocabulary):
return vocabulary.subjects
@pytest.fixture(scope="module")
def document_corpus(subject_index):
docfile = os.path.join(
os.path.dirname(__file__), "corpora", "archaeology", "documents.tsv"
)
doc_corpus = annif.corpus.DocumentFile(docfile, subject_index)
return doc_corpus
@pytest.fixture(scope="module")
def fulltext_corpus(subject_index):
ftdir = os.path.join(
os.path.dirname(__file__), "corpora", "archaeology", "fulltext"
)
ft_corpus = annif.corpus.DocumentDirectory(
ftdir, subject_index, "fi", require_subjects=True
)
return ft_corpus
@pytest.fixture(scope="module")
def pretrained_vectors():
return py.path.local(
os.path.join(
os.path.dirname(__file__), "corpora", "archaeology", "fasttext.vec"
)
)
@pytest.fixture(scope="module")
def project(subject_index, datadir, registry, vocabulary):
proj = unittest.mock.Mock()
proj.analyzer = annif.analyzer.get_analyzer("snowball(finnish)")
proj.language = "fi"
proj.vocab = vocabulary
proj.vocab_lang = "fi"
proj.subjects = subject_index
proj.datadir = str(datadir)
proj.registry = registry
return proj
@pytest.fixture(scope="module")
def app_project(app):
with app.app_context():
dir = py.path.local(app.config["DATADIR"])
shutil.rmtree(os.path.join(str(dir), "projects"), ignore_errors=True)
return annif.registry.get_project("dummy-en")
@pytest.fixture(scope="function")
def empty_corpus(tmpdir, subject_index):
empty_file = tmpdir.ensure("empty.tsv")
return annif.corpus.DocumentFile(str(empty_file), subject_index)