Skip to content

Commit

Permalink
Address some dep warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinKlaesRackspace committed Jan 22, 2025
1 parent 69c971e commit ef98c50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
6 changes: 3 additions & 3 deletions pydanticrud/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Backend:
def __init__(self, cls):
cfg = cls.db_config
self.cls = cls
self.schema = cls.schema()
self.schema = cls.model_json_schema()
self.hash_key = cfg.hash_key
self.range_key = getattr(cfg, "range_key", None)
self.serializer = DynamoSerializer(self.schema, ttl_field=getattr(cfg, "ttl", None))
Expand Down Expand Up @@ -427,7 +427,7 @@ def get(self, key: Union[Dict, Any]):
return self.serializer.deserialize_record(resp["Item"])

def save(self, item, condition: Optional[Rule] = None) -> bool:
data = self.serializer.serialize_record(item.dict(by_alias=True))
data = self.serializer.serialize_record(item.model_dump(by_alias=True))

try:
if condition:
Expand Down Expand Up @@ -462,7 +462,7 @@ def batch_save(self, items: list) -> dict:
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/client/batch_write_item.html#:~:text=The%20BatchWriteItem%20operation,Data%20Types.
for chunk in chunk_list(items, 25):
serialized_items = [
self.serializer.serialize_record(item.dict(by_alias=True)) for item in chunk
self.serializer.serialize_record(item.model_dump(by_alias=True)) for item in chunk
]
for serialized_item in serialized_items:
request_items[self.table_name].append({"PutRequest": {"Item": serialized_item}})
Expand Down
2 changes: 1 addition & 1 deletion pydanticrud/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def save(self, item, condition: Optional[Rule] = None) -> bool:
key = getattr(item, hash_key)
fields = tuple(self._columns.keys())

item_data = item.dict()
item_data = item.model_dump()
values = tuple([item_data[field] for field in fields])
try:
old_item = self.get(key)
Expand Down
46 changes: 24 additions & 22 deletions tests/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,14 @@ def nested_model_data_generator(include_ticket=True, **kwargs):
expires=future_datetime(
days=1, hours=random.randint(1, 12), minutes=random.randint(1, 58)
).isoformat(),
ticket={
"created_time": random_datetime().isoformat(),
"number": str(random.randint(0, 1000)),
}
if include_ticket
else None,
ticket=(
{
"created_time": random_datetime().isoformat(),
"number": str(random.randint(0, 1000)),
}
if include_ticket
else None
),
other=random.choice(
[
{
Expand Down Expand Up @@ -321,7 +323,7 @@ def test_save_get_delete_simple(dynamo, simple_table):
a.save()
try:
b = SimpleKeyModel.get(data["name"])
assert b.dict() == a.model_dump()
assert b.model_dump() == a.model_dump()
finally:
SimpleKeyModel.delete(data["name"])

Expand Down Expand Up @@ -349,7 +351,7 @@ def test_save_ttl_field_is_float(dynamo, simple_query_data):

def test_query_with_hash_key_simple(dynamo, simple_query_data):
res = SimpleKeyModel.query(Rule(f"name == '{simple_query_data[0]['name']}'"))
res_data = {m.name: m.dict() for m in res}
res_data = {m.name: m.model_dump() for m in res}
simple_query_data[0]["data"] = {} # This is a default value and should be populated as such
assert res_data == {simple_query_data[0]["name"]: simple_query_data[0]}

Expand All @@ -375,7 +377,7 @@ def test_query_with_indexed_hash_key_simple(dynamo, simple_query_data):
data_by_timestamp = simple_query_data[:]
data_by_timestamp.sort(key=lambda d: d["timestamp"])
res = SimpleKeyModel.query(Rule(f"id == {data_by_timestamp[0]['id']}"))
res_data = {m.name: m.dict() for m in res}
res_data = {m.name: m.model_dump() for m in res}
assert res_data == {data_by_timestamp[0]["name"]: data_by_timestamp[0]}


Expand All @@ -402,13 +404,13 @@ def test_query_scan_simple(dynamo, simple_query_data):
res = SimpleKeyModel.query(
filter_expr=Rule(f"timestamp <= '{data_by_timestamp[2]['timestamp']}'")
)
res_data = {m.name: m.dict() for m in res}
res_data = {m.name: m.model_dump() for m in res}
assert res_data == {d["name"]: d for d in data_by_timestamp[:2]}


def test_query_scan_contains_simple(dynamo, simple_query_data):
res = SimpleKeyModel.query(filter_expr=Rule(f"'{simple_query_data[2]['items'][1]}' in items"))
res_data = {m.name: m.dict() for m in res}
res_data = {m.name: m.model_dump() for m in res}
assert res_data == {simple_query_data[2]["name"]: simple_query_data[2]}


Expand All @@ -418,7 +420,7 @@ def test_save_get_delete_complex(dynamo, complex_table):
a.save()
try:
b = ComplexKeyModel.get((data["account"], data["sort_date_key"]))
assert b.dict() == a.model_dump()
assert b.model_dump() == a.model_dump()
finally:
ComplexKeyModel.delete((data["account"], data["sort_date_key"]))

Expand All @@ -433,14 +435,14 @@ def test_query_with_hash_key_complex(dynamo, complex_query_data):
res = ComplexKeyModel.query(
Rule(f"account == '{record['account']}' and sort_date_key == '{record['sort_date_key']}'")
)
res_data = {(m.account, m.sort_date_key): m.dict() for m in res}
res_data = {(m.account, m.sort_date_key): m.model_dump() for m in res}
assert res_data == {(record["account"], record["sort_date_key"]): record}

# Check that it works regardless of query attribute order
res = ComplexKeyModel.query(
Rule(f"sort_date_key == '{record['sort_date_key']}' and account == '{record['account']}'")
)
res_data = {(m.account, m.sort_date_key): m.dict() for m in res}
res_data = {(m.account, m.sort_date_key): m.model_dump() for m in res}
assert res_data == {(record["account"], record["sort_date_key"]): record}


Expand Down Expand Up @@ -561,21 +563,21 @@ def test_query_with_indexed_hash_key_complex(dynamo, complex_query_data):
res = ComplexKeyModel.query(
Rule(f"account == '{record['account']}' and thread_id == '{record['thread_id']}'")
)
res_data = {(m.account, m.thread_id): m.dict() for m in res}
res_data = {(m.account, m.thread_id): m.model_dump() for m in res}
assert res_data == {(record["account"], record["thread_id"]): record}

res = ComplexKeyModel.query(
Rule(f"thread_id == '{record['thread_id']}' and account == '{record['account']}'")
)
res_data = {(m.account, m.thread_id): m.dict() for m in res}
res_data = {(m.account, m.thread_id): m.model_dump() for m in res}
assert res_data == {(record["account"], record["thread_id"]): record}


def test_query_scan_complex(dynamo, complex_query_data):
data_by_expires = complex_query_data[:]
data_by_expires.sort(key=lambda d: d["expires"])
res = ComplexKeyModel.query(filter_expr=Rule(f"expires <= '{data_by_expires[2]['expires']}'"))
res_data = {(m.account, m.sort_date_key): m.dict() for m in res}
res_data = {(m.account, m.sort_date_key): m.model_dump() for m in res}
assert res_data == {(d["account"], d["sort_date_key"]): d for d in data_by_expires[:3]}


Expand Down Expand Up @@ -607,30 +609,30 @@ def test_query_alias_save(dynamo):
def test_get_alias_model_data(dynamo, alias_query_data):
data = alias_model_data_generator()
res = AliasKeyModel.get(alias_query_data[0]["name"])
assert res.dict(by_alias=True) == alias_query_data[0]
assert res.model_dump(by_alias=True) == alias_query_data[0]


def test_get_simple_model_data_via_index(dynamo, simple_query_data):
data = simple_model_data_generator()
res = SimpleKeyModel.get(simple_query_data[0]["name"])
assert res.dict(by_alias=True) == simple_query_data[0]
assert res.model_dump(by_alias=True) == simple_query_data[0]
res = SimpleKeyModel.get({"id": simple_query_data[0]["id"]})
assert res.dict(by_alias=True) == simple_query_data[0]
assert res.model_dump(by_alias=True) == simple_query_data[0]


def test_get_complex_model_data_via_index(dynamo, complex_query_data):
data = complex_model_data_generator()
res = ComplexKeyModel.get(
(complex_query_data[0]["account"], complex_query_data[0]["sort_date_key"])
)
assert res.dict(by_alias=True) == complex_query_data[0]
assert res.model_dump(by_alias=True) == complex_query_data[0]
res = ComplexKeyModel.get(
{
"account": complex_query_data[0]["account"],
"notification_id": complex_query_data[0]["notification_id"],
}
)
assert res.dict(by_alias=True) == complex_query_data[0]
assert res.model_dump(by_alias=True) == complex_query_data[0]


def test_alias_model_validator_ingest(dynamo):
Expand Down

0 comments on commit ef98c50

Please sign in to comment.