mirror of
https://github.com/django/daphne.git
synced 2025-07-10 16:02:18 +03:00
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:
parent
4185798731
commit
95c9925fe4
|
@ -120,7 +120,8 @@ class Binding(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def pre_save_receiver(cls, instance, **kwargs):
|
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
|
@classmethod
|
||||||
def post_save_receiver(cls, instance, created, **kwargs):
|
def post_save_receiver(cls, instance, created, **kwargs):
|
||||||
|
|
12
channels/tests/models.py
Normal file
12
channels/tests/models.py
Normal 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)
|
|
@ -6,7 +6,8 @@ INSTALLED_APPS = (
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'channels',
|
'channels',
|
||||||
'channels.delay'
|
'channels.delay',
|
||||||
|
'channels.tests'
|
||||||
)
|
)
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
|
|
|
@ -6,7 +6,7 @@ from channels import route
|
||||||
from channels.binding.base import CREATE, DELETE, UPDATE
|
from channels.binding.base import CREATE, DELETE, UPDATE
|
||||||
from channels.binding.websockets import WebsocketBinding
|
from channels.binding.websockets import WebsocketBinding
|
||||||
from channels.generic.websockets import WebsocketDemultiplexer
|
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()
|
User = get_user_model()
|
||||||
|
|
||||||
|
@ -55,6 +55,42 @@ class TestsBinding(ChannelTestCase):
|
||||||
received = client.receive()
|
received = client.receive()
|
||||||
self.assertIsNone(received)
|
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):
|
def test_trigger_outbound_create_exclude(self):
|
||||||
class TestBinding(WebsocketBinding):
|
class TestBinding(WebsocketBinding):
|
||||||
model = User
|
model = User
|
||||||
|
|
Loading…
Reference in New Issue
Block a user