Skip to content

Commit 0356f85

Browse files
committed
tests for PYTHON-246
1 parent ce8765a commit 0356f85

2 files changed

Lines changed: 151 additions & 22 deletions

File tree

tests/integration/cqlengine/model/test_model_io.py

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from uuid import uuid4
15+
from uuid import uuid4, UUID
1616
import random
17-
from datetime import date
17+
from datetime import date, datetime
18+
from decimal import Decimal
1819
from operator import itemgetter
1920
from cassandra.cqlengine import CQLEngineException
2021
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):
124125
sync_table(TestModel)
125126
sync_table(TestModel)
126127

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+
127224

128225
class TestMultiKeyModel(Model):
129226

tests/integration/cqlengine/model/test_udts.py

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,22 @@ class DepthModel(Model):
187187
self.assertEqual(udts[2], output.v_2)
188188
self.assertEqual(udts[3], output.v_3)
189189

190-
def test_can_insert_udts_with_nulls(self):
190+
def test_can_insert_udts_with_nones(self):
191+
"""
192+
Test for inserting all column types as empty into a UserType as None's
193+
194+
test_can_insert_udts_with_nones tests that each cqlengine column type can be inserted into a UserType as None's.
195+
It first creates a UserType that has each cqlengine column type, and a corresponding table/Model. It then creates
196+
a UserType instance where all the fields are None's and inserts the UserType as an instance of the Model. Finally,
197+
it verifies that each column read from the UserType from Cassandra is None.
198+
199+
@since 2.5.0
200+
@jira_ticket PYTHON-251
201+
@expected_result The UserType is inserted with each column type, and the resulting read yields None's for each column.
202+
203+
@test_category data_types:udt
204+
"""
205+
191206
class AllDatatypes(UserType):
192207
a = columns.Ascii()
193208
b = columns.BigInt()
@@ -196,13 +211,14 @@ class AllDatatypes(UserType):
196211
e = columns.Date()
197212
f = columns.DateTime()
198213
g = columns.Decimal()
199-
h = columns.Float(double_precision=False)
200-
i = columns.Inet()
201-
j = columns.Integer()
202-
k = columns.Text()
203-
l = columns.TimeUUID()
204-
m = columns.UUID()
205-
n = columns.VarInt()
214+
h = columns.Double()
215+
i = columns.Float(double_precision=False)
216+
j = columns.Inet()
217+
k = columns.Integer()
218+
l = columns.Text()
219+
m = columns.TimeUUID()
220+
n = columns.UUID()
221+
o = columns.VarInt()
206222

207223
class AllDatatypesModel(Model):
208224
id = columns.Integer(primary_key=True)
@@ -219,6 +235,21 @@ class AllDatatypesModel(Model):
219235
self.assertEqual(input, output)
220236

221237
def test_can_insert_udts_with_all_datatypes(self):
238+
"""
239+
Test for inserting all column types into a UserType
240+
241+
test_can_insert_udts_with_all_datatypes tests that each cqlengine column type can be inserted into a UserType.
242+
It first creates a UserType that has each cqlengine column type, and a corresponding table/Model. It then creates
243+
a UserType instance where all the fields have corresponding data, and inserts the UserType as an instance of the Model.
244+
Finally, it verifies that each column read from the UserType from Cassandra is the same as the input parameters.
245+
246+
@since 2.5.0
247+
@jira_ticket PYTHON-251
248+
@expected_result The UserType is inserted with each column type, and the resulting read yields proper data for each column.
249+
250+
@test_category data_types:udt
251+
"""
252+
222253
class AllDatatypes(UserType):
223254
a = columns.Ascii()
224255
b = columns.BigInt()
@@ -228,12 +259,13 @@ class AllDatatypes(UserType):
228259
f = columns.DateTime()
229260
g = columns.Decimal()
230261
h = columns.Double()
231-
i = columns.Inet()
232-
j = columns.Integer()
233-
k = columns.Text()
234-
l = columns.TimeUUID()
235-
m = columns.UUID()
236-
n = columns.VarInt()
262+
i = columns.Float(double_precision=False)
263+
j = columns.Inet()
264+
k = columns.Integer()
265+
l = columns.Text()
266+
m = columns.TimeUUID()
267+
n = columns.UUID()
268+
o = columns.VarInt()
237269

238270
class AllDatatypesModel(Model):
239271
id = columns.Integer(primary_key=True)
@@ -242,14 +274,14 @@ class AllDatatypesModel(Model):
242274
sync_table(AllDatatypesModel)
243275

244276
input = AllDatatypes(a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True, e=date(1970, 1, 1),
245-
f=datetime.utcfromtimestamp(872835240),
246-
g=Decimal('12.3E+7'), h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647,
247-
k='text', l= UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
248-
m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'))
249-
alldata = AllDatatypesModel.create(id=0, data=input)
277+
f=datetime.utcfromtimestamp(872835240), g=Decimal('12.3E+7'), h=2.39,
278+
i=3.4028234663852886e+38, j='123.123.123.123', k=2147483647, l='text',
279+
m=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), n=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'),
280+
o=int(str(2147483647) + '000'))
281+
AllDatatypesModel.create(id=0, data=input)
250282

251283
self.assertEqual(1, AllDatatypesModel.objects.count())
252284
output = AllDatatypesModel.objects().first().data
253285

254-
for i in range(ord('a'), ord('a') + 14):
286+
for i in range(ord('a'), ord('a') + 15):
255287
self.assertEqual(input[chr(i)], output[chr(i)])

0 commit comments

Comments
 (0)