neurobagel / api

https://api.neurobagel.org/
MIT License
4 stars 4 forks source link

Env var in tests #316

Open rmanaem opened 3 months ago

rmanaem commented 3 months ago

For testing the time at which the env vars are read is at import time as opposed to at app start time/test runtime.

In the n-API (and soon f-API: https://github.com/neurobagel/federation-api/pull/90) we load ENV variables in a utility module like so

https://github.com/neurobagel/api/blob/84c0e99d7c797bb8b5c79ba2aeeeed404045695f/app/api/utility.py#L16-L18

Because the loading happens at the top level of the module, the ENV variable gets read in when the module is imported. In other words: we have no control over when the ENV variable is read in (e.g. during or after app startup).

This mainly matters during testing where we have to make sure we set ENV variables before the test run starts at all using something like https://pypi.org/project/pytest-env/ because by the time we have launched the TestApp client or imported the utility module any further changes to ENV variables will have no effect (because the reading has already happened).

This has two immediate consequences:

So we should look into better options

surchs commented 3 months ago

Transferred this to n-API because we use env vars here more so far.

github-actions[bot] commented 1 month ago

We want to keep our issues up to date and active. This issue hasn't seen any activity in the last 75 days. We have applied the _flag:stale label to indicate that this issue should be reviewed again. When you review, please reread the spec and then apply one of these three options: