I have a Django app that exposes the following views: A,B,C...Z
. This app is deployed on MACHINE-1.
There was a new business requirement to deploy on MACHINE-2 the same Django app but with a subset of those views, let's say A,B, C
.
My simple solution was to set a config variable (RUN_ON_MACHINE_2=True
for the second case and False
for the first case). In conclusion, the urls.py
file looks something like below (Please do note that I assimilate the views with paths here just to make it shorter):
urlpatterns = [A, B, C, ... Z]
# If the env var is set to True then only load a subset of the views
if config.RUN_ON_MACHINE_2:
urlpatterns = [A, B, C]
The tests work fine cause they are always executed for A,B,C ... Z
which of course include A, B, C
.
However, some new business requirements are asking to make changes on view A. I could implement the changes in view A branching the code by the config.RUN_ON_MACHINE_2
variable but I reached the conclusion it's much cleaner to create a new view, A_ForMachine2
, which implements only the needed stuff for MACHINE_2. A_ForMachine2
must have the same interface as view A
and must be mounted to the same path. So the new urls.py
would look like:
urlpatterns = [A, B, C, ... Z]
# If the env var is set to True then only load a subset of the views
if config.RUN_ON_MACHINE_2:
# A_ForMachine2 is mounted on the same path as A
urlpatterns = [A_ForMachine2, B, C]
Now I have 2 views that are mounted to the same path so they have the same URL.
Now my api tests are testing only the view A
because by default the config.RUN_ON_MACHINE_2
is False.
I tried to patch config.RUN_ON_MACHINE_2
in my test case but from what I understand, when the tests are run the application is already loaded with all the urls A,B,..Z
so it wont load/mount [A_ForMachine2, B, C]
.
What's a clean way to have a Django app (in my case a DRF app) that loads different urls at startup (depending on some env var) having all its views easily testable?
Cheers!