|
| 1 | +import jsonschema |
1 | 2 | import pytest
|
2 | 3 | from pydantic import BaseModel, ConfigDict, ValidationError
|
3 | 4 |
|
4 |
| -from nwastdlib.vlans import VlanRanges |
| 5 | +from nwastdlib.vlans import VLAN_RANGE_JSON_SCHEMA_REGEX, VlanRanges |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def test_vlan_ranges_instantiation():
|
@@ -235,7 +236,7 @@ class MyModel(BaseModel):
|
235 | 236 | "title": "Vlanranges",
|
236 | 237 | "type": "string",
|
237 | 238 | "format": "vlan",
|
238 |
| - "pattern": "^([1-4][0-9]{0,3}(-[1-4][0-9]{0,3})?,?)+$", |
| 239 | + "pattern": VLAN_RANGE_JSON_SCHEMA_REGEX, |
239 | 240 | "examples": ["345", "20-23,45,50-100"],
|
240 | 241 | },
|
241 | 242 | },
|
@@ -293,3 +294,37 @@ def get_dummy_model():
|
293 | 294 |
|
294 | 295 | response = fastapi_test_client.get("/dynamic_model")
|
295 | 296 | assert response.json() == {"name": "DummyModel", "vlanrange": "10"}
|
| 297 | + |
| 298 | + |
| 299 | +@pytest.fixture(scope="module") |
| 300 | +def vr_schema(): |
| 301 | + class MyModel(BaseModel): |
| 302 | + vr: VlanRanges |
| 303 | + |
| 304 | + return MyModel.model_json_schema() |
| 305 | + |
| 306 | + |
| 307 | +@pytest.mark.parametrize("vlan", [1, 9, 10, 49, 50, 99, 100, 101, 499, 500, 999, 1000, 3999, 4000, 4999]) |
| 308 | +def test_vlan_allowed_by_schema(vlan, vr_schema): |
| 309 | + """Test vlans which should be allowed by the JSON Schema.""" |
| 310 | + assert jsonschema.validate({"vr": str(vlan)}, vr_schema) is None |
| 311 | + |
| 312 | + |
| 313 | +@pytest.mark.parametrize("vlan", [-5000, -50, -1, 0, 5000, 5001, 9999, 10000, 14999]) |
| 314 | +def test_vlan_rejected_by_schema(vlan, vr_schema): |
| 315 | + """Test vlans which should be rejected by the JSON Schema.""" |
| 316 | + with pytest.raises(jsonschema.ValidationError): |
| 317 | + jsonschema.validate({"vr": str(vlan)}, vr_schema) |
| 318 | + |
| 319 | + |
| 320 | +@pytest.mark.parametrize("vlanrange", ["1-1", "9-9", "49-59", "99-300", "999-1001", "3999-4999"]) |
| 321 | +def test_vlan_ranges_allowed_by_schema(vlanrange, vr_schema): |
| 322 | + """Test vlan ranges which should be allowed by the JSON Schema.""" |
| 323 | + assert jsonschema.validate({"vr": vlanrange}, vr_schema) is None |
| 324 | + |
| 325 | + |
| 326 | +@pytest.mark.parametrize("vlanrange", ["-1", "-10-0", "0-1", "4999-5000", "5000-5002"]) |
| 327 | +def test_vlan_ranges_rejected_by_schema(vlanrange, vr_schema): |
| 328 | + """Test vlan ranges which should be rejected by the JSON Schema.""" |
| 329 | + with pytest.raises(jsonschema.ValidationError): |
| 330 | + jsonschema.validate({"vr": vlanrange}, vr_schema) |
0 commit comments