I am writing tests for django application, and I don’t understand why put and patch requests don’t change the actual models fields
setUp
def setUp(self) -> None:
self.c = APIClient()
user_kw = dict(username='just_user', password='password', email='user@gmail.com')
user_kw['password'] = make_password(user_kw['password'])
self.user = User.objects.create(**user_kw)
self.superuser = User.objects.create_superuser(username='admin', email='admin@test.com', password='password')
self.discount = Discount.objects.get(user=self.user)
The test functions
def test_put_superuser(self):
self.c.force_login(self.superuser)
response = self.c.put(f'/api/accounts/discount/{self.discount.id}',
{"user": self.user.id,
"pointcount": OrderedDict([("points", 0.0), ("id", self.user.id)]),
"discount": 0.9,
"prev_count": 100.0}
, format='json', follow = True)
self.assertEqual(response.data, {"user": self.user.id,
"pointcount": OrderedDict([("points", 0.0),
("id", self.user.id), ('user',self.user.id)]),
"discount": 0.9,
"prev_count": 100.0})
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_patch_superuser(self):
self.c.force_login(self.superuser)
response = self.c.patch(f'/api/accounts/discount/{self.user.id}',
{"user": self.user.id,
"pointcount": OrderedDict([("points", 0.0), ("id", self.user.id)]),
"discount": 0.9,
"prev_count": 100.0}
, format='json', follow = True)
obj = Discount.objects.get(user=self.user.id)
obj.refresh_from_db()
self.assertEqual(response.data, {"user": self.user.id,
"pointcount": OrderedDict([("points", 0.0),
("id", self.user.id)]),
"discount": 0.9,
"prev_count": 100.0})
self.assertEqual(response.status_code, status.HTTP_200_OK)
The Discount model:
class Discount(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
pointcount = models.OneToOneField(PointCount,on_delete=models.CASCADE)
discount = models.FloatField(default=0.05,validators=[MinValueValidator(0),MaxValueValidator(1)]) #range 0-1. 0.05=5%
prev_count = models.FloatField(default=0,validators=[MinValueValidator(0)]) #points collected previous month
The PointCount model:
class PointCount(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
points = models.FloatField(validators=[MinValueValidator(0)])
The Discount serializer:
class DiscountModelSerializer(serializers.ModelSerializer):
pointcount = PointCountNestedSerializer()
class Meta:
model = Discount
fields = ['user', 'pointcount', 'discount', 'prev_count']
def __parce_nested(self, validated_data, field_name, prop_name):
pointcount_data = validated_data.pop(field_name)
validated_data.update({prop_name: pointcount_data.get("id")})
return validated_data
def create(self, validated_data):
validated_data = self.__parce_nested(validated_data, 'pointcount', 'pointcount_id')
return Discount.objects.create(**validated_data)
def update(self, instance, validated_data):
pointcount_data = validated_data.pop('pointcount')
instance = super().update(instance, validated_data)
instance.pointcount_id = pointcount_data.get('id')
instance.save()
return instance
The failure in the tests:
FAIL: test_patch_superuser (apps.accounts.tests.tests_discount.DiscountTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ubuntu/febPy/eshop/apps/accounts/tests/tests_discount.py", line 106, in test_patch_superuser
self.assertEqual(response.data, {"user": self.user.id,
AssertionError: {'use[40 chars]nts', 0.0), ('id', 11)]), 'discount': 0.05, 'prev_count': 0.0} != {'use[40 chars]nts', 0.0), ('id', 11)]), 'discount': 0.9, 'prev_count': 100.0}
======================================================================
FAIL: test_put_superuser (apps.accounts.tests.tests_discount.DiscountTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ubuntu/febPy/eshop/apps/accounts/tests/tests_discount.py", line 84, in test_put_superuser
self.assertEqual(response.data, {"user": self.user.id,
AssertionError: {'use[54 chars]id', 15)]), 'discount': 0.05, 'prev_count': 0.0} != {'use[54 chars]id', 15), ('user', 15)]), 'discount': 0.9, 'prev_count': 100.0}
----------------------------------------------------------------------
Ran 9 tests in 5.118s
FAILED (failures=2)
When I try to change the serializer directly in browser, everything works well, but there is a problem in tests.