Giter Site home page Giter Site logo

Comments (21)

carltongibson avatar carltongibson commented on June 19, 2024 1

Ok, thanks. I will try and have a look, and perhaps expand the tutorial example to show interactions with the DB.

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

Are you sure your environment variables are being set correctly? (I'd expect the test DB name to be test_testasgi from the sample .env file + settings.py you have in your repo.)

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

Pardon me, coming from pytest I was unaware unittest already required a database and then simply cloned the already-existing one, prefixing it with test_. While the database error is gone, data created within a test function still cannot be used from the browser.

E.g. using the following code does not allow the user to log into the admin using the browser:

user_staff = User.objects.create(
    username='test',
    email='[email protected]',
    password=make_password('hunter42'),
    is_staff=True,
    is_superuser=True
)
self.client.login(
    username=user_staff.username, 
    password='hunter42'
)
# returns True

self.driver.get(self.live_server_url + "/admin/")
self.driver.find_element('name', 'username').send_keys(user_staff.username)
self.driver.find_element('name', 'password').send_keys('hunter42')
self.driver.find_element(By.CSS_SELECTOR, "input[type='submit']").click()
# error in admin - login does not work

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

Ok, but in general that works, so you need to put a breakpoint there and see why the login isn't working. (This isn't really a channels issue.)

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

It's not a channels issue per-se, but what I assume is an asgiref issue / ChannelsLiveServerTestCase issue, since the intended use to test the frontend does not work as outlined in the docs.

It's like the database is out of sync between the sync test routing and the async server worker thread. I threw in a pdb in the test function, but everything there is working fine there. Not sure how I would debug the server function serving the frontend.

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

It works if you follow the steps in the tutorial. I'm not sure what else I can say given the info provided. Sorry.

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

I just followed the steps in the tutorial and setting up a test database that is available via the frontend does not work as expected.

Users that are created as part of the regular Python functions are not available to the frontend (see chat.tests.DatabaseTests). The only reason the tutorial "works" is because it does not use any part of the ORM but unauthenticated views testing the mere websockets functionality.

One could argue that this is a sufficient for unit tests and the rest if out of scope for this library. However I would argue if you provide a dedicated test server for the use case, it should work with the ORM, because it's unlikely that it's used only with unauthenticated interactions.

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

OK, your test project has way too much going on to identify any issue. Can you trim it down to a minimal please, with a clear error case, rather than a sleep. (The initial ticket here was "Database not created", so if you can clarify that too, that would be great.

I'd move the user creation into setup, and then inspect the DB to make sure that's actually committed. Then compare Django's Admin Selenium test cases, which do this at length.

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

OK, your test project has way too much going on to identify any issue

Not sure what exactly you are referring to. I simply implemented the tutorial boilerplate in chat and config-related settings as requested.

I slimmed down chat.tests โ€“ including a SyncDatabaseTests and AsyncDatabaseTests, both testing for the same result, only using the different servers. The former one works.

then inspect the DB to make sure that's actually committed. Then compare Django's Admin Selenium test cases, which do this at length

From what I can see in e.g. the Django admin_changelist test, the user is simply created, then used to log in thereafter. Nevertheless I added a simple check to test_user_model_creation that asserts the user is present before opening the browser.

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

Heya! Have you had a chance to look into it?

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

Hi @alfonsrv โ€” no, not as yet. It's on my list for later in the autumn, but short of someone (you maybe?) digging deeper (and highlighting either the error in the project or the bug in the test case) it'll have to wait until I can get to it.

from channels.

ashtonrobinson avatar ashtonrobinson commented on June 19, 2024

#2033

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

@ashtonrobinson I tried using your PR but it leads to a ProgrammingError at /chat/test/ yellow error page, as if migrations didn't run for the frontend / test database.

But it seems like you're onto something โ€“ because using the async test case (chat.tests.AsyncDatabaseTests) I created, it appears to use the default database instead of the test database.

And users that are created as part of python manage.py createsuperuser are listed in the AsyncDatabaseTests frontend, while they are not when using the regular unit test SyncDatabaseTests.

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

From the PR...

a multiprocessing.Process object which takes care of creating a new process. This uses the SpawnProcess of multiprocessing class on Windows and macOS. Linux uses the Fork method...

Ah, spawn vs fork

This will be related to the discover runner updates in Django 4.1. https://docs.djangoproject.com/en/5.0/releases/4.1/#tests

See https://code.djangoproject.com/ticket/31169 โ€” We'll likely need to use some of the added test setup steps.

Progress. ๐ŸŽ

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

Just to add in the meantime, if the spawn startup is the issue running the tests with --parallel=1 should function as a workaround.

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

That doesn't work either.

How can something as fundamental as tests (albeit in the context of database interactions) be kept broken for what is essentially a core Django library for this long?

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

@alfonsrv by it not being exercised. Django is built on volunteer effort, so short of someone picking it up and investigating it can take a while.

Since you're using this, perhaps you could be the person to work out the cause and resolve it.

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

I tried looking into it, but I'm afraid it's a bit too advanced for my understanding.

Willing to sponsor and offer a bounty though.

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

@alfonsrv the first step would be a failing test case demonstrating the issue. If folks can instantly get to the problem it's much easier to work on.

from channels.

alfonsrv avatar alfonsrv commented on June 19, 2024

That exists: https://github.com/alfonsrv/django-asgitest โ€“ the test cases inherit the same base class, to run the same code both Sync and Async.

Mentioned the repo here previously. It has some boilerplate code due to following the channels tutorial โ€“ as I was asked to previously โ€“ but the test cases are very basic.

from channels.

carltongibson avatar carltongibson commented on June 19, 2024

I really meant as a unit test against the channels test suite, but yes.

The bottom line is that until somebody can find the time to dig into it, this will sit there. It's on my list, but I have quite a few ๐Ÿคน other things which are more pressing to me, so it's not something I'm going to get to quickly.

from channels.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.