-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathdocument_test.py
More file actions
184 lines (152 loc) · 5.29 KB
/
document_test.py
File metadata and controls
184 lines (152 loc) · 5.29 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Tests for the Document class."""
import pytest
from tests.utils.object_assertions import (
assert_match_object,
assert_object_lists_match,
assert_to_contain_object,
)
from typesense.sync.api_call import ApiCall
from typesense.async_.api_call import AsyncApiCall
from typesense.async_.document import AsyncDocument
from typesense.async_.documents import AsyncDocuments
from typesense.sync.document import Document
from typesense.sync.documents import Documents
from typesense.exceptions import ObjectNotFound
def test_init(fake_api_call: ApiCall) -> None:
"""Test that the Document object is initialized correctly."""
document = Document(fake_api_call, "companies", "0")
assert document.document_id == "0"
assert document.collection_name == "companies"
assert_match_object(document.api_call, fake_api_call)
assert_object_lists_match(
document.api_call.node_manager.nodes,
fake_api_call.node_manager.nodes,
)
assert_match_object(
document.api_call.config.nearest_node,
fake_api_call.config.nearest_node,
)
assert (
document._endpoint_path == "/collections/companies/documents/0" # noqa: WPS437
)
def test_init_async(fake_async_api_call: AsyncApiCall) -> None:
"""Test that the AsyncDocument object is initialized correctly."""
document = AsyncDocument(fake_async_api_call, "companies", "0")
assert document.document_id == "0"
assert document.collection_name == "companies"
assert_match_object(document.api_call, fake_async_api_call)
assert_object_lists_match(
document.api_call.node_manager.nodes,
fake_async_api_call.node_manager.nodes,
)
assert_match_object(
document.api_call.config.nearest_node,
fake_async_api_call.config.nearest_node,
)
assert (
document._endpoint_path == "/collections/companies/documents/0" # noqa: WPS437
)
def test_actual_update(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can update an document on Typesense Server."""
response = actual_documents["0"].update(
{"company_name": "Company", "num_employees": 20},
{
"action": "update",
},
)
assert_to_contain_object(
response,
{"id": "0", "company_name": "Company", "num_employees": 20},
)
def test_actual_retrieve(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can retrieve an document from Typesense Server."""
response = actual_documents["0"].retrieve()
assert_to_contain_object(
response,
{"id": "0", "company_name": "Company", "num_employees": 10},
)
def test_actual_delete(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can delete an document from Typesense Server."""
response = actual_documents["0"].delete()
assert response == {
"id": "0",
"company_name": "Company",
"num_employees": 10,
}
def test_actual_delete_non_existent(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can delete an document from Typesense Server."""
with pytest.raises(ObjectNotFound):
actual_documents["1"].delete()
def test_actual_delete_non_existent_ignore_not_found(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can delete an document from Typesense Server."""
response = actual_documents["1"].delete(
delete_parameters={"ignore_not_found": True},
)
assert response == {"id": "1"}
async def test_actual_update_async(
actual_async_documents: AsyncDocuments,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the AsyncDocument object can update an document on Typesense Server."""
response = await actual_async_documents["0"].update(
{"company_name": "Company", "num_employees": 20},
{
"action": "update",
},
)
assert_to_contain_object(
response,
{"id": "0", "company_name": "Company", "num_employees": 20},
)
async def test_actual_retrieve_async(
actual_async_documents: AsyncDocuments,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the AsyncDocument object can retrieve an document from Typesense Server."""
response = await actual_async_documents["0"].retrieve()
assert_to_contain_object(
response,
{"id": "0", "company_name": "Company", "num_employees": 10},
)
async def test_actual_delete_async(
actual_async_documents: AsyncDocuments,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the AsyncDocument object can delete an document from Typesense Server."""
response = await actual_async_documents["0"].delete()
assert response == {
"id": "0",
"company_name": "Company",
"num_employees": 10,
}