Skip to content
Permalink
Browse files

Add in an async test for our websocket communicator

  • Loading branch information...
overshard committed Jul 4, 2018
1 parent e44aeb2 commit 0081e1a09ac87e457d37d61a1b04f629d08028ae
Showing with 116 additions and 24 deletions.
  1. +1 −0 .coveragerc
  2. +2 −0 Pipfile
  3. +68 −4 Pipfile.lock
  4. +3 −2 core/{tests → }/tests.py
  5. 0 core/tests/__init__.py
  6. +20 −18 sockets/consumers.py
  7. +22 −0 sockets/tests.py
@@ -13,6 +13,7 @@ omit =
*/admin.py
*/apps.py
*/urls.py
*/routing.py
*/tests*.py
*/migrations/*
*/management/*
@@ -12,6 +12,8 @@ selenium = "*"
sphinx = "*"
recommonmark = "*"
"sphinx-rtd-theme" = "*"
pytest-django = "*"
pytest-asyncio = "*"

[packages]
django = "*"
@@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "1edfecc48346a442018ec88ced24951dbe826a987a8a6449076ccb2a47600f42"
"sha256": "534b48d99dcab08dea19c6bd8b7bf759e1361c0eeb612951598594992d7e4eef"
},
"pipfile-spec": 6,
"requires": {
@@ -71,6 +71,7 @@
"sha256:5b28ce1c5bf44ec3c13109bb8783e39e3e90d7384fd3dad4bb773425dd439d84",
"sha256:ea058f94b0e9fedf6fc604f47d192d81313c0f014ebd7c1160487f34187bb266"
],
"markers": "python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.0.*' and python_version != '3.2.*'",
"version": "==2.2.0"
},
"diff-match-patch": {
@@ -272,6 +273,7 @@
"sha256:3ad1e553c7b66a501abbcf7736620210cc58928730d3e1b905b345fc30043d4b",
"sha256:a4cc164a781859c74de47f17f0e85f4bce8a3321a9d0892c015c8f80c4158ad9"
],
"markers": "python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.0.*' and python_version != '3.2.*'",
"version": "==18.4.0"
},
"txaio": {
@@ -320,7 +322,7 @@
"sha256:e881ef610ff48aece2f4ee2af03d2db1a146dc7c705561bd6089b2356f61641f",
"sha256:f41037260deaacb875db250021fe883bf536bf6414a4fd25b25059b02e31b120"
],
"markers": "python_version != '3.0.*' and python_version >= '2.7' and python_version != '3.2.*' and python_version != '3.1.*'",
"markers": "python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.0.*' and python_version != '3.2.*'",
"version": "==4.5.0"
}
},
@@ -332,6 +334,20 @@
],
"version": "==0.7.11"
},
"atomicwrites": {
"hashes": [
"sha256:240831ea22da9ab882b551b31d4225591e5e447a68c5e188db5b89ca1d487585",
"sha256:a24da68318b08ac9c9c45029f4a10371ab5b20e4226738e150e6e7c571630ae6"
],
"version": "==1.1.5"
},
"attrs": {
"hashes": [
"sha256:4b90b09eeeb9b88c35bc642cbac057e45a5fd85367b985bd2809c62b7b939265",
"sha256:e0d0eb91441a3b53dab4d9b743eafc1ac44476296a2053b6ca3af0b139faf87b"
],
"version": "==18.1.0"
},
"babel": {
"hashes": [
"sha256:6778d85147d5d85345c14a26aada5e478ab04e39b078b0745ee6870c2b5cf669",
@@ -390,7 +406,7 @@
"sha256:e05cb4d9aad6233d67e0541caa7e511fa4047ed7750ec2510d466e806e0255d6",
"sha256:f3f501f345f24383c0000395b26b726e46758b71393267aeae0bd36f8b3ade80"
],
"markers": "python_version < '4' and python_version != '3.1.*' and python_version >= '2.6' and python_version != '3.2.*' and python_version != '3.0.*'",
"markers": "python_version < '4' and python_version != '3.2.*' and python_version >= '2.6' and python_version != '3.0.*' and python_version != '3.1.*'",
"version": "==4.5.1"
},
"coveralls": {
@@ -455,13 +471,38 @@
],
"version": "==0.6.1"
},
"more-itertools": {
"hashes": [
"sha256:2b6b9893337bfd9166bee6a62c2b0c9fe7735dcf85948b387ec8cba30e85d8e8",
"sha256:6703844a52d3588f951883005efcf555e49566a48afd4db4e965d69b883980d3",
"sha256:a18d870ef2ffca2b8463c0070ad17b5978056f403fb64e3f15fe62a52db21cc0"
],
"version": "==4.2.0"
},
"packaging": {
"hashes": [
"sha256:e9215d2d2535d3ae866c3d6efc77d5b24a0192cce0ff20e42896cc0664f889c0",
"sha256:f019b770dd64e585a99714f1fd5e01c7a8f11b45635aa953fd41c689a657375b"
],
"version": "==17.1"
},
"pluggy": {
"hashes": [
"sha256:7f8ae7f5bdf75671a718d2daf0a64b7885f74510bcd98b1a0bb420eb9a9d0cff",
"sha256:d345c8fe681115900d6da8d048ba67c25df42973bda370783cd58826442dcd7c",
"sha256:e160a7fcf25762bb60efc7e171d4497ff1d8d2d75a3d0df7a21b76821ecbf5c5"
],
"markers": "python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.2.*' and python_version != '3.0.*' and python_version != '3.3.*'",
"version": "==0.6.0"
},
"py": {
"hashes": [
"sha256:3fd59af7435864e1a243790d322d763925431213b6b8529c6ca71081ace3bbf7",
"sha256:e31fb2767eb657cbde86c454f02e99cb846d3cd9d61b318525140214fdc0e98e"
],
"markers": "python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.2.*' and python_version != '3.0.*' and python_version != '3.3.*'",
"version": "==1.5.4"
},
"pycodestyle": {
"hashes": [
"sha256:682256a5b318149ca0d2a9185d365d8864a768a28db66a84a2ea946bcc426766",
@@ -490,6 +531,27 @@
],
"version": "==2.2.0"
},
"pytest": {
"hashes": [
"sha256:8ea01fc4fcc8e1b1e305252b4bc80a1528019ab99fd3b88666c9dc38d754406c",
"sha256:90898786b3d0b880b47645bae7b51aa9bbf1e9d1e4510c2cfd15dd65c70ea0cd"
],
"version": "==3.6.2"
},
"pytest-asyncio": {
"hashes": [
"sha256:286b50773e996c80d894b95afaf45df6952408a67a59979ca9839f94693ec7fd",
"sha256:f32804bb58a66e13a3eda11f8942a71b1b6a30466b0d2ffe9214787aab0e172e"
],
"version": "==0.8.0"
},
"pytest-django": {
"hashes": [
"sha256:088b66211c57972e7bd072ba73ccec1c3b6f6185f894b3b8eb966d2f3b7d46cb",
"sha256:3fea4d0a84bf3af1f1e82448b9a91b3ddb22b659d802e026ae843040da0c3220"
],
"version": "==3.3.2"
},
"pytz": {
"hashes": [
"sha256:a061aa0a9e06881eb8b3b2b43f05b9439d6583c206d0a6c340ff72a7b6669053",
@@ -509,6 +571,7 @@
"sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1",
"sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a"
],
"markers": "python_version < '4' and python_version != '3.2.*' and python_version != '3.3.*' and python_version >= '2.6' and python_version != '3.0.*' and python_version != '3.1.*'",
"version": "==2.19.1"
},
"selenium": {
@@ -551,14 +614,15 @@
"sha256:68ca7ff70785cbe1e7bccc71a48b5b6d965d79ca50629606c7861a21b206d9dd",
"sha256:9de47f375baf1ea07cdb3436ff39d7a9c76042c10a769c52353ec46e4e8fc3b9"
],
"markers": "python_version != '3.1.*' and python_version != '3.0.*' and python_version != '3.2.*' and python_version >= '2.7' and python_version != '3.3.*'",
"markers": "python_version >= '2.7' and python_version != '3.2.*' and python_version != '3.3.*' and python_version != '3.0.*' and python_version != '3.1.*'",
"version": "==1.1.0"
},
"urllib3": {
"hashes": [
"sha256:a68ac5e15e76e7e5dd2b8f94007233e01effe3e50e8daddf69acfd81cb686baf",
"sha256:b5725a0bd4ba422ab0e66e89e030c806576753ea3ee08554382c14e685d117b5"
],
"markers": "python_version < '4' and python_version != '3.2.*' and python_version != '3.3.*' and python_version >= '2.6' and python_version != '3.0.*' and python_version != '3.1.*'",
"version": "==1.23"
}
}
@@ -11,8 +11,9 @@
from faker import Factory

from conf.models import Site, SitePermission
from ..models import Client, Project, Entry, Task
from ..utils import parse_duration, duration_string, duration_decimal

from .models import Client, Project, Entry, Task
from .utils import parse_duration, duration_string, duration_decimal


fake = Factory.create()
No changes.
@@ -1,32 +1,34 @@
import json

from channels.generic.websocket import WebsocketConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async

from core.models import Task, Client, Project


class UpdateConsumer(WebsocketConsumer):
class UpdateConsumer(AsyncWebsocketConsumer):
# TODO: This really should use channel layers and not something as janky as
# storing everything currently avaliable on connection and double check on
# polling. That defeats the purpose of using websockets.

tasks = 0
clients = 0
projects = 0
async def connect(self):
await self.accept()

def connect(self):
self.accept()
async def receive(self, text_data):
await self.send(json.dumps({
'tasks': await self.get_task_count(),
'clients': await self.get_client_count(),
'projects': await self.get_project_count(),
}))

def disconnect(self, close_code):
pass
@database_sync_to_async
async def get_task_count(self):
return Task.objects.count()

def receive(self, text_data):
self.tasks = Task.objects.count()
self.clients = Client.objects.filter(archive=False).count()
self.projects = Project.objects.filter(archive=False).count()
@database_sync_to_async
async def get_client_count(self):
return Client.objects.filter(archive=False).count()

self.send(json.dumps({
'tasks': self.tasks,
'clients': self.clients,
'projects': self.projects,
}))
@database_sync_to_async
async def get_project_count(self):
return Project.objects.filter(archive=False).count()
@@ -0,0 +1,22 @@
import pytest

from django.core.management import call_command

from channels.testing import WebsocketCommunicator

from .consumers import UpdateConsumer


call_command('migrate', verbosity=0)
call_command('fake', iterations=1, verbosity=0)


@pytest.mark.asyncio
async def test_websocket(self):
communicator = WebsocketCommunicator(UpdateConsumer, '/socket/')
connected, subprotocol = await communicator.connect()
assert connected
await communicator.send_to(text_data="hello")
response = await communicator.receive_from()
assert response == "hello"
await communicator.disconnect()

0 comments on commit 0081e1a

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.