-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathoverrides_test.py
192 lines (158 loc) · 5.68 KB
/
overrides_test.py
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
185
186
187
188
189
190
191
192
"""Tests for the Overrides class."""
from __future__ import annotations
import requests_mock
from tests.utils.object_assertions import (
assert_match_object,
assert_object_lists_match,
assert_to_contain_object,
)
from typesense.api_call import ApiCall
from typesense.collections import Collections
from typesense.overrides import OverrideRetrieveSchema, Overrides, OverrideSchema
def test_init(fake_api_call: ApiCall) -> None:
"""Test that the Overrides object is initialized correctly."""
overrides = Overrides(fake_api_call, "companies")
assert_match_object(overrides.api_call, fake_api_call)
assert_object_lists_match(
overrides.api_call.node_manager.nodes,
fake_api_call.node_manager.nodes,
)
assert_match_object(
overrides.api_call.config.nearest_node,
fake_api_call.config.nearest_node,
)
assert not overrides.overrides
def test_get_missing_override(fake_overrides: Overrides) -> None:
"""Test that the Overrides object can get a missing override."""
override = fake_overrides["company_override"]
assert override.override_id == "company_override"
assert_match_object(override.api_call, fake_overrides.api_call)
assert_object_lists_match(
override.api_call.node_manager.nodes, fake_overrides.api_call.node_manager.nodes
)
assert_match_object(
override.api_call.config.nearest_node,
fake_overrides.api_call.config.nearest_node,
)
assert override.collection_name == "companies"
assert (
override._endpoint_path() # noqa: WPS437
== "/collections/companies/overrides/company_override"
)
def test_get_existing_override(fake_overrides: Overrides) -> None:
"""Test that the Overrides object can get an existing override."""
override = fake_overrides["companies"]
fetched_override = fake_overrides["companies"]
assert len(fake_overrides.overrides) == 1
assert override is fetched_override
def test_retrieve(fake_overrides: Overrides) -> None:
"""Test that the Overrides object can retrieve overrides."""
json_response: OverrideRetrieveSchema = {
"overrides": [
{
"id": "company_override",
"rule": {"match": "exact", "query": "companies"},
},
],
}
with requests_mock.Mocker() as mock:
mock.get(
"http://nearest:8108/collections/companies/overrides/",
json=json_response,
)
response = fake_overrides.retrieve()
assert len(response) == 1
assert response["overrides"][0] == {
"id": "company_override",
"rule": {"match": "exact", "query": "companies"},
}
assert response == json_response
def test_create(fake_overrides: Overrides) -> None:
"""Test that the Overrides object can create a override."""
json_response: OverrideSchema = {
"id": "company_override",
"rule": {"match": "exact", "query": "companies"},
}
with requests_mock.Mocker() as mock:
mock.put(
"http://nearest:8108/collections/companies/overrides/company_override",
json=json_response,
)
fake_overrides.upsert(
"company_override",
{"rule": {"match": "exact", "query": "companies"}},
)
assert mock.call_count == 1
assert mock.called is True
assert mock.last_request.method == "PUT"
assert (
mock.last_request.url
== "http://nearest:8108/collections/companies/overrides/company_override"
)
assert mock.last_request.json() == {
"rule": {"match": "exact", "query": "companies"},
}
def test_actual_create(
actual_overrides: Overrides,
delete_all: None,
create_collection: None,
) -> None:
"""Test that the Overrides object can create an override on Typesense Server."""
response = actual_overrides.upsert(
"company_override",
{
"rule": {"match": "exact", "query": "companies"},
"filter_by": "num_employees>10",
},
)
assert response == {
"id": "company_override",
"rule": {"match": "exact", "query": "companies"},
"filter_by": "num_employees>10",
}
def test_actual_update(
actual_overrides: Overrides,
delete_all: None,
create_collection: None,
) -> None:
"""Test that the Overrides object can update an override on Typesense Server."""
create_response = actual_overrides.upsert(
"company_override",
{
"rule": {"match": "exact", "query": "companies"},
"filter_by": "num_employees>10",
},
)
assert create_response == {
"id": "company_override",
"rule": {"match": "exact", "query": "companies"},
"filter_by": "num_employees>10",
}
update_response = actual_overrides.upsert(
"company_override",
{
"rule": {"match": "contains", "query": "companies"},
"filter_by": "num_employees>20",
},
)
assert update_response == {
"id": "company_override",
"rule": {"match": "contains", "query": "companies"},
"filter_by": "num_employees>20",
}
def test_actual_retrieve(
delete_all: None,
create_override: None,
actual_collections: Collections,
) -> None:
"""Test that the Overrides object can retrieve an override from Typesense Server."""
response = actual_collections["companies"].overrides.retrieve()
assert len(response["overrides"]) == 1
assert_to_contain_object(
response["overrides"][0],
{
"id": "company_override",
"rule": {"match": "exact", "query": "companies"},
"filter_by": "num_employees>10",
},
)