def test_import_regex(mock_urlconf_module): import_urlconf.from_json([{ "regex": "^login/$", "name": "login" }], urlconf="mock_urlconf_module") assert reverse("login", urlconf="mock_urlconf_module") == "/login/"
def test_import_include_with_namespace(mock_urlconf_module): import_urlconf.from_json( [{ "regex": "^colors/", "namespace": "colors_ns", "app_name": "colors_app", "includes": [ { "regex": "^red/$", "name": "red" }, { "regex": "^blue/$", "name": "blue" }, ], }], urlconf="mock_urlconf_module", ) assert reverse("colors_ns:red", urlconf="mock_urlconf_module") == "/colors/red/" assert reverse("colors_ns:blue", urlconf="mock_urlconf_module") == "/colors/blue/"
def test_urlpatterns_use_kwargs_will_fail(mock_urlconf_module): import_urlconf.from_json( [ { # ERROR: using unnamed args in the pattern "regex": "^(.+)-(.+)/$", "name": "designer-products", } ], urlconf="mock_urlconf_module", ) with pytest.raises(AssertionError): urlconf_qa.assert_all_urls_use_kwargs_not_args("mock_urlconf_module")
def test_urlpatterns_use_kwargs_will_pass(mock_urlconf_module): import_urlconf.from_json( [ { # e.g. /gucci-bags/ "regex": "^(?P<designer_name>.+)-(?P<product_type>.+)/$", "name": "designer-products", } ], urlconf="mock_urlconf_module", ) # The url is using kwargs, not args, so this won't error urlconf_qa.assert_all_urls_use_kwargs_not_args("mock_urlconf_module")
def test_import_multi_language_without_country(mock_urlconf_module): import_urlconf.from_json([{ "regex": { "en": "^color/$" }, "name": "color" }], urlconf="mock_urlconf_module") with translation.override("en"): assert reverse("color", urlconf="mock_urlconf_module") == "/color/" with translation.override("en-gb"): # There's no 'en-gb' value so if will use the 'en' value assert reverse("color", urlconf="mock_urlconf_module") == "/color/"
def test_import_will_overwrite_existing_urlconf(cleanup_created_modules): import_urlconf.from_json([{ "route": "login/", "name": "login" }], urlconf=METHOD_ARGUMENT) assert reverse("login", urlconf=METHOD_ARGUMENT) == "/login/" import_urlconf.from_json([{ "route": "new-login/", "name": "login" }], urlconf=METHOD_ARGUMENT) assert reverse("login", urlconf=METHOD_ARGUMENT) == "/new-login/"
def test_import_locale_prefix_pattern(mock_urlconf_module): import_urlconf.from_json( [{ "isLocalePrefix": True, "classPath": "django.urls.resolvers.LocalePrefixPattern", "includes": [{ "regex": "^$", "name": "index" }], }], urlconf="mock_urlconf_module", ) with translation.override("en"): assert reverse("index", urlconf="mock_urlconf_module") == "/en/" with translation.override("fr"): assert reverse("index", urlconf="mock_urlconf_module") == "/fr/"
def test_check_urlpattern_translations_will_pass(mock_urlconf_module): import_urlconf.from_json( [ { "regex": { # e.g. /gucci-bags/ "en": "^(?P<designer_name>.+)-(?P<product_type>.+)/$", # e.g. /sacs-gucci/ "fr": "^(?P<product_type>.+)-(?P<designer_name>.+)/$", }, "name": "designer-products", } ], urlconf="mock_urlconf_module", ) # kwargs are same for both languages, so this will not error urlconf_qa.assert_url_kwargs_are_the_same_for_all_languages("mock_urlconf_module")
def test_check_urlpattern_translations_will_fail(mock_urlconf_module): import_urlconf.from_json( [ { "regex": { # e.g. /gucci-bags/ "en": "^(?P<designer_name>.+)-(?P<product_type>.+)/$", # ERROR: the kwarg names have been translated by mistake # so they're no longer the same as the English pattern kwargs. "fr": "^(?P<type_de_produit>.+)-(?P<nom_du_créateur>.+)/$", }, "name": "designer-products", } ], urlconf="mock_urlconf_module", ) with pytest.raises(AssertionError): urlconf_qa.assert_url_kwargs_are_the_same_for_all_languages("mock_urlconf_module")
def test_import_multi_language(mock_urlconf_module): import_urlconf.from_json( [{ "regex": { "en": "^color/$", "en-gb": "^colour/$", "fr": "^couleur/$" }, "name": "color" }], urlconf="mock_urlconf_module", ) with translation.override("en"): assert reverse("color", urlconf="mock_urlconf_module") == "/color/" with translation.override("en-gb"): assert reverse("color", urlconf="mock_urlconf_module") == "/colour/" with translation.override("fr"): assert reverse("color", urlconf="mock_urlconf_module") == "/couleur/"
def test_import_will_create_urlconf_module(cleanup_created_modules, method_argument, library_setting, root_setting, expected_created_module): with override_settings(URLCONF_IMPORT_ROOT_URLCONF=library_setting, ROOT_URLCONF=root_setting): import_urlconf.from_json([{ "route": "login/", "name": "login" }], urlconf=method_argument) # Check the right module was created and we can use it to make URLs assert sys.modules.get(expected_created_module) assert reverse("login", urlconf=expected_created_module) == "/login/" # Check no other modules were created other_possible_created_modules = [ m for m in POSSIBLE_CREATED_MODULES if m != expected_created_module ] for module_name in other_possible_created_modules: assert not sys.modules.get(module_name)
def test_import_will_raise_if_no_urlconf_module_specified( cleanup_created_modules): with pytest.raises(ValueError): import_urlconf.from_json([{"route": "login/", "name": "login"}])