|
| 1 | +import json |
| 2 | +import pytest |
| 3 | +import meilisearch |
| 4 | +from meilisearch.tests import BASE_URL, MASTER_KEY |
| 5 | + |
| 6 | +class TestDocument: |
| 7 | + |
| 8 | + """ TESTS: all documents routes and optional params """ |
| 9 | + |
| 10 | + client = meilisearch.Client(BASE_URL, MASTER_KEY) |
| 11 | + index = None |
| 12 | + dataset_file = None |
| 13 | + dataset_json = None |
| 14 | + |
| 15 | + def setup_class(self): |
| 16 | + self.index = self.client.create_index(uid='indexUID') |
| 17 | + self.dataset_file = open("./datasets/small_movies.json", "r") |
| 18 | + self.dataset_json = json.loads(self.dataset_file.read()) |
| 19 | + self.dataset_file.close() |
| 20 | + |
| 21 | + def teardown_class(self): |
| 22 | + self.index.delete() |
| 23 | + |
| 24 | + def test_get_documents_default(self): |
| 25 | + """Tests getting documents on a clean index""" |
| 26 | + response = self.index.get_documents() |
| 27 | + assert isinstance(response, list) |
| 28 | + assert response == [] |
| 29 | + |
| 30 | + def test_add_documents(self): |
| 31 | + """Tests adding new documents to a clean index""" |
| 32 | + response = self.index.add_documents(self.dataset_json) |
| 33 | + assert isinstance(response, object) |
| 34 | + assert 'updateId' in response |
| 35 | + assert self.index.get_primary_key() == 'id' |
| 36 | + update = self.index.wait_for_pending_update(response['updateId']) |
| 37 | + assert update['status'] == 'processed' |
| 38 | + |
| 39 | + def test_get_document(self): |
| 40 | + """Tests getting one document on a populated index""" |
| 41 | + response = self.index.get_document("500682") |
| 42 | + assert isinstance(response, object) |
| 43 | + assert 'title' in response |
| 44 | + assert response['title'] == 'The Highwaymen' |
| 45 | + |
| 46 | + def test_get_document_inexistent(self): |
| 47 | + """Tests getting one INEXISTENT document on a populated index""" |
| 48 | + with pytest.raises(Exception): |
| 49 | + self.index.get_document("123") |
| 50 | + |
| 51 | + def test_get_documents_populated(self): |
| 52 | + """Tests getting documents on a populated index""" |
| 53 | + response = self.index.get_documents() |
| 54 | + assert isinstance(response, list) |
| 55 | + assert len(response) == 20 |
| 56 | + |
| 57 | + def test_get_documents_offset_optional_params(self): |
| 58 | + """Tests getting documents on a populated index with optional parameters""" |
| 59 | + response = self.index.get_documents() |
| 60 | + assert isinstance(response, list) |
| 61 | + assert len(response) == 20 |
| 62 | + response_offset_limit = self.index.get_documents({ |
| 63 | + 'limit': 3, |
| 64 | + 'offset': 1, |
| 65 | + 'attributesToRetrieve': 'title' |
| 66 | + }) |
| 67 | + assert len(response_offset_limit) == 3 |
| 68 | + assert response_offset_limit[0]['title'] == response[1]['title'] |
| 69 | + |
| 70 | + def test_update_documents(self): |
| 71 | + """Tests updating a single document and a set of documents """ |
| 72 | + response = self.index.get_documents() |
| 73 | + response[0]['title'] = "Some title" |
| 74 | + update = self.index.update_documents([response[0]]) |
| 75 | + assert isinstance(update, object) |
| 76 | + assert 'updateId' in update |
| 77 | + self.index.wait_for_pending_update(update['updateId']) |
| 78 | + response = self.index.get_documents() |
| 79 | + assert response[0]['title'] == "Some title" |
| 80 | + update = self.index.update_documents(self.dataset_json) |
| 81 | + self.index.wait_for_pending_update(update['updateId']) |
| 82 | + response = self.index.get_documents() |
| 83 | + assert response[0]['title'] != "Some title" |
| 84 | + |
| 85 | + def test_delete_document(self): |
| 86 | + """Tests deleting a single document""" |
| 87 | + response = self.index.delete_document("500682") |
| 88 | + assert isinstance(response, object) |
| 89 | + assert 'updateId' in response |
| 90 | + self.index.wait_for_pending_update(response['updateId']) |
| 91 | + with pytest.raises(Exception): |
| 92 | + self.index.get_document("500682") |
| 93 | + |
| 94 | + def test_delete_documents(self): |
| 95 | + """Tests deleting a set of documents """ |
| 96 | + to_delete = ["522681", "450465", "329996"] |
| 97 | + response = self.index.delete_documents(to_delete) |
| 98 | + assert isinstance(response, object) |
| 99 | + assert 'updateId' in response |
| 100 | + self.index.wait_for_pending_update(response['updateId']) |
| 101 | + for document in to_delete: |
| 102 | + with pytest.raises(Exception): |
| 103 | + self.index.get_document(document) |
| 104 | + |
| 105 | + def test_delete_all_documents(self): |
| 106 | + """Tests updating all the documents in the index""" |
| 107 | + response = self.index.delete_all_documents() |
| 108 | + assert isinstance(response, object) |
| 109 | + assert 'updateId' in response |
| 110 | + self.index.wait_for_pending_update(response['updateId']) |
| 111 | + response = self.index.get_documents() |
| 112 | + assert isinstance(response, list) |
| 113 | + assert response == [] |
0 commit comments