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

Commit 9fb81ac

Browse files
authored
Merge pull request #3 from swiftype/christopherjwang/remove_document_id_constraint
Remove check for id in document, allow api to autogenerate id
2 parents c5d5b21 + 797f631 commit 9fb81ac

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

swiftype_app_search/client.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
class Client:
88

99
SWIFTYPE_APP_SEARCH_BASE_ENDPOINT = 'api.swiftype.com/api/as/v1'
10-
REQUIRED_DOCUMENT_TOP_LEVEL_KEYS = ['id']
1110
SIGNED_SEARCH_TOKEN_JWT_ALGORITHM = 'HS256'
1211

1312
def __init__(self, account_host_key, api_key,
@@ -36,8 +35,7 @@ def index_document(self, engine_name, document):
3635
"""
3736
Create or update a document for an engine. Raises
3837
:class:`~swiftype_app_search.exceptions.InvalidDocument` when the document
39-
is missing required fields, contains unsupported fields, or has
40-
processing errors
38+
has processing errors
4139
4240
:param engine_name: Name of engine to index documents into.
4341
:param document: Hash representing a single document.
@@ -56,34 +54,18 @@ def index_document(self, engine_name, document):
5654

5755
def index_documents(self, engine_name, documents):
5856
"""
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
57+
Create or update documents for an engine.
6258
6359
:param engine_name: Name of engine to index documents into.
6460
:param documents: Hashes representing documents.
6561
:return: Array of document status dictionaries. Errors will be present
6662
in a document status with a key of `errors`.
6763
"""
68-
self._raise_if_documents_invalid(documents)
69-
7064
endpoint = "engines/{}/documents".format(engine_name)
7165
data = json.dumps(documents)
7266

7367
return self.swiftype_session.request('post', endpoint, data=data)
7468

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-
8769
def destroy_documents(self, engine_name, document_ids):
8870
"""
8971
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)