|
| 1 | +# Copyright DataStax, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from cassandra.query import named_tuple_factory |
| 17 | + |
| 18 | +import logging |
| 19 | +import warnings |
| 20 | + |
| 21 | +import sys |
| 22 | + |
| 23 | +try: |
| 24 | + from unittest import TestCase |
| 25 | +except ImportError: |
| 26 | + from unittest2 import TestCase |
| 27 | + |
| 28 | + |
| 29 | +log = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +NAMEDTUPLE_CREATION_BUG = sys.version_info >= (3,) and sys.version_info < (3, 7) |
| 33 | + |
| 34 | +class TestNamedTupleFactory(TestCase): |
| 35 | + |
| 36 | + long_colnames, long_rows = ( |
| 37 | + ['col{}'.format(x) for x in range(300)], |
| 38 | + [ |
| 39 | + ['value{}'.format(x) for x in range(300)] |
| 40 | + for _ in range(100) |
| 41 | + ] |
| 42 | + ) |
| 43 | + short_colnames, short_rows = ( |
| 44 | + ['col{}'.format(x) for x in range(200)], |
| 45 | + [ |
| 46 | + ['value{}'.format(x) for x in range(200)] |
| 47 | + for _ in range(100) |
| 48 | + ] |
| 49 | + ) |
| 50 | + |
| 51 | + def test_creation_warning_on_long_column_list(self): |
| 52 | + """ |
| 53 | + Reproduces the failure described in PYTHON-893 |
| 54 | +
|
| 55 | + @since 3.15 |
| 56 | + @jira_ticket PYTHON-893 |
| 57 | + @expected_result creation fails on Python > 3 and < 3.7 |
| 58 | +
|
| 59 | + @test_category row_factory |
| 60 | + """ |
| 61 | + if not NAMEDTUPLE_CREATION_BUG: |
| 62 | + named_tuple_factory(self.long_colnames, self.long_rows) |
| 63 | + return |
| 64 | + |
| 65 | + with warnings.catch_warnings(record=True) as w: |
| 66 | + rows = named_tuple_factory(self.long_colnames, self.long_rows) |
| 67 | + self.assertEqual(len(w), 1) |
| 68 | + warning = w[0] |
| 69 | + self.assertIn('pseudo_namedtuple_factory', str(warning)) |
| 70 | + self.assertIn('3.7', str(warning)) |
| 71 | + |
| 72 | + for r in rows: |
| 73 | + self.assertEqual(r.col0, self.long_rows[0][0]) |
| 74 | + |
| 75 | + def test_creation_no_warning_on_short_column_list(self): |
| 76 | + """ |
| 77 | + Tests that normal namedtuple row creation still works after PYTHON-893 fix |
| 78 | +
|
| 79 | + @since 3.15 |
| 80 | + @jira_ticket PYTHON-893 |
| 81 | + @expected_result creates namedtuple-based Rows |
| 82 | +
|
| 83 | + @test_category row_factory |
| 84 | + """ |
| 85 | + with warnings.catch_warnings(record=True) as w: |
| 86 | + rows = named_tuple_factory(self.short_colnames, self.short_rows) |
| 87 | + self.assertEqual(len(w), 0) |
| 88 | + # check that this is a real namedtuple |
| 89 | + self.assertTrue(hasattr(rows[0], '_fields')) |
| 90 | + self.assertIsInstance(rows[0], tuple) |
0 commit comments