A contex manaager to reload urls routing in unittests changing a django setting.
Project description
Karpyncho Reload-Urls
Goal
This package main goal is to extend unittest to have a context manager that can change a setting and then reload the django urls routing.
Without this package if a url.py file that defines the urlpatterns to be using by the django routing module have a conditional url like:
if setting.DEBUG:
urlpatterns += [url("home", my_home_view, name="home")]
this routing will be loaded only once in the pytest suit, and depending on the django_settings_module file configuration, the routing will be loaded with (or without) the optional url. If in a test method, self.settings(DEBUG=True) is used the routing will not be reloaded
With this package you can inherit from karpyncho.reload_urls.ReloadUrlsContextManager and then use the method self.reload_url(url_module, settings) within a context. When the context is freed, first the setting is removed, and then the urls are reloaded again
For example:
from django.urls import reverse, NoReverseMatch
from karpyncho.reload_urls import TestCaseReloadableURL
class TestExample(TestCaseReloadableURL):
def test_example(self):
with self.reload_urls(None, ADD_HELLO_URL=True):
self.client.get(reverse("home"))
with self.assertRaises(NoReverseMatch):
reverse("home")
reload_url method
this method is added to the class django.tests.TestCase, the paramters are:
- urls_module: a string with the specific app url module to be reloaded (ie: "my_app.urls"). If None, the url module definen in settings.ROOT_URLCONF is reloaded
- **settings: the keywords parameters, each key is the setting to be overriden, just as the with self.settings(**settings) works
Install
pip install karpyncho_reload_urls
https://pypi.org/project/karpyncho-reload-urls/
Future improvements
- Fix potential bugs
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for karpyncho-reload-urls-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1378631d3da4417e2a5f139034e96f34ad0f18a7283ae9cfc2bd106d335ab583 |
|
MD5 | 42d01352e2c5b38725221dc84bc058c2 |
|
BLAKE2b-256 | 9242f37935ef17d66019d6fac50778d66cd698374cc00aaece434e002c059617 |
Hashes for karpyncho_reload_urls-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4babc1d3c102f26ad0c011a53f7f915ec341aa9a01d9b3d9e953429ce571b8fd |
|
MD5 | 7996983a1d1f33ba0e3454cd87860abc |
|
BLAKE2b-256 | db0476883a918acd7cc1613f5438641a7d14c394ab14d6e03f85fc90adf94e96 |