From 4893cc2066483794f916581ca71df8dfe2c8c126 Mon Sep 17 00:00:00 2001 From: Lakmina Pramodya Gamage Date: Mon, 13 Jan 2025 00:34:17 +0530 Subject: [PATCH] feat: add pytest configuration and update CRUD tests with new student data --- .env | 1 + pytest.ini | 4 +++ tests/__init__.py | 0 tests/test_crud.py | 65 ++++++---------------------------------------- 4 files changed, 13 insertions(+), 57 deletions(-) create mode 100644 pytest.ini create mode 100644 tests/__init__.py diff --git a/.env b/.env index 0394605..9c946d3 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ DATABASE_URL=postgresql://postgres:password@student-service-db:5432/student_db +PYTHONPATH=. \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..ec651ef --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +testpaths = tests +python_files = test_*.py +addopts = -v --disable-warnings \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_crud.py b/tests/test_crud.py index 3eabaf3..017ed30 100644 --- a/tests/test_crud.py +++ b/tests/test_crud.py @@ -1,7 +1,8 @@ import pytest from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from database import models, schemas +import database.models as models +import database.schemas as schemas from api import crud SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" @@ -19,73 +20,23 @@ def db(): db.close() def test_create_student(db): - student = schemas.StudentCreate(name="John Doe", age=20) + student = schemas.StudentCreate(name="Luke", email="luke@gmail.com", address="Kandy", contactNo="0771234567", cgpa=3.77) db_student = crud.create_student(db, student) - assert db_student.name == "John Doe" - assert db_student.age == 20 + assert db_student.name == "Luke" + assert db_student.email == "luke@gmail.com" def test_get_student(db): - student = schemas.StudentCreate(name="Jane Doe", age=22) + student = schemas.StudentCreate(name="Lukes", email="luke2@gmail.com", address="Kandy", contactNo="0771234567", cgpa=3.77) 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 + assert len(students) > 0 def test_delete_student(db): - student = schemas.StudentCreate(name="Mark Smith", age=21) + student = schemas.StudentCreate(name="luke", email="lukes@gmail.com", address="Kandy", contactNo="0771234567", cgpa=3.77) 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