diff --git a/src/repository/base.py b/src/repository/base.py index d3493c1..8115822 100644 --- a/src/repository/base.py +++ b/src/repository/base.py @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 521a923..6cfaa03 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +import json import pytest from httpx import ASGITransport, AsyncClient from sqlalchemy import NullPool, insert diff --git a/tests/mock_tasks.json b/tests/mock_tasks.json new file mode 100644 index 0000000..8f50794 --- /dev/null +++ b/tests/mock_tasks.json @@ -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" + } +] \ No newline at end of file diff --git a/tests/unit_tests/test_repo_db.py b/tests/unit_tests/test_repo_db.py index e0fe1ae..6d2adab 100644 --- a/tests/unit_tests/test_repo_db.py +++ b/tests/unit_tests/test_repo_db.py @@ -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