Skip to content

Commit

Permalink
added bulk insert for course
Browse files Browse the repository at this point in the history
  • Loading branch information
chabwick committed Aug 22, 2020
1 parent b576062 commit b4c0608
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
18 changes: 15 additions & 3 deletions coursecake/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_courses(db: Session, offset: int = 0, limit: int = 100) -> list:
return db.query(models.Course).offset(offset).limit(limit).all()


def add_course(db: Session, university: str, courseObj: Course) -> models.Course:
def add_course(db: Session, university: str, courseObj: Course, commit: bool = True) -> models.Course:
'''
courseObj is the basic python instance of Course defined in
scrapers.course
Expand All @@ -35,11 +35,23 @@ def add_course(db: Session, university: str, courseObj: Course) -> models.Course
'''
courseModel = models.Course(courseObj, university.upper())
db.add(courseModel)
db.commit()
db.refresh(courseModel)

if commit:
db.commit()
db.refresh(courseModel)
return courseModel


def bulk_add_course(db: Session, university: str, courses: list):
toInsert = list()
university = university.upper()
for courseObj in courses:
toInsert.append(models.Course(courseObj, university))

db.bulk_save_objects(toInsert)
db.commit()



class CourseQuery:
'''
Expand Down
86 changes: 65 additions & 21 deletions coursecake/database/tests/test_courses_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ def __init__(self, name):

university = University("test1")


course = Course()
course.name = "test 101"
course.title = "intro to course"
course.code = "123456"
course.department = "test"
course.type = "lecture"
course.instructor = "Dr. Test"
course.time = "time is an illusion"

course.location = "Testing Hall 200"
course.building = "Testing Hall"
course.room = "200"
course.status = "OPEN"

course.units = 4

course.final = "never"

course.enrolled = 100
course.school = "School of Test"
course.departmentTitle = "Test ing"



@pytest.fixture(scope="module")
def db():
models.Base.metadata.create_all(bind=engine)
Expand All @@ -32,27 +57,6 @@ def test_add_university(db):


def test_add_course(db):
course = Course()
course.name = "test 101"
course.title = "intro to course"
course.code = "123456"
course.department = "test"
course.type = "lecture"
course.instructor = "Dr. Test"
course.time = "time is an illusion"

course.location = "Testing Hall 200"
course.building = "Testing Hall"
course.room = "200"
course.status = "OPEN"

course.units = 4

course.final = "never"

course.enrolled = 100
course.school = "School of Test"
course.departmentTitle = "Test ing"
crud.add_course(db, university.name, course)

courseRow = crud.CourseQuery(db, university.name, {"code[equals]": course.code}).search()[0]
Expand All @@ -61,3 +65,43 @@ def test_add_course(db):

print("first thing in course", courseRow)
assert True


def test_add_many_course(db):
'''
Load testing for adding a course
'''
tempCode = course.code
limit = 9000
for i in range(limit):
newCourse = Course(course.__dict__)
newCourse.code = tempCode + str(i)
crud.add_course(db, university.name, newCourse, commit = False)

db.commit()
courses = crud.get_courses(db, limit = limit)

assert len(courses) >= limit


def test_bulk_add_course(db):
'''
tests crud.bulk_add_course(db: Session)
'''
tempCode = course.code
limit = 9000
courseList = list()

for i in range(limit):
newCourse = Course(course.__dict__)
newCourse.code = str(i) + tempCode
courseList.append(newCourse)

print("created course list, now bulk inserting")

crud.bulk_add_course(db, university.name, courseList)


courses = crud.get_courses(db, limit = limit)

assert len(courses) >= limit

0 comments on commit b4c0608

Please sign in to comment.