From 0e23452fc0353a0cd6d0f1cd363c026103a16c70 Mon Sep 17 00:00:00 2001 From: DenitsaTH Date: Fri, 7 Jun 2024 11:55:09 +0300 Subject: [PATCH 1/2] restore pyproject.toml --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..78271f1 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[tool.pytest.ini_options] +pythonpath = [ + "src.app" +] \ No newline at end of file From 59960547c3b7bf1bc652cb392a39d01c151c58af Mon Sep 17 00:00:00 2001 From: DenitsaTH Date: Fri, 7 Jun 2024 11:55:24 +0300 Subject: [PATCH 2/2] add crud tag tests --- src/app/crud/crud_tag.py | 8 +-- src/app/tests/crud/crud_tag_test.py | 75 +++++++++++++++++++++++++++++ src/app/tests/dummies.py | 23 ++++++++- 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 src/app/tests/crud/crud_tag_test.py diff --git a/src/app/crud/crud_tag.py b/src/app/crud/crud_tag.py index 883af0d..ddf5fa1 100644 --- a/src/app/crud/crud_tag.py +++ b/src/app/crud/crud_tag.py @@ -33,23 +33,23 @@ async def create_tags(db: Session, tags: List[TagBase], course_id: int) -> Dict[ return result -async def course_has_tag(db: Session, course_id: int, tag_id: int): +async def course_has_tag(db: Session, course_id: int, tag_id: int) -> CourseTag: course_tag = db.query(CourseTag).filter_by(course_id=course_id, tag_id=tag_id).first() return course_tag -async def delete_tag_from_course(db: Session, course_tag: CourseTag): +async def delete_tag_from_course(db: Session, course_tag: CourseTag) -> None: db.delete(course_tag) db.commit() -async def check_tag_associations(db: Session, tag_id: int): +async def check_tag_associations(db: Session, tag_id: int) -> int: tag_associations = db.query(CourseTag).filter_by(tag_id=tag_id).count() return tag_associations -async def delete_tag(db: Session, tag_id: int): +async def delete_tag(db: Session, tag_id: int) -> None: tag = db.query(Tag).filter_by(tag_id=tag_id).first() if tag: db.delete(tag) diff --git a/src/app/tests/crud/crud_tag_test.py b/src/app/tests/crud/crud_tag_test.py new file mode 100644 index 0000000..28be46b --- /dev/null +++ b/src/app/tests/crud/crud_tag_test.py @@ -0,0 +1,75 @@ +import pytest +from crud import crud_tag +from tests import dummies + + +@pytest.mark.asyncio +async def test_create_tags_returns_dict_with_created_tags_when_no_duplicates(db): + dummy_tags = [await dummies.create_dummy_tag_base(tag_id=1, name='dummyTag1'), + await dummies.create_dummy_tag_base(tag_id=2, name='dummyTag2')] + + course = await dummies.create_dummy_course(db) + + res = await crud_tag.create_tags(db, dummy_tags, course.course_id) + + assert res == { + "created": dummy_tags, + "duplicated_tags_ids": [] + } + + +@pytest.mark.asyncio +async def test_create_tags_returns_dict_with_created_tags_when_has_duplicates(db): + dummy_tags = [await dummies.create_dummy_tag_base(tag_id=1, name='dummyTag'), + await dummies.create_dummy_tag_base(tag_id=2, name='dummyTag2')] + + course = await dummies.create_dummy_course(db) + tag = await dummies.create_dummy_tag(db) + await dummies.add_dummy_tag(db, course.course_id, tag.tag_id) + + res = await crud_tag.create_tags(db, dummy_tags, course.course_id) + + assert res == { + "created": [dummy_tags[1]], + "duplicated_tags_ids": [1] + } + + +@pytest.mark.asyncio +async def test_course_has_tags_returns_course_tags_if_tags_exist(db): + course = await dummies.create_dummy_course(db) + tag = await dummies.create_dummy_tag(db) + course_tag = await dummies.add_dummy_tag(db, course.course_id, tag.tag_id) + + res = await crud_tag.course_has_tag(db, course.course_id, tag.tag_id) + + assert res == course_tag + + +@pytest.mark.asyncio +async def test_course_has_tags_returns_none_if_not_tags(db): + course = await dummies.create_dummy_course(db) + tag = await dummies.create_dummy_tag(db) + res = await crud_tag.course_has_tag(db, course.course_id, tag.tag_id) + + assert res is None + + +@pytest.mark.asyncio +async def test_check_tag_associations_returns_tag_count_if_tags_exist(db): + course = await dummies.create_dummy_course(db) + tag = await dummies.create_dummy_tag(db) + await dummies.add_dummy_tag(db, course.course_id, tag.tag_id) + + res = await crud_tag.check_tag_associations(db, tag.tag_id) + + assert res == 1 + + +@pytest.mark.asyncio +async def test_check_tag_associations_returns_zero_if_not_tags(db): + tag = await dummies.create_dummy_tag(db) + + res = await crud_tag.check_tag_associations(db, tag.tag_id) + + assert res == 0 diff --git a/src/app/tests/dummies.py b/src/app/tests/dummies.py index b18f495..d52280d 100644 --- a/src/app/tests/dummies.py +++ b/src/app/tests/dummies.py @@ -1,7 +1,8 @@ from sqlalchemy.orm import Session from db.models import Account, Student, Teacher, Course, StudentCourse, Status, Admin, Section, StudentRating, \ - StudentSection + StudentSection, Tag, CourseTag +from schemas.tag import TagBase async def get_non_existent_account_id(): @@ -132,3 +133,23 @@ async def dummy_view_section(db, student_id, section_id): db.add(visited_section) db.commit() + + +async def create_dummy_tag(db): + tag = Tag(tag_id=1, name='dummyTag') + db.add(tag) + db.commit() + + return tag + + +async def add_dummy_tag(db, course_id, tag_id): + add_tag = CourseTag(course_id=course_id, tag_id=tag_id) + db.add(add_tag) + db.commit() + + return add_tag + + +async def create_dummy_tag_base(tag_id, name): + return TagBase(tag_id=tag_id, name=name)