Added URLType
This commit is contained in:
@@ -8,12 +8,12 @@ from sqlalchemy import types
|
|||||||
from .scalar_coercible import ScalarCoercible
|
from .scalar_coercible import ScalarCoercible
|
||||||
|
|
||||||
|
|
||||||
class UrlType(types.TypeDecorator, ScalarCoercible):
|
class URLType(types.TypeDecorator, ScalarCoercible):
|
||||||
impl = types.UnicodeText
|
impl = types.UnicodeText
|
||||||
|
|
||||||
def process_bind_param(self, value, dialect):
|
def process_bind_param(self, value, dialect):
|
||||||
if isinstance(value, furl):
|
if isinstance(value, furl):
|
||||||
return value.code
|
return six.text_type(value)
|
||||||
|
|
||||||
if isinstance(value, six.string_types):
|
if isinstance(value, six.string_types):
|
||||||
return value
|
return value
|
||||||
|
35
tests/types/test_url.py
Normal file
35
tests/types/test_url.py
Normal 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)
|
Reference in New Issue
Block a user