Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Add way to override configuration during tests #12

Closed
girardinsamuel opened this issue Feb 3, 2021 · 3 comments
Closed

Add way to override configuration during tests #12

girardinsamuel opened this issue Feb 3, 2021 · 3 comments
Labels
hard p4 Priority 4 (low) question Further information is requested

Comments

@girardinsamuel
Copy link
Contributor

girardinsamuel commented Feb 3, 2021

I would like to be able to update configuration during unit tests.

(It could be handy to be able to do it outside of tests too ? (or maybe not))

What do we currently have to do now?
Now we can get config with config() helper but not set it.

Solutions I have in mind:
Solution 1/

from masonite.helpers import set_config
set_config("application.name", "Brand new name !")

Solution 2/
We can change environment loaded during tests, but changing env vars during a unit test does not change the config and the config() function still returns the same value.

Solution 3/
Create a Configuration provider, load it early in the app lifecycle and load in it configuration settings defined in config/,
the config helper would fetch settings from the configuration provider instead. settings would be maybe cached inside it to avoid accessing params from config files again.
Then it would be possible to set a config value because values would be hold inside this provider.

make("Configuration").update("app.name", "Masonite 4")
make("Configuration").get("app.name") #== config("app.name")

Solution 4/
As this would be mostly used during unit tests, maybe we can call "something" in setUp() to load a different configuration ?

Temporary workaround

from unittest.mock import patch

@patch("config.application.APP_NAME", "Masonite 4.0")   # patch one config value
@patch.dict("config.broadcast.DRIVERS", {"pusher": {"app_id": None, "client": None} })   # patch a whole config dict
def my_unit_test(self):
     pass

Totally open about this !

The idea is to be able to as in Laravel
https://laravel.com/docs/master/configuration#accessing-configuration-values
and Django
https://docs.djangoproject.com/en/3.1/topics/testing/tools/#overriding-settings

@girardinsamuel girardinsamuel added the question Further information is requested label Feb 3, 2021
@josephmancuso
Copy link
Member

josephmancuso commented Feb 6, 2021

Idk if I asked this question already but now we to do it like:

  • put your config in the config directory and set it equal to an ENV variable:
from masonite import env

NAME = env("name")

then set the NAME in the .env:

NAME=Masonite 4

But in testing your create a .env.testing file and the environment is loaded over it.

NAME=Masonite Testing

Does that not really accomplish this same thing? or is this for overriding values on a test case by test case basis?

@girardinsamuel
Copy link
Contributor Author

Yeah I was thinking about the last one actually (the most difficult lol).
But that's not the priority, this rewrite is just an occasion to think about it

@josephmancuso josephmancuso added hard p4 Priority 4 (low) labels Feb 11, 2021
@josephmancuso
Copy link
Member

I'm gonna close this for now. I think the simpler approach is to structure the app to pull from environment variables and then just set them in some kind of testing environment or the .env.testing file

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hard p4 Priority 4 (low) question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants