|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -from uuid import uuid4 |
| 15 | +from uuid import uuid4, UUID |
16 | 16 | import random |
17 | | -from datetime import date |
| 17 | +from datetime import date, datetime |
| 18 | +from decimal import Decimal |
18 | 19 | from operator import itemgetter |
19 | 20 | from cassandra.cqlengine import CQLEngineException |
20 | 21 | from tests.integration.cqlengine.base import BaseCassEngTestCase |
@@ -124,6 +125,102 @@ def test_a_sensical_error_is_raised_if_you_try_to_create_a_table_twice(self): |
124 | 125 | sync_table(TestModel) |
125 | 126 | sync_table(TestModel) |
126 | 127 |
|
| 128 | + def test_can_insert_model_with_all_column_types(self): |
| 129 | + """ |
| 130 | + Test for inserting all column types into a Model |
| 131 | +
|
| 132 | + test_can_insert_model_with_all_column_types tests that each cqlengine column type can be inserted into a Model. |
| 133 | + It first creates a Model that has each cqlengine column type. It then creates a Model instance where all the fields |
| 134 | + have corresponding data, which performs the insert into the Cassandra table. |
| 135 | + Finally, it verifies that each column read from the Model from Cassandra is the same as the input parameters. |
| 136 | +
|
| 137 | + @since 2.6.0 |
| 138 | + @jira_ticket PYTHON-246 |
| 139 | + @expected_result The Model is inserted with each column type, and the resulting read yields proper data for each column. |
| 140 | +
|
| 141 | + @test_category data_types:primitive |
| 142 | + """ |
| 143 | + |
| 144 | + class AllDatatypesModel(Model): |
| 145 | + id = columns.Integer(primary_key=True) |
| 146 | + a = columns.Ascii() |
| 147 | + b = columns.BigInt() |
| 148 | + c = columns.Blob() |
| 149 | + d = columns.Boolean() |
| 150 | + e = columns.Date() |
| 151 | + f = columns.DateTime() |
| 152 | + g = columns.Decimal() |
| 153 | + h = columns.Double() |
| 154 | + i = columns.Float(double_precision=False) |
| 155 | + j = columns.Inet() |
| 156 | + k = columns.Integer() |
| 157 | + l = columns.Text() |
| 158 | + m = columns.TimeUUID() |
| 159 | + n = columns.UUID() |
| 160 | + o = columns.VarInt() |
| 161 | + |
| 162 | + sync_table(AllDatatypesModel) |
| 163 | + |
| 164 | + input = ['ascii', 2 ** 63 - 1, bytearray(b'hello world'), True, date(1970, 1, 1), |
| 165 | + datetime.utcfromtimestamp(872835240), Decimal('12.3E+7'), 2.39, |
| 166 | + 3.4028234663852886e+38, '123.123.123.123', 2147483647, 'text', |
| 167 | + UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), |
| 168 | + int(str(2147483647) + '000')] |
| 169 | + |
| 170 | + AllDatatypesModel.create(id=0, a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True, e=date(1970, 1, 1), |
| 171 | + f=datetime.utcfromtimestamp(872835240), g=Decimal('12.3E+7'), h=2.39, |
| 172 | + i=3.4028234663852886e+38, j='123.123.123.123', k=2147483647, l='text', |
| 173 | + m=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), n=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), |
| 174 | + o=int(str(2147483647) + '000')) |
| 175 | + |
| 176 | + self.assertEqual(1, AllDatatypesModel.objects.count()) |
| 177 | + output = AllDatatypesModel.objects().first() |
| 178 | + |
| 179 | + for i, i_char in enumerate(range(ord('a'), ord('a') + 15)): |
| 180 | + self.assertEqual(input[i], output[chr(i_char)]) |
| 181 | + |
| 182 | + def test_can_insert_double_and_float(self): |
| 183 | + """ |
| 184 | + Test for inserting single-precision and double-precision values into a Float and Double columns |
| 185 | +
|
| 186 | + test_can_insert_double_and_float tests a Float can only hold a single-precision value, unless |
| 187 | + "double_precision" attribute is specified as True or is unspecified. This test first tests that an AttributeError |
| 188 | + is raised when attempting to input a double-precision value into a single-precision Float. It then verifies that |
| 189 | + Double, Float(double_precision=True) and Float() can hold double-precision values by default. It also verifies that |
| 190 | + columns.Float(double_precision=False) can hold a single-precision value, and a Double can hold a single-precision value. |
| 191 | +
|
| 192 | + @since 2.6.0 |
| 193 | + @jira_ticket PYTHON-246 |
| 194 | + @expected_result Each floating point column type is able to hold their respective precision values. |
| 195 | +
|
| 196 | + @test_category data_types:primitive |
| 197 | + """ |
| 198 | + |
| 199 | + class FloatingPointModel(Model): |
| 200 | + id = columns.Integer(primary_key=True) |
| 201 | + a = columns.Float(double_precision=False) |
| 202 | + b = columns.Float(double_precision=True) |
| 203 | + c = columns.Float() |
| 204 | + d = columns.Double() |
| 205 | + |
| 206 | + sync_table(FloatingPointModel) |
| 207 | + |
| 208 | + FloatingPointModel.create(id=0, a=2.39) |
| 209 | + output = FloatingPointModel.objects().first() |
| 210 | + self.assertEqual(2.390000104904175, output.a) |
| 211 | + |
| 212 | + FloatingPointModel.create(id=0, a=3.4028234663852886e+38, b=2.39, c=2.39, d=2.39) |
| 213 | + output = FloatingPointModel.objects().first() |
| 214 | + |
| 215 | + self.assertEqual(3.4028234663852886e+38, output.a) |
| 216 | + self.assertEqual(2.39, output.b) |
| 217 | + self.assertEqual(2.39, output.c) |
| 218 | + self.assertEqual(2.39, output.d) |
| 219 | + |
| 220 | + FloatingPointModel.create(id=0, d=3.4028234663852886e+38) |
| 221 | + output = FloatingPointModel.objects().first() |
| 222 | + self.assertEqual(3.4028234663852886e+38, output.d) |
| 223 | + |
127 | 224 |
|
128 | 225 | class TestMultiKeyModel(Model): |
129 | 226 |
|
|
0 commit comments