Test crashes after Django version update
I encountered a problem after upgrading from django 3.1 to 4.2. There is a test for creating a device twice in a row with identical data:
@parameterized.expand([(MOBILE_APP_KIND.INSPECTOR,), (MOBILE_APP_KIND.VOLUNTEER,)])
def test_double_create_device(self, app_kind):
if app_kind == MOBILE_APP_KIND.INSPECTOR:
url = self.url_inspector
InspectorFactory(user=self.user)
else:
url = self.url_volunteer
response = self.client.post(url, self.data, format="json")
print(response.data)
self.assertEqual(response.status_code, 201)
devices = self.user.fcm_devices
self.assertEqual(devices.count(), 1)
response = self.client.post(url, self.data, format="json")
print(response.data)
self.assertEqual(response.status_code, 201)
self.user.refresh_from_db()
devices = self.user.fcm_devices
self.assertEqual(devices.count(), 1)
When adding a second device, the view should simply return the last device from the DB instead of creating it, to avoid registration_id uniqueness issues.
class FCMViewSet(FCMDeviceAuthorizedViewSet):
serializer_class = CommonFCMDeviceSerializer
http_method_names = ["post", "delete"]
def get_object(self):
return get_object_or_404(
FCMDevice,
registration_id=self.kwargs.get("registration_id"),
user=self.request.user,
active=True,
)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
instance.active = False
instance.save()
return Response(status=status.HTTP_204_NO_CONTENT)
class CommonFCMDeviceSerializer(serializers.ModelSerializer):
user = serializers.HiddenField(default=serializers.CurrentUserDefault())
app = serializers.ChoiceField(
choices=MOBILE_APP_KIND, required=True, write_only=True
)
class Meta(FCMSerializer.Meta):
model = FCMDevice
fields = (
"user",
"app",
"name",
"registration_id",
"device_id",
"active",
"type",
)
def create(self, validated_data):
devices = FCMDevice.objects.filter(
user=validated_data["user"],
app=validated_data["app"],
registration_id=validated_data["registration_id"],
active=True,
type=validated_data["type"],
)
if devices.exists():
return devices.last()
return FCMDevice.objects.create(**validated_data)
======================================================================
FAIL: test_double_create_device_0_inspector (api.mobile.common.v2.test.test_fcm_device.FCMCreateTestCase.test_double_create_device_0_inspector)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\xacla\AppData\Local\pypoetry\Cache\virtualenvs\core-2EZgQNjY-py3.11\Lib\site-packages\parameterized\parameterized.py", line 533, in standalone_func
return func(*(a + p.args), **p.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xacla\Desktop\Work\Work\DobroFon.Core\core\www\api\mobile\common\v2\test\test_fcm_device.py", line 89, in test_double_create_device
self.assertEqual(response.status_code, 201)
AssertionError: 400 != 201
{'name': '', 'registration_id': 'test_reg_id', 'device_id': 'test_dev_id', 'active': True, 'type': 'ios'}
{'registration_id': [ErrorDetail(string='FCM device with this Registration token already exists.', code='unique')]}
{'name': '', 'registration_id': 'test_reg_id', 'device_id': 'test_dev_id', 'active': True, 'type': 'ios'}
{'registration_id': [ErrorDetail(string='FCM device with this Registration token already exists.', code='unique')]}
I tried using create_or_update, I tried moving some of the logic to the presentation level - nothing helped.