-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from TwigWorld/MD/pytest
Make package python2/3 compatible
- Loading branch information
Showing
11 changed files
with
221 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.