Skip to content

Commit

Permalink
feat: add pytest configuration and update CRUD tests with new student…
Browse files Browse the repository at this point in the history
… data
  • Loading branch information
lakminagamage committed Jan 12, 2025
1 parent 951db28 commit 4893cc2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 57 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL=postgresql://postgres:password@student-service-db:5432/student_db
PYTHONPATH=.
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
testpaths = tests
python_files = test_*.py
addopts = -v --disable-warnings
Empty file added tests/__init__.py
Empty file.
65 changes: 8 additions & 57 deletions tests/test_crud.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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

0 comments on commit 4893cc2

Please sign in to comment.