def test_delete_education(self): """ Test that we delete Educations which aren't specified in the PATCH """ with mute_signals(post_save): education1 = EducationFactory.create() EducationFactory.create(profile=education1.profile) # has a different profile education3 = EducationFactory.create() assert education1.profile.education.count() == 2 education_object1 = EducationSerializer().to_representation(education1) serializer = ProfileSerializer(instance=education1.profile, data={ 'education': [education_object1], 'work_history': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert education1.profile.education.count() == 1 assert education1.profile.education.first() == education1 # Other profile is unaffected assert education3.profile.education.count() == 1
def test_add_education(self): """ Test that we handle adding an Education correctly """ education_object = { 'degree_name': DOCTORATE, 'graduation_date': '9876-04-23', 'field_of_study': 'subject', 'online_degree': True, 'school_name': 'school_name', 'school_city': 'school_city', 'school_state_or_territory': 'school_state_or_territory', 'school_country': 'school_country,' } user1 = UserFactory.create() user2 = UserFactory.create() serializer = ProfileSerializer(instance=user1.profile, data={ 'education': [education_object], 'work_history': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert user1.profile.education.count() == 1 education = user1.profile.education.first() education_object['id'] = education.id assert EducationSerializer().to_representation( education) == education_object # Other profile did not get the education assigned to it assert user2.profile.education.count() == 0
class PreferenceResponseSerializer(serializers.ModelSerializer): user = UserSerializer(required=False) area = AreaSerializer(many=True, required=False) education = EducationSerializer(many=True, required=False) job = JobSerializer(many=True, required=False) religion = ReligionSerializer(many=True, required=False) body = BodySerializer(many=True, required=False) class Meta: model = Preference fields = '__all__'
def test_update_education(self): """ Test that we handle updating an Education correctly """ with mute_signals(post_save): education = EducationFactory.create() education_data = EducationSerializer(education).data education_data['degree_name'] = BACHELORS serializer = ProfileSerializer(instance=education.profile, data={ 'education': [education_data], 'work_history': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert education.profile.education.count() == 1 education = education.profile.education.first() assert EducationSerializer(education).data == education_data
def test_limited(self): # pylint: disable=no-self-use """ Test limited serializer """ profile = self.create_profile() assert ProfileLimitedSerializer().to_representation(profile) == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'last_name': profile.last_name, 'preferred_name': profile.preferred_name, 'gender': profile.gender, 'account_privacy': profile.account_privacy, 'has_profile_image': profile.has_profile_image, 'profile_url_full': format_gravatar_url(profile.user.email, GravatarImgSize.FULL), 'profile_url_large': format_gravatar_url(profile.user.email, GravatarImgSize.LARGE), 'profile_url_medium': format_gravatar_url(profile.user.email, GravatarImgSize.MEDIUM), 'profile_url_small': format_gravatar_url(profile.user.email, GravatarImgSize.SMALL), 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'birth_country': profile.birth_country, 'preferred_language': profile.preferred_language, 'edx_level_of_education': profile.edx_level_of_education, 'education': [ EducationSerializer().to_representation(education) for education in profile.education.all() ], 'work_history': [ EmploymentSerializer().to_representation(work_history) for work_history in profile.work_history.all() ] }
def test_limited(self): """ Test limited serializer """ profile = self.create_profile() data = ProfileLimitedSerializer(profile).data assert data == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'last_name': profile.last_name, 'full_name': profile.full_name, 'preferred_name': profile.preferred_name, 'gender': profile.gender, 'account_privacy': profile.account_privacy, 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'birth_country': profile.birth_country, 'preferred_language': profile.preferred_language, 'edx_level_of_education': profile.edx_level_of_education, 'education': EducationSerializer(profile.education.all(), many=True).data, 'work_history': (EmploymentSerializer(profile.work_history.all(), many=True).data), 'image_medium': profile.image_medium.url, 'about_me': profile.about_me, 'romanized_first_name': profile.romanized_first_name, 'romanized_last_name': profile.romanized_last_name, }
def test_update_education_different_profile(self): """ Make sure we can't edit an education for a different profile """ with mute_signals(post_save): education1 = EducationFactory.create() education2 = EducationFactory.create() education_object = EducationSerializer().to_representation(education1) education_object['id'] = education2.id serializer = ProfileSerializer(instance=education1.profile, data={ 'education': [education_object], 'work_history': [] }) serializer.is_valid(raise_exception=True) with self.assertRaises(ValidationError) as ex: serializer.save() assert ex.exception.detail == [ "Education {} does not exist".format(education2.id) ]
def test_full(self): # pylint: disable=no-self-use """ Test full serializer """ profile = self.create_profile() assert ProfileSerializer().to_representation(profile) == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'filled_out': profile.filled_out, 'agreed_to_terms_of_service': profile.agreed_to_terms_of_service, 'last_name': profile.last_name, 'preferred_name': profile.preferred_name, 'email_optin': profile.email_optin, 'email': profile.email, 'gender': profile.gender, 'date_of_birth': DateTimeField().to_representation(profile.date_of_birth), 'account_privacy': profile.account_privacy, 'has_profile_image': profile.has_profile_image, 'profile_url_full': format_gravatar_url(profile.user.email, GravatarImgSize.FULL), 'profile_url_large': format_gravatar_url(profile.user.email, GravatarImgSize.LARGE), 'profile_url_medium': format_gravatar_url(profile.user.email, GravatarImgSize.MEDIUM), 'profile_url_small': format_gravatar_url(profile.user.email, GravatarImgSize.SMALL), 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'birth_country': profile.birth_country, 'nationality': profile.nationality, 'preferred_language': profile.preferred_language, 'pretty_printed_student_id': profile.pretty_printed_student_id, 'edx_level_of_education': profile.edx_level_of_education, 'education': [ EducationSerializer().to_representation(education) for education in profile.education.all() ], 'work_history': [ EmploymentSerializer().to_representation(work_history) for work_history in profile.work_history.all() ] }
def test_full(self): """ Test full serializer """ birthdate = date(1980, 1, 2) profile = self.create_profile(date_of_birth=birthdate) data = ProfileSerializer(profile).data assert data == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'full_name': profile.full_name, 'filled_out': profile.filled_out, 'agreed_to_terms_of_service': profile.agreed_to_terms_of_service, 'last_name': profile.last_name, 'preferred_name': profile.preferred_name, 'email_optin': profile.email_optin, 'email': profile.email, 'gender': profile.gender, 'date_of_birth': "1980-01-02", 'account_privacy': profile.account_privacy, 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'address': profile.address, 'postal_code': profile.postal_code, 'birth_country': profile.birth_country, 'nationality': profile.nationality, 'preferred_language': profile.preferred_language, 'pretty_printed_student_id': profile.pretty_printed_student_id, 'edx_level_of_education': profile.edx_level_of_education, 'education': EducationSerializer(profile.education.all(), many=True).data, 'work_history': (EmploymentSerializer(profile.work_history.all(), many=True).data), 'image': profile.image.url, 'image_small': profile.image_small.url, 'image_medium': profile.image_medium.url, 'about_me': profile.about_me, 'romanized_first_name': profile.romanized_first_name, 'romanized_last_name': profile.romanized_last_name, 'phone_number': profile.phone_number, 'student_id': profile.student_id, }