This commit is contained in:
dni ⚡
2024-10-10 09:07:36 +02:00
parent d813544bf4
commit 06bdd61c8e
3 changed files with 52 additions and 13 deletions
+15
View File
@@ -2,6 +2,8 @@ import random
import string
from typing import Optional
from pydantic import BaseModel
from lnbits.db import FromRowModel
from lnbits.wallets import get_funding_source, set_funding_source
@@ -16,6 +18,19 @@ class DbTestModel(FromRowModel):
value: Optional[str] = None
class DbTestModelInner(BaseModel):
id: int
label: str
description: Optional[str] = None
class DbTestModel2(BaseModel):
id: int
name: str
value: Optional[str] = None
child: DbTestModelInner
def get_random_string(iterations: int = 10):
return "".join(
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
+31 -7
View File
@@ -1,28 +1,52 @@
import pytest
from lnbits.db import (
dict_to_model,
insert_query,
model_to_dict,
update_query,
)
from tests.helpers import DbTestModel
from tests.helpers import DbTestModel2, DbTestModelInner
test = DbTestModel(id=1, name="test", value="yes")
test_data = DbTestModel2(
id=1,
name="test",
value="myvalue",
child=DbTestModelInner(id=2, label="mylabel", description="mydesc"),
)
@pytest.mark.asyncio
async def test_helpers_insert_query():
q = insert_query("test_helpers_query", test)
q = insert_query("test_helpers_query", test_data)
assert (
q == "INSERT INTO test_helpers_query (id, name, value) "
"VALUES (:id, :name, :value)"
q == "INSERT INTO test_helpers_query (id, name, value, child) "
"VALUES (:id, :name, :value, :child)"
)
@pytest.mark.asyncio
async def test_helpers_update_query():
q = update_query("test_helpers_query", test)
q = update_query("test_helpers_query", test_data)
assert (
q == "UPDATE test_helpers_query "
"SET id = :id, name = :name, value = :value "
"SET id = :id, name = :name, value = :value, child = :child "
"WHERE id = :id"
)
@pytest.mark.asyncio
async def test_helpers_model_to_dict():
d = model_to_dict(test_data)
assert d == {
"id": 1,
"name": "test",
"value": "myvalue",
"child": {"id": 2, "label": "mylabel", "description": "mydesc"},
}
@pytest.mark.asyncio
async def test_helpers_dict_to_model():
m = dict_to_model(model_to_dict(test_data), DbTestModel2)
assert m == test_data