Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit e4db416

Browse files
authored
Fix CI (#16)
- Locked down versions - Making sure matchers work
1 parent b86be18 commit e4db416

File tree

2 files changed

+113
-75
lines changed

2 files changed

+113
-75
lines changed

.circleci/config.yml

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
2-
32
version: 2.1
43

54
executors:
6-
python-27: {docker: [{image: 'python:2.7'}]}
7-
python-35: {docker: [{image: 'python:3.5'}]}
8-
python-36: {docker: [{image: 'python:3.6'}]}
9-
python-37: {docker: [{image: 'python:3.7'}]}
5+
python-27: { docker: [{ image: "python:2.7.16" }] }
6+
python-35: { docker: [{ image: "python:3.5.7" }] }
7+
python-36: { docker: [{ image: "python:3.6.9" }] }
8+
python-37: { docker: [{ image: "python:3.7.4" }] }
109

1110
jobs:
1211
build:
@@ -16,31 +15,31 @@ jobs:
1615
executor: << parameters.executor >>
1716
working_directory: ~/repo
1817
steps:
19-
- checkout
20-
- restore_cache:
21-
keys:
22-
- v1-dependencies-{{ .Environment.CIRCLE_JOB }}-{{ checksum "requirements.txt" }}
23-
- run:
24-
name: install dependencies
25-
command: |
26-
pip install virtualenv
27-
virtualenv venv
28-
. venv/bin/activate
29-
python setup.py install
30-
- save_cache:
31-
paths:
32-
- ./venv
33-
key: v1-dependencies-{{ .Environment.CIRCLE_JOB }}-{{ checksum "requirements.txt" }}
34-
- run:
35-
name: run tests
36-
command: |
37-
. venv/bin/activate
38-
python setup.py test
18+
- checkout
19+
- restore_cache:
20+
keys:
21+
- v1-dependencies-{{ .Environment.CIRCLE_JOB }}-{{ checksum "requirements.txt" }}
22+
- run:
23+
name: install dependencies
24+
command: |
25+
pip install virtualenv
26+
virtualenv venv
27+
. venv/bin/activate
28+
python setup.py install
29+
- save_cache:
30+
paths:
31+
- ./venv
32+
key: v1-dependencies-{{ .Environment.CIRCLE_JOB }}-{{ checksum "requirements.txt" }}
33+
- run:
34+
name: run tests
35+
command: |
36+
. venv/bin/activate
37+
python setup.py test
3938
4039
workflows:
4140
run-tests:
4241
jobs:
43-
- build: {name: run-tests-python-2.7, executor: python-27}
44-
- build: {name: run-tests-python-3.5, executor: python-35}
45-
- build: {name: run-tests-python-3.6, executor: python-36}
46-
- build: {name: run-tests-python-3.7, executor: python-37}
42+
- build: { name: run-tests-python-2.7, executor: python-27 }
43+
- build: { name: run-tests-python-3.5, executor: python-35 }
44+
- build: { name: run-tests-python-3.6, executor: python-36 }
45+
- build: { name: run-tests-python-3.7, executor: python-37 }

tests/test_client.py

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from unittest import TestCase
22
import requests_mock
3+
import json
34

45
from elastic_app_search import Client
56
from elastic_app_search.exceptions import InvalidDocument
67

8+
79
class TestClient(TestCase):
810

911
def setUp(self):
@@ -16,15 +18,18 @@ def setUp(self):
1618
)
1719

1820
def test_deprecated_init_support_with_old_names(self):
19-
self.client = Client(account_host_key='host_identifier', api_key='api_key')
21+
self.client = Client(
22+
account_host_key='host_identifier', api_key='api_key')
2023
self.assertEqual(self.client.account_host_key, 'host_identifier')
2124

2225
def test_deprecated_init_support_with_new_names(self):
23-
self.client = Client(host_identifier='host_identifier', api_key='api_key')
26+
self.client = Client(
27+
host_identifier='host_identifier', api_key='api_key')
2428
self.assertEqual(self.client.account_host_key, 'host_identifier')
2529

2630
def test_deprecated_init_support_with_positional(self):
27-
self.client = Client('host_identifier', 'api_key', 'example.com', False)
31+
self.client = Client('host_identifier', 'api_key',
32+
'example.com', False)
2833
self.assertEqual(self.client.account_host_key, 'host_identifier')
2934

3035
def test_host_identifier_is_optional(self):
@@ -41,7 +46,8 @@ def test_index_document_processing_error(self):
4146
error = 'some processing error'
4247
stubbed_return = [{'id': 'something', 'errors': [error]}]
4348
with requests_mock.Mocker() as m:
44-
m.register_uri('POST', self.document_index_url, json=stubbed_return, status_code=200)
49+
m.register_uri('POST', self.document_index_url,
50+
json=stubbed_return, status_code=200)
4551

4652
with self.assertRaises(InvalidDocument) as context:
4753
self.client.index_document(self.engine_name, invalid_document)
@@ -52,38 +58,44 @@ def test_index_document_no_error_key_in_response(self):
5258
stubbed_return = [{'id': 'auto generated', 'errors': []}]
5359

5460
with requests_mock.Mocker() as m:
55-
m.register_uri('POST', self.document_index_url, json=stubbed_return, status_code=200)
56-
response = self.client.index_document(self.engine_name, document_without_id)
61+
m.register_uri('POST', self.document_index_url,
62+
json=stubbed_return, status_code=200)
63+
response = self.client.index_document(
64+
self.engine_name, document_without_id)
5765
self.assertEqual(response, {'id': 'auto generated'})
5866

5967
def test_index_documents(self):
6068
id = 'INscMGmhmX4'
6169
valid_document = {'id': id}
62-
other_document = { 'body': 'some value' }
70+
other_document = {'body': 'some value'}
6371

6472
expected_return = [
6573
{'id': id, 'errors': []},
6674
{'id': 'some autogenerated id', 'errors': []}
6775
]
6876

6977
with requests_mock.Mocker() as m:
70-
m.register_uri('POST', self.document_index_url, json=expected_return, status_code=200)
71-
response = self.client.index_documents(self.engine_name, [valid_document, other_document])
78+
m.register_uri('POST', self.document_index_url,
79+
json=expected_return, status_code=200)
80+
response = self.client.index_documents(
81+
self.engine_name, [valid_document, other_document])
7282
self.assertEqual(response, expected_return)
7383

7484
def test_update_documents(self):
7585
id = 'INscMGmhmX4'
7686
valid_document = {'id': id}
77-
other_document = { 'body': 'some value' }
87+
other_document = {'body': 'some value'}
7888

7989
expected_return = [
8090
{'id': id, 'errors': []},
8191
{'id': 'some autogenerated id', 'errors': []}
8292
]
8393

8494
with requests_mock.Mocker() as m:
85-
m.register_uri('PATCH', self.document_index_url, json=expected_return, status_code=200)
86-
response = self.client.update_documents(self.engine_name, [valid_document, other_document])
95+
m.register_uri('PATCH', self.document_index_url,
96+
json=expected_return, status_code=200)
97+
response = self.client.update_documents(
98+
self.engine_name, [valid_document, other_document])
8799
self.assertEqual(response, expected_return)
88100

89101
def test_get_documents(self):
@@ -98,7 +110,8 @@ def test_get_documents(self):
98110
]
99111

100112
with requests_mock.Mocker() as m:
101-
m.register_uri('GET', self.document_index_url, json=expected_return, status_code=200)
113+
m.register_uri('GET', self.document_index_url,
114+
json=expected_return, status_code=200)
102115
response = self.client.get_documents(self.engine_name, [id])
103116
self.assertEqual(response, expected_return)
104117

@@ -113,14 +126,19 @@ def test_list_documents(self):
113126
}
114127
}
115128

129+
def match_request_text(request):
130+
data = json.loads(request.text)
131+
return data["page"]["current"] == 1 and data["page"]["size"] == 20
132+
116133
with requests_mock.Mocker() as m:
117-
url = "{}/engines/{}/documents/list".format(self.client.session.base_url, self.engine_name)
134+
url = "{}/engines/{}/documents/list".format(
135+
self.client.session.base_url, self.engine_name)
118136
m.register_uri('GET',
119-
url,
120-
additional_matcher=lambda x: x.text == '{"page": {"current": 1, "size": 20}}',
121-
json=expected_return,
122-
status_code=200
123-
)
137+
url,
138+
additional_matcher=match_request_text,
139+
json=expected_return,
140+
status_code=200
141+
)
124142

125143
response = self.client.list_documents(self.engine_name)
126144
self.assertEqual(response, expected_return)
@@ -132,7 +150,8 @@ def test_destroy_documents(self):
132150
]
133151

134152
with requests_mock.Mocker() as m:
135-
m.register_uri('DELETE', self.document_index_url, json=expected_return, status_code=200)
153+
m.register_uri('DELETE', self.document_index_url,
154+
json=expected_return, status_code=200)
136155
response = self.client.destroy_documents(self.engine_name, [id])
137156
self.assertEqual(response, expected_return)
138157

@@ -142,12 +161,13 @@ def test_get_schema(self):
142161
}
143162

144163
with requests_mock.Mocker() as m:
145-
url = "{}/engines/{}/schema".format(self.client.session.base_url, self.engine_name)
164+
url = "{}/engines/{}/schema".format(
165+
self.client.session.base_url, self.engine_name)
146166
m.register_uri('GET',
147-
url,
148-
json=expected_return,
149-
status_code=200
150-
)
167+
url,
168+
json=expected_return,
169+
status_code=200
170+
)
151171

152172
response = self.client.get_schema(self.engine_name)
153173
self.assertEqual(response, expected_return)
@@ -159,29 +179,35 @@ def test_update_schema(self):
159179
}
160180

161181
with requests_mock.Mocker() as m:
162-
url = "{}/engines/{}/schema".format(self.client.session.base_url, self.engine_name)
182+
url = "{}/engines/{}/schema".format(
183+
self.client.session.base_url, self.engine_name)
163184
m.register_uri('POST',
164-
url,
165-
json=expected_return,
166-
status_code=200
167-
)
185+
url,
186+
json=expected_return,
187+
status_code=200
188+
)
168189

169-
response = self.client.update_schema(self.engine_name, expected_return)
190+
response = self.client.update_schema(
191+
self.engine_name, expected_return)
170192
self.assertEqual(response, expected_return)
171193

172194
def test_list_engines(self):
173195
expected_return = [
174-
{ 'name': 'myawesomeengine' }
196+
{'name': 'myawesomeengine'}
175197
]
176198

199+
def match_request_text(request):
200+
data = json.loads(request.text)
201+
return data["page"]["current"] == 1 and data["page"]["size"] == 20
202+
177203
with requests_mock.Mocker() as m:
178204
url = "{}/{}".format(self.client.session.base_url, 'engines')
179205
m.register_uri('GET',
180-
url,
181-
additional_matcher=lambda x: x.text == '{"page": {"current": 1, "size": 20}}',
182-
json=expected_return,
183-
status_code=200
184-
)
206+
url,
207+
additional_matcher=match_request_text,
208+
json=expected_return,
209+
status_code=200
210+
)
185211
response = self.client.list_engines()
186212
self.assertEqual(response, expected_return)
187213

@@ -190,12 +216,16 @@ def test_list_engines_with_paging(self):
190216
{'name': 'myawesomeengine'}
191217
]
192218

219+
def match_request_text(request):
220+
data = json.loads(request.text)
221+
return data["page"]["current"] == 10 and data["page"]["size"] == 2
222+
193223
with requests_mock.Mocker() as m:
194224
url = "{}/{}".format(self.client.session.base_url, 'engines')
195225
m.register_uri(
196226
'GET',
197227
url,
198-
additional_matcher=lambda x: x.text == '{"page": {"current": 10, "size": 2}}',
228+
additional_matcher=match_request_text,
199229
json=expected_return,
200230
status_code=200
201231
)
@@ -205,7 +235,7 @@ def test_list_engines_with_paging(self):
205235
def test_get_engine(self):
206236
engine_name = 'myawesomeengine'
207237
expected_return = [
208-
{ 'name': engine_name }
238+
{'name': engine_name}
209239
]
210240

211241
with requests_mock.Mocker() as m:
@@ -234,7 +264,8 @@ def test_destroy_engine(self):
234264
url = "{}/{}/{}".format(self.client.session.base_url,
235265
'engines',
236266
engine_name)
237-
m.register_uri('DELETE', url, json=expected_return, status_code=200)
267+
m.register_uri('DELETE', url, json=expected_return,
268+
status_code=200)
238269
response = self.client.destroy_engine(engine_name)
239270
self.assertEqual(response, expected_return)
240271

@@ -278,10 +309,15 @@ def test_list_synonym_sets(self):
278309
self.client.session.base_url,
279310
self.engine_name
280311
)
312+
313+
def match_request_text(request):
314+
data = json.loads(request.text)
315+
return data["page"]["current"] == 1 and data["page"]["size"] == 20
316+
281317
m.register_uri(
282318
'GET',
283319
url,
284-
additional_matcher=lambda x: x.text == '{"page": {"current": 1, "size": 20}}',
320+
additional_matcher=match_request_text,
285321
json=expected_return,
286322
status_code=200
287323
)
@@ -404,7 +440,7 @@ def test_destroy_synonym_set(self):
404440

405441
def test_search(self):
406442
query = 'query'
407-
expected_return = { 'meta': {}, 'results': []}
443+
expected_return = {'meta': {}, 'results': []}
408444

409445
with requests_mock.Mocker() as m:
410446
url = "{}/{}".format(
@@ -416,7 +452,8 @@ def test_search(self):
416452
self.assertEqual(response, expected_return)
417453

418454
def test_multi_search(self):
419-
expected_return = [{ 'meta': {}, 'results': []}, { 'meta': {}, 'results': []}]
455+
expected_return = [{'meta': {}, 'results': []},
456+
{'meta': {}, 'results': []}]
420457

421458
with requests_mock.Mocker() as m:
422459
url = "{}/{}".format(
@@ -429,15 +466,16 @@ def test_multi_search(self):
429466

430467
def test_query_suggestion(self):
431468
query = 'query'
432-
expected_return = { 'meta': {}, 'results': {}}
469+
expected_return = {'meta': {}, 'results': {}}
433470

434471
with requests_mock.Mocker() as m:
435472
url = "{}/{}".format(
436473
self.client.session.base_url,
437474
"engines/{}/query_suggestion".format(self.engine_name)
438475
)
439476
m.register_uri('GET', url, json=expected_return, status_code=200)
440-
response = self.client.query_suggestion(self.engine_name, query, {})
477+
response = self.client.query_suggestion(
478+
self.engine_name, query, {})
441479
self.assertEqual(response, expected_return)
442480

443481
def test_click(self):
@@ -447,4 +485,5 @@ def test_click(self):
447485
"engines/{}/click".format(self.engine_name)
448486
)
449487
m.register_uri('POST', url, json={}, status_code=200)
450-
self.client.click(self.engine_name, {'query': 'cat', 'document_id': 'INscMGmhmX4'})
488+
self.client.click(self.engine_name, {
489+
'query': 'cat', 'document_id': 'INscMGmhmX4'})

0 commit comments

Comments
 (0)