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

Commit a0aa62b

Browse files
Remove check for id in document, allow api to autogenerate id
1 parent c5d5b21 commit a0aa62b

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

swiftype_app_search/client.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def index_document(self, engine_name, document):
3636
"""
3737
Create or update a document for an engine. Raises
3838
:class:`~swiftype_app_search.exceptions.InvalidDocument` when the document
39-
is missing required fields, contains unsupported fields, or has
40-
processing errors
39+
has processing errors
4140
4241
:param engine_name: Name of engine to index documents into.
4342
:param document: Hash representing a single document.
@@ -56,34 +55,18 @@ def index_document(self, engine_name, document):
5655

5756
def index_documents(self, engine_name, documents):
5857
"""
59-
Create or update documents for an engine. Raises
60-
:class:`~swiftype_app_search.exceptions.InvalidDocument` when the document
61-
is missing required fields or contains unsupported fields
58+
Create or update documents for an engine.
6259
6360
:param engine_name: Name of engine to index documents into.
6461
:param documents: Hashes representing documents.
6562
:return: Array of document status dictionaries. Errors will be present
6663
in a document status with a key of `errors`.
6764
"""
68-
self._raise_if_documents_invalid(documents)
69-
7065
endpoint = "engines/{}/documents".format(engine_name)
7166
data = json.dumps(documents)
7267

7368
return self.swiftype_session.request('post', endpoint, data=data)
7469

75-
def _raise_if_documents_invalid(self, documents):
76-
for document in documents:
77-
missing_required_keys = set(self.REQUIRED_DOCUMENT_TOP_LEVEL_KEYS) -\
78-
set(document.keys())
79-
if len(missing_required_keys):
80-
raise InvalidDocument(
81-
"Missing required fields: {}".format(
82-
','.join(missing_required_keys)),
83-
document
84-
)
85-
86-
8770
def destroy_documents(self, engine_name, document_ids):
8871
"""
8972
Destroys documents by id for an engine.

tests/test_client.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from swiftype_app_search import Client
55
from swiftype_app_search.exceptions import InvalidDocument
66

7+
78
class TestClient(TestCase):
89

910
def setUp(self):
@@ -15,13 +16,6 @@ def setUp(self):
1516
"engines/{}/documents".format(self.engine_name)
1617
)
1718

18-
def test_index_document_validation(self):
19-
invalid_document = {'does': 'not have the id field'}
20-
with self.assertRaises(InvalidDocument) as context:
21-
self.client.index_documents(self.engine_name, [invalid_document])
22-
self.assertEqual(str(context.exception), 'Missing required fields: id')
23-
self.assertEqual(context.exception.document, invalid_document)
24-
2519
def test_index_document_processing_error(self):
2620
invalid_document = {'id': 'something', 'bad': {'no': 'nested'}}
2721
error = 'some processing error'
@@ -33,27 +27,28 @@ def test_index_document_processing_error(self):
3327
self.client.index_document(self.engine_name, invalid_document)
3428
self.assertEqual(str(context.exception), error)
3529

36-
def test_index_documents_validation(self):
37-
invalid_documents = [
38-
{
39-
'does': 'not have the id field'
40-
}
41-
]
42-
with self.assertRaises(InvalidDocument) as context:
43-
self.client.index_documents(self.engine_name, invalid_documents)
44-
self.assertEqual(str(context.exception), 'Missing required fields: id')
45-
self.assertEqual(context.exception.document, invalid_documents[0])
30+
def test_index_document_no_error_key_in_response(self):
31+
document_without_id = {'body': 'some value'}
32+
stubbed_return = [{'id': 'auto generated', 'errors': []}]
33+
34+
with requests_mock.Mocker() as m:
35+
m.register_uri('POST', self.document_index_url, json=stubbed_return, status_code=200)
36+
response = self.client.index_document(self.engine_name, document_without_id)
37+
self.assertEqual(response, {'id': 'auto generated'})
4638

4739
def test_index_documents(self):
4840
id = 'INscMGmhmX4'
4941
valid_document = {'id': id}
42+
other_document = { 'body': 'some value' }
43+
5044
expected_return = [
51-
{'id': id, 'errors': []}
45+
{'id': id, 'errors': []},
46+
{'id': 'some autogenerated id', 'errors': []}
5247
]
5348

5449
with requests_mock.Mocker() as m:
5550
m.register_uri('POST', self.document_index_url, json=expected_return, status_code=200)
56-
response = self.client.index_documents(self.engine_name, [valid_document])
51+
response = self.client.index_documents(self.engine_name, [valid_document, other_document])
5752
self.assertEqual(response, expected_return)
5853

5954
def test_get_documents(self):

0 commit comments

Comments
 (0)