-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathanalytics_rules_v1_test.py
More file actions
291 lines (248 loc) · 9.44 KB
/
analytics_rules_v1_test.py
File metadata and controls
291 lines (248 loc) · 9.44 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Tests for the AnalyticsRulesV1 class."""
import pytest
from tests.utils.object_assertions import assert_match_object, assert_object_lists_match
from tests.utils.version import is_v30_or_above
from typesense.sync.client import Client
from typesense.sync.analytics_rules_v1 import AnalyticsRulesV1
from typesense.sync.api_call import ApiCall
from typesense.async_.api_call import AsyncApiCall
from typesense.async_.analytics_rules_v1 import AsyncAnalyticsRulesV1
pytestmark = pytest.mark.skipif(
is_v30_or_above(
Client(
{
"api_key": "xyz",
"nodes": [{"host": "localhost", "port": 8108, "protocol": "http"}],
}
)
),
reason="Skip AnalyticsV1 tests on v30+",
)
def test_init(fake_api_call: ApiCall) -> None:
"""Test that the AnalyticsRulesV1 object is initialized correctly."""
analytics_rules = AnalyticsRulesV1(fake_api_call)
assert_match_object(analytics_rules.api_call, fake_api_call)
assert_object_lists_match(
analytics_rules.api_call.node_manager.nodes,
fake_api_call.node_manager.nodes,
)
assert_match_object(
analytics_rules.api_call.config.nearest_node,
fake_api_call.config.nearest_node,
)
assert not analytics_rules.rules
def test_get_missing_analytics_rule(fake_analytics_rules: AnalyticsRulesV1) -> None:
"""Test that the AnalyticsRulesV1 object can get a missing analytics_rule."""
analytics_rule = fake_analytics_rules["company_analytics_rule"]
assert analytics_rule.rule_id == "company_analytics_rule"
assert_match_object(analytics_rule.api_call, fake_analytics_rules.api_call)
assert_object_lists_match(
analytics_rule.api_call.node_manager.nodes,
fake_analytics_rules.api_call.node_manager.nodes,
)
assert_match_object(
analytics_rule.api_call.config.nearest_node,
fake_analytics_rules.api_call.config.nearest_node,
)
assert (
analytics_rule._endpoint_path # noqa: WPS437
== "/analytics/rules/company_analytics_rule"
)
def test_get_existing_analytics_rule(fake_analytics_rules: AnalyticsRulesV1) -> None:
"""Test that the AnalyticsRulesV1 object can get an existing analytics_rule."""
analytics_rule = fake_analytics_rules["company_analytics_rule"]
fetched_analytics_rule = fake_analytics_rules["company_analytics_rule"]
assert len(fake_analytics_rules.rules) == 1
assert analytics_rule is fetched_analytics_rule
def test_actual_create(
actual_analytics_rules: AnalyticsRulesV1,
delete_all: None,
delete_all_analytics_rules_v1: None,
create_collection: None,
create_query_collection: None,
) -> None:
"""Test that the AnalyticsRulesV1 object can create an analytics_rule on Typesense Server."""
response = actual_analytics_rules.create(
rule={
"name": "company_analytics_rule",
"type": "nohits_queries",
"params": {
"source": {
"collections": ["companies"],
},
"destination": {"collection": "companies_queries"},
},
},
)
assert response == {
"name": "company_analytics_rule",
"type": "nohits_queries",
"params": {
"source": {"collections": ["companies"]},
"destination": {"collection": "companies_queries"},
},
}
def test_actual_update(
actual_analytics_rules: AnalyticsRulesV1,
delete_all: None,
delete_all_analytics_rules_v1: None,
create_analytics_rule_v1: None,
) -> None:
"""Test that the AnalyticsRulesV1 object can update an analytics_rule on Typesense Server."""
response = actual_analytics_rules.upsert(
"company_analytics_rule",
{
"type": "popular_queries",
"params": {
"source": {
"collections": ["companies"],
},
"destination": {"collection": "companies_queries"},
},
},
)
assert response == {
"name": "company_analytics_rule",
"type": "popular_queries",
"params": {
"source": {"collections": ["companies"]},
"destination": {"collection": "companies_queries"},
},
}
def test_actual_retrieve(
actual_analytics_rules: AnalyticsRulesV1,
delete_all: None,
delete_all_analytics_rules_v1: None,
create_analytics_rule_v1: None,
) -> None:
"""Test that the AnalyticsRulesV1 object can retrieve the rules from Typesense Server."""
response = actual_analytics_rules.retrieve()
assert len(response["rules"]) == 1
assert_match_object(
response["rules"][0],
{
"name": "company_analytics_rule",
"params": {
"destination": {"collection": "companies_queries"},
"limit": 1000,
"source": {"collections": ["companies"]},
},
"type": "nohits_queries",
},
)
def test_init_async(fake_async_api_call: AsyncApiCall) -> None:
"""Test that the AsyncAnalyticsRulesV1 object is initialized correctly."""
analytics_rules = AsyncAnalyticsRulesV1(fake_async_api_call)
assert_match_object(analytics_rules.api_call, fake_async_api_call)
assert_object_lists_match(
analytics_rules.api_call.node_manager.nodes,
fake_async_api_call.node_manager.nodes,
)
assert_match_object(
analytics_rules.api_call.config.nearest_node,
fake_async_api_call.config.nearest_node,
)
assert not analytics_rules.rules
def test_get_missing_analytics_rule_async(
fake_async_analytics_rules_v1: AsyncAnalyticsRulesV1,
) -> None:
"""Test that the AsyncAnalyticsRulesV1 object can get a missing analytics_rule."""
analytics_rule = fake_async_analytics_rules_v1["company_analytics_rule"]
assert analytics_rule.rule_id == "company_analytics_rule"
assert_match_object(analytics_rule.api_call, fake_async_analytics_rules_v1.api_call)
assert_object_lists_match(
analytics_rule.api_call.node_manager.nodes,
fake_async_analytics_rules_v1.api_call.node_manager.nodes,
)
assert_match_object(
analytics_rule.api_call.config.nearest_node,
fake_async_analytics_rules_v1.api_call.config.nearest_node,
)
assert (
analytics_rule._endpoint_path # noqa: WPS437
== "/analytics/rules/company_analytics_rule"
)
def test_get_existing_analytics_rule_async(
fake_async_analytics_rules_v1: AsyncAnalyticsRulesV1,
) -> None:
"""Test that the AsyncAnalyticsRulesV1 object can get an existing analytics_rule."""
analytics_rule = fake_async_analytics_rules_v1["company_analytics_rule"]
fetched_analytics_rule = fake_async_analytics_rules_v1["company_analytics_rule"]
assert len(fake_async_analytics_rules_v1.rules) == 1
assert analytics_rule is fetched_analytics_rule
async def test_actual_create_async(
actual_async_analytics_rules_v1: AsyncAnalyticsRulesV1,
delete_all: None,
delete_all_analytics_rules_v1: None,
create_collection: None,
create_query_collection: None,
) -> None:
"""Test that the AsyncAnalyticsRulesV1 object can create an analytics_rule on Typesense Server."""
response = await actual_async_analytics_rules_v1.create(
rule={
"name": "company_analytics_rule",
"type": "nohits_queries",
"params": {
"source": {
"collections": ["companies"],
},
"destination": {"collection": "companies_queries"},
},
},
)
assert response == {
"name": "company_analytics_rule",
"type": "nohits_queries",
"params": {
"source": {"collections": ["companies"]},
"destination": {"collection": "companies_queries"},
},
}
async def test_actual_update_async(
actual_async_analytics_rules_v1: AsyncAnalyticsRulesV1,
delete_all: None,
delete_all_analytics_rules_v1: None,
create_analytics_rule_v1: None,
) -> None:
"""Test that the AsyncAnalyticsRulesV1 object can update an analytics_rule on Typesense Server."""
response = await actual_async_analytics_rules_v1.upsert(
"company_analytics_rule",
{
"type": "popular_queries",
"params": {
"source": {
"collections": ["companies"],
},
"destination": {"collection": "companies_queries"},
},
},
)
assert response == {
"name": "company_analytics_rule",
"type": "popular_queries",
"params": {
"source": {"collections": ["companies"]},
"destination": {"collection": "companies_queries"},
},
}
async def test_actual_retrieve_async(
actual_async_analytics_rules_v1: AsyncAnalyticsRulesV1,
delete_all: None,
delete_all_analytics_rules_v1: None,
create_analytics_rule_v1: None,
) -> None:
"""Test that the AsyncAnalyticsRulesV1 object can retrieve the rules from Typesense Server."""
response = await actual_async_analytics_rules_v1.retrieve()
assert len(response["rules"]) == 1
assert_match_object(
response["rules"][0],
{
"name": "company_analytics_rule",
"params": {
"destination": {"collection": "companies_queries"},
"limit": 1000,
"source": {"collections": ["companies"]},
},
"type": "nohits_queries",
},
)