django / channels

Developer-friendly asynchrony for Django

Home Page:https://channels.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot support use django reverse() path() to testing routing

fan9704 opened this issue · comments

commented

Issues are for concrete, actionable bugs and feature requests only - if you're just asking for debugging help or technical support we have to direct you elsewhere. If you just have questions or support requests please use:

We have to limit this because of limited volunteer time to respond to issues!

Please also try and include, if you can:

  • Windows 11 22H2
  • A pip freeze output showing your package versions
aiohttp==3.8.5
aiosignal==1.3.1
amqp==5.1.1
annotated-types==0.5.0
asgiref==3.7.2
async-timeout==4.0.2
attrs==23.1.0
autobahn==23.6.2
Automat==22.10.0
billiard==4.1.0
CacheControl==0.13.1
cachetools==5.3.1
celery==5.3.0
certifi==2023.7.22
cffi==1.15.1
channels==4.0.0
channels-redis==4.1.0
charset-normalizer==3.2.0
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
colorama==0.4.6
constantly==15.1.0
coreapi==2.3.3
coreschema==0.0.4
cryptography==41.0.3
daphne==4.0.0
defusedxml==0.7.1
Django==4.2.4
django-cors-headers==4.2.0
django-elasticsearch-dsl==7.3
django-forestadmin==1.6.3
django-health-check==3.17.0
django-ipware==4.0.2
django-prometheus==2.3.1
django-redis==5.3.0
django-seed==0.3.1
djangorestframework==3.14.0
djangorestframework-simplejwt==5.2.2
drf-spectacular==0.26.2
drf-yasg==1.21.7
ecdsa==0.18.0
elasticsearch==7.17.9
elasticsearch-dsl==7.4.1
Faker==19.2.0
firebase-admin==6.2.0
frozenlist==1.4.0
future==0.18.3
google-api-core==2.11.1
google-api-python-client==2.95.0
google-auth==2.22.0
google-auth-httplib2==0.1.0
google-cloud-core==2.3.3
google-cloud-firestore==2.11.1
google-cloud-storage==2.10.0
google-crc32c==1.5.0
google-resumable-media==2.5.0
googleapis-common-protos==1.60.0
grpcio==1.56.2
grpcio-status==1.56.2
httplib2==0.22.0
hyperlink==21.0.0
idna==3.4
incremental==22.10.0
inflection==0.5.1
itypes==1.2.0
Jinja2==3.1.2
jsonschema==4.17.3
kombu==5.3.0
Mako==1.2.4
MarkupSafe==2.1.3
marshmallow==3.20.1
marshmallow-jsonapi==0.24.0
msgpack==1.0.5
multidict==6.0.4
numpy==1.25.2
oic==1.6.1
openai==0.27.8
packaging==23.1
paho-mqtt==1.6.1
pandas==1.5.0
pika==1.3.2
pika-stubs==0.1.3
Pillow==8.4.0
prometheus-client==0.17.0
prompt-toolkit==3.0.38
proto-plus==1.22.3
protobuf==4.23.4
psycopg2-binary==2.9.6
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycparser==2.21
pycryptodomex==3.18.0
pydantic==2.1.1
pydantic-settings==2.0.2
pydantic_core==2.4.0
pyjwkest==1.4.2
PyJWT==2.8.0
pyOpenSSL==23.2.0
pyparsing==3.1.1
pyrsistent==0.19.3
python-dateutil==2.8.2
python-dotenv==1.0.0
python-jose==3.3.0
python-json-logger==2.0.7
python-logstash==0.4.8
pytz==2023.3
PyYAML==6.0.1
redis==4.6.0
requests==2.31.0
rsa==4.9
ruamel.yaml==0.17.31
ruamel.yaml.clib==0.2.7
service-identity==23.1.0
six==1.16.0
sqlparse==0.4.4
toposort==1.10
tqdm==4.65.0
Twisted==22.10.0
twisted-iocpsupport==1.0.3
txaio==23.1.1
typing_extensions==4.7.1
tzdata==2023.3
uritemplate==4.1.1
urllib3==1.26.16
vine==5.0.0
wcwidth==0.2.6
yarl==1.9.2
zope.interface==6.0
  • What you expected to happen vs. what actually happened

I want to testing the websocket url about testing url in django, you should use reverse() let django url to match the name to route, but django didn,t support not View class url

Here is my routing.py

import logging

from channels.testing import ChannelsLiveServerTestCase, WebsocketCommunicator

from api.consumers.notifyConsumer import NotifyConsumer

logger = logging.getLogger(__name__)

websocket_urlpatterns = [
    path('ws/notify/<str:username>/', NotifyConsumer.as_asgi(), name='notify-consumer'),
    path('ws/chat/<str:username>/', ChatConsumer.as_asgi(), name='chat-consumer'),
]

My Testing test_routing.py

from django.test import TestCase, Client
from django.urls import reverse

class WebSocketRoutingTestCase(TestCase):
    def test_notify_consumer_routing(self):
        client = Client()
        url = reverse('notify-consumer', kwargs={'username': 'testuser'})
        response = client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'WebSocket Handshake')

    def test_chat_consumer_routing(self):
        client = Client()
        url = reverse('chat-consumer', kwargs={'username': 'testuser'})
        response = client.get(url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'WebSocket Handshake')
  • How you're running Channels (runserver? daphne/runworker? Nginx/Apache in front?)

Daphne

  • Console logs and full tracebacks of any errors
Traceback (most recent call last):
  File "C:\Users\cxz12\PycharmProjects\PetMonitoringSystemBackend\api\tests\unit\routing\test_routing.py", line 41, in test_notify_consumer_routing
    url = reverse('notify-consumer', kwargs={'username': 'testuser'})
  File "C:\Users\cxz12\.virtualenvs\PetMonitoringSystemBackend-NwbnpaKV\lib\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\cxz12\.virtualenvs\PetMonitoringSystemBackend-NwbnpaKV\lib\site-packages\django\urls\resolvers.py", line 828, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'notify-consumer' not found. 'notify-consumer' is not a valid view function or pattern name.

Yes, it's not supported. See #1912.