From 16d0f89e5904fe55cf9aa0568c6e0549c081a61c Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Wed, 23 Oct 2013 08:32:26 +0300 Subject: [PATCH] Added URLType --- sqlalchemy_utils/types/url.py | 4 ++-- tests/types/test_url.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/types/test_url.py diff --git a/sqlalchemy_utils/types/url.py b/sqlalchemy_utils/types/url.py index 7f725c9..b5b7624 100644 --- a/sqlalchemy_utils/types/url.py +++ b/sqlalchemy_utils/types/url.py @@ -8,12 +8,12 @@ from sqlalchemy import types from .scalar_coercible import ScalarCoercible -class UrlType(types.TypeDecorator, ScalarCoercible): +class URLType(types.TypeDecorator, ScalarCoercible): impl = types.UnicodeText def process_bind_param(self, value, dialect): if isinstance(value, furl): - return value.code + return six.text_type(value) if isinstance(value, six.string_types): return value diff --git a/tests/types/test_url.py b/tests/types/test_url.py new file mode 100644 index 0000000..f23b1a8 --- /dev/null +++ b/tests/types/test_url.py @@ -0,0 +1,35 @@ +from pytest import mark +import sqlalchemy as sa +from sqlalchemy_utils.types import url + +from tests import TestCase + + +@mark.skipif('url.furl is None') +class TestURLType(TestCase): + def create_models(self): + class User(self.Base): + __tablename__ = 'user' + id = sa.Column(sa.Integer, primary_key=True) + website = sa.Column(url.URLType) + + def __repr__(self): + return 'User(%r)' % self.id + + self.User = User + + def test_color_parameter_processing(self): + user = self.User( + website=url.furl(u'www.example.com') + ) + + self.session.add(user) + self.session.commit() + + user = self.session.query(self.User).first() + assert isinstance(user.website, url.furl) + + def test_scalar_attributes_get_coerced_to_objects(self): + user = self.User(website=u'www.example.com') + + assert isinstance(user.website, url.furl)