def profil_api(request): profil = Profil.objects.first() if request.method == "GET": serializer = ProfilSerializer(profil) return Response(serializer.data) elif request.method == "PUT": serializer = ProfilSerializer(instance=profil, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def assert_with_missing_key(self, key, required=True): data = self.valid_data data.pop(key) serializer = ProfilSerializer(data=data) if required: self.assertFalse(serializer.is_valid()) self.assertEqual(serializer.validated_data, {}) self.assertEqual(serializer.errors, {key:['This field is required.']}) else: self.assertTrue(serializer.is_valid()) self.assertEqual(dict(serializer.validated_data), data)
def test_with_partial_update(self): self.valid_data.pop('deskripsi') serializer = ProfilSerializer(data=self.valid_data, partial=True) self.assertTrue(serializer.is_valid()) self.assertEqual(dict(serializer.validated_data), self.valid_data) self.assertEqual(dict(serializer.errors), {})
def test_with_valid_data(self): serializer = ProfilSerializer(data=self.valid_data) self.assertTrue(serializer.is_valid()) self.assertEqual(dict(serializer.validated_data) , self.valid_data)