Added URLType

This commit is contained in:
Konsta Vesterinen
2013-10-23 08:32:26 +03:00
parent b43214429d
commit 16d0f89e59
2 changed files with 37 additions and 2 deletions

View File

@@ -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

35
tests/types/test_url.py Normal file
View File

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