-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: table_spec: add new table_from_dict and table_dump_astuple APIs…
…, update table_dump_* and table_from_* methods signature (#51) Detailed changes include: 1. table_spec: add table_from_dict and table_dump_astuple APIs. 2. table_spec: now table_from_* APIs take with_validation arg to allow skip pydantic validation when importing. 3. table_spec: table_dump_* and table_from_* APIs now take kwargs, which will be passed to pydantic model_dump API underneath. 4. add tests for the from and dump series APIs. Other minor changes: 1. table_spec: remove debug loggings from this module.
- Loading branch information
Showing
3 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
from typing import Any, Iterable, Optional | ||
|
||
import pytest | ||
from typing_extensions import Annotated | ||
|
||
from simple_sqlite3_orm import ConstrainRepr, TableSpec, TypeAffinityRepr | ||
|
||
|
||
class SimpleTableForTest(TableSpec): | ||
id: Annotated[ | ||
int, | ||
TypeAffinityRepr(int), | ||
ConstrainRepr("PRIMARY KEY"), | ||
] | ||
|
||
id_str: Annotated[ | ||
str, | ||
TypeAffinityRepr(str), | ||
ConstrainRepr("NOT NULL"), | ||
] | ||
|
||
extra: Annotated[ | ||
Optional[float], | ||
TypeAffinityRepr(float), | ||
] = None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"_in, _validate, _expected", | ||
( | ||
([1, "1", 1.0], True, SimpleTableForTest(id=1, id_str="1", extra=1.0)), | ||
([1, "1", 1.0], False, SimpleTableForTest(id=1, id_str="1", extra=1.0)), | ||
), | ||
) | ||
def test_table_from_tuple( | ||
_in: Iterable[Any], _validate: bool, _expected: SimpleTableForTest | ||
): | ||
assert ( | ||
SimpleTableForTest.table_from_tuple(_in, with_validation=_validate) == _expected | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"_in, _validate, _expected", | ||
( | ||
( | ||
{"id": 1, "id_str": "1", "extra": 1.0}, | ||
True, | ||
SimpleTableForTest(id=1, id_str="1", extra=1.0), | ||
), | ||
( | ||
{"id": 1, "id_str": "1", "extra": 1.0}, | ||
False, | ||
SimpleTableForTest(id=1, id_str="1", extra=1.0), | ||
), | ||
), | ||
) | ||
def test_table_from_dict( | ||
_in: Mapping[str, Any], _validate: bool, _expected: SimpleTableForTest | ||
): | ||
assert ( | ||
SimpleTableForTest.table_from_dict(_in, with_validation=_validate) == _expected | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"_in, _expected", | ||
( | ||
( | ||
SimpleTableForTest(id=1, id_str="1", extra=1.0), | ||
{"id": 1, "id_str": "1", "extra": 1.0}, | ||
), | ||
( | ||
SimpleTableForTest(id=1, id_str="1", extra=1.0), | ||
{"id": 1, "extra": 1.0}, | ||
), | ||
), | ||
) | ||
def test_table_dump_asdict(_in: SimpleTableForTest, _expected: dict[str, Any]): | ||
assert _in.table_dump_asdict(*_expected) == _expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"_in, _cols, _expected", | ||
( | ||
( | ||
SimpleTableForTest(id=1, id_str="1", extra=1.0), | ||
["id", "id_str", "extra"], | ||
(1, "1", 1.0), | ||
), | ||
( | ||
SimpleTableForTest(id=1, id_str="1", extra=1.0), | ||
["extra"], | ||
(1.0,), | ||
), | ||
), | ||
) | ||
def test_table_dump_astuple( | ||
_in: SimpleTableForTest, _cols: tuple[str, ...], _expected: tuple[Any, ...] | ||
): | ||
assert _in.table_dump_astuple(*_cols) == _expected |
5044f51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report