tests user_with_load

This commit is contained in:
IluaAir
2025-08-27 01:14:10 +03:00
parent 2d54f595db
commit cd4eb11604
4 changed files with 67 additions and 1 deletions

View File

@@ -26,9 +26,12 @@ class BaseRepo(Generic[ModelType]):
statement = insert(self.model).values(data).returning(self.model)
result = await self.session.execute(statement)
obj: ModelType = result.scalar_one()
print(obj)
return obj
async def create_bulk(self, data: Sequence[Mapping[str, Any]]) -> list[ModelType]:
result = [await self.create_one(item) for item in data]
return result
async def get_one_or_none(self, **filter_by: Any) -> ModelType | None:
query = select(self.model).filter_by(**filter_by)
result = await self.session.execute(query)

View File

@@ -1,3 +1,4 @@
import json
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy import NullPool, insert

38
tests/mock_tasks.json Normal file
View File

@@ -0,0 +1,38 @@
[
{
"title": "test1",
"description": "test1",
"due_date": "2026-06-01",
"status": "open",
"priority": "medium"
},
{
"title": "test2",
"description": "test2",
"due_date": "2026-06-01",
"status": "open",
"priority": "high"
},
{
"title": "test3",
"description": "test3",
"due_date": "2026-06-02",
"status": "todo",
"priority": "medium"
},
{
"title": "test4",
"description": "test4",
"due_date": "2026-06-02",
"status": "open",
"priority": "high"
},
{
"title": "test5",
"description": "test5",
"due_date": "2026-06-02",
"status": "todo",
"priority": "low"
}
]

View File

@@ -1,5 +1,8 @@
import json
from datetime import datetime
from typing import TYPE_CHECKING
from src.models.users import UsersORM
from src.schemas.users import User
if TYPE_CHECKING:
@@ -29,3 +32,24 @@ async def test_user_crud(db: "TestDBManager"):
await db.user.delete_one(id=updated_user.id)
delete_user = await db.user.get_one_or_none(id=new_user.id)
assert not delete_user
async def test_tasks_user(db: "TestDBManager"):
with open("tests/mock_tasks.json") as jsonfile:
data = json.load(jsonfile)
admin_user: UsersORM | None = await db.user.get_one_or_none(id=1)
assert admin_user
data = [
{
**item,
"user_id": admin_user.id,
"due_date": datetime.strptime(item["due_date"], "%Y-%m-%d"),
}
for item in data
]
result = await db.task.create_bulk(data)
await db.commit()
assert result
user_with_tasks = await db.user.get_one_with_load(user_id=admin_user.id)
assert user_with_tasks
assert user_with_tasks.tasks