From 046e7316b916ef5707cab6811d371fd255d1d706 Mon Sep 17 00:00:00 2001 From: Lakmina Pramodya Gamage Date: Sun, 15 Dec 2024 00:57:22 +0530 Subject: [PATCH] feat: add pytest for CRUD operations and update requirements.txt --- requirements.txt | 1 + tests/test_crud.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/test_crud.py diff --git a/requirements.txt b/requirements.txt index ff675e5..4abb2d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ starlette==0.41.3 typing_extensions==4.12.2 uvicorn==0.32.1 python-dotenv +pytest \ No newline at end of file diff --git a/tests/test_crud.py b/tests/test_crud.py new file mode 100644 index 0000000..3eabaf3 --- /dev/null +++ b/tests/test_crud.py @@ -0,0 +1,91 @@ +import pytest +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from database import models, schemas +from api import crud + +SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" +engine = create_engine(SQLALCHEMY_DATABASE_URL) +TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +models.Base.metadata.create_all(bind=engine) + +@pytest.fixture(scope="module") +def db(): + db = TestingSessionLocal() + try: + yield db + finally: + db.close() + +def test_create_student(db): + student = schemas.StudentCreate(name="John Doe", age=20) + db_student = crud.create_student(db, student) + assert db_student.name == "John Doe" + assert db_student.age == 20 + +def test_get_student(db): + student = schemas.StudentCreate(name="Jane Doe", age=22) + db_student = crud.create_student(db, student) + fetched_student = crud.get_student(db, db_student.id) + assert fetched_student.id == db_student.id + +def test_get_students(db): + students = crud.get_students(db) + assert len(students) >= 0 + +def test_delete_student(db): + student = schemas.StudentCreate(name="Mark Smith", age=21) + db_student = crud.create_student(db, student) + deleted_student = crud.delete_student(db, db_student.id) + assert deleted_student.id == db_student.id + +def test_create_academic_history(db): + student = schemas.StudentCreate(name="Alice Johnson", age=23) + db_student = crud.create_student(db, student) + academic = schemas.AcademicHistoryCreate(course_name="Math", grade="A") + db_academic = crud.create_academic_history(db, academic, db_student.id) + assert db_academic.course_name == "Math" + assert db_academic.grade == "A" + +def test_get_academic_history(db): + student = schemas.StudentCreate(name="Bob Brown", age=24) + db_student = crud.create_student(db, student) + academic = schemas.AcademicHistoryCreate(course_name="Science", grade="B") + crud.create_academic_history(db, academic, db_student.id) + academic_history = crud.get_academic_history(db, db_student.id) + assert len(academic_history) > 0 + +def test_create_course(db): + course = schemas.CourseCreate(name="Physics", description="Physics course") + db_course = crud.create_course(db, course) + assert db_course.name == "Physics" + assert db_course.description == "Physics course" + +def test_get_course(db): + course = schemas.CourseCreate(name="Chemistry", description="Chemistry course") + db_course = crud.create_course(db, course) + fetched_course = crud.get_course(db, db_course.id) + assert fetched_course.id == db_course.id + +def test_get_courses(db): + courses = crud.get_courses(db) + assert len(courses) >= 0 + +def test_enroll_student_in_course(db): + student = schemas.StudentCreate(name="Charlie Green", age=25) + db_student = crud.create_student(db, student) + course = schemas.CourseCreate(name="Biology", description="Biology course") + db_course = crud.create_course(db, course) + enrollment = crud.enroll_student_in_course(db, db_student.id, db_course.id) + assert enrollment.student_id == db_student.id + assert enrollment.course_id == db_course.id + +def test_get_student_courses(db): + student = schemas.StudentCreate(name="Diana White", age=26) + db_student = crud.create_student(db, student) + course = schemas.CourseCreate(name="History", description="History course") + db_course = crud.create_course(db, course) + crud.enroll_student_in_course(db, db_student.id, db_course.id) + student_courses = crud.get_student_courses(db, db_student.id) + assert len(student_courses) > 0