From 91a1022227499c8efce038c4a0a9bdcdb14a69d0 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 20 Jun 2016 10:08:36 -0400 Subject: Disable Enum string validation by default Rolled back the validation rules a bit in :class:`.Enum` to allow unknown string values to pass through, unless the flag ``validate_string=True`` is passed to the Enum; any other kind of object is still of course rejected. While the immediate use is to allow comparisons to enums with LIKE, the fact that this use exists indicates there may be more unknown-string-comparsion use cases than we expected, which hints that perhaps there are some unknown string-INSERT cases too. Change-Id: I7d1d79b374a7d47966d410998f77cd19294ab7b0 Fixes: #3725 --- test/dialect/mysql/test_types.py | 4 ++- test/sql/test_types.py | 71 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/dialect/mysql/test_types.py b/test/dialect/mysql/test_types.py index e570e0db1..7b7cf3667 100644 --- a/test/dialect/mysql/test_types.py +++ b/test/dialect/mysql/test_types.py @@ -666,7 +666,9 @@ class EnumSetTest( 'mysql_enum', self.metadata, Column('e1', e1), Column('e2', e2, nullable=False), - Column('e2generic', Enum("a", "b"), nullable=False), + Column( + 'e2generic', + Enum("a", "b", validate_strings=True), nullable=False), Column('e3', e3), Column('e4', e4, nullable=False), diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 67d20871c..e540f9246 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -1121,7 +1121,8 @@ class EnumTest(AssertsCompiledSQL, fixtures.TablesTest): Column('someenum', Enum('one', 'two', 'three', native_enum=False)), Column('someotherenum', Enum('one', 'two', 'three', - create_constraint=False, native_enum=False)), + create_constraint=False, native_enum=False, + validate_strings=True)), ) Table( @@ -1149,14 +1150,24 @@ class EnumTest(AssertsCompiledSQL, fixtures.TablesTest): def test_validators_pep435(self): type_ = Enum(self.SomeEnum) + validate_type = Enum(self.SomeEnum, validate_strings=True) bind_processor = type_.bind_processor(testing.db.dialect) + bind_processor_validates = validate_type.bind_processor( + testing.db.dialect) eq_(bind_processor('one'), "one") eq_(bind_processor(self.one), "one") + eq_(bind_processor("foo"), "foo") + assert_raises_message( + LookupError, + '"5" is not among the defined enum values', + bind_processor, 5 + ) + assert_raises_message( LookupError, '"foo" is not among the defined enum values', - bind_processor, "foo" + bind_processor_validates, "foo" ) result_processor = type_.result_processor(testing.db.dialect, None) @@ -1169,22 +1180,43 @@ class EnumTest(AssertsCompiledSQL, fixtures.TablesTest): ) literal_processor = type_.literal_processor(testing.db.dialect) + validate_literal_processor = validate_type.literal_processor( + testing.db.dialect) eq_(literal_processor("one"), "'one'") + + eq_(literal_processor("foo"), "'foo'") + + assert_raises_message( + LookupError, + '"5" is not among the defined enum values', + literal_processor, 5 + ) + assert_raises_message( LookupError, '"foo" is not among the defined enum values', - literal_processor, "foo" + validate_literal_processor, "foo" ) def test_validators_plain(self): type_ = Enum("one", "two") + validate_type = Enum("one", "two", validate_strings=True) bind_processor = type_.bind_processor(testing.db.dialect) + bind_processor_validates = validate_type.bind_processor( + testing.db.dialect) eq_(bind_processor('one'), "one") + eq_(bind_processor('foo'), "foo") + assert_raises_message( + LookupError, + '"5" is not among the defined enum values', + bind_processor, 5 + ) + assert_raises_message( LookupError, '"foo" is not among the defined enum values', - bind_processor, "foo" + bind_processor_validates, "foo" ) result_processor = type_.result_processor(testing.db.dialect, None) @@ -1197,13 +1229,40 @@ class EnumTest(AssertsCompiledSQL, fixtures.TablesTest): ) literal_processor = type_.literal_processor(testing.db.dialect) + validate_literal_processor = validate_type.literal_processor( + testing.db.dialect) eq_(literal_processor("one"), "'one'") + eq_(literal_processor("foo"), "'foo'") + assert_raises_message( + LookupError, + '"5" is not among the defined enum values', + literal_processor, 5 + ) + assert_raises_message( LookupError, '"foo" is not among the defined enum values', - literal_processor, "foo" + validate_literal_processor, "foo" ) + def test_validators_not_in_like_roundtrip(self): + enum_table = self.tables['non_native_enum_table'] + + enum_table.insert().execute([ + {'id': 1, 'someenum': 'two'}, + {'id': 2, 'someenum': 'two'}, + {'id': 3, 'someenum': 'one'}, + ]) + + eq_( + enum_table.select(). + where(enum_table.c.someenum.like('%wo%')). + order_by(enum_table.c.id).execute().fetchall(), + [ + (1, 'two', None), + (2, 'two', None), + ] + ) @testing.fails_on( 'postgresql+zxjdbc', @@ -1364,7 +1423,7 @@ class EnumTest(AssertsCompiledSQL, fixtures.TablesTest): assert_raises( exc.StatementError, self.tables['non_native_enum_table'].insert().execute, - {'id': 4, 'someenum': 'four'} + {'id': 4, 'someotherenum': 'four'} ) def test_mock_engine_no_prob(self): -- cgit v1.2.1