Skip to content

Commit

Permalink
add crud tag tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DenitsaTH committed Jun 7, 2024
1 parent 0e23452 commit 5996054
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/app/crud/crud_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions src/app/tests/crud/crud_tag_test.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 22 additions & 1 deletion src/app/tests/dummies.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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)

0 comments on commit 5996054

Please sign in to comment.