def test_rating_and_elective_filters(courses, all_courses): # expect to return only Course3 db_courses = Course.get_filtered_courses_by_rating(2.5, all_courses) db_courses = Course.get_elective_courses(db_courses) assert db_courses.filter(pk=courses[2].course_id) assert not db_courses.filter( pk=courses[1].course_id) and not db_courses.filter( pk=courses[0].course_id)
def courses(): courses_list = [ Course(1, "Course1", True, 3, None, 1.3, 4, 5, 1), Course(2, "Course2", False, 2, None, 3.5, 2, 13, 5), Course(3, "Course3", False, 1, None, 2, 2.5, 2, 0) ] for course in courses_list: course.save() return courses_list
def test_get_courses(courses): flag = True db_courses = Course.get_courses() for course in courses: flag = flag and db_courses.filter(pk=course.pk).exists() assert flag
def test_get_filtered_courses_by_rating(courses, all_courses): # testing for the average rating >= 2.5 which should include Course1 and Course3 db_courses = Course.get_filtered_courses_by_rating(2.5, all_courses) assert db_courses.filter(pk=courses[0].course_id) and db_courses.filter( pk=courses[2].course_id) assert not db_courses.filter(pk=courses[1].course_id)
def test_get_filtered_courses_by_load(courses, all_courses): # testing for the average load <= 2 which should include Course1 and Course3 db_courses = Course.get_filtered_courses_by_load(2, all_courses) assert db_courses.filter(pk=courses[0].course_id) and db_courses.filter( pk=courses[2].course_id) assert not db_courses.filter(pk=courses[1].course_id)
def generate_data(apps, schema_editor): from homepage.models import Course from homepage.models import Prerequisites test_data_course = [ (10231, 'UnFogging the Future', True, 4, 'Currently Unavailable', 3.25, 1.5, 12, 12), (10221, 'Grammatica in Arithmancy', False, 3, 'Currently Unavailable', 4.25, 4.5, 10, 9), (10111, 'Resonance in Runes and Signs', True, 3, 'Currently Unavailable', 2.25, 2.0, 13, 13), (12357, 'Numerology', False, 5, 'Currently Unavailable', 3.5, 2.5, 7, 3), ] courses = [Course(*tdc) for tdc in test_data_course] test_data_prerequisites = [ (courses[3], courses[0], 1), (courses[2], courses[1], 0), ] with transaction.atomic(): for course in courses: course.save() for preq in test_data_prerequisites: Prerequisites(course_id=preq[0], req_course_id=preq[1], req_code=preq[2]).save()
def generate_data(apps, schema_editor): from homepage.models import Course from homepage.models import Prerequisites test_data_course = [ (10340, 'No Return - through the Lense', True, 4, None), (10341, 'Open Source 101', False, 3, "https://www.github.com", 4.25, 4.5, 10, 9), (10342, 'Normalized Spatial Schemes', True, 3, None, 3, 5, 1, 0), (12343, 'Easier than Zoology', False, 5, None, 3.5, 2.5, 7, 3), ] courses = [Course(*tdc) for tdc in test_data_course] test_data_prerequisites = [ (courses[3], courses[0], 1), (courses[2], courses[1], 0), ] with transaction.atomic(): for course in courses: course.save() for preq in test_data_prerequisites: Prerequisites(course_id=preq[0], req_course_id=preq[1], req_code=preq[2]).save()
def add_review_search(request): if request.method == "GET": course_name = request.GET.get('course') if not course_name: course_name = '' courses = Course.get_courses_ordered_by_name(course_name) return render(request, 'homepage/add_review_search.html', {'course_name': course_name, 'courses': courses})
def test_get_elective_courses(courses, all_courses): # should get back only Course1 from courses db_courses = Course.get_elective_courses(all_courses) assert not db_courses.filter(pk=courses[0].course_id) assert db_courses.filter(pk=courses[1].course_id) and db_courses.filter( pk=courses[2].course_id)
def test_load_and_elective_and_mandatory_filters(courses, all_courses): # expect to return only Course3 db_courses = Course.get_filtered_courses_by_load(2, all_courses) db_courses_elect = Course.get_elective_courses(db_courses) db_courses_mand = Course.get_mandatory_courses(db_courses) # checking elective is only Course3 assert db_courses_elect.filter(pk=courses[2].course_id) assert not db_courses_elect.filter( pk=courses[1].course_id) and not db_courses_elect.filter( pk=courses[0].course_id) # checking mandatory is only Course1 assert db_courses_mand.filter(pk=courses[0].course_id) assert not db_courses_mand.filter( pk=courses[1].course_id) and not db_courses_mand.filter( pk=courses[2].course_id)
def test_sort_by_rating(all_courses): courses = list(Course.sort_by_rating(all_courses)) for index in range(len(courses) - 1): if courses[index].avg_rating is None: assert courses[index + 1].avg_rating is None elif courses[index + 1].avg_rating is not None: assert Decimal.compare(courses[index].avg_rating, courses[index + 1].avg_rating) > -1
def test_course_validation_invalids(invalid_courses): flag = False try: Course(*invalid_courses).full_clean() except ValidationError: flag = True assert flag
def test_create_multiple_courses(): details = [(1, "Linear Algebra 1", True, 4, "please contribute", 3.5, 4, 13, 7), (2, "Project Management", False, 3, "please contribute", 1.5, 3.5, 15, 3)] for args in details: Course(*args).save() # make sure that multiple courses are saved accurately courses = [Course.objects.get(pk=arg[0]).get_details() for arg in details] assert courses == details
def test_course_validation_valids(valid_courses): flag = True # check that valid input doesn't raise exception try: Course(*valid_courses).clean_fields() except ValidationError: flag = False assert flag
def courses(request): all_courses = Course.get_courses() filters_active = [] sort_active = '' if request.method == "GET": form = FilterAndSortForm(request.GET) if form.is_valid(): filters = form.cleaned_data.get('filter_by') sort_val = form.cleaned_data.get('sort_by') sort_result = Course.get_sorted_courses(all_courses, sort_val) filter_result = Course.get_filtered_courses(sort_result['result'], filters) all_courses = filter_result['result'] filters_active = filter_result['active'] sort_active = sort_result['active'] else: form = FilterAndSortForm() context = {'all_courses': all_courses, 'filters': filters_active, 'sort': sort_active} context['form'] = form return render(request, 'homepage/courses/courses.html', context)
def filters_to_results(all_courses): return { 'mand': Course.get_mandatory_courses(all_courses), 'elect': Course.get_elective_courses(all_courses), 'load_below': Course.get_filtered_courses_by_load(3.5, all_courses), 'rate_over': Course.get_filtered_courses_by_rating(3.5, all_courses), 'has_preqs': Course.get_courses_with_preqs(all_courses), 'no_preqs': Course.get_courses_without_preqs(all_courses), 'rater_num': Course.get_courses_with_ratings(all_courses, 5) }
def test_course_print_details(capsys): course = (1, "Linear Algebra 1", True, 4, None, 3.500, 4.000, 13, 7) Course(*course).save() msg = ( "------------------------------------------------------------\n" "Course indentifier: 1 \nName: Linear Algebra 1\nMandatory? yes\n" "Credit Points: 4\nSyllabi: N/A\n" "Average Rating: 4.000 \tAverage Load: 3.500\t" "13 Raters\nNumber Of Reviews: 7\n" ) # make sure that message comes out as expected Course.objects.get(pk=1).print_details() assert capsys.readouterr().out == msg
def sort_to_result(all_courses): return { 'name': Course.sort_by_name(all_courses), 'id': Course.sort_by_id(all_courses), 'rating': Course.sort_by_rating(all_courses), 'load': Course.sort_by_load(all_courses), 'num_reviews': Course.sort_by_num_reviews(all_courses), 'num_raters': Course.sort_by_num_raters(all_courses) }
def test_course_save_course(valid_courses): course = Course(*valid_courses) course.save() # check that it's the same object db_course = Course.objects.get(pk=course.course_id) assert db_course == course
def test_get_courses_without_preq(all_courses): courses = Course.get_courses_without_preqs(all_courses) assert all(not course.has_preqs() for course in courses)
def test_get_courses_with_ratings(all_courses): courses = Course.get_courses_with_ratings(all_courses, 5) assert all(course.num_of_raters >= 5 for course in courses)
def test_get_sorted_courses_active(all_courses, sort_to_active): for key, val in sort_to_active.items(): assert Course.get_sorted_courses(all_courses, key)['active'] == val
def test_get_sorted_courses_results(all_courses, sort_to_result): for key, val in sort_to_result.items(): assert list(Course.get_sorted_courses(all_courses, key)['result']) == list(val)
def test_sort_by_load(all_courses): courses = list(Course.sort_by_load(all_courses)) for index in range(len(courses) - 1): if courses[index].avg_load is not None: assert Decimal.compare(courses[index].avg_load, courses[index + 1].avg_load) < 1
def test_sort_by_num_raters(all_courses): courses = list(Course.sort_by_num_raters(all_courses)) for index in range(len(courses) - 1): assert courses[index].num_of_raters >= courses[index + 1].num_of_raters
def test_elective_and_mandatory_filters(all_courses): # expect to return only Course1 db_courses = Course.get_elective_courses(all_courses) db_courses = Course.get_mandatory_courses(db_courses) assert not db_courses
def test_sort_by_id(all_courses): courses = list(Course.sort_by_id(all_courses)) for index in range(len(courses) - 1): assert courses[index].course_id < courses[index + 1].course_id
def test_get_filtered_courses_active_filters(all_courses, filters_to_active): for key, val in filters_to_active.items(): assert Course.get_filtered_courses(all_courses, [key])['active'][0] == val
def test_get_filtered_courses_result(all_courses, filters_to_results): for key, val in filters_to_results.items(): assert list(Course.get_filtered_courses(all_courses, [key])['result']) == list(val)
def test_get_rating_and_load_filters(courses, all_courses): # expect only Course1 db_courses = Course.get_filtered_courses_by_load(2, all_courses) db_courses = Course.get_filtered_courses_by_rating(3, db_courses) assert db_courses.filter(pk=courses[0].course_id)