From 95c9925fe4a2e824dc11cbc61ee26a1b13532c56 Mon Sep 17 00:00:00 2001 From: Pierre Chiquet Date: Tue, 14 Feb 2017 18:48:00 +0100 Subject: [PATCH] Update Binding to support models with UUIDField as primary key (#528) * Add custom TestUUIDModel for auto tests * Update Binding to support models with UUIDField as primary key Add and fix test_trigger_outbound_create_non_auto_pk. Before updating pre_save_receiver, this new test failed with this error: ====================================================================== FAIL: test_trigger_outbound_create_non_auto_pk (channels.tests.test_binding.TestsBinding) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\User\git\contribs\python\channels\channels\tests\test_binding.py", line 85, in test_trigger_outbound_create_non_auto_pk self.assertEqual(received['payload']['action'], 'create') AssertionError: u'update' != u'create' --- channels/binding/base.py | 3 ++- channels/tests/models.py | 12 +++++++++++ channels/tests/settings.py | 3 ++- channels/tests/test_binding.py | 38 +++++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 channels/tests/models.py diff --git a/channels/binding/base.py b/channels/binding/base.py index 85ec720..88ae8e8 100644 --- a/channels/binding/base.py +++ b/channels/binding/base.py @@ -120,7 +120,8 @@ class Binding(object): @classmethod def pre_save_receiver(cls, instance, **kwargs): - cls.pre_change_receiver(instance, CREATE if instance.pk is None else UPDATE) + creating = instance._state.adding + cls.pre_change_receiver(instance, CREATE if creating else UPDATE) @classmethod def post_save_receiver(cls, instance, created, **kwargs): diff --git a/channels/tests/models.py b/channels/tests/models.py new file mode 100644 index 0000000..4b1da2d --- /dev/null +++ b/channels/tests/models.py @@ -0,0 +1,12 @@ +from uuid import uuid4 + +from django.db import models + + +class TestUUIDModel(models.Model): + """ + Simple model with UUIDField as primary key for tests. + """ + + id = models.UUIDField(primary_key=True, default=uuid4) + name = models.CharField(max_length=255) diff --git a/channels/tests/settings.py b/channels/tests/settings.py index 2ddf8ac..47fc407 100644 --- a/channels/tests/settings.py +++ b/channels/tests/settings.py @@ -6,7 +6,8 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.admin', 'channels', - 'channels.delay' + 'channels.delay', + 'channels.tests' ) DATABASES = { diff --git a/channels/tests/test_binding.py b/channels/tests/test_binding.py index 6b32a02..edd5126 100644 --- a/channels/tests/test_binding.py +++ b/channels/tests/test_binding.py @@ -6,7 +6,7 @@ from channels import route from channels.binding.base import CREATE, DELETE, UPDATE from channels.binding.websockets import WebsocketBinding from channels.generic.websockets import WebsocketDemultiplexer -from channels.tests import ChannelTestCase, HttpClient, apply_routes +from channels.tests import ChannelTestCase, HttpClient, apply_routes, models User = get_user_model() @@ -55,6 +55,42 @@ class TestsBinding(ChannelTestCase): received = client.receive() self.assertIsNone(received) + def test_trigger_outbound_create_non_auto_pk(self): + + class TestBinding(WebsocketBinding): + model = models.TestUUIDModel + stream = 'test' + fields = ['name'] + + @classmethod + def group_names(cls, instance): + return ["testuuidmodels"] + + def has_permission(self, user, action, pk): + return True + + client = HttpClient() + client.join_group('testuuidmodels') + + instance = models.TestUUIDModel.objects.create(name='testname') + + received = client.receive() + self.assertTrue('payload' in received) + self.assertTrue('action' in received['payload']) + self.assertTrue('data' in received['payload']) + self.assertTrue('name' in received['payload']['data']) + self.assertTrue('model' in received['payload']) + self.assertTrue('pk' in received['payload']) + + self.assertEqual(received['payload']['action'], 'create') + self.assertEqual(received['payload']['model'], 'tests.testuuidmodel') + self.assertEqual(received['payload']['pk'], str(instance.pk)) + + self.assertEqual(received['payload']['data']['name'], 'testname') + + received = client.receive() + self.assertIsNone(received) + def test_trigger_outbound_create_exclude(self): class TestBinding(WebsocketBinding): model = User