-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_routing.py
More file actions
172 lines (151 loc) · 5.36 KB
/
Copy pathtest_routing.py
File metadata and controls
172 lines (151 loc) · 5.36 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
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from gapic.schema import wrappers
import gc
import json
import uuid
import weakref
import proto
import pytest
class RoutingTestRequest(proto.Message):
table_name = proto.Field(proto.STRING, number=1)
app_profile_id = proto.Field(proto.STRING, number=2)
@pytest.mark.parametrize(
"req, expected",
[
(RoutingTestRequest(app_profile_id="foo.123"), {"app_profile_id": "foo.123"}),
(
RoutingTestRequest(app_profile_id="projects/100"),
{"app_profile_id": "projects/100"},
),
(RoutingTestRequest(app_profile_id=""), {"app_profile_id": ""}),
],
)
def test_routing_rule_resolve_simple_extraction(req, expected):
rule = wrappers.RoutingRule([wrappers.RoutingParameter("app_profile_id", "")])
assert (
wrappers.RoutingRule.resolve(rule, RoutingTestRequest.to_dict(req)) == expected
)
@pytest.mark.parametrize(
"req, expected",
[
(RoutingTestRequest(app_profile_id="foo.123"), {"routing_id": "foo.123"}),
(
RoutingTestRequest(app_profile_id="projects/100"),
{"routing_id": "projects/100"},
),
(RoutingTestRequest(app_profile_id=""), {"routing_id": ""}),
],
)
def test_routing_rule_resolve_rename_extraction(req, expected):
rule = wrappers.RoutingRule(
[wrappers.RoutingParameter("app_profile_id", "{routing_id=**}")]
)
assert (
wrappers.RoutingRule.resolve(rule, RoutingTestRequest.to_dict(req)) == expected
)
@pytest.mark.parametrize(
"req, expected",
[
(
RoutingTestRequest(table_name="projects/100/instances/200"),
{"table_name": "projects/100/instances/200"},
),
(
RoutingTestRequest(table_name="projects/100/instances/200/whatever"),
{"table_name": "projects/100/instances/200/whatever"},
),
(RoutingTestRequest(table_name="foo"), {}),
],
)
def test_routing_rule_resolve_field_match(req, expected):
rule = wrappers.RoutingRule(
[
wrappers.RoutingParameter(
"table_name", "{table_name=projects/*/instances/*/**}"
),
wrappers.RoutingParameter(
"table_name", "{table_name=regions/*/zones/*/**}"
),
]
)
assert (
wrappers.RoutingRule.resolve(rule, RoutingTestRequest.to_dict(req)) == expected
)
@pytest.mark.parametrize(
"routing_parameters, req, expected",
[
(
[
wrappers.RoutingParameter(
"table_name", "{project_id=projects/*}/instances/*/**"
)
],
RoutingTestRequest(table_name="projects/100/instances/200/tables/300"),
{"project_id": "projects/100"},
),
(
[
wrappers.RoutingParameter(
"table_name", "{project_id=projects/*}/instances/*/**"
),
wrappers.RoutingParameter(
"table_name", "projects/*/{instance_id=instances/*}/**"
),
wrappers.RoutingParameter(
"doesnotexist", "projects/*/{instance_id=instances/*}/**"
),
],
RoutingTestRequest(table_name="projects/100/instances/200/tables/300"),
{"project_id": "projects/100", "instance_id": "instances/200"},
),
],
)
def test_routing_rule_resolve(routing_parameters, req, expected):
rule = wrappers.RoutingRule(routing_parameters)
got = wrappers.RoutingRule.resolve(rule, RoutingTestRequest.to_dict(req))
assert got == expected
rule = wrappers.RoutingRule(routing_parameters)
got = wrappers.RoutingRule.resolve(
rule, json.dumps(RoutingTestRequest.to_dict(req))
)
assert got == expected
@pytest.mark.parametrize(
"field, path_template, expected",
[
("table_name", "{project_id=projects/*}/instances/*/**", "project_id"),
("table_name", "projects/*/{instance_id=instances/*}/**", "instance_id"),
("table_name", "projects/*/{instance_id}/**", "instance_id"),
],
)
def test_routing_parameter_key(field, path_template, expected):
param = wrappers.RoutingParameter(field, path_template)
assert param.key == expected
def test_routing_parameter_cache_does_not_retain_instance():
unique_id = f"id_{uuid.uuid4().hex}"
param = wrappers.RoutingParameter(
f"table_name_{unique_id}", f"{{{unique_id}=projects/*}}/instances/*/**"
)
_ = param.to_regex()
_ = param.key
param_ref = weakref.ref(param)
del param
gc.collect()
assert param_ref() is None
def test_routing_parameter_multi_segment_raises():
param = wrappers.RoutingParameter(
"table_name", "{project_id=projects/*}/{instance_id=instances/*}/*/**"
)
with pytest.raises(ValueError):
param.key