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'
This commit is contained in:
Pierre Chiquet 2017-02-14 18:48:00 +01:00 committed by Andrew Godwin
parent 4185798731
commit 95c9925fe4
4 changed files with 53 additions and 3 deletions

View File

@ -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):

12
channels/tests/models.py Normal file
View File

@ -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)

View File

@ -6,7 +6,8 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.admin',
'channels',
'channels.delay'
'channels.delay',
'channels.tests'
)
DATABASES = {

View File

@ -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