Skip to content

Commit

Permalink
[channel] Added test for signal receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Nov 21, 2017
1 parent babe7ba commit 8eb7f6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion django_loci/channels/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@receiver(post_save, sender=Location, dispatch_uid='ws_update_mobile_location')
def update_mobile_location(sender, instance, **kwargs):
if instance.geometry:
if not kwargs.get('created') and instance.geometry:
group_name = 'geo.mobile-location.{0}'.format(str(instance.pk))
message = {'text': instance.geometry.geojson}
Group(group_name).send(message, immediately=True)
14 changes: 11 additions & 3 deletions django_loci/tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _test_ws_add(self, pk=None, user=None):
if user:
self.client.force_login(user)
self.client.send_and_consume('websocket.connect', path=path)
return path
return {'ol': ol, 'path': path}

def test_ws_add_unauthenticated(self):
try:
Expand All @@ -47,9 +47,9 @@ def test_ws_add_unauthenticated(self):
self.fail('AssertionError not raised')

def test_connect_and_disconnect(self):
path = self._test_ws_add(user=self._create_admin())
res = self._test_ws_add(user=self._create_admin())
self.assertEqual(self.client.receive(), None)
self.client.send_and_consume('websocket.disconnect', path=path)
self.client.send_and_consume('websocket.disconnect', path=res['path'])

def test_ws_add_not_staff(self):
user = self.user_model.objects.create_user(username='user',
Expand All @@ -71,3 +71,11 @@ def test_ws_add_404(self):
self.assertIn('Connection rejected', str(e))
else:
self.fail('AssertionError not raised')

def test_location_update(self):
res = self._test_ws_add(user=self._create_admin())
res['ol'].location.geometry = 'POINT (12.513124 41.897903)'
res['ol'].location.save()
result = self.client.receive()
self.assertIsInstance(result, dict)
self.assertDictEqual(result, {'type': 'Point', 'coordinates': [12.513124, 41.897903]})

0 comments on commit 8eb7f6f

Please sign in to comment.