def test_es_userlist_serializer(list_type, privacy_level, user): """ Test that ESUserListSerializer correctly serializes a UserList object """ user_list = UserListFactory.create( list_type=list_type, privacy_level=privacy_level, author=user ) serialized = ESUserListSerializer(user_list).data assert_json_equal( serialized, { "author": user.id, "object_type": list_type, "list_type": list_type, "privacy_level": privacy_level, "id": user_list.id, "short_description": user_list.short_description, "title": user_list.title, "image_src": user_list.image_src.url, "topics": list(user_list.topics.values_list("name", flat=True)), "created": drf_datetime(user_list.created_on), "default_search_priority": 0, "minimum_price": 0, }, )
def test_es_userlist_serializer_image_src(): """ Test that ESUserListSerializer uses 1st non-list list item image_src if the list image_src is None """ user_list = UserListFactory.create(image_src=None) UserListItemFactory.create(user_list=user_list, position=1, is_userlist=True) list_item_course = UserListItemFactory.create( user_list=user_list, position=2, is_course=True ) serialized = ESUserListSerializer(user_list).data assert_json_equal( serialized, { "author": user_list.author.id, "object_type": user_list.list_type, "list_type": user_list.list_type, "privacy_level": user_list.privacy_level, "id": user_list.id, "short_description": user_list.short_description, "title": user_list.title, "image_src": list_item_course.item.image_src, "topics": list(user_list.topics.values_list("name", flat=True)), "created": drf_datetime(user_list.created_on), "default_search_priority": 0, "minimum_price": 0, }, )
def test_es_program_serializer(offered_by): """ Test that ESProgramSerializer correctly serializes a program object """ program = ProgramFactory.create(offered_by=offered_by) serialized = ESProgramSerializer(program).data assert_json_equal( serialized, { "object_type": PROGRAM_TYPE, "id": program.id, "short_description": program.short_description, "title": program.title, "image_src": program.image_src, "topics": list(program.topics.values_list("name", flat=True)), "runs": [ ESRunSerializer(program_run).data for program_run in program.runs.order_by("-best_start_date") ], "offered_by": list(program.offered_by.values_list("name", flat=True)), "created": drf_datetime(program.created_on), "default_search_priority": 1, "minimum_price": minimum_price(program), }, )
def test_es_course_serializer(offered_by): """ Test that ESCourseSerializer correctly serializes a course object """ course = CourseFactory.create(offered_by=offered_by) serialized = ESCourseSerializer(course).data assert_json_equal( serialized, { "object_type": COURSE_TYPE, "id": course.id, "course_id": course.course_id, "coursenum": course.course_id.split("+")[-1], "short_description": course.short_description, "full_description": course.full_description, "platform": course.platform, "title": course.title, "image_src": course.image_src, "topics": list(course.topics.values_list("name", flat=True)), "runs": [ ESRunSerializer(course_run).data for course_run in course.runs.order_by("-best_start_date") ], "published": True, "offered_by": list(course.offered_by.values_list("name", flat=True)), "created": drf_datetime(course.created_on), "default_search_priority": 1, "minimum_price": minimum_price(course), "resource_relations": {"name": "resource"}, }, )
def test_es_course_price_serializer(): """Test that the course price serializer serializes a price""" price = CoursePriceFactory.create() assert_json_equal( ESCoursePriceSerializer(price).data, {"price": f"{price.price:.2f}", "mode": price.mode}, )
def test_serialize_course_for_bulk(): """ Test that serialize_course_for_bulk yields a valid ESCourseSerializer """ course = CourseFactory.create() assert_json_equal( serialize_course_for_bulk(course), { "_id": gen_course_id(course.platform, course.course_id), **ESCourseSerializer(course).data, }, )
def test_es_run_serializer(has_full_name): """ Test that ESRunSerializer correctly serializes a run object """ learning_resource_run = ( LearningResourceRunFactory.create() if has_full_name else LearningResourceRunFactory.create(instructors__full_name=None) ) serialized = ESRunSerializer(learning_resource_run).data assert_json_equal( serialized, { "id": learning_resource_run.id, "run_id": learning_resource_run.run_id, "short_description": learning_resource_run.short_description, "full_description": learning_resource_run.full_description, "language": learning_resource_run.language, "semester": learning_resource_run.semester, "year": int(learning_resource_run.year), "level": learning_resource_run.level, "start_date": learning_resource_run.start_date.strftime(ISOFORMAT), "end_date": learning_resource_run.end_date.strftime(ISOFORMAT), "enrollment_start": learning_resource_run.enrollment_start.strftime( ISOFORMAT ), "enrollment_end": learning_resource_run.enrollment_end.strftime(ISOFORMAT), "best_start_date": learning_resource_run.best_start_date, "best_end_date": learning_resource_run.best_end_date, "title": learning_resource_run.title, "image_src": learning_resource_run.image_src, "instructors": [ ( instructor.full_name if has_full_name else " ".join([instructor.first_name, instructor.last_name]) ) for instructor in learning_resource_run.instructors.all() ], "prices": [ ESCoursePriceSerializer(price).data for price in learning_resource_run.prices.all() ], "published": True, "availability": learning_resource_run.availability, "offered_by": list( learning_resource_run.offered_by.values_list("name", flat=True) ), }, )
def test_popular_content_serializer(mocker, is_deleted, user): """Test PopularContentSerializer""" resources = [ VideoFactory.create(), ProgramFactory.create(), CourseFactory.create(), UserListFactory.create(), BootcampFactory.create(), ] data = [{ "content_type_id": ContentType.objects.get_for_model(resource).id, "content_id": resource.id, } for resource in resources] if is_deleted: for resource in resources: resource.delete() resources = [] resources = [ type(resource).objects.filter( id=resource.id).prefetch_list_items_for_user( user).annotate_is_favorite_for_user(user).first() for resource in resources ] context = {"request": mocker.Mock(user=user)} # NOTE: we test PopularContentSerializer instead of PopularContentListSerializer # because the list serializer is never used directly, but rather many=True tells # PopularContentSerializer to delegate to PopularContentListSerializer results = PopularContentSerializer(data, context=context, many=True).data # should be sorted by the same order they were passed in assert_json_equal( results, [ GenericForeignKeyFieldSerializer(resource, context=context).data for resource in resources ], )
def test_es_content_file_serializer(): """ Verify that the ESContentFileSerializer has the correct data""" content_kwargs = { "content": "Some text", "content_author": "MIT", "content_language": "en", "content_title": "test title", } content_file = ContentFileFactory.create(**content_kwargs) serialized = ESContentFileSerializer(content_file).data assert_json_equal( serialized, { "object_type": RESOURCE_FILE_TYPE, "run_id": content_file.run.run_id, "run_title": content_file.run.title, "semester": content_file.run.semester, "year": int(content_file.run.year), "topics": list(content_file.run.topics.values_list("name", flat=True)), "key": content_file.key, "uid": content_file.uid, "resource_relations": { "name": "resourcefile", "parent": gen_course_id( content_file.run.content_object.platform, content_file.run.content_object.course_id, ), }, "title": content_file.title, "short_description": content_file.description, "file_type": content_file.file_type, "content_type": content_file.content_type, "url": content_file.url, "section": content_file.section, "content": content_kwargs["content"], "content_title": content_kwargs["content_title"], "content_author": content_kwargs["content_author"], "content_language": content_kwargs["content_language"], }, )
def test_assert_json_equal(): """Asserts that objects are equal in JSON""" assert_json_equal({"a": 1}, {"a": 1}) assert_json_equal(2, 2) assert_json_equal([2], [2])