|
| 1 | +import pytest |
1 | 2 | from graphql import graphql |
2 | 3 |
|
3 | 4 | from infrahub.core.branch import Branch |
4 | 5 | from infrahub.core.node import Node |
| 6 | +from infrahub.core.utils import collapse_ipv6 |
5 | 7 | from infrahub.database import InfrahubDatabase |
6 | 8 | from infrahub.graphql.initialization import prepare_graphql_params |
7 | 9 |
|
@@ -170,11 +172,60 @@ async def test_search_ipv6_network_extended_format( |
170 | 172 | ) |
171 | 173 |
|
172 | 174 |
|
| 175 | +async def test_search_ipv6_partial_address( |
| 176 | + db: InfrahubDatabase, |
| 177 | + ip_dataset_01, |
| 178 | + branch: Branch, |
| 179 | +): |
| 180 | + gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=branch) |
| 181 | + |
| 182 | + res_two_segments = await graphql( |
| 183 | + schema=gql_params.schema, |
| 184 | + source=SEARCH_QUERY, |
| 185 | + context_value=gql_params.context, |
| 186 | + root_value=None, |
| 187 | + variable_values={"search": "2001:0db8"}, |
| 188 | + ) |
| 189 | + |
| 190 | + res_partial_segment_1 = await graphql( |
| 191 | + schema=gql_params.schema, |
| 192 | + source=SEARCH_QUERY, |
| 193 | + context_value=gql_params.context, |
| 194 | + root_value=None, |
| 195 | + variable_values={"search": "2001:0db8:0"}, |
| 196 | + ) |
| 197 | + |
| 198 | + res_partial_segment_2 = await graphql( |
| 199 | + schema=gql_params.schema, |
| 200 | + source=SEARCH_QUERY, |
| 201 | + context_value=gql_params.context, |
| 202 | + root_value=None, |
| 203 | + variable_values={"search": "2001:0db8:0000:0"}, |
| 204 | + ) |
| 205 | + |
| 206 | + assert ( |
| 207 | + res_two_segments.data["InfrahubSearchAnywhere"]["count"] |
| 208 | + == res_partial_segment_1.data["InfrahubSearchAnywhere"]["count"] |
| 209 | + == res_partial_segment_2.data["InfrahubSearchAnywhere"]["count"] |
| 210 | + == 2 |
| 211 | + ) |
| 212 | + |
| 213 | + assert ( |
| 214 | + res_two_segments.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 215 | + == res_partial_segment_1.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 216 | + == res_partial_segment_2.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
| 217 | + ) |
| 218 | + |
| 219 | + |
173 | 220 | async def test_search_ipv4( |
174 | 221 | db: InfrahubDatabase, |
175 | 222 | ip_dataset_01, |
176 | 223 | branch: Branch, |
177 | 224 | ): |
| 225 | + """ |
| 226 | + This only tests that ipv6 search specific behavior does not break ipv4 search. |
| 227 | + """ |
| 228 | + |
178 | 229 | gql_params = prepare_graphql_params(db=db, include_subscription=False, branch=branch) |
179 | 230 |
|
180 | 231 | result_address = await graphql( |
@@ -203,3 +254,37 @@ async def test_search_ipv4( |
203 | 254 | result_address.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
204 | 255 | == result_network.data["InfrahubSearchAnywhere"]["edges"][0]["node"]["id"] |
205 | 256 | ) |
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.parametrize( |
| 260 | + "query,expected", |
| 261 | + [ |
| 262 | + ("2001:0db8:0000:0000:0000:0000:0000:0000/48", "2001:db8::/48"), |
| 263 | + ("2001:0db8:0000:0000:0000:0000:0000:0000", "2001:db8::"), |
| 264 | + ("2001:0db8", "2001:db8"), |
| 265 | + ("2001:0db8:0", "2001:db8"), |
| 266 | + ("2001:0db8:0000", "2001:db8"), |
| 267 | + ("2001:0db8:0000:0", "2001:db8"), |
| 268 | + ("2001:0db8:0000:0000:00", "2001:db8"), |
| 269 | + ("2001:0db8:0000:0001:00", "2001:db8:0:1"), |
| 270 | + ("2001:0db8:0001:0002:00", "2001:db8:1:2"), |
| 271 | + ("2001:0db8:0001:0000:0002:0000:0003", "2001:db8:1:0:2:0:3"), |
| 272 | + ], |
| 273 | +) |
| 274 | +def test_collapse_ipv6_address_or_network(query, expected): |
| 275 | + assert collapse_ipv6(query) == expected |
| 276 | + |
| 277 | + |
| 278 | +@pytest.mark.parametrize( |
| 279 | + "query", |
| 280 | + [ |
| 281 | + "invalid", |
| 282 | + "invalid:case", |
| 283 | + "2001:invalid", |
| 284 | + "2001:0db81:0000", |
| 285 | + "10.0.0.0", |
| 286 | + ], |
| 287 | +) |
| 288 | +def test_collapse_ipv6_address_or_network_invalid_cases(query): |
| 289 | + with pytest.raises(ValueError): |
| 290 | + collapse_ipv6(query) |
0 commit comments