def test_load_course(mock_upsert_tasks, course_exists, is_published, blacklisted): """Test that load_course loads the course""" course = (CourseFactory.create(runs=None, published=is_published) if course_exists else CourseFactory.build()) assert Course.objects.count() == (1 if course_exists else 0) assert LearningResourceRun.objects.count() == 0 props = model_to_dict( CourseFactory.build(course_id=course.course_id, platform=course.platform, published=is_published)) del props["id"] run = model_to_dict( LearningResourceRunFactory.build(platform=course.platform)) del run["content_type"] del run["object_id"] del run["id"] props["runs"] = [run] blacklist = [course.course_id] if blacklisted else [] result = load_course(props, blacklist, []) if course_exists and not is_published and not blacklisted: mock_upsert_tasks.delete_course.assert_called_with(result) elif is_published and not blacklisted: mock_upsert_tasks.upsert_course.assert_called_with(result.id) else: mock_upsert_tasks.delete_program.assert_not_called() mock_upsert_tasks.upsert_course.assert_not_called() assert Course.objects.count() == 1 assert LearningResourceRun.objects.count() == 1 # assert we got a course back assert isinstance(result, Course) for key, value in props.items(): assert getattr(result, key) == value, f"Property {key} should equal {value}"
def test_load_duplicate_course(mock_upsert_tasks, course_exists, course_id_is_duplicate, duplicate_course_exists): """Test that load_course loads the course""" course = CourseFactory.create( runs=None) if course_exists else CourseFactory.build() duplicate_course = (CourseFactory.create(runs=None, platform=course.platform) if duplicate_course_exists else CourseFactory.build()) if course_exists and duplicate_course_exists: assert Course.objects.count() == 2 elif course_exists or duplicate_course_exists: assert Course.objects.count() == 1 else: assert Course.objects.count() == 0 assert LearningResourceRun.objects.count() == 0 duplicates = [{ "course_id": course.course_id, "duplicate_course_ids": [course.course_id, duplicate_course.course_id], }] course_id = (duplicate_course.course_id if course_id_is_duplicate else course.course_id) props = model_to_dict( CourseFactory.build(course_id=course_id, platform=course.platform)) del props["id"] run = model_to_dict( LearningResourceRunFactory.build(platform=course.platform)) del run["content_type"] del run["object_id"] del run["id"] props["runs"] = [run] result = load_course(props, [], duplicates) if course_id_is_duplicate and duplicate_course_exists: mock_upsert_tasks.delete_course.assert_called() mock_upsert_tasks.upsert_course.assert_called_with(result.id) assert Course.objects.count() == (2 if duplicate_course_exists else 1) assert LearningResourceRun.objects.count() == 1 # assert we got a course back assert isinstance(result, Course) saved_course = Course.objects.filter(course_id=course.course_id).first() for key, value in props.items(): assert getattr(result, key) == value, f"Property {key} should equal {value}" assert ( getattr(saved_course, key) == value ), f"Property {key} should be updated to {value} in the database"