forked from tatiana/estester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_querytestcase.py
171 lines (145 loc) · 4.92 KB
/
test_querytestcase.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
161
162
163
164
165
166
167
168
169
170
171
import unittest
from mock import patch
from estester import ElasticSearchQueryTestCase, ExtendedTestCase,\
MultipleIndexesQueryTestCase
SIMPLE_QUERY = {
"query": {
"query_string": {
"fields": [
"name"
],
"query": "nina"
}
}
}
def raise_interruption(self):
raise KeyboardInterrupt
class TestExtendedTestCase(ExtendedTestCase):
value = 1
def _pre_setup(self):
self.value = self.value * 2
def setUp(self):
self.value = self.value ** 3
def test_pre_setup(self):
self.assertEqual(self.value, 8)
class DefaultValuesTestCase(unittest.TestCase):
def test_default_values(self):
ESQTC = ElasticSearchQueryTestCase
self.assertEqual(ESQTC.index, "sample.test")
self.assertEqual(ESQTC.reset_index, True)
self.assertEqual(ESQTC.host, "http://0.0.0.0:9200/")
self.assertEqual(ESQTC.fixtures, [])
self.assertEqual(ESQTC.timeout, 5)
self.assertEqual(ESQTC.proxies, {})
class SimpleMultipleIndexesQueryTestCase(MultipleIndexesQueryTestCase):
data = {
"personal": {
"fixtures": [
{
"type": "contact",
"id": "1",
"body": {"name": "Dmitriy"}
},
{
"type": "contact",
"id": "2",
"body": {"name": "Agnessa"}
}
]
},
"professional": {
"fixtures": [
{
"type": "contact",
"id": "1",
"body": {"name": "Nikolay"}
}
]
}
}
def test_search_all_indexes(self):
response = self.search()
expected = [
{
u'_score': 1.0,
u'_type': u'contact',
u'_id': u'1',
u'_source': {u'name': u'Dmitriy'},
u'_index': u'personal'
},
{
u'_score': 1.0,
u'_type': u'contact',
u'_id': u'2',
u'_source': {u'name': u'Agnessa'},
u'_index': u'personal'
},
{
u'_score': 1.0,
u'_type': u'contact',
u'_id': u'1', u'_source': {u'name': u'Nikolay'},
u'_index': u'professional'
}
]
self.assertEqual(response["hits"]["total"], 3)
self.assertEqual(sorted(response["hits"]["hits"]), sorted(expected))
def test_search_one_index_that_has_item(self):
query = {
"query": {
"match": {
"name": "Agnessa"
}
}
}
response = self.search_in_index("personal", query)
self.assertEqual(response["hits"]["total"], 1)
expected = {u'name': u'Agnessa'}
self.assertEqual(response["hits"]["hits"][0]["_source"], expected)
def test_search_one_index_that_doesnt_have_item(self):
query = {
"query": {
"match": {
"name": "Agnessa"
}
}
}
response = self.search_in_index("professional", query)
self.assertEqual(response["hits"]["total"], 0)
class SimpleQueryTestCase(ElasticSearchQueryTestCase):
fixtures = [
{
"type": "dog",
"id": "1",
"body": {"name": "Nina Fox"}
},
{
"type": "dog",
"id": "2",
"body": {"name": "Charles M."}
}
]
timeout = 1
def test_search_by_nothing_returns_two_results(self):
response = self.search()
expected = {u"name": u"Nina Fox"}
self.assertEqual(response["hits"]["total"], 2)
self.assertEqual(response["hits"]["hits"][0]["_id"], u"1")
self.assertEqual(response["hits"]["hits"][1]["_id"], u"2")
def test_search_by_nina_returns_one_result(self):
response = self.search(SIMPLE_QUERY)
expected = {u"name": u"Nina Fox"}
self.assertEqual(response["hits"]["total"], 1)
self.assertEqual(response["hits"]["hits"][0]["_id"], u"1")
self.assertEqual(response["hits"]["hits"][0]["_source"], expected)
def test_tokenize_with_default_analyzer(self):
response = self.tokenize("Nothing to declare", "default")
items_list = response["tokens"]
self.assertEqual(len(items_list), 2)
tokens = [item["token"] for item in items_list]
self.assertEqual(sorted(tokens), ["declare", "nothing"])
def test_tokenize_with_default_analyzer(self):
response = self.tokenize("Nothing to declare", "whitespace")
items_list = response["tokens"]
self.assertEqual(len(items_list), 3)
tokens = [item["token"] for item in items_list]
self.assertEqual(sorted(tokens), ['"Nothing', 'declare"', "to"])