|
4 | 4 | from typing import Any |
5 | 5 |
|
6 | 6 | import pytest |
| 7 | +from pydantic import BaseModel |
7 | 8 |
|
8 | | -from structured_tutorials.models import FilePartModel, TutorialModel |
| 9 | +from structured_tutorials.models import ( |
| 10 | + CommandBaseModel, |
| 11 | + FilePartModel, |
| 12 | + TestCommandModel, |
| 13 | + TestPortModel, |
| 14 | + TutorialModel, |
| 15 | +) |
9 | 16 |
|
10 | 17 |
|
11 | 18 | def test_from_file(tutorial_paths: Path) -> None: |
@@ -44,7 +51,58 @@ def test_absolute_cwd() -> None: |
44 | 51 | TutorialModel.model_validate({"path": "/foo/test.yaml", "cwd": "/foo", "parts": []}) |
45 | 52 |
|
46 | 53 |
|
| 54 | +def test_file_part_with_absolute_source() -> None: |
| 55 | + """Test that file parts have an absolute source (if set).""" |
| 56 | + with pytest.raises(ValueError, match=r"/foo: Must be a relative path"): |
| 57 | + FilePartModel.model_validate({"source": "/foo", "destination": "foo.txt"}) |
| 58 | + |
| 59 | + |
47 | 60 | def test_file_part_with_no_source_and_no_contents() -> None: |
48 | 61 | """Test that file parts have either contents or a source.""" |
49 | 62 | with pytest.raises(ValueError, match=r"Either contents or source is required\."): |
50 | 63 | FilePartModel.model_validate({"destination": "foo.txt"}) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + ("model", "data", "error"), |
| 68 | + ( |
| 69 | + (CommandBaseModel, {"status_code": -1}, r"status_code\s*Input should be greater than or equal to 0"), |
| 70 | + (CommandBaseModel, {"status_code": 256}, r"status_code\s*Input should be less than or equal to 255"), |
| 71 | + (TestCommandModel, {"delay": -1}, r"delay\s*Input should be greater than or equal to 0"), |
| 72 | + (TestCommandModel, {"retry": -1}, r"retry\s*Input should be greater than or equal to 0"), |
| 73 | + ( |
| 74 | + TestCommandModel, |
| 75 | + {"backoff_factor": -1}, |
| 76 | + r"backoff_factor\s*Input should be greater than or equal to 0", |
| 77 | + ), |
| 78 | + ( |
| 79 | + TestPortModel, |
| 80 | + {"host": "example.com", "port": 443, "delay": -1}, |
| 81 | + r"delay\s*Input should be greater than or equal to 0", |
| 82 | + ), |
| 83 | + ( |
| 84 | + TestPortModel, |
| 85 | + {"host": "example.com", "port": 443, "retry": -1}, |
| 86 | + r"retry\s*Input should be greater than or equal to 0", |
| 87 | + ), |
| 88 | + ( |
| 89 | + TestPortModel, |
| 90 | + {"host": "example.com", "port": 443, "backoff_factor": -1}, |
| 91 | + r"backoff_factor\s*Input should be greater than or equal to 0", |
| 92 | + ), |
| 93 | + ( |
| 94 | + TestPortModel, |
| 95 | + {"host": "example.com", "port": -1}, |
| 96 | + r"port\s*Input should be greater than or equal to 0", |
| 97 | + ), |
| 98 | + ( |
| 99 | + TestPortModel, |
| 100 | + {"host": "example.com", "port": 65536}, |
| 101 | + r"port\s*Input should be less than or equal to 65535", |
| 102 | + ), |
| 103 | + ), |
| 104 | +) |
| 105 | +def test_annotated_field_constraints(model: type[BaseModel], data: dict[str, Any], error: str) -> None: |
| 106 | + """Test annotated field constraints.""" |
| 107 | + with pytest.raises(ValueError, match=error): |
| 108 | + model.model_validate(data) |
0 commit comments