tests user_with_load
This commit is contained in:
@@ -26,9 +26,12 @@ class BaseRepo(Generic[ModelType]):
|
|||||||
statement = insert(self.model).values(data).returning(self.model)
|
statement = insert(self.model).values(data).returning(self.model)
|
||||||
result = await self.session.execute(statement)
|
result = await self.session.execute(statement)
|
||||||
obj: ModelType = result.scalar_one()
|
obj: ModelType = result.scalar_one()
|
||||||
print(obj)
|
|
||||||
return 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:
|
async def get_one_or_none(self, **filter_by: Any) -> ModelType | None:
|
||||||
query = select(self.model).filter_by(**filter_by)
|
query = select(self.model).filter_by(**filter_by)
|
||||||
result = await self.session.execute(query)
|
result = await self.session.execute(query)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import pytest
|
import pytest
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
from sqlalchemy import NullPool, insert
|
from sqlalchemy import NullPool, insert
|
||||||
|
|||||||
38
tests/mock_tasks.json
Normal file
38
tests/mock_tasks.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from src.models.users import UsersORM
|
||||||
from src.schemas.users import User
|
from src.schemas.users import User
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -29,3 +32,24 @@ async def test_user_crud(db: "TestDBManager"):
|
|||||||
await db.user.delete_one(id=updated_user.id)
|
await db.user.delete_one(id=updated_user.id)
|
||||||
delete_user = await db.user.get_one_or_none(id=new_user.id)
|
delete_user = await db.user.get_one_or_none(id=new_user.id)
|
||||||
assert not delete_user
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user