|
5 | 5 | import random |
6 | 6 |
|
7 | 7 | import docker |
8 | | -from pydantic import BaseModel as PydanticBaseModel |
| 8 | +from pydantic import BaseModel as PydanticBaseModel, Field |
9 | 9 | from pydanticrud import BaseModel, DynamoDbBackend, ConditionCheckFailed |
10 | 10 | import pytest |
11 | 11 | from pydanticrud.exceptions import DoesNotExist |
@@ -39,6 +39,19 @@ class Config: |
39 | 39 | global_indexes = {"by-id": ("id",)} |
40 | 40 |
|
41 | 41 |
|
| 42 | +class AliasKeyModel(BaseModel): |
| 43 | + id: int |
| 44 | + value: int |
| 45 | + name: str |
| 46 | + type_: str = Field(alias="type") |
| 47 | + |
| 48 | + class Config: |
| 49 | + title = "AliasTitle123" |
| 50 | + hash_key = "name" |
| 51 | + backend = DynamoDbBackend |
| 52 | + endpoint = "http://localhost:18002" |
| 53 | + |
| 54 | + |
42 | 55 | class ComplexKeyModel(BaseModel): |
43 | 56 | account: str |
44 | 57 | sort_date_key: str |
@@ -79,6 +92,17 @@ class Config: |
79 | 92 | endpoint = "http://localhost:18002" |
80 | 93 |
|
81 | 94 |
|
| 95 | +def alias_model_data_generator(**kwargs): |
| 96 | + data = dict( |
| 97 | + id=random.randint(0, 100000), |
| 98 | + value=random.randint(0, 100000), |
| 99 | + name=random_unique_name(), |
| 100 | + type="aliasType" |
| 101 | + ) |
| 102 | + data.update(kwargs) |
| 103 | + return data |
| 104 | + |
| 105 | + |
82 | 106 | def simple_model_data_generator(**kwargs): |
83 | 107 | data = dict( |
84 | 108 | id=random.randint(0, 100000), |
@@ -163,6 +187,14 @@ def nested_table(dynamo): |
163 | 187 | return NestedModel |
164 | 188 |
|
165 | 189 |
|
| 190 | +@pytest.fixture(scope="module") |
| 191 | +def alias_table(dynamo): |
| 192 | + if not AliasKeyModel.exists(): |
| 193 | + AliasKeyModel.initialize() |
| 194 | + assert AliasKeyModel.exists() |
| 195 | + return AliasKeyModel |
| 196 | + |
| 197 | + |
166 | 198 | @pytest.fixture(scope="module") |
167 | 199 | def simple_query_data(simple_table): |
168 | 200 | presets = [dict(name="Jerry"), dict(name="Hermione"), dict(), dict(), dict()] |
@@ -190,6 +222,18 @@ def complex_query_data(complex_table): |
190 | 222 | ComplexKeyModel.delete((datum[ComplexKeyModel.Config.hash_key], datum[ComplexKeyModel.Config.range_key])) |
191 | 223 |
|
192 | 224 |
|
| 225 | +@pytest.fixture(scope="module") |
| 226 | +def alias_query_data(alias_table): |
| 227 | + presets = [dict(name="Jerry"), dict(name="Hermione"), dict(), dict(), dict()] |
| 228 | + data = [datum for datum in [alias_model_data_generator(**i) for i in presets]] |
| 229 | + for datum in data: |
| 230 | + AliasKeyModel.parse_obj(datum).save() |
| 231 | + try: |
| 232 | + yield data |
| 233 | + finally: |
| 234 | + for datum in data: |
| 235 | + AliasKeyModel.delete(datum["name"]) |
| 236 | + |
193 | 237 | @pytest.fixture(scope="module") |
194 | 238 | def nested_query_data(nested_table): |
195 | 239 | presets = [dict()] * 5 |
@@ -348,3 +392,21 @@ def test_query_with_nested_model_optional(dynamo, nested_query_data_optional): |
348 | 392 | res = NestedModel.query(filter_expr=Rule(f"expires <= '{data_by_expires['expires']}'")) |
349 | 393 | res_data = [m.ticket for m in res] |
350 | 394 | assert any(elem is None for elem in res_data) |
| 395 | + |
| 396 | + |
| 397 | +def test_query_alias_save(dynamo): |
| 398 | + presets = [dict(name="Jerry"), dict(name="Hermione"), dict(), dict(), dict()] |
| 399 | + data = [datum for datum in [alias_model_data_generator(**i) for i in presets]] |
| 400 | + AliasKeyModel.initialize() |
| 401 | + try: |
| 402 | + for datum in data: |
| 403 | + AliasKeyModel.parse_obj(datum).save() |
| 404 | + except Exception as e: |
| 405 | + raise pytest.fail("Failed to save Alias model!") |
| 406 | + |
| 407 | +def test_get_alias_model_data(dynamo, alias_query_data): |
| 408 | + data = alias_model_data_generator() |
| 409 | + res = AliasKeyModel.get(alias_query_data[0]['name']) |
| 410 | + assert res.dict(by_alias=True) == alias_query_data[0] |
| 411 | + |
| 412 | + |
0 commit comments