Skip to content

Commit

Permalink
Merge 5197e23 into 1cd53a4
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed Oct 20, 2016
2 parents 1cd53a4 + 5197e23 commit 080f93b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ python:
# - "pypy3"

env:
- WITHSIMPLEJSON=true
- WITHSIMPLEJSON=false
- WITHSIMPLEJSON=false WITHFUTURE=false
- WITHSIMPLEJSON=false WITHFUTURE=true
- WITHSIMPLEJSON=true WITHFUTURE=false
- WITHSIMPLEJSON=true WITHFUTURE=true

addons:
# The default Travis build environment uses PostgreSQL 9.1, but some of the
Expand Down Expand Up @@ -54,6 +56,11 @@ install:
if [[ "$WITHSIMPLEJSON" = true ]]; then
pip install simplejson
fi
# Install future, if necessary for this build.
- |
if [[ "$WITHFUTURE" = true ]]; then
pip install future
fi
# Install the requirements specific to Travis tests.
- pip install -r requirements-doc.txt
- pip install coveralls
Expand Down
13 changes: 13 additions & 0 deletions flask_restless/serialization/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def total_seconds(td):
return (td.microseconds + secs * 10**6) / 10**6


def to_unicode(s):
"""Convert a string to a Unicode string, if on Python 2."""
try:
return unicode(s) # noqa
except NameError:
return s


def create_relationship(model, instance, relation):
"""Creates a relationship from the given relation name.
Expand Down Expand Up @@ -379,6 +387,11 @@ def _dump(self, instance, only=None):
except BuildError:
pass
else:
# HACK In order to support users using Python 2.7 with
# the `future` compatibility library, we need to ensure
# that both `request.url_root` and `path` are of the
# same type.
path = to_unicode(path)
url = urljoin(request.url_root, path)
result['links'] = dict(self=url)
# # add any included methods
Expand Down
31 changes: 30 additions & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@
from datetime import datetime
from datetime import time
from datetime import timedelta
from unittest2 import skipUnless
from uuid import UUID
from uuid import uuid1

import warnings

try:
# HACK The future package uses code that is pending deprecation in
# Python 3.4 or later. We catch the warning here so that the test
# suite does not complain about it.
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=PendingDeprecationWarning)
from future.standard_library import install_aliases
install_aliases()
except ImportError:
is_future_available = False
else:
is_future_available = True
from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import DateTime
Expand Down Expand Up @@ -579,6 +593,21 @@ def test_non_id_primary_key(self):
self.assertEqual(tag['type'], 'tag')
self.assertEqual(tag['attributes']['tagid'], 1)

@skipUnless(is_future_available, 'required "future" library')
def test_unicode_self_link(self):
"""Test that serializing the "self" link handles unicode on Python 2.
This is a specific test for code using the :mod:`future` library.
For more information, see GitHub issue #594.
"""
person = self.Person(id=1)
self.session.add(person)
self.session.commit()
self.manager.create_api(self.Person)
self.app.get('/api/person/1')


class TestFetchRelation(ManagerTestBase):

Expand Down

0 comments on commit 080f93b

Please sign in to comment.