Skip to content

Commit

Permalink
Update schedule generation to remove get_or_create()
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell-Waterhouse committed Jul 19, 2022
1 parent 12a1ecb commit 0c6b096
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ data/
.idea/
.env.*
/schedule.json
/.db_init_done
6 changes: 5 additions & 1 deletion schedule/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def course_to_course_offering(course: Course) -> A_CourseOffering:


def add_course_offering_to_schedule(course: Course, a_course_offering: A_CourseOffering):
schedule, _ = A_Schedule.objects.get_or_create(id=1)
schedule = A_Schedule.objects.first()
if schedule is None:
print("ERROR: NO DATABASE DATA FOUND. HAVE YOU RUN init_db.sh?")
return None

if course.fall_offering:
schedule.fall.add(a_course_offering)
if course.spring_offering:
Expand Down
6 changes: 5 additions & 1 deletion schedule/alg_data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ def get_program_enrollment_data() -> typing.Dict[str, str]:


def get_schedule():
schedule, created = A_Schedule.objects.get_or_create(id=1)
schedule = A_Schedule.objects.first()
if schedule is None:
print("ERROR: NO DATABASE DATA FOUND. HAVE YOU RUN init_db.sh?")
raise FileNotFoundError
schedule_serializer = A_ScheduleSerializer(instance=schedule)
data = schedule_serializer.data
return json.loads(json.dumps(data))


#difficulty: 1 = able, 2 = with effort, 0 = no selection
#willingness: 1 = unwilling, 2 = willing, 3 = very willing, 0 = no selection

Expand Down
45 changes: 8 additions & 37 deletions schedule/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_GET_company_1_no_courses(self):
return
response = self.client.get('/schedule/2022/FALL/1', format='json')
self.assertIsNotNone(response)
self.assertEquals(status.HTTP_200_OK, response.status_code)
self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)

def test_POST_company_1(self):
response = self.client.post('/schedule/schedule_id/course_id/1', format='json')
Expand All @@ -131,7 +131,7 @@ def test_GET_company_2_no_courses(self):
return
response = self.client.get('/schedule/2022/FALL/2', format='json')
self.assertIsNotNone(response)
self.assertEquals(status.HTTP_200_OK, response.status_code)
self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)

def test_GET_company_2_two_courses(self):
if quick_test_mode:
Expand All @@ -140,16 +140,7 @@ def test_GET_company_2_two_courses(self):
self.init_course2()
response = self.client.get('/schedule/2022/FALL/2', format='json')
self.assertIsNotNone(response)
self.assertEquals(status.HTTP_200_OK, response.status_code)

def test_GET_company_1_two_courses(self):
if quick_test_mode:
return
self.init_course1()
self.init_course2()
response = self.client.get('/schedule/2022/FALL/1', format='json')
self.assertIsNotNone(response)
self.assertEquals(status.HTTP_200_OK, response.status_code)
self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)

def test_GET_company_2_error(self):
response = self.client.get('/schedule/2022/FALL/2?use_mock_data=true', format='json')
Expand All @@ -176,28 +167,8 @@ def test_program_enrollment_data(self):
self.assertEquals(8, len(historic_data_dict))

def test_get_schedule_no_courses(self):
schedule = get_schedule()
expected = {"fall": [], "spring": [], "summer": []}
self.assertDictEqual(expected, schedule)

def test_get_schedule_one_course(self):
self.init_course1()
schedule = get_schedule()
expected_course_offering: typing.Dict = self.get_course1_dict()
expected = {}
expected["fall"] = [expected_course_offering]
expected["spring"] = [expected_course_offering]
expected["summer"] = [expected_course_offering]
self.assertDictEqual(expected, schedule)

def test_get_schedule_many_courses(self):
self.init_course1()
self.init_course2()
expected_course_offering = self.get_course1_dict()
expected_course_offering2 = self.get_course2_dict()
expected = {'fall': [expected_course_offering, expected_course_offering2],
'spring': [expected_course_offering, expected_course_offering2],
'summer': [expected_course_offering, expected_course_offering2]}

schedule = get_schedule()
self.assertEquals(expected, schedule)
try:
schedule = get_schedule()
self.fail() # Should have thrown an error
except FileNotFoundError:
pass # expected behaviour
7 changes: 6 additions & 1 deletion schedule/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ def get(self, request: HttpRequest, year: int, semester: str, requested_company_
# Create params for algorithms packages
historical_data = get_historic_course_data()
previous_enrollment = get_program_enrollment_data()
schedule = get_schedule()
try:
schedule = get_schedule()
except FileNotFoundError as e:
return HttpResponse("Error generating schedule! Did you initialize the database?",
status=status.HTTP_500_INTERNAL_SERVER_ERROR)

professors = get_professor_dict_mock()
professors_company1 = get_professor_object_company1()

Expand Down

0 comments on commit 0c6b096

Please sign in to comment.