Skip to content

Commit

Permalink
refactor: orm: directly define __class_getitem__ by assigning with pa…
Browse files Browse the repository at this point in the history
…rameterized_class_getitem (#66)
  • Loading branch information
pga2rn authored Dec 25, 2024
1 parent 6eb3a44 commit 85c15cc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/simple_sqlite3_orm/_orm/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async def _gen() -> AsyncGenerator[TableSpecType]:

return _gen()

_wrapped.__doc__ = func.__doc__
return _wrapped


Expand Down Expand Up @@ -139,8 +140,7 @@ def __init__(

self._loop = asyncio.get_running_loop()

def __class_getitem__(cls, params):
return parameterized_class_getitem(cls, params)
__class_getitem__ = classmethod(parameterized_class_getitem)

@cached_property
def orm_table_name(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions src/simple_sqlite3_orm/_orm/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ def __init__(
self._con = con()
row_factory_setter(self._con, self.orm_table_spec, row_factory)

def __class_getitem__(cls, params):
return parameterized_class_getitem(cls, params)
__class_getitem__ = classmethod(parameterized_class_getitem)

@property
def orm_con(self) -> sqlite3.Connection:
Expand Down
3 changes: 1 addition & 2 deletions src/simple_sqlite3_orm/_orm/_multi_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def __init__(
thread_name_prefix=thread_name_prefix,
)

def __class_getitem__(cls, params):
return parameterized_class_getitem(cls, params)
__class_getitem__ = classmethod(parameterized_class_getitem)

def _thread_initializer(self, con_factory, row_factory) -> None:
"""Prepare thread_scope ORMBase instance for this worker thread."""
Expand Down
10 changes: 4 additions & 6 deletions tests/test__orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@


class ORMTest(ORMBase[SampleTable]):
pass
_orm_table_name = "test_orm_table"


TABLE_NAME = "test_orm_table"

_cur_timestamp = time.time()
mstr = Mystr(_generate_random_str())
entry_for_test = SampleTable(
Expand Down Expand Up @@ -49,14 +47,14 @@ class TestORMBase:
@pytest.fixture(scope="class")
def setup_connection(self):
with sqlite3.connect(":memory:") as conn:
orm_inst = ORMTest(conn, table_name=TABLE_NAME)
orm_inst = ORMTest(conn)
yield orm_inst

def test_create_without_rowid_table(self):
"""NOTE: to test select_all_with_pagination, we cannot specify without_rowid, so we
create this test case dedicated for creating without_rowid table test."""
with sqlite3.connect(":memory:") as conn:
orm_inst = ORMTest(conn, table_name=TABLE_NAME)
orm_inst = ORMTest(conn)
orm_inst.orm_create_table(without_rowid=True)

def test_create_table(self, setup_connection: ORMTest):
Expand Down Expand Up @@ -137,7 +135,7 @@ def test_select_all_entries(self, setup_connection: ORMTest):

def test_function_call(self, setup_connection: ORMTest):
with setup_connection.orm_con as con:
cur = con.execute(f"SELECT count(*) FROM {TABLE_NAME};")
cur = con.execute(f"SELECT count(*) FROM {ORMTest._orm_table_name};")
res = cur.fetchone()
assert res[0] == 1

Expand Down

1 comment on commit 85c15cc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/simple_sqlite3_orm
   __init__.py12375%28–30
   _sqlite_spec.py360100% 
   _table_spec.py1883481%49, 58, 69, 73–74, 86, 98, 176, 181–183, 223, 304, 306, 308–309, 357, 360, 364–366, 368–372, 374, 380, 423–424, 534–535, 549–550
   _types.py24195%34
   _utils.py681085%40, 42–43, 46–47, 69, 84, 87, 97, 110
   utils.py1243670%127, 134–135, 170–173, 219, 232, 252–256, 282, 286, 333, 338–345, 357–359, 361–362, 365, 367–369, 378–379
src/simple_sqlite3_orm/_orm
   __init__.py40100% 
   _async.py91990%28, 70, 72–73, 86–87, 89, 124, 126
   _base.py1221389%98, 149, 171–173, 186–188, 287, 327, 409, 449, 472
   _multi_thread.py1031090%28, 30–31, 66, 68–69, 82–83, 85, 118
   _utils.py18288%18, 40
TOTAL79011885% 

Tests Skipped Failures Errors Time
78 0 💤 0 ❌ 0 🔥 1m 52s ⏱️

Please sign in to comment.