Skip to content

Commit 7d5e896

Browse files
authored
Merge pull request #11 from TwigWorld/MD/pytest
Make package python2/3 compatible
2 parents afd94c5 + 46bf458 commit 7d5e896

File tree

11 files changed

+221
-220
lines changed

11 files changed

+221
-220
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
*.py[cod]
22
.coverage
3+
.DS_Store
4+
5+
*.egg-info/
36

47
# pycharm
58
.idea/
69

710
# pyenv
8-
.python-version
11+
.python-version

MANIFEST.in

Lines changed: 0 additions & 4 deletions
This file was deleted.

runtests.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

setup.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
scripts=[],
88
description='Use fixed keys in your Django template to refer to dynamic URLs',
99
long_description=open('README.md').read(),
10-
python_requires='>=3.7.0',
1110
install_requires=[
12-
"Django>=2.2, <3.0",
11+
"Django<3",
1312
],
13+
extras_require={
14+
"testing": [
15+
"pytest",
16+
"pytest-django",
17+
]
18+
},
1419
packages=find_packages(),
15-
include_package_data=True
20+
include_package_data=True,
1621
)
File renamed without changes.

tests/conftest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
3+
from django.conf import settings
4+
5+
# PYTHON3 use pathlib
6+
TEST_FOLDER = os.path.dirname(os.path.abspath(__file__))
7+
8+
9+
def pytest_configure():
10+
settings.configure(
11+
DATABASES={
12+
"default": {
13+
"ENGINE": "django.db.backends.sqlite3",
14+
}
15+
},
16+
INSTALLED_APPS=[
17+
'django.contrib.auth',
18+
'django.contrib.contenttypes',
19+
'django.contrib.sessions',
20+
'django.contrib.staticfiles',
21+
'django.contrib.sites',
22+
'urlmapper',
23+
],
24+
STATIC_URL='/',
25+
MIDDLEWARE_CLASSES=(
26+
'django.contrib.sessions.middleware.SessionMiddleware',
27+
'django.middleware.common.CommonMiddleware',
28+
),
29+
ROOT_URLCONF='tests.urls',
30+
SITE_ID=1,
31+
TEMPLATES=[
32+
{
33+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
34+
'APP_DIRS': True,
35+
'OPTIONS': {
36+
'context_processors': [
37+
'django.template.context_processors.debug',
38+
'django.template.context_processors.request',
39+
'django.contrib.auth.context_processors.auth',
40+
'django.contrib.messages.context_processors.messages',
41+
],
42+
},
43+
},
44+
],
45+
URLMAPPER_KEYS=[
46+
'test_1',
47+
'test_2',
48+
'test_3',
49+
'test_4',
50+
'test_5'
51+
],
52+
URLMAPPER_FUNCTIONS={
53+
'test_1': lambda: 'test_1_success',
54+
'test_2': lambda request: 'test_2_success'
55+
},
56+
)

urlmapper/tests/test_helpers.py renamed to tests/test_helpers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from importlib import reload
1+
try:
2+
from importlib import reload
3+
except ImportError:
4+
# python2
5+
pass
26

37
from django.test import TestCase
48

5-
from ..helpers import get_mapped_url, check_mapped_url
6-
from ..models import URLMap
7-
from .. import settings
9+
from urlmapper.helpers import get_mapped_url, check_mapped_url
10+
from urlmapper.models import URLMap
11+
from urlmapper import settings
812

913

1014
class TestGetMappedURL(TestCase):

tests/test_models.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from django.contrib.auth.models import User
2+
from django.contrib.contenttypes.models import ContentType
3+
from django.contrib.sites.models import Site
4+
from django.core.exceptions import ValidationError
5+
from django.test import TestCase
6+
import pytest
7+
8+
from urlmapper.models import URLMap
9+
10+
11+
class TestModels(TestCase):
12+
13+
def setUp(self):
14+
self.url_map = URLMap.objects.create(
15+
key='test_3',
16+
url='/test/3/'
17+
)
18+
self.invalid_url_map = URLMap.objects.create(
19+
key='test_1',
20+
url='/invalid/'
21+
)
22+
23+
def test_get_key_choices(self):
24+
self.assertEquals(
25+
set(self.url_map._meta.get_field(field_name='key').choices),
26+
set(
27+
(
28+
('test_3', 'test_3'),
29+
('test_4', 'test_4'),
30+
('test_5', 'test_5')
31+
)
32+
)
33+
)
34+
35+
def test_invalid_map_does_not_show(self):
36+
self.assertEquals(
37+
list(URLMap.objects.all()),
38+
[self.url_map]
39+
)
40+
41+
def test_only_one_mapping_allowed(self):
42+
# No mapping
43+
map = URLMap(key='test_4')
44+
with self.assertRaises(ValidationError):
45+
map.full_clean()
46+
47+
# More than one mapping
48+
map = URLMap(key='test_4', url='abc', content_type=ContentType.objects.first())
49+
with self.assertRaises(ValidationError):
50+
map.full_clean()
51+
52+
map = URLMap(key='test_4', url='abc', object_id=1)
53+
with self.assertRaises(ValidationError):
54+
map.full_clean()
55+
56+
map = URLMap(key='test_4', object_id=1, view_name='test')
57+
with self.assertRaises(ValidationError):
58+
map.full_clean()
59+
60+
def test_only_valid_url_allowed(self):
61+
map = URLMap(key='test_4', url='/invalid/')
62+
with self.assertRaises(ValidationError):
63+
map.full_clean()
64+
65+
map = URLMap(key='test_4', url='/test/')
66+
self.assertIsNone(map.full_clean())
67+
68+
@pytest.mark.skip(reason="No get_absolute_url on user")
69+
def test_only_valid_object_allowed(self):
70+
# No get_absolute_url on site
71+
site = Site.objects.first()
72+
map = URLMap(
73+
key='test_4',
74+
content_object=site
75+
)
76+
with self.assertRaises(ValidationError):
77+
map.full_clean()
78+
79+
# get_absolute_url on user
80+
user = User.objects.create_user('test')
81+
map = URLMap(
82+
key='test_4',
83+
content_object=user
84+
)
85+
self.assertIsNone(map.full_clean())
86+
87+
def test_only_valid_view_allowed(self):
88+
map = URLMap(
89+
key='test_4',
90+
view_name='invalid'
91+
)
92+
with self.assertRaises(ValidationError):
93+
map.full_clean()
94+
95+
map = URLMap(
96+
key='test_4',
97+
view_name='test'
98+
)
99+
self.assertIsNone(map.full_clean())
100+
101+
map = URLMap(
102+
key='test_4',
103+
view_name='test',
104+
view_keywords='invalid'
105+
)
106+
with self.assertRaises(ValidationError):
107+
map.full_clean()
108+
109+
map = URLMap(
110+
key='test_4',
111+
view_name='test',
112+
view_keywords='still=invalid'
113+
)
114+
with self.assertRaises(ValidationError):
115+
map.full_clean()
116+
117+
map = URLMap(
118+
key='test_4',
119+
view_name='test',
120+
view_keywords='pk=12345'
121+
)
122+
self.assertIsNone(map.full_clean())
123+
124+
map = URLMap(
125+
key='test_4',
126+
view_name='test',
127+
view_keywords='slug=test-it-works'
128+
)
129+
self.assertIsNone(map.full_clean())
130+
131+
def test_get_url(self):
132+
self.assertEquals(self.url_map.get_url(), '/test/3/')
133+
134+
def test_get_mapping_type(self):
135+
self.assertEquals(
136+
self.url_map.mapping_type(),
137+
u"Direct"
138+
)

urlmapper/tests/test_tags.py renamed to tests/test_tags.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from importlib import reload
1+
try:
2+
from importlib import reload
3+
except ImportError:
4+
# python2
5+
pass
26

37
from django.template import Template, Context
48
from django.test import TestCase
59

6-
from .. import settings
7-
from ..models import URLMap
10+
from urlmapper import settings
11+
from urlmapper.models import URLMap
812

913

1014
class TestTags(TestCase):
File renamed without changes.

0 commit comments

Comments
 (0)