|
| 1 | +"""Integration tests for JSONAttribute with RootModel and arbitrary types. |
| 2 | +
|
| 3 | +Regression tests for https://github.com/ferrumio/pydynox/issues/367 |
| 4 | +""" |
| 5 | + |
| 6 | +import uuid |
| 7 | + |
| 8 | +from pydantic import BaseModel, RootModel |
| 9 | +from pydynox import Model, ModelConfig |
| 10 | +from pydynox.attributes import JSONAttribute, StringAttribute |
| 11 | +from pydynox.testing import MemoryBackend |
| 12 | + |
| 13 | + |
| 14 | +class Item(BaseModel): |
| 15 | + name: str |
| 16 | + qty: int |
| 17 | + |
| 18 | + |
| 19 | +class ItemList(RootModel[list[Item]]): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +class TagList(RootModel[list[str]]): |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class Inventory(Model): |
| 28 | + model_config = ModelConfig(table="test_table") |
| 29 | + pk = StringAttribute(partition_key=True) |
| 30 | + sk = StringAttribute(sort_key=True) |
| 31 | + items = JSONAttribute(ItemList) |
| 32 | + |
| 33 | + |
| 34 | +class TagModel(Model): |
| 35 | + model_config = ModelConfig(table="test_table") |
| 36 | + pk = StringAttribute(partition_key=True) |
| 37 | + sk = StringAttribute(sort_key=True) |
| 38 | + tags = JSONAttribute(TagList) |
| 39 | + |
| 40 | + |
| 41 | +class ScoreModel(Model): |
| 42 | + model_config = ModelConfig(table="test_table") |
| 43 | + pk = StringAttribute(partition_key=True) |
| 44 | + sk = StringAttribute(sort_key=True) |
| 45 | + scores = JSONAttribute(list[str]) |
| 46 | + |
| 47 | + |
| 48 | +def test_rootmodel_list_save_and_get(dynamo): |
| 49 | + """RootModel[list[...]] round-trips through save/get.""" |
| 50 | + pk = f"RM#{uuid.uuid4().hex[:8]}" |
| 51 | + Inventory.model_config = ModelConfig(table="test_table", client=dynamo) |
| 52 | + |
| 53 | + # GIVEN items as a RootModel list |
| 54 | + items = ItemList([Item(name="widget", qty=5), Item(name="gadget", qty=3)]) |
| 55 | + inv = Inventory(pk=pk, sk="INV", items=items) |
| 56 | + |
| 57 | + # WHEN saving and retrieving |
| 58 | + inv.sync_save() |
| 59 | + retrieved = Inventory.sync_get(pk=pk, sk="INV") |
| 60 | + |
| 61 | + # THEN the RootModel is reconstructed |
| 62 | + assert isinstance(retrieved.items, ItemList) |
| 63 | + assert len(retrieved.items.root) == 2 |
| 64 | + assert retrieved.items.root[0].name == "widget" |
| 65 | + assert retrieved.items.root[1].qty == 3 |
| 66 | + |
| 67 | + |
| 68 | +def test_rootmodel_list_update(dynamo): |
| 69 | + """RootModel[list[...]] works with sync_update.""" |
| 70 | + pk = f"RM_UPD#{uuid.uuid4().hex[:8]}" |
| 71 | + Inventory.model_config = ModelConfig(table="test_table", client=dynamo) |
| 72 | + |
| 73 | + # GIVEN a saved inventory |
| 74 | + items = ItemList([Item(name="widget", qty=5)]) |
| 75 | + inv = Inventory(pk=pk, sk="INV", items=items) |
| 76 | + inv.sync_save() |
| 77 | + |
| 78 | + # WHEN updating with a new list |
| 79 | + new_items = ItemList([Item(name="gizmo", qty=10)]) |
| 80 | + inv.sync_update(items=new_items) |
| 81 | + |
| 82 | + # THEN the update is persisted |
| 83 | + retrieved = Inventory.sync_get(pk=pk, sk="INV") |
| 84 | + assert retrieved.items.root[0].name == "gizmo" |
| 85 | + assert retrieved.items.root[0].qty == 10 |
| 86 | + |
| 87 | + |
| 88 | +def test_rootmodel_strings_save_and_get(dynamo): |
| 89 | + """RootModel[list[str]] round-trips correctly.""" |
| 90 | + pk = f"RM_STR#{uuid.uuid4().hex[:8]}" |
| 91 | + TagModel.model_config = ModelConfig(table="test_table", client=dynamo) |
| 92 | + |
| 93 | + tags = TagList(["python", "rust", "dynamodb"]) |
| 94 | + model = TagModel(pk=pk, sk="TAGS", tags=tags) |
| 95 | + model.sync_save() |
| 96 | + |
| 97 | + retrieved = TagModel.sync_get(pk=pk, sk="TAGS") |
| 98 | + assert isinstance(retrieved.tags, TagList) |
| 99 | + assert retrieved.tags.root == ["python", "rust", "dynamodb"] |
| 100 | + |
| 101 | + |
| 102 | +def test_arbitrary_type_save_and_get(dynamo): |
| 103 | + """JSONAttribute(list[str]) works with arbitrary types via TypeAdapter.""" |
| 104 | + pk = f"ARB#{uuid.uuid4().hex[:8]}" |
| 105 | + ScoreModel.model_config = ModelConfig(table="test_table", client=dynamo) |
| 106 | + |
| 107 | + model = ScoreModel(pk=pk, sk="SCORES", scores=["high", "medium", "low"]) |
| 108 | + model.sync_save() |
| 109 | + |
| 110 | + retrieved = ScoreModel.sync_get(pk=pk, sk="SCORES") |
| 111 | + assert retrieved.scores == ["high", "medium", "low"] |
| 112 | + |
| 113 | + |
| 114 | +@MemoryBackend() |
| 115 | +def test_rootmodel_inplace_mutation_detected(): |
| 116 | + """In-place mutation on RootModel is detected on save.""" |
| 117 | + items = ItemList([Item(name="widget", qty=5)]) |
| 118 | + inv = Inventory(pk="INV#1", sk="INV", items=items) |
| 119 | + inv.sync_save() |
| 120 | + |
| 121 | + loaded = Inventory.sync_get(pk="INV#1", sk="INV") |
| 122 | + loaded.items.root.append(Item(name="gadget", qty=2)) |
| 123 | + loaded.sync_save() |
| 124 | + |
| 125 | + result = Inventory.sync_get(pk="INV#1", sk="INV") |
| 126 | + assert len(result.items.root) == 2 |
| 127 | + assert result.items.root[1].name == "gadget" |
0 commit comments