def test_add_employment(self): """ Test that we handle adding an employment correctly """ employment_object = { "city": "NY", "state_or_territory": "NY", "country": "USA", "company_name": "XYZ-ABC", "position": "SSE", "industry": "IT", "end_date": "2016-05-17", "start_date": "2016-05-28" } user1 = UserFactory.create() user2 = UserFactory.create() serializer = ProfileSerializer(instance=user1.profile, data={ 'work_history': [employment_object], 'education': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert user1.profile.work_history.count() == 1 employment = user1.profile.work_history.first() employment_object['id'] = employment.id assert EmploymentSerializer(employment).data == employment_object # Other profile did not get the employment assigned to it assert user2.profile.work_history.count() == 0
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_employment(self): """ Test that we handle adding an employment correctly """ employment_object = { "city": "NY", "state_or_territory": "NY", "country": "USA", "company_name": "XYZ-ABC", "position": "SSE", "industry": "IT", "end_date": "2016-05-17", "start_date": "2016-05-28" } user1 = UserFactory.create() user2 = UserFactory.create() serializer = ProfileSerializer(instance=user1.profile, data={ 'work_history': [employment_object], 'education': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert user1.profile.work_history.count() == 1 employment = user1.profile.work_history.first() employment_object['id'] = employment.id assert EmploymentSerializer().to_representation( employment) == employment_object # Other profile did not get the employment assigned to it assert user2.profile.work_history.count() == 0
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 education_data = EducationSerializer(education).data assert education_data == education_object # Other profile did not get the education assigned to it assert user2.profile.education.count() == 0
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
def test_delete_employment(self): """ Test that we delete employments which aren't specified in the PATCH """ with mute_signals(post_save): employment1 = EmploymentFactory.create() EmploymentFactory.create(profile=employment1.profile) # has a different profile employment3 = EmploymentFactory.create() assert employment1.profile.work_history.count() == 2 employment_object1 = EmploymentSerializer().to_representation( employment1) serializer = ProfileSerializer(instance=employment1.profile, data={ 'work_history': [employment_object1], 'education': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert employment1.profile.work_history.count() == 1 assert employment1.profile.work_history.first() == employment1 # Other profile is unaffected assert employment3.profile.work_history.count() == 1
def patch(self, request, *args, **kwargs): profile = self.get_object() serializer = ProfileSerializer(profile, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(status=HTTP_200_OK) return Response(data=serializer.errors, status=HTTP_400_BAD_REQUEST)
def patch(self, request): user = request.user profile = Profile.objects.get(user=user) serializer = ProfileSerializer(profile, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): profile = request.user.base_profile() serializer = ProfileSerializer(data=request.data, instance=profile) if serializer.is_valid(): data = serializer.save() response_data = update_template( **{ 'as_json': False, 'status': status.HTTP_200_OK, 'request': request, 'result': ProfileSerializer(data).data }) return Response(response_data, status=status.HTTP_200_OK) else: response_data = update_template( **{ 'as_json': False, 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'request': request, 'result': serializer.errors }) return Response(response_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_update_employment_different_profile(self): """ Make sure we can't edit an employment for a different profile """ with mute_signals(post_save): employment1 = EmploymentFactory.create() employment2 = EmploymentFactory.create() employment_object = EmploymentSerializer(employment1).data employment_object['id'] = employment2.id serializer = ProfileSerializer(instance=employment1.profile, data={ 'work_history': [employment_object], 'education': [] }) serializer.is_valid(raise_exception=True) with self.assertRaises(ValidationError) as ex: serializer.save() assert ex.exception.detail == ["Work history {} does not exist".format(employment2.id)]
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_update_employment(self): """ Test that we handle updating an employment correctly """ with mute_signals(post_save): employment = EmploymentFactory.create() employment_object = EmploymentSerializer(employment).data employment_object['position'] = "SE" serializer = ProfileSerializer(instance=employment.profile, data={ 'work_history': [employment_object], 'education': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert employment.profile.work_history.count() == 1 employment = employment.profile.work_history.first() assert EmploymentSerializer(employment).data == employment_object
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_update_employment_different_profile(self): """ Make sure we can't edit an employment for a different profile """ with mute_signals(post_save): employment1 = EmploymentFactory.create() employment2 = EmploymentFactory.create() employment_object = EmploymentSerializer(employment1).data employment_object['id'] = employment2.id serializer = ProfileSerializer(instance=employment1.profile, data={ 'work_history': [employment_object], 'education': [] }) serializer.is_valid(raise_exception=True) with self.assertRaises(ValidationError) as ex: serializer.save() assert ex.exception.detail == [ "Work history {} does not exist".format(employment2.id) ]
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(education1).data 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_delete_employment(self): """ Test that we delete employments which aren't specified in the PATCH """ with mute_signals(post_save): employment1 = EmploymentFactory.create() EmploymentFactory.create(profile=employment1.profile) # has a different profile employment3 = EmploymentFactory.create() assert employment1.profile.work_history.count() == 2 employment_object1 = EmploymentSerializer(employment1).data serializer = ProfileSerializer(instance=employment1.profile, data={ 'work_history': [employment_object1], 'education': [] }) serializer.is_valid(raise_exception=True) serializer.save() assert employment1.profile.work_history.count() == 1 assert employment1.profile.work_history.first() == employment1 # Other profile is unaffected assert employment3.profile.work_history.count() == 1