-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathaliases_test.py
161 lines (126 loc) · 4.65 KB
/
aliases_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
"""Tests for the Aliases 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.aliases import Aliases
from typesense.api_call import ApiCall
from typesense.types.alias import AliasesResponseSchema, AliasSchema
def test_init(fake_api_call: ApiCall) -> None:
"""Test that the Aliases object is initialized correctly."""
aliases = Aliases(fake_api_call)
assert_match_object(aliases.api_call, fake_api_call)
assert_object_lists_match(
aliases.api_call.node_manager.nodes,
fake_api_call.node_manager.nodes,
)
assert_match_object(
aliases.api_call.config.nearest_node,
fake_api_call.config.nearest_node,
)
assert not aliases.aliases
def test_get_missing_alias(fake_aliases: Aliases) -> None:
"""Test that the Aliases object can get a missing alias."""
alias = fake_aliases["company_alias"]
assert alias.name == "company_alias"
assert_match_object(alias.api_call, fake_aliases.api_call)
assert_object_lists_match(
alias.api_call.node_manager.nodes, fake_aliases.api_call.node_manager.nodes
)
assert_match_object(
alias.api_call.config.nearest_node,
fake_aliases.api_call.config.nearest_node,
)
assert alias._endpoint_path == "/aliases/company_alias" # noqa: WPS437
def test_get_existing_alias(fake_aliases: Aliases) -> None:
"""Test that the Aliases object can get an existing alias."""
alias = fake_aliases["companies"]
fetched_alias = fake_aliases["companies"]
assert len(fake_aliases.aliases) == 1
assert alias is fetched_alias
def test_retrieve(fake_aliases: Aliases) -> None:
"""Test that the Aliases object can retrieve aliases."""
json_response: AliasesResponseSchema = {
"aliases": [
{
"collection_name": "companies",
"name": "company_alias",
},
],
}
with requests_mock.Mocker() as mock:
mock.get(
"http://nearest:8108/aliases",
json=json_response,
)
response = fake_aliases.retrieve()
assert len(response) == 1
assert response["aliases"][0] == {
"collection_name": "companies",
"name": "company_alias",
}
assert response == json_response
def test_create(fake_aliases: Aliases) -> None:
"""Test that the Aliases object can create a alias."""
json_response: AliasSchema = {
"collection_name": "companies",
"name": "company_alias",
}
with requests_mock.Mocker() as mock:
mock.put(
"http://nearest:8108/aliases/company_alias",
json=json_response,
)
fake_aliases.upsert(
"company_alias",
{"collection_name": "companies", "name": "company_alias"},
)
assert mock.call_count == 1
assert mock.called is True
assert mock.last_request.method == "PUT"
assert mock.last_request.url == "http://nearest:8108/aliases/company_alias"
assert mock.last_request.json() == json_response
def test_actual_create(actual_aliases: Aliases, delete_all_aliases: None) -> None:
"""Test that the Aliases object can create an alias on Typesense Server."""
response = actual_aliases.upsert("company_alias", {"collection_name": "companies"})
assert response == {"collection_name": "companies", "name": "company_alias"}
def test_actual_update(
actual_aliases: Aliases,
delete_all_aliases: None,
delete_all: None,
create_collection: None,
create_another_collection: None,
) -> None:
"""Test that the Aliases object can update an alias on Typesense Server."""
create_response = actual_aliases.upsert(
"company_alias",
{"collection_name": "companies"},
)
assert create_response == {"collection_name": "companies", "name": "company_alias"}
update_response = actual_aliases.upsert(
"company_alias",
{"collection_name": "companies_2"},
)
assert update_response == {
"collection_name": "companies_2",
"name": "company_alias",
}
def test_actual_retrieve(
delete_all: None,
delete_all_aliases: None,
create_alias: None,
actual_aliases: Aliases,
) -> None:
"""Test that the Aliases object can retrieve an alias from Typesense Server."""
response = actual_aliases.retrieve()
assert len(response["aliases"]) == 1
assert_to_contain_object(
response["aliases"][0],
{
"collection_name": "companies",
"name": "company_alias",
},
)