From 06bdd61c8e630d398e8fa674c88ee9d64b63e688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Thu, 26 Sep 2024 12:21:23 +0200 Subject: [PATCH] fixup! --- lnbits/db.py | 12 +++++----- tests/helpers.py | 15 +++++++++++++ tests/unit/test_helpers_query.py | 38 ++++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/lnbits/db.py b/lnbits/db.py index 5261b478d..95c526722 100644 --- a/lnbits/db.py +++ b/lnbits/db.py @@ -155,7 +155,7 @@ class Connection(Compat): if not row: return [] if model: - return [_dict_to_model(r, model) for r in row] + return [dict_to_model(r, model) for r in row] return row async def fetchone( @@ -166,18 +166,18 @@ class Connection(Compat): row = result.mappings().first() result.close() if model and row: - return _dict_to_model(row, model) + return dict_to_model(row, model) return row async def update(self, table_name: str, model: BaseModel, where: str = "id = :id"): await self.conn.execute( - text(update_query(table_name, model, where)), _model_to_dict(model) + text(update_query(table_name, model, where)), model_to_dict(model) ) await self.conn.commit() async def insert(self, table_name: str, model: BaseModel): await self.conn.execute( - text(insert_query(table_name, model)), _model_to_dict(model) + text(insert_query(table_name, model)), model_to_dict(model) ) await self.conn.commit() @@ -584,7 +584,7 @@ def update_query( return f"UPDATE {table_name} SET {query} {where}" -def _model_to_dict(model: BaseModel) -> dict: +def model_to_dict(model: BaseModel) -> dict: _dict = model.dict() for key, value in _dict.items(): if key.startswith("_"): @@ -595,7 +595,7 @@ def _model_to_dict(model: BaseModel) -> dict: return _dict -def _dict_to_model(_dict: dict, model: TModel) -> TModel: +def dict_to_model(_dict: dict, model: TModel) -> TModel: for key, value in _dict.items(): type_ = model.__fields__[key].type_ if type_ is BaseModel: diff --git a/tests/helpers.py b/tests/helpers.py index dd039cfa6..8f477b727 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -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) diff --git a/tests/unit/test_helpers_query.py b/tests/unit/test_helpers_query.py index ce8d7cc56..c99a203d0 100644 --- a/tests/unit/test_helpers_query.py +++ b/tests/unit/test_helpers_query.py @@ -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