def test_serializer_should_be_invalid_if_phone_number_is_not_unique(self): self.serialized_mobile_user['location'] = self.district.id serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertTrue(serializer.is_valid()) serializer.save() serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertFalse(serializer.is_valid())
def test_serializer_should_be_valid_if_model_is_being_updated(self): self.serialized_mobile_user['location'] = self.district.id serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertTrue(serializer.is_valid()) saved_profile = serializer.save() self.serialized_mobile_user['id'] = saved_profile.id serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertTrue(serializer.is_valid())
def test_empty_email_is_not_unique(self): self.serialized_mobile_user['location'] = self.district.id del self.serialized_mobile_user['email'] serializer = UserProfileSerializer(data=self.serialized_mobile_user) serializer.is_valid() user_without_email = serializer.save() another_user_attr = dict(name='Haha', phone='+2567711111', location=self.district.id) serializer = UserProfileSerializer(data=another_user_attr) self.assertTrue(serializer.is_valid())
def test_should_deserialize_mobile_user_object(self): self.serialized_mobile_user['location'] = self.district.id serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertTrue(serializer.is_valid()) saved_mobile_user = serializer.save() self.assertTrue(isinstance(saved_mobile_user, UserProfile)) for attribute, value in self.mobile_user.items(): self.assertEqual(value, getattr(saved_mobile_user, attribute))
def test_serializer_should_be_valid_if_no_email_is_passed(self): self.serialized_mobile_user['location'] = self.district.id del self.serialized_mobile_user['email'] serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertTrue(serializer.is_valid())
def test_serializer_should_be_invalid_if_username_is_not_unique(self): User(username='******', password='******').save() self.serialized_mobile_user['location'] = self.district.id self.serialized_mobile_user['username'] = '******' serializer = UserProfileSerializer(data=self.serialized_mobile_user) self.assertFalse(serializer.is_valid())