Skip to content

Commit

Permalink
Merge pull request #11 from TwigWorld/MD/pytest
Browse files Browse the repository at this point in the history
Make package python2/3 compatible
  • Loading branch information
davismr authored Feb 15, 2023
2 parents afd94c5 + 46bf458 commit 7d5e896
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 220 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
*.py[cod]
.coverage
.DS_Store

*.egg-info/

# pycharm
.idea/

# pyenv
.python-version
.python-version
4 changes: 0 additions & 4 deletions MANIFEST.in

This file was deleted.

59 changes: 0 additions & 59 deletions runtests.py

This file was deleted.

11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
File renamed without changes.
56 changes: 56 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os

from django.conf import settings

# PYTHON3 use pathlib
TEST_FOLDER = os.path.dirname(os.path.abspath(__file__))


def pytest_configure():
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'
},
)
12 changes: 8 additions & 4 deletions urlmapper/tests/test_helpers.py → tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from importlib import reload
try:
from importlib import reload
except ImportError:
# python2
pass

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):
Expand Down
138 changes: 138 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
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


class TestModels(TestCase):

def setUp(self):
self.url_map = URLMap.objects.create(
key='test_3',
url='/test/3/'
)
self.invalid_url_map = URLMap.objects.create(
key='test_1',
url='/invalid/'
)

def test_get_key_choices(self):
self.assertEquals(
set(self.url_map._meta.get_field(field_name='key').choices),
set(
(
('test_3', 'test_3'),
('test_4', 'test_4'),
('test_5', 'test_5')
)
)
)

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())

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(
self.url_map.mapping_type(),
u"Direct"
)
10 changes: 7 additions & 3 deletions urlmapper/tests/test_tags.py → tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from importlib import reload
try:
from importlib import reload
except ImportError:
# python2
pass

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):
Expand Down
File renamed without changes.
Loading

0 comments on commit 7d5e896

Please sign in to comment.