autoused, session-scoped fake fs in django project #1266
-
|
Describe the bug Tests crash in a counterintuitive way when attempting to autouse a fixture which uses fs_session
see full stack trace in the attachment. How To Reproduce The following minimal test case will probably produce the above result in any django project. Your environment Linux-6.14.0-37-generic-x86_64-with-glibc2.39 |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
|
Thanks - the error message is indeed counter-intuitive, most likely the error is in reality related to a mix-up of real and fake filesystems.
I will have a closer look tonight to understand the actual problem. |
Beta Was this translation helpful? Give feedback.
-
|
The problem is that django looks up the commands in the file system, but doesn't find them because it uses the fake fs. @pytest.fixture(autouse=True)
def auto_use_fake_fs(db, fs_session): # note the added `db` fixture before `fs_session` to ensure the correct order
yield
def test_fake_fs(db, fs): # `fs` here is a reference to `fs_session`
User.objects.create()If none of these solutions work for you, we have to look further into this (specifically, how the You still may need to map part of your application and/or django into the fake filesystem, but I'm not sure what part. For reference: def auto_use_fake_fs(fs_session):
fs_session.add_real_directory(Path(django.__file__).parent)
yieldI also remember having to map tzdata into the fake fs, though you may not need that. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for looking into this. To give some background: in our use case, the fake fs has become quite large, affecting performance a lot. I thought, session-scoping it could help.
I considered overwriting the Unless I am overlooking something here, I will consider this a limitation. And my approach unsuitable. Is a clearer error message possible here? I presume this is a bit of an edge case. Thank you |
Beta Was this translation helpful? Give feedback.
-
The problem is that the error comes from Generally, So, get tl;dr - I will think about it - maybe I will come up with a better idea how to handle this, but no promises here. |
Beta Was this translation helpful? Give feedback.
-
|
A couple more remarks:
Actually, I wrote that wrong, it should have been: def auto_use_fake_fs(django_db_setup, fs_session):
yielde.g. make sure the session-scoped db fixture is used before the session-scoped fs fixture.
In the example, the memory-based SQLite-db is used, which It is difficult to see what your actual problems are without seing the project (or an example project that has the same problems). Your current example is easy to reproduce and easy to fix, but I guess the real problems lie elsewhere. |
Beta Was this translation helpful? Give feedback.
-
|
I will convert this into a discussion issue for reference. With the current information, I don't see a way to improve the current behavior besides the mentioned ideas, but I would be happy do add more functionality or documentation for this kind of issues, given some more information. |
Beta Was this translation helpful? Give feedback.
-
|
thank you
indeed with postgres this fix does not work. but i now understand why this limitation exists. thank you for clarification |
Beta Was this translation helpful? Give feedback.
A couple more remarks:
Actually, I wrote that wrong, it should have been:
e.g. make sure the session-scoped db fixture is used before the session-scoped fs fixture.
In the example, the memory-based SQLite-db is used, which
djangouses per default for the tests. If you use your real database (something like postgreSQL) in your test instead, this would …