1
|
|
|
import nose.tools as nt |
|
|
|
|
2
|
|
|
import logging |
3
|
|
|
|
4
|
|
|
from topik.fileio.in_elastic import read_elastic |
5
|
|
|
from topik.fileio.project import TopikProject |
6
|
|
|
from topik.fileio.tests import test_data_path |
7
|
|
|
from ._solutions import solution_elastic |
8
|
|
|
from elasticsearch.exceptions import ConnectionError |
|
|
|
|
9
|
|
|
from nose.plugins.skip import SkipTest |
|
|
|
|
10
|
|
|
|
11
|
|
|
INDEX = "test_elastic" |
12
|
|
|
|
13
|
|
|
# make logging quiet during testing, to keep Travis CI logs short. |
14
|
|
|
|
15
|
|
|
logging.basicConfig() |
16
|
|
|
logging.getLogger('elasticsearch').setLevel(logging.ERROR) |
|
|
|
|
17
|
|
|
logging.getLogger('urllib3').setLevel(logging.ERROR) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def test_elastic_import(): |
|
|
|
|
21
|
|
|
output_args = {'source': 'localhost', |
22
|
|
|
'index': INDEX, |
23
|
|
|
'content_field': 'abstract'} |
24
|
|
|
# import data from file into known elastic server |
25
|
|
|
project = TopikProject("test_project", output_type='ElasticSearchOutput', |
26
|
|
|
output_args=output_args) |
27
|
|
|
|
28
|
|
|
try: |
29
|
|
|
project.read_input('{}/test_data_json_stream.json'.format( |
30
|
|
|
test_data_path), content_field="abstract")#, |
|
|
|
|
31
|
|
|
#output_type=elastic.ElasticSearchOutput.class_key(), |
32
|
|
|
#output_args=output_args, synchronous_wait=30) |
33
|
|
|
except ConnectionError: |
|
|
|
|
34
|
|
|
raise SkipTest("Skipping Elasticsearch test - elasticsearch not running") |
35
|
|
|
|
36
|
|
|
loaded_corpus = read_elastic("localhost", index=INDEX) |
37
|
|
|
solution_found = False |
38
|
|
|
for doc in list(iter(loaded_corpus)): |
39
|
|
|
if solution_elastic == doc['abstract']: |
|
|
|
|
40
|
|
|
solution_found = True |
41
|
|
|
break |
42
|
|
|
nt.assert_true(solution_found) |
43
|
|
|
|
44
|
|
|
# tear-down |
45
|
|
|
from elasticsearch import Elasticsearch |
46
|
|
|
instance = Elasticsearch("localhost") |
47
|
|
|
if instance.indices.exists(INDEX): |
48
|
|
|
instance.indices.delete(INDEX) |
49
|
|
|
|
50
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.