A context 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
File details
Details for the file karpyncho-reload-urls-0.1.1.tar.gz
.
File metadata
- Download URL: karpyncho-reload-urls-0.1.1.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84c011d8fe1a512bb230a3fa490879cd2b4e0fb85b80e8feb628bdeb632a6d00 |
|
MD5 | 0c3607c1a6457ac9e8b774fa6bf72bde |
|
BLAKE2b-256 | 5e0f2ba078e0f29dbb35c2372af7fdf5064ebf5cd97ef41e8b1fad91c3994a37 |
File details
Details for the file karpyncho_reload_urls-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: karpyncho_reload_urls-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3731589f60703185ee8b76b0b0a0c3b3a11c62d74de637379dc8fb5c126b06b3 |
|
MD5 | 52c965c8d09df5efdf8ff07cb3f8590d |
|
BLAKE2b-256 | 3804c9c5327b784c1f2bf1d2c9dc2547152c894c326f6b56692639ccb5286554 |