Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crud_teacher_test: created test for edit_account, get_my_courses, mak… #29

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/crud/crud_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def edit_account(db: Session, teacher: Teacher, updates: TeacherEdit):
return teacher


async def get_info(teacher, teacher_email):
async def get_info(teacher: Teacher, teacher_email: str):
return TeacherSchema(
teacher_id=teacher.teacher_id,
email=teacher_email,
Expand Down
24 changes: 21 additions & 3 deletions src/app/tests/api/api_v1/endpoints/teacher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
last_name="dummyName",
account=dummy_account
)

dummy_student_account = Account(account_id=2, email='[email protected]')
dummy_student = Student(
student_id=2
student_id=2,
account=dummy_student_account
)

teacher_request = {
Expand Down Expand Up @@ -629,4 +630,21 @@ def test_generate_courses_reports_invalid_sort(client: TestClient):
response = client.get('/teachers/reports?min_progress=0.0&sort=invalid')

assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == {'detail': 'Invalid sort parameter'}
assert response.json() == {'detail': 'Invalid sort parameter'}


def test_approve_enrollment_returns_success_msg(client: TestClient, mocker):
mocker.patch('app.api.api_v1.routes.teachers.get_course_by_id', return_value=dummy_course)
mocker.patch('app.api.api_v1.routes.teachers.crud_teacher.is_teacher_owner', return_value=True)
mocker.patch('app.api.api_v1.routes.teachers.crud_student.get_by_email', return_value=dummy_student)
mocker.patch('app.api.api_v1.routes.teachers.crud_teacher.student_enroll_response', return_value='Request response submitted')

response = client.put('/courses/requests', json={
"student": dummy_student.account.email,
"course_id": dummy_course.course_id,
"response": "Approve"
})

assert response.status_code == 201
assert response.json() == {'detail': 'Request response submitted'}

106 changes: 106 additions & 0 deletions src/app/tests/crud/crud_teacher_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import pytest
from sqlalchemy.orm import Session
from crud import crud_teacher
from db.models import Teacher, Course, Status, Section
from fastapi import status, HTTPException
from schemas.course import CourseCreate, CourseInfo
from schemas.teacher import TeacherEdit
from schemas.tag import TagBase
from schemas.section import SectionBase
from tests import dummies


TITLE="Test Title",
CONTENT_TYPE="video",
EXTERNAL_LINK="http://example.com",
DESCRIPTION="Test Description"

async def create_dummy_course(db: Session, teacher: Teacher):
course = Course(
course_id=1,
title="dummy",
description='dummy',
objectives='dummy',
owner_id=teacher.teacher_id,
)
db.add(course)
db.commit()
return course

def create_dummy_sectionbase(section_id=None, title=TITLE, content_type=CONTENT_TYPE,
external_link=EXTERNAL_LINK, description=DESCRIPTION, course_id=None):
return SectionBase(
section_id=section_id,
title=title,
content_type=content_type,
external_link=external_link,
description=description,
course_id=course_id
)

@pytest.mark.asyncio
async def test_edit_account_returns_updated_teacher_account(db):
_, teacher = await dummies.create_dummy_teacher(db)

updates = TeacherEdit(
first_name="UpdatedFirst",
last_name="UpdatedLast",
phone_number="1234567890",
linked_in="newLinkedInProfile"
)

updated_teacher = await crud_teacher.edit_account(db, teacher, updates)

assert updated_teacher.first_name == "UpdatedFirst"
assert updated_teacher.last_name == "UpdatedLast"
assert updated_teacher.phone_number == "1234567890"
assert updated_teacher.linked_in == "newLinkedInProfile"


@pytest.mark.asyncio
async def test_get_my_courses_returns_list_of_courses(db):
_, teacher = await dummies.create_dummy_teacher(db)
course = await create_dummy_course(db, teacher)

courses = await crud_teacher.get_my_courses(db, teacher)

assert len(courses) == 1
assert courses[0].course_id == course.course_id


@pytest.mark.asyncio
async def test_make_course_returns_created_CourseSectionsTags_model(db):
_, teacher = await dummies.create_dummy_teacher(db)
new_course = CourseCreate(
title="New Course",
description="New Course Description",
objectives="Objectives",
is_premium=False,
tags=[TagBase(name="NewTag")],
sections=[create_dummy_sectionbase()]
)

course_with_tags_and_sections = await crud_teacher.make_course(db, teacher, new_course)

assert course_with_tags_and_sections.course.title == new_course.title
assert len(course_with_tags_and_sections.tags) == 1
assert len(course_with_tags_and_sections.sections) == 1


@pytest.mark.asyncio
async def test_get_entire_course_returns_CourseSectionsTags_model_with_sorting(db):
_, teacher = await dummies.create_dummy_teacher(db)
course = create_dummy_course(db, teacher)
tag = dummies.create_dummy_tag(db)
dummies.add_dummy_tag(db, course.course_id, tag.tag_id)

section_1 = create_dummy_sectionbase(section_id=1, course_id=course.course_id)
section_2 = create_dummy_sectionbase(section_id=2, course_id=course.course_id)

course_with_details = await crud_teacher.get_entire_course(db, course, teacher, sort='asc', sort_by='section_id')

assert course_with_details.course.course_id == course.course_id
assert len(course_with_details.tags) == 1
assert len(course_with_details.sections) == 2
assert course_with_details.sections[1].section_id == section_2.section_id

2 changes: 1 addition & 1 deletion src/app/tests/dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def create_dummy_student(db: Session, is_premium=0) -> tuple[Account, Stud
return account, student


async def create_dummy_teacher(db: Session):
async def create_dummy_teacher(db: Session) -> tuple[Account, Teacher]:
account_id = 2
account = Account(
account_id=account_id,
Expand Down
Loading