Hi Andrew,

thanks for your reply.

This test is passing (using the same packages):

def test_fixture(db_with_segment, admin_client):
    assert models.MyModel.objects.count() == 1
    response = admin_client.get('/api/my_model/list/')
    assert len(response.json()['results']) == 1

I tried to write a simpler async test with asyncio.wait but I didn't 
complete it so far.

I thought to just write ChannelsLiveServerTestCase tests but I couldn't 
manage to serve static files there. (Both, setting static_handler = 
StaticFilesHandler – what StaticLiveServerTestCase does – and running 
collectstatic didn't work.)

Finally I ran my development server and did some tests in the browser 
console. There it works. I'll continue coding using this technique to 
"test" my code and try to find a solution for the other (real) tests as I 
have time.

Am Mittwoch, 7. Februar 2018 04:31:14 UTC+1 schrieb Andrew Godwin:
>
> I'm not quite sure either. Does the test setup work if you are not in 
> async mode?
>
> Andrew
>
> On Tue, Feb 6, 2018 at 9:45 AM, Daniel Gilge <d.g...@gmail.com 
> <javascript:>> wrote:
>
>> Hi,
>>
>> this question is related to Channels 2.0, Django 2.0, pytest 3.4, 
>> pytest-django 3.1 and pytest-asyncio 0.8.
>>
>> I created a pytest:
>>
>> @pytest.fixture
>> def db_with_obj(db):
>>     factories.MyModelFactory()
>>
>> @pytest.mark.asyncio
>> async def test_something(db_with_obj):
>>     print(models.MyModelFactory.objects.all())
>>     obj = models.MyModelFactory.objects.first()
>>     communicator = WebsocketCommunicator(MyConsumer, 'ws/')
>>     connected, subprotocol = await communicator.connect()
>>     assert connected
>>     await communicator.send_to(text_data=json.dumps({
>>         'id': obj.pk,
>>         'content': 'my content',
>>     }))
>>     response = await communicator.receive_from()
>>     assert response == 'my content'
>>     await communicator.disconnect()
>>
>> print gives me <QuerySet [<MyModel: 1>]>
>>
>> Here my Consumer code:
>>
>> class MyConsumer(WebsocketConsumer):
>>     def receive(self, text_data=None, bytes_data=None):
>>         print(models.MyModel.objects.all())
>>         # ...
>>
>> print gives me <QuerySet []>
>>
>> I don't know what I'm doing wrong. I didn't use both, pytest and Python 
>> async code, before. Therefore I don't know where to look for the solution, 
>> though I think it's easy to solve. Any hint appreciated.
>>
>> Thanks :)
>>
>> -- 
>> 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...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <javascript:>.
>> 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/7e5f63f4-789c-413e-a8d2-fcf8ea62a2cb%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/7e5f63f4-789c-413e-a8d2-fcf8ea62a2cb%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/464c465b-8b24-4061-977d-a44b4868fc84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to