From c1693482e39d7b6c44d15af416a492295b6b48e1 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:29:36 +0000 Subject: [PATCH 01/10] Add missing build artifacts to gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 42223f3..5004021 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,11 @@ *.py[cod] .coverage +.DS_Store + +*.egg-info/ # pycharm .idea/ # pyenv -.python-version \ No newline at end of file +.python-version From 5e8f426d5d8d17c93bcd47d9cc562720fbaeb226 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:31:19 +0000 Subject: [PATCH 02/10] Remove python_requires and pin to django 1.x and 2.x --- setup.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index c2005a7..5dc1c15 100644 --- a/setup.py +++ b/setup.py @@ -7,10 +7,15 @@ scripts=[], description='Use fixed keys in your Django template to refer to dynamic URLs', long_description=open('README.md').read(), - python_requires='>=3.7.0', install_requires=[ - "Django>=2.2, <3.0", + "Django<3", ], + extras_require={ + "testing": [ + "pytest", + "pytest-django", + ] + }, packages=find_packages(), - include_package_data=True + include_package_data=True, ) From de7aadbeb46e1b3d662914209ee1ada3b9222300 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:32:50 +0000 Subject: [PATCH 03/10] Move tests out of the package --- {urlmapper/tests => tests}/test_helpers.py | 6 +++--- {urlmapper/tests => tests}/test_models.py | 3 +-- {urlmapper/tests => tests}/test_tags.py | 4 ++-- {urlmapper/tests => tests}/urls.py | 0 urlmapper/tests/__init__.py | 0 5 files changed, 6 insertions(+), 7 deletions(-) rename {urlmapper/tests => tests}/test_helpers.py (94%) rename {urlmapper/tests => tests}/test_models.py (98%) rename {urlmapper/tests => tests}/test_tags.py (96%) rename {urlmapper/tests => tests}/urls.py (100%) delete mode 100644 urlmapper/tests/__init__.py diff --git a/urlmapper/tests/test_helpers.py b/tests/test_helpers.py similarity index 94% rename from urlmapper/tests/test_helpers.py rename to tests/test_helpers.py index cdb7ba2..e77d056 100644 --- a/urlmapper/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -2,9 +2,9 @@ from django.test import TestCase -from ..helpers import get_mapped_url, check_mapped_url -from ..models import URLMap -from .. import settings +from urlmapper.helpers import get_mapped_url, check_mapped_url +from urlmapper.models import URLMap +from urlmapper import settings class TestGetMappedURL(TestCase): diff --git a/urlmapper/tests/test_models.py b/tests/test_models.py similarity index 98% rename from urlmapper/tests/test_models.py rename to tests/test_models.py index bd3e556..0353f68 100644 --- a/urlmapper/tests/test_models.py +++ b/tests/test_models.py @@ -1,4 +1,3 @@ -# py3 from builtins import str as unicode from django.contrib.auth.models import User @@ -7,7 +6,7 @@ from django.core.exceptions import ValidationError from django.test import TestCase -from ..models import URLMap +from urlmapper.models import URLMap class TestModels(TestCase): diff --git a/urlmapper/tests/test_tags.py b/tests/test_tags.py similarity index 96% rename from urlmapper/tests/test_tags.py rename to tests/test_tags.py index 429adf8..85a4881 100644 --- a/urlmapper/tests/test_tags.py +++ b/tests/test_tags.py @@ -3,8 +3,8 @@ from django.template import Template, Context from django.test import TestCase -from .. import settings -from ..models import URLMap +from urlmapper import settings +from urlmapper.models import URLMap class TestTags(TestCase): diff --git a/urlmapper/tests/urls.py b/tests/urls.py similarity index 100% rename from urlmapper/tests/urls.py rename to tests/urls.py diff --git a/urlmapper/tests/__init__.py b/urlmapper/tests/__init__.py deleted file mode 100644 index e69de29..0000000 From 81c08905f699d1519462ded05f40fe29a2d0b0ef Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:34:31 +0000 Subject: [PATCH 04/10] Move the settings set up into conftest --- runtests.py | 59 ----------------------------------------------- tests/conftest.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 runtests.py create mode 100644 tests/conftest.py diff --git a/runtests.py b/runtests.py deleted file mode 100644 index c14c4c8..0000000 --- a/runtests.py +++ /dev/null @@ -1,59 +0,0 @@ -import django - -from django.conf import settings -from django.core.management import call_command - -settings.configure( - - DATABASES={ - "default": { - "ENGINE": "django.db.backends.sqlite3", - } - }, - INSTALLED_APPS=( - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.staticfiles', - 'django.contrib.sites', - 'urlmapper' - ), - - STATIC_URL='/', - MIDDLEWARE_CLASSES=( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - ), - ROOT_URLCONF='urlmapper.tests.urls', - SITE_ID=1, - - TEMPLATES=[ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, - ], - - URLMAPPER_KEYS=[ - 'test_1', - 'test_2', - 'test_3', - 'test_4', - 'test_5' - ], - URLMAPPER_FUNCTIONS={ - 'test_1': lambda: 'test_1_success', - 'test_2': lambda request: 'test_2_success' - }, -) -django.setup() - -call_command('test') diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..631321d --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,59 @@ +import os + +from django.conf import settings + +# PYTHON3 use pathlib +TEST_FOLDER = os.path.dirname(os.path.abspath(__file__)) +# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "urlmapper.settings") + + +def pytest_configure(): + # import pdb;pdb.set_trace() + # from urlmapper import settings + settings.configure( + DATABASES={ + "default": { + "ENGINE": "django.db.backends.sqlite3", + } + }, + INSTALLED_APPS=[ + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.staticfiles', + 'django.contrib.sites', + 'urlmapper', + ], + STATIC_URL='/', + MIDDLEWARE_CLASSES=( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + ), + ROOT_URLCONF='tests.urls', + SITE_ID=1, + TEMPLATES=[ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, + ], + URLMAPPER_KEYS=[ + 'test_1', + 'test_2', + 'test_3', + 'test_4', + 'test_5' + ], + URLMAPPER_FUNCTIONS={ + 'test_1': lambda: 'test_1_success', + 'test_2': lambda request: 'test_2_success' + }, + ) From 8c4b777f82dfd2e56d4bd18fc66281e51ceb8d35 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:35:49 +0000 Subject: [PATCH 05/10] Reenable tests One does not work as get_abolute_url on user model was removed in django 1.7 --- tests/test_models.py | 213 +++++++++++++++++++++---------------------- 1 file changed, 104 insertions(+), 109 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index 0353f68..70ff51d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -5,6 +5,7 @@ from django.contrib.sites.models import Site from django.core.exceptions import ValidationError from django.test import TestCase +import pytest from urlmapper.models import URLMap @@ -33,113 +34,107 @@ def test_get_key_choices(self): ) ) + def test_invalid_map_does_not_show(self): + self.assertEquals( + list(URLMap.objects.all()), + [self.url_map] + ) + + def test_only_one_mapping_allowed(self): + # No mapping + map = URLMap(key='test_4') + with self.assertRaises(ValidationError): + map.full_clean() + + # More than one mapping + map = URLMap(key='test_4', url='abc', content_type=ContentType.objects.first()) + with self.assertRaises(ValidationError): + map.full_clean() + + map = URLMap(key='test_4', url='abc', object_id=1) + with self.assertRaises(ValidationError): + map.full_clean() + + map = URLMap(key='test_4', object_id=1, view_name='test') + with self.assertRaises(ValidationError): + map.full_clean() + + def test_only_valid_url_allowed(self): + map = URLMap(key='test_4', url='/invalid/') + with self.assertRaises(ValidationError): + map.full_clean() + + map = URLMap(key='test_4', url='/test/') + self.assertIsNone(map.full_clean()) + + @pytest.mark.skip(reason="No get_absolute_url on user") + def test_only_valid_object_allowed(self): + # No get_absolute_url on site + site = Site.objects.first() + map = URLMap( + key='test_4', + content_object=site + ) + with self.assertRaises(ValidationError): + map.full_clean() + + # get_absolute_url on user + user = User.objects.create_user('test') + map = URLMap( + key='test_4', + content_object=user + ) + self.assertIsNone(map.full_clean()) + + def test_only_valid_view_allowed(self): + map = URLMap( + key='test_4', + view_name='invalid' + ) + with self.assertRaises(ValidationError): + map.full_clean() + + map = URLMap( + key='test_4', + view_name='test' + ) + self.assertIsNone(map.full_clean()) + + map = URLMap( + key='test_4', + view_name='test', + view_keywords='invalid' + ) + with self.assertRaises(ValidationError): + map.full_clean() + + map = URLMap( + key='test_4', + view_name='test', + view_keywords='still=invalid' + ) + with self.assertRaises(ValidationError): + map.full_clean() + + map = URLMap( + key='test_4', + view_name='test', + view_keywords='pk=12345' + ) + self.assertIsNone(map.full_clean()) -def test_invalid_map_does_not_show(self): - self.assertEquals( - list(URLMap.objects.all()), - [self.url_map] - ) - - -def test_only_one_mapping_allowed(self): - # No mapping - map = URLMap(key='test_4') - with self.assertRaises(ValidationError): - map.full_clean() - - # More than one mapping - map = URLMap(key='test_4', url='abc', content_type=ContentType.objects.first()) - with self.assertRaises(ValidationError): - map.full_clean() - - map = URLMap(key='test_4', url='abc', object_id=1) - with self.assertRaises(ValidationError): - map.full_clean() - - map = URLMap(key='test_4', object_id=1, view_name='test') - with self.assertRaises(ValidationError): - map.full_clean() - - -def test_only_valid_url_allowed(self): - map = URLMap(key='test_4', url='/invalid/') - with self.assertRaises(ValidationError): - map.full_clean() - - map = URLMap(key='test_4', url='/test/') - self.assertIsNone(map.full_clean()) - - -def test_only_valid_object_allowed(self): - # No get_absolute_url on site - site = Site.objects.first() - map = URLMap( - key='test_4', - content_object=site - ) - with self.assertRaises(ValidationError): - map.full_clean() - - # get_absolute_url on user - user = User.objects.create_user('test') - map = URLMap( - key='test_4', - content_object=user - ) - self.assertIsNone(map.full_clean()) - - -def test_only_valid_view_allowed(self): - map = URLMap( - key='test_4', - view_name='invalid' - ) - with self.assertRaises(ValidationError): - map.full_clean() - - map = URLMap( - key='test_4', - view_name='test' - ) - self.assertIsNone(map.full_clean()) - - map = URLMap( - key='test_4', - view_name='test', - view_keywords='invalid' - ) - with self.assertRaises(ValidationError): - map.full_clean() - - map = URLMap( - key='test_4', - view_name='test', - view_keywords='still=invalid' - ) - with self.assertRaises(ValidationError): - map.full_clean() - - map = URLMap( - key='test_4', - view_name='test', - view_keywords='pk=12345' - ) - self.assertIsNone(map.full_clean()) - - map = URLMap( - key='test_4', - view_name='test', - view_keywords='slug=test-it-works' - ) - self.assertIsNone(map.full_clean()) - - -def test_get_url(self): - self.assertEquals(self.url_map.get_url(), '/test/3/') - - -def test_get_mapping_type(self): - self.assertEquals( - unicode(self.url_map.mapping_type()), - u"Direct" - ) + map = URLMap( + key='test_4', + view_name='test', + view_keywords='slug=test-it-works' + ) + self.assertIsNone(map.full_clean()) + + def test_get_url(self): + self.assertEquals(self.url_map.get_url(), '/test/3/') + + def test_get_mapping_type(self): + self.assertEquals( + unicode(self.url_map.mapping_type()), + u"Direct" + ) From 6ce69532b26f66936376bcfacbad31cb56a6f885 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:36:59 +0000 Subject: [PATCH 06/10] Delete manifest file --- MANIFEST.in | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 2b8ca9b..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,4 +0,0 @@ -include setup.py -include README.md -include MANIFEST.in -recursive-include urlmapper * \ No newline at end of file From 287deaff0250e6d10014a403672ee12376890c66 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:54:51 +0000 Subject: [PATCH 07/10] Add an init file in tests folder Not ideal, but django can not import tests.urls in python2 --- tests/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 From 7ef73717902c273ace609b4ac87382691ca2024d Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:55:45 +0000 Subject: [PATCH 08/10] Delete cruft from conftest --- tests/conftest.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 631321d..3ad2c94 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,12 +4,9 @@ # PYTHON3 use pathlib TEST_FOLDER = os.path.dirname(os.path.abspath(__file__)) -# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "urlmapper.settings") def pytest_configure(): - # import pdb;pdb.set_trace() - # from urlmapper import settings settings.configure( DATABASES={ "default": { From 6a596e166df2bc4a0e380069de5a3eadc4e874f0 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:56:17 +0000 Subject: [PATCH 09/10] Remove need for unicode from builtins --- tests/test_models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index 70ff51d..a78a4a1 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,5 +1,3 @@ -from builtins import str as unicode - from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site @@ -135,6 +133,6 @@ def test_get_url(self): def test_get_mapping_type(self): self.assertEquals( - unicode(self.url_map.mapping_type()), + self.url_map.mapping_type(), u"Direct" ) From 46bf4583d1b4057913b68ab0c39267994d4adebb Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 15 Feb 2023 10:56:35 +0000 Subject: [PATCH 10/10] Reload is a builtin in python2 --- tests/test_helpers.py | 6 +++++- tests/test_tags.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index e77d056..04319e2 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,4 +1,8 @@ -from importlib import reload +try: + from importlib import reload +except ImportError: + # python2 + pass from django.test import TestCase diff --git a/tests/test_tags.py b/tests/test_tags.py index 85a4881..7557e6a 100644 --- a/tests/test_tags.py +++ b/tests/test_tags.py @@ -1,4 +1,8 @@ -from importlib import reload +try: + from importlib import reload +except ImportError: + # python2 + pass from django.template import Template, Context from django.test import TestCase