Skip to content

Commit

Permalink
added tests for drf>=2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Chibisov Gennady committed Sep 7, 2014
1 parent 65a3a67 commit 8aed65b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 38 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ env:
- TOX_ENV=py27-drf2.3.12
- TOX_ENV=py27-drf2.3.13
- TOX_ENV=py27-drf2.3.14
- TOX_ENV=py27-drf2.4.0
- TOX_ENV=py27-drf2.4.1
- TOX_ENV=py27-drf2.4.2
- TOX_ENV=py3
- TOX_ENV=django1.5.5
- TOX_ENV=django1.6.2
- TOX_ENV=django1.5
- TOX_ENV=django1.6
- TOX_ENV=django1.7

script:
Expand Down
11 changes: 10 additions & 1 deletion rest_framework_extensions/compat_drf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ def add_trailing_slash_if_needed(regexp_string):
if get_rest_framework_features()['router_trailing_slash']:
return regexp_string[:-2] + '{trailing_slash}$'
else:
return regexp_string
return regexp_string


def get_lookup_allowed_symbols(kwarg_name='pk', force_dot=False):
# todo: test me

if get_rest_framework_features()['use_dot_in_lookup_regex_by_default'] or force_dot:
return '(?P<{0}>[^/.]+)'.format(kwarg_name)
else:
return '(?P<{0}>[^/]+)'.format(kwarg_name)
1 change: 1 addition & 0 deletions rest_framework_extensions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def get_rest_framework_features():
return {
'router_trailing_slash': get_rest_framework_version() >= (2, 3, 6),
'allow_dot_in_lookup_regex_without_trailing_slash': get_rest_framework_version() >= (2, 3, 8),
'use_dot_in_lookup_regex_by_default': get_rest_framework_version() >= (2, 4, 0), # todo: test me
'max_paginate_by': get_rest_framework_version() >= (2, 3, 8),
'django_object_permissions_class': get_rest_framework_version() >= (2, 3, 8),
'save_related_serializers': get_rest_framework_version() >= (2, 3, 8), # todo: test me
Expand Down
18 changes: 10 additions & 8 deletions tests_app/tests/functional/routers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.test import TestCase
from django.utils import unittest

from rest_framework_extensions.compat_drf import get_lookup_allowed_symbols
from rest_framework_extensions.utils import get_rest_framework_features
from rest_framework_extensions.routers import ExtendedSimpleRouter

Expand All @@ -15,10 +16,12 @@ def test_urls_have_trailing_slash_by_default(self):
router.register(r'router-viewset', RouterViewSet)
urls = router.urls

lookup_allowed_symbols = get_lookup_allowed_symbols()

for exp in ['^router-viewset/$',
'^router-viewset/(?P<pk>[^/]+)/$',
'^router-viewset/{0}/$'.format(lookup_allowed_symbols),
'^router-viewset/list_controller/$',
'^router-viewset/(?P<pk>[^/]+)/detail_controller/$']:
'^router-viewset/{0}/detail_controller/$'.format(lookup_allowed_symbols)]:
msg = 'Should find url pattern with regexp %s' % exp
self.assertIsNotNone(get_url_pattern_by_regex_pattern(urls, exp), msg=msg)

Expand All @@ -33,14 +36,13 @@ def test_urls_can_have_trailing_slash_removed(self):
router.register(r'router-viewset', RouterViewSet)
urls = router.urls

if get_rest_framework_features()['allow_dot_in_lookup_regex_without_trailing_slash']:
lookup_allowed_symbols = '(?P<pk>[^/.]+)'
else:
lookup_allowed_symbols = '(?P<pk>[^/]+)'
lookup_allowed_symbols = get_lookup_allowed_symbols(
force_dot=get_rest_framework_features()['allow_dot_in_lookup_regex_without_trailing_slash']
)

for exp in ['^router-viewset$',
'^router-viewset/' + lookup_allowed_symbols + r'$',
'^router-viewset/{0}$'.format(lookup_allowed_symbols),
'^router-viewset/list_controller$',
'^router-viewset/' + lookup_allowed_symbols + '/detail_controller$']:
'^router-viewset/{0}/detail_controller$'.format(lookup_allowed_symbols)]:
msg = 'Should find url pattern with regexp %s' % exp
self.assertIsNotNone(get_url_pattern_by_regex_pattern(urls, exp), msg=msg)
7 changes: 5 additions & 2 deletions tests_app/tests/unit/routers/nested_router_mixin/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from rest_framework_extensions.compat_drf import get_lookup_allowed_symbols
from rest_framework_extensions.test import APITestCase
from rest_framework_extensions.routers import ExtendedSimpleRouter
from rest_framework_extensions.utils import compose_parent_pk_kwarg_name
Expand All @@ -11,10 +12,12 @@

class NestedRouterMixinTest(APITestCase):
def get_lookup_regex(self, value):
return '(?P<{0}>[^/]+)'.format(value)
return get_lookup_allowed_symbols(value)
# return '(?P<{0}>[^/]+)'.format(value)

def get_parent_lookup_regex(self, value):
return '(?P<{0}>[^/.]+)'.format(compose_parent_pk_kwarg_name(value))
return get_lookup_allowed_symbols(compose_parent_pk_kwarg_name(value), force_dot=True)
# return '(?P<{0}>[^/.]+)'.format(compose_parent_pk_kwarg_name(value))

def test_one_route(self):
router = ExtendedSimpleRouter()
Expand Down
15 changes: 12 additions & 3 deletions tests_app/tests/unit/serializers/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
import sys

from django.test import TestCase
from django.core.files import File
from rest_framework_extensions.utils import get_rest_framework_features
from django.utils import unittest

from rest_framework_extensions.utils import get_rest_framework_features
from rest_framework_extensions.compat import BytesIO

from .serializers import CommentSerializer, UserSerializer, CommentSerializerWithExpandedUsersLiked, CommentSerializerWithAllowedUserId
Expand All @@ -28,13 +31,19 @@ def setUp(self):
def get_comment(self):
return CommentModel.objects.get(pk=self.comment.pk)

@unittest.skipIf(
sys.version_info[0] == 3,
"Skipped for python3 because of https://github.com/tomchristie/django-rest-framework/issues/1642"
)
def test_should_use_default_saving_without_partial(self):
serializer = CommentSerializer(data={
'user': self.user.id,
'title': 'hola',
'text': 'amigos'
'text': 'amigos',
})
self.assertTrue(serializer.is_valid())

self.assertTrue(serializer.is_valid()) # bug for python3 comes from here

saved_object = serializer.save()
self.assertEqual(saved_object.user, self.user)
self.assertEqual(saved_object.title, 'hola')
Expand Down
65 changes: 43 additions & 22 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ envlist =
py27-drf2.3.13,
py27-drf2.3.14,
py3,
django1.5.5,
django1.6.2,
django1.5,
django1.6,
django1.7

[testenv]
Expand All @@ -27,105 +27,126 @@ commands=
[testenv:py27-drf2.3.5]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.5
django-guardian==1.1.1

[testenv:py27-drf2.3.6]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.6
django-guardian==1.1.1

[testenv:py27-drf2.3.7]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.7
django-guardian==1.1.1

[testenv:py27-drf2.3.8]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.8
django-guardian==1.1.1

[testenv:py27-drf2.3.9]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.9
django-guardian==1.1.1

[testenv:py27-drf2.3.10]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.10
django-guardian==1.1.1

[testenv:py27-drf2.3.11]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.11
django-guardian==1.1.1

[testenv:py27-drf2.3.12]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.12
django-guardian==1.1.1

[testenv:py27-drf2.3.13]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.13
django-guardian==1.1.1

[testenv:py27-drf2.3.14]
deps=
{[testenv]deps}
Django>=1.5.0
Django>=1.5,<1.6
djangorestframework==2.3.14
django-guardian==1.1.1

[testenv:py27-drf2.4.0]
deps=
{[testenv]deps}
Django>=1.5,<1.6
djangorestframework==2.4.0
django-guardian==1.1.1

[testenv:py27-drf2.4.1]
deps=
{[testenv]deps}
Django>=1.5,<1.6
djangorestframework==2.4.1
django-guardian==1.1.1

[testenv:py27-drf2.4.2]
deps=
{[testenv]deps}
Django>=1.5,<1.6
djangorestframework==2.4.2
django-guardian==1.1.1

[testenv:py3]
basepython = python3
deps=
{[testenv]deps}
Django>=1.5.0
djangorestframework==2.3.13
Django>=1.5,<1.6
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.1.1

[testenv:django1.5.5]
[testenv:django1.5]
basepython = python2.7
deps=
{[testenv]deps}
Django==1.5.5
djangorestframework==2.3.13
Django>=1.5,<1.6
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.1.1

[testenv:django1.6.2]
[testenv:django1.6]
basepython = python2.7
deps=
{[testenv]deps}
Django==1.6.2
djangorestframework==2.3.13
Django>=1.6,<1.7
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.1.1

[testenv:django1.7]
basepython = python2.7
deps=
{[testenv]deps}
https://www.djangoproject.com/download/1.7b3/tarball/
djangorestframework==2.3.13
Django>=1.7,<1.8
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.2

0 comments on commit 8aed65b

Please sign in to comment.