-
-
Notifications
You must be signed in to change notification settings - Fork 800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running ChannelsLiveServerTestCase does not create database for browser #2048
Comments
Are you sure your environment variables are being set correctly? (I'd expect the test DB name to be |
Pardon me, coming from 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 |
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.) |
It's not a It's like the database is out of sync between the sync test routing and the async server worker thread. I threw in a |
It works if you follow the steps in the tutorial. I'm not sure what else I can say given the info provided. Sorry. |
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 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. |
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 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. |
Not sure what exactly you are referring to. I simply implemented the tutorial boilerplate in I slimmed down
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 |
Ok, thanks. I will try and have a look, and perhaps expand the tutorial example to show interactions with the DB. |
Heya! Have you had a chance to look into it? |
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. |
@ashtonrobinson I tried using your PR but it leads to a But it seems like you're onto something – because using the async test case ( And users that are created as part of |
From the PR...
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. 🎁 |
Just to add in the meantime, if the spawn startup is the issue running the tests with |
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? |
@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. |
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. |
@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. |
That exists: https://github.com/alfonsrv/django-asgitest – the test cases inherit the same base class, to run the same code both 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. |
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. |
Any update? |
@alfonsrv: can you provide the output of what's failing in your example repo? (i.e. the stack trace) |
There's no relevant stack trace – the test fails when attempting to look up a user in the frontend via browser that was previously created by backend code. However I finally managed to fix this issue using pytest by jumping through some hoops. |
Hello, Iv also run into this issue, During testing, i can see that my DB is being created and migrations are being added. To reproduce: Check your database name within the test
and then also in your view called by selenium
You will see that the output inside your test will show and the output in your daphne view will be Whereas if you use StaticLiveServerTestCase instead of ChannelsLiveServerTestCase Both outputs will show the test_defaultdatabase I would rename the issue to - "ChannelsLiveServerTestCase Daphne server does not read from the Mirrored Test database's created by django" based on this information |
Quick solution, but can probably be better implemented - In ChannelsLiveServerTestCase Override the run function in DaphneProcess to modify your database config class CustomDaphneProcess(DaphneProcess):
This leads me to a new issue, that the tests override_settings are not applied to the daphne server, And i have not figured out how to apply patches to the selenium views yet Update: To add patches you can also do that in the Run before you create the server class - and you can change system settings the same way you change the database names |
Thanks @Checkm3out — that gives us a lead. There have been various similar issues over the years on Django that may well be ≈the same. 🕵️ |
Using
ChannelsLiveServerTestCase
as described in documentation's Tutorial Part 4: Automated Testing yields the yellow error pageconnection to server at "127.0.0.1", port 5432 failed: FATAL: database "test" does not exist
. Running database-altering code within a test function works and the database is available, but the frontend worker serving the browser request (using Daphne) seems to be totally detached from that database. Somehow the asgiref workers are working in another thread?I created a small test repository with a small readme to reproduce the issue: https://github.com/alfonsrv/django-asgitest
Tried using macOS Ventura + Ubuntu 22.04 with Chrome.
The text was updated successfully, but these errors were encountered: