From 5529a8648167cb33271b577a38acecdb7f393c49 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Wed, 20 Jan 2016 15:07:13 +0200 Subject: [PATCH] Add tests for literal param processing --- tests/types/test_color.py | 12 ++++++------ tests/types/test_country.py | 5 +++++ tests/types/test_currency.py | 5 +++++ tests/types/test_date_range.py | 8 ++++++++ tests/types/test_email.py | 10 ++++++---- tests/types/test_int_range.py | 6 +++++- tests/types/test_locale.py | 5 +++++ tests/types/test_numeric_range.py | 5 +++++ 8 files changed, 45 insertions(+), 11 deletions(-) diff --git a/tests/types/test_color.py b/tests/types/test_color.py index 3842ec5..a3d1712 100644 --- a/tests/types/test_color.py +++ b/tests/types/test_color.py @@ -24,7 +24,6 @@ def init_models(Document): @pytest.mark.skipif('types.color.python_colour_type is None') class TestColorType(object): - def test_string_parameter_processing(self, session, Document): from colour import Color @@ -44,10 +43,7 @@ class TestColorType(object): def test_color_parameter_processing(self, session, Document): from colour import Color - document = Document( - bg_color=Color(u'white') - ) - + document = Document(bg_color=Color(u'white')) session.add(document) session.commit() @@ -58,5 +54,9 @@ class TestColorType(object): from colour import Color document = Document(bg_color='white') - assert isinstance(document.bg_color, Color) + + def test_literal_param(self, session, Document): + clause = Document.bg_color == 'white' + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == "document.bg_color = 'white'" diff --git a/tests/types/test_country.py b/tests/types/test_country.py index 7ef53b4..ed56287 100644 --- a/tests/types/test_country.py +++ b/tests/types/test_country.py @@ -38,3 +38,8 @@ class TestCountryType(object): def test_scalar_attributes_get_coerced_to_objects(self, User): user = User(country='FI') assert isinstance(user.country, Country) + + def test_literal_param(self, session, User): + clause = User.country == 'FI' + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == '"user".country = \'FI\'' diff --git a/tests/types/test_currency.py b/tests/types/test_currency.py index 8266170..0e10631 100644 --- a/tests/types/test_currency.py +++ b/tests/types/test_currency.py @@ -49,3 +49,8 @@ class TestCurrencyType(object): ): user = User(currency='USD') assert isinstance(user.currency, Currency) + + def test_literal_param(self, session, User): + clause = User.currency == 'USD' + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == '"user".currency = \'USD\'' diff --git a/tests/types/test_date_range.py b/tests/types/test_date_range.py index 779bd62..7325364 100644 --- a/tests/types/test_date_range.py +++ b/tests/types/test_date_range.py @@ -115,3 +115,11 @@ class TestDateRangeOnPostgres(object): session.query(Booking.during.length) ) assert query.scalar() == length + + def test_literal_param(self, session, Booking): + clause = Booking.during == [ + datetime(2015, 1, 1).date(), + datetime(2015, 1, 3).date() + ] + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == "booking.during = '[2015-01-01, 2015-01-03]'" diff --git a/tests/types/test_email.py b/tests/types/test_email.py index 9bc5c78..501f5ee 100644 --- a/tests/types/test_email.py +++ b/tests/types/test_email.py @@ -17,14 +17,16 @@ def User(Base): class TestEmailType(object): - def test_saves_email_as_lowercased(self, session, User): - user = User( - email=u'Someone@example.com' - ) + user = User(email=u'Someone@example.com') session.add(user) session.commit() user = session.query(User).first() assert user.email == u'someone@example.com' + + def test_literal_param(self, session, User): + clause = User.email == 'Someone@example.com' + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == '"user".email = lower(\'Someone@example.com\')' diff --git a/tests/types/test_int_range.py b/tests/types/test_int_range.py index 41c6607..165aac0 100644 --- a/tests/types/test_int_range.py +++ b/tests/types/test_int_range.py @@ -106,7 +106,6 @@ class NumberRangeTestCase(object): @pytest.mark.usefixtures('postgresql_dsn') class TestIntRangeTypeOnPostgres(NumberRangeTestCase): - @pytest.mark.parametrize( 'number_range', ( @@ -391,6 +390,11 @@ class TestIntRangeTypeOnPostgres(NumberRangeTestCase): ) assert query.count() + def test_literal_param(self, session, Building): + clause = Building.persons_at_night == [1, 3] + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == "building.persons_at_night = '[1, 3]'" + class TestNumberRangeTypeOnSqlite(NumberRangeTestCase): pass diff --git a/tests/types/test_locale.py b/tests/types/test_locale.py index ee6225b..5b7134c 100644 --- a/tests/types/test_locale.py +++ b/tests/types/test_locale.py @@ -55,3 +55,8 @@ class TestLocaleType(object): def test_unknown_locale_throws_exception(self, User): with pytest.raises(locale.babel.UnknownLocaleError): User(locale=u'unknown') + + def test_literal_param(self, session, User): + clause = User.locale == 'en_US' + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == '"user".locale = \'en_US\'' diff --git a/tests/types/test_numeric_range.py b/tests/types/test_numeric_range.py index 0ffd9bd..d665c84 100644 --- a/tests/types/test_numeric_range.py +++ b/tests/types/test_numeric_range.py @@ -116,6 +116,11 @@ class TestNumericRangeOnPostgres(NumericRangeTestCase): ) assert query.scalar() == length + def test_literal_param(self, session, Car): + clause = Car.price_range == [1, 3] + compiled = str(clause.compile(compile_kwargs={'literal_binds': True})) + assert compiled == "car.price_range = '[1, 3]'" + @pytest.mark.skipif('intervals is None') class TestNumericRangeWithStep(object):