Skip to content

Commit

Permalink
feat(test): enhance ORM tests and introduce QueryWrapper test
Browse files Browse the repository at this point in the history
- Added `ExampleRecord` model with `TimestampsMixin` to test models.
- Implemented new tests for `ExampleRecord` in `orm_test.py`.
- Introduced `test_wrapper.py` for testing `QueryWrapper` types and functionalities.

Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Jan 20, 2025
1 parent ac53278 commit be98abb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from sqlmodel import SQLModel

import activemodel
from activemodel.session_manager import get_engine
from sqlmodel import SQLModel

from .utils import database_url, temporary_tables

Expand Down
9 changes: 9 additions & 0 deletions test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@

from activemodel import BaseModel
from activemodel.mixins import TypeIDMixin
from activemodel.mixins.timestamps import TimestampsMixin
from activemodel.types.typeid import TypeIDType

TYPEID_PREFIX = "myid"

EXAMPLE_TABLE_PREFIX = "test_record"


class ExampleRecord(
BaseModel, TimestampsMixin, TypeIDMixin(EXAMPLE_TABLE_PREFIX), table=True
):
something: str | None


class AnotherExample(BaseModel, TypeIDMixin("myotherid"), table=True):
note: str | None = Field(nullable=True)
Expand Down
23 changes: 14 additions & 9 deletions test/orm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
Test core ORM functions
"""

from activemodel import BaseModel
from activemodel.mixins.timestamps import TimestampsMixin
from activemodel.mixins.typeid import TypeIDMixin
from test.models import EXAMPLE_TABLE_PREFIX, AnotherExample, ExampleRecord

EXAMPLE_TABLE_PREFIX = "test_record"


class ExampleRecord(
BaseModel, TimestampsMixin, TypeIDMixin(EXAMPLE_TABLE_PREFIX), table=True
):
something: str | None
def test_empty_count(create_and_wipe_database):
assert ExampleRecord.count() == 0


def test_all_and_count(create_and_wipe_database):
AnotherExample().save()

records_to_create = 10

# create 10 example records
Expand Down Expand Up @@ -44,3 +40,12 @@ def test_basic_query(create_and_wipe_database):

query_as_str = str(query)
result = query.first()


def test_query_count(create_and_wipe_database):
AnotherExample().save()

example = ExampleRecord(something="hi").save()
count = ExampleRecord.select().where(ExampleRecord.something == "hi").count()

assert count == 1
37 changes: 37 additions & 0 deletions test/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import Any, Generator, assert_type

import sqlmodel as sm
from sqlmodel.sql.expression import SelectOfScalar

from activemodel.query_wrapper import QueryWrapper
from test.models import ExampleRecord


def test_basic_types(create_and_wipe_database):
qw = ExampleRecord.select()

sm_query = sm.select(ExampleRecord)
assert_type(sm_query, SelectOfScalar[ExampleRecord])

# assert type annotation of qw is QueryWrapper[ExampleRecord]
assert_type(qw, QueryWrapper[ExampleRecord])
assert isinstance(qw, QueryWrapper)

all_records = qw.all()
assert_type(all_records, Generator[ExampleRecord, Any, None])

all_records_list = list(all_records)
assert_type(all_records_list, list[ExampleRecord])


def test_select_with_args():
result = ExampleRecord.select(sm.func.max(ExampleRecord.id)).one()


def test_result_types():
"ensure the result types are lists of the specific classes the wrapper was generated from"

ExampleRecord().save()

s = sm.select("id").select_from(ExampleRecord)
result = ExampleRecord.select().all()

0 comments on commit be98abb

Please sign in to comment.