|
| 1 | +import typing |
1 | 2 | from typing import Annotated |
2 | 3 |
|
3 | 4 | # pylint: disable=redefined-outer-name |
|
6 | 7 | import pytest |
7 | 8 | from faker import Faker |
8 | 9 | from pydantic import StringConstraints |
9 | | -from servicelib.celery.models import OwnerMetadata, TaskUUID, Wildcard |
| 10 | +from servicelib.celery.models import ( |
| 11 | + _VALID_VALUE_TYPES, |
| 12 | + OwnerMetadata, |
| 13 | + TaskUUID, |
| 14 | + Wildcard, |
| 15 | +) |
10 | 16 |
|
11 | 17 | _faker = Faker() |
12 | 18 |
|
@@ -68,17 +74,31 @@ async def test_task_filter_task_uuid( |
68 | 74 | async def test_create_task_filter_from_task_id(): |
69 | 75 |
|
70 | 76 | class MyModel(OwnerMetadata): |
71 | | - _int: int |
72 | | - _bool: bool |
73 | | - _str: str |
74 | | - _list: list[str] |
75 | | - |
76 | | - mymodel = MyModel( |
77 | | - _int=1, _bool=True, _str="test", _list=["a", "b"], owner="myowner" |
78 | | - ) |
| 77 | + int_: int |
| 78 | + bool_: bool |
| 79 | + str_: str |
| 80 | + float_: float |
| 81 | + |
| 82 | + # Check that all elements in _VALID_VALUE_TYPES are represented in MyModel's field types |
| 83 | + mymodel_types = set() |
| 84 | + for field in MyModel.model_fields.values(): |
| 85 | + field_type = field.annotation |
| 86 | + origin = typing.get_origin(field_type) |
| 87 | + if origin is typing.Union: |
| 88 | + types_to_check = typing.get_args(field_type) |
| 89 | + else: |
| 90 | + types_to_check = [field_type] |
| 91 | + for t in types_to_check: |
| 92 | + if t is not Wildcard: |
| 93 | + mymodel_types.add(t) |
| 94 | + for valid_type in _VALID_VALUE_TYPES: |
| 95 | + assert valid_type in mymodel_types, f"{valid_type} not represented in MyModel" |
| 96 | + |
| 97 | + mymodel = MyModel(int_=1, bool_=True, str_="test", float_=1.0, owner="myowner") |
79 | 98 | task_uuid = TaskUUID(_faker.uuid4()) |
80 | 99 | task_id = mymodel.create_task_id(task_uuid) |
81 | | - assert OwnerMetadata.recreate_as_model(task_id=task_id, schema=MyModel) == mymodel |
| 100 | + mymodel_recreated = MyModel.validate_from_task_id(task_id=task_id) |
| 101 | + assert mymodel_recreated == mymodel |
82 | 102 |
|
83 | 103 |
|
84 | 104 | @pytest.mark.parametrize( |
|
0 commit comments