|
26 | 26 | from cassandra.concurrent import execute_concurrent_with_args
|
27 | 27 | from cassandra.cqltypes import Int32Type, EMPTY
|
28 | 28 | from cassandra.query import dict_factory, ordered_dict_factory
|
29 |
| -from cassandra.util import sortedset, Duration |
| 29 | +from cassandra.util import sortedset, Duration, OrderedMap |
30 | 30 | from tests.unit.cython.utils import cythontest
|
31 | 31 |
|
32 | 32 | from tests.integration import use_singledc, execute_until_pass, notprotocolv1, \
|
@@ -723,6 +723,51 @@ def test_can_insert_tuples_with_nulls(self):
|
723 | 723 | self.assertEqual(('', None, None, b''), result[0].t)
|
724 | 724 | self.assertEqual(('', None, None, b''), s.execute(read)[0].t)
|
725 | 725 |
|
| 726 | + def test_insert_collection_with_null_fails(self): |
| 727 | + """ |
| 728 | + NULLs in list / sets / maps are forbidden. |
| 729 | + This is a regression test - there was a bug that serialized None values |
| 730 | + in collections as empty values instead of nulls. |
| 731 | + """ |
| 732 | + s = self.session |
| 733 | + columns = [] |
| 734 | + for collection_type in ['list', 'set']: |
| 735 | + for simple_type in PRIMITIVE_DATATYPES_KEYS: |
| 736 | + columns.append(f'{collection_type}_{simple_type} {collection_type}<{simple_type}>') |
| 737 | + for simple_type in PRIMITIVE_DATATYPES_KEYS: |
| 738 | + columns.append(f'map_k_{simple_type} map<{simple_type}, ascii>') |
| 739 | + columns.append(f'map_v_{simple_type} map<ascii, {simple_type}>') |
| 740 | + s.execute(f'CREATE TABLE collection_nulls (k int PRIMARY KEY, {", ".join(columns)})') |
| 741 | + |
| 742 | + def raises_simple_and_prepared(exc_type, query_str, args): |
| 743 | + self.assertRaises(exc_type, lambda: s.execute(query_str, args)) |
| 744 | + p = s.prepare(query_str.replace('%s', '?')) |
| 745 | + self.assertRaises(exc_type, lambda: s.execute(p, args)) |
| 746 | + |
| 747 | + i = 0 |
| 748 | + for simple_type in PRIMITIVE_DATATYPES_KEYS: |
| 749 | + query_str = f'INSERT INTO collection_nulls (k, set_{simple_type}) VALUES (%s, %s)' |
| 750 | + args = [i, sortedset([None, get_sample(simple_type)])] |
| 751 | + raises_simple_and_prepared(InvalidRequest, query_str, args) |
| 752 | + i += 1 |
| 753 | + for simple_type in PRIMITIVE_DATATYPES_KEYS: |
| 754 | + query_str = f'INSERT INTO collection_nulls (k, list_{simple_type}) VALUES (%s, %s)' |
| 755 | + args = [i, [None, get_sample(simple_type)]] |
| 756 | + raises_simple_and_prepared(InvalidRequest, query_str, args) |
| 757 | + i += 1 |
| 758 | + for simple_type in PRIMITIVE_DATATYPES_KEYS: |
| 759 | + query_str = f'INSERT INTO collection_nulls (k, map_k_{simple_type}) VALUES (%s, %s)' |
| 760 | + args = [i, OrderedMap([(get_sample(simple_type), 'abc'), (None, 'def')])] |
| 761 | + raises_simple_and_prepared(InvalidRequest, query_str, args) |
| 762 | + i += 1 |
| 763 | + for simple_type in PRIMITIVE_DATATYPES_KEYS: |
| 764 | + query_str = f'INSERT INTO collection_nulls (k, map_v_{simple_type}) VALUES (%s, %s)' |
| 765 | + args = [i, OrderedMap([('abc', None), ('def', get_sample(simple_type))])] |
| 766 | + raises_simple_and_prepared(InvalidRequest, query_str, args) |
| 767 | + i += 1 |
| 768 | + |
| 769 | + |
| 770 | + |
726 | 771 | def test_can_insert_unicode_query_string(self):
|
727 | 772 | """
|
728 | 773 | Test to ensure unicode strings can be used in a query
|
|
0 commit comments