I'm not going to address the mocking, as there's subtle details there (e.g.
you mocked group_name as a non-property, I think), but look at the main
issue.

Can you make your code print out what _is_ in `self.scope`? url_route
should definitely be in there, but I am curious what it looks like if it is
not.

Andrew

On Wed, Feb 7, 2018 at 12:16 PM, Azamat Galimzhanov <aza...@galimzhanov.com>
wrote:

> Hello,
>
> I'm setting up an auctions application using channels and websockets. When
> I'm running the server the code works just fine, troubles arise when I'm
> trying to test my app.
>
> asgiref==2.1.3
> daphne==2.0.2
> Django==2.0.2
> channels==2.0.1
> channels-redis==2.0.2
> pytest==3.4.0
> pytest-asyncio==0.8.0
>
> a bunch of other stuff, but I think that's it for relevant libraries.
>
> Here is my consumer:
>
> class AuctionConsumer(WebsocketConsumer):
>     @property
>     @lru_cache(maxsize=None)
>     def group_name(self):
>         auction_id = self.scope['url_route']['kwargs']['auction_id']
>         return 'auction_{}'.format(auction_id)
>
>     def connect(self):
>         AsyncToSync(self.channel_layer.group_add)(self.group_name, 
> self.channel_name)
>         self.accept()
>
>
> Here is my routing:
>
> application = ProtocolTypeRouter({
>     'websocket': URLRouter([
>         path("auctions/<auction_id>/", AuctionConsumer),
>     ]),
> })
>
>
> And here is my test:
>
>
> @pytest.mark.asyncio
> async def test_auction_consumer(mocker):
>     communicator = WebsocketCommunicator(AuctionConsumer, 'auctions/1/')
>     connected, subprotocol = await communicator.connect()
>     assert connected
>     # Test sending text
>     await communicator.send_to(text_data='1')
>     response = await communicator.receive_from()
>     assert response == '1'
>     # Close
>     await communicator.disconnect()
>
>
>
> I'm running into the following problem;
>
>     @property
>     @lru_cache(maxsize=None)
>     def group_name(self):
> >       auction_id = self.scope['url_route']['kwargs']['auction_id']
> E       KeyError: 'url_route'
>
> I tried mocking at the top of my test:
>
>     group_name = mocker.patch('auctions.consumers.AuctionConsumer.group_name')
>     group_name.return_value = 'auction'
>
>
> But then I run into:
>
>     def valid_group_name(self, name):
>         if self.match_type_and_length(name):
>             if bool(self.group_name_regex.match(name)):
>                 return True
>         raise TypeError(
>             "Group name must be a valid unicode string containing only
> ASCII " +
> >           "alphanumerics, hyphens, or periods."
>         )
> E       TypeError: Group name must be a valid unicode string containing
> only ASCII alphanumerics, hyphens, or periods.
>
>
> I also tried:
>
> scope = mocker.patch('auctions.consumers.AuctionConsumer.scope')
> scope.return_value = {'url_route': {}}
>
>
> But that got me:
>
>         if not self.create and original is DEFAULT:
>             raise AttributeError(
> >               "%s does not have the attribute %r" % (target, name)
>             )
> E           AttributeError: <class 'auctions.consumers.AuctionConsumer'>
> does not have the attribute 'scope'
>
>
> At this point I'm out of ideas. Anything I should try?
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/528e93c5-dfb1-4cc1-a599-b99404c59533%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/528e93c5-dfb1-4cc1-a599-b99404c59533%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uoZ875%3DO%3DPdurHw%3DtrHR960Pm1KnEjue0EKCzx%2BAnWdMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to