Skip to content
This repository was archived by the owner on Aug 5, 2020. It is now read-only.

Commit 1ba07f1

Browse files
committed
Merge pull request apache#334 from kishkaru/master
Fix failing cqlengine tests related to protocol v4 datatypes
2 parents 8412d86 + c0b8c13 commit 1ba07f1

4 files changed

Lines changed: 197 additions & 71 deletions

File tree

tests/integration/cqlengine/columns/test_validation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from datetime import datetime, timedelta, date, tzinfo
1616
from decimal import Decimal as D
17-
from unittest import TestCase
17+
from unittest import TestCase, SkipTest
1818
from uuid import uuid4, uuid1
1919

2020
from cassandra import InvalidRequest
@@ -34,6 +34,7 @@
3434
from cassandra.cqlengine.models import Model, ValidationError
3535
from cassandra import util
3636

37+
from tests.integration import PROTOCOL_VERSION
3738
from tests.integration.cqlengine.base import BaseCassEngTestCase
3839

3940

@@ -153,6 +154,9 @@ class DateTest(Model):
153154

154155
@classmethod
155156
def setUpClass(cls):
157+
if PROTOCOL_VERSION < 4:
158+
raise SkipTest("Protocol v4 datatypes require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
159+
156160
super(TestDate, cls).setUpClass()
157161
sync_table(cls.DateTest)
158162

tests/integration/cqlengine/columns/test_value_io.py

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

15-
from datetime import datetime, timedelta
15+
from datetime import datetime, timedelta, time
1616
from decimal import Decimal
1717
from uuid import uuid1, uuid4, UUID
1818
import six
19+
import unittest
1920

2021
from cassandra.cqlengine import columns
2122
from cassandra.cqlengine.management import sync_table
2223
from cassandra.cqlengine.management import drop_table
2324
from cassandra.cqlengine.models import Model
24-
from cassandra import util
2525

26+
from cassandra.util import Date, Time
27+
28+
from tests.integration import PROTOCOL_VERSION
2629
from tests.integration.cqlengine.base import BaseCassEngTestCase
2730

2831

@@ -55,10 +58,10 @@ def setUpClass(cls):
5558

5659
# create a table with the given column
5760
class IOTestModel(Model):
58-
5961
table_name = cls.column.db_type + "_io_test_model_{}".format(uuid4().hex[:8])
6062
pkey = cls.column(primary_key=True)
6163
data = cls.column()
64+
6265
cls._generated_model = IOTestModel
6366
sync_table(cls._generated_model)
6467

@@ -149,11 +152,18 @@ class TestDateTime(BaseColumnIOTest):
149152

150153
class TestDate(BaseColumnIOTest):
151154

155+
@classmethod
156+
def setUpClass(cls):
157+
if PROTOCOL_VERSION < 4:
158+
raise unittest.SkipTest("Protocol v4 datatypes require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
159+
160+
super(TestDate, cls).setUpClass()
161+
152162
column = columns.Date
153163

154-
now = util.Date(datetime.now().date())
164+
now = Date(datetime.now().date())
155165
pkey_val = now
156-
data_val = util.Date(now.days_from_epoch + 1)
166+
data_val = Date(now.days_from_epoch + 1)
157167

158168

159169
class TestUUID(BaseColumnIOTest):
@@ -210,3 +220,46 @@ class TestDecimalIO(BaseColumnIOTest):
210220
def comparator_converter(self, val):
211221
return Decimal(val)
212222

223+
class TestTime(BaseColumnIOTest):
224+
225+
@classmethod
226+
def setUpClass(cls):
227+
if PROTOCOL_VERSION < 4:
228+
raise unittest.SkipTest("Protocol v4 datatypes require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
229+
230+
super(TestTime, cls).setUpClass()
231+
232+
column = columns.Time
233+
234+
pkey_val = Time(time(2, 12, 7, 48))
235+
data_val = Time(time(16, 47, 25, 7))
236+
237+
238+
class TestSmallInt(BaseColumnIOTest):
239+
240+
@classmethod
241+
def setUpClass(cls):
242+
if PROTOCOL_VERSION < 4:
243+
raise unittest.SkipTest("Protocol v4 datatypes require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
244+
245+
super(TestSmallInt, cls).setUpClass()
246+
247+
column = columns.SmallInt
248+
249+
pkey_val = 16768
250+
data_val = 32523
251+
252+
253+
class TestTinyInt(BaseColumnIOTest):
254+
255+
@classmethod
256+
def setUpClass(cls):
257+
if PROTOCOL_VERSION < 4:
258+
raise unittest.SkipTest("Protocol v4 datatypes require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
259+
260+
super(TestTinyInt, cls).setUpClass()
261+
262+
column = columns.TinyInt
263+
264+
pkey_val = 1
265+
data_val = 123

tests/integration/cqlengine/model/test_model_io.py

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from uuid import uuid4, UUID
1616
import random
17+
import unittest
1718
from datetime import datetime, date, time
1819
from decimal import Decimal
1920
from operator import itemgetter
@@ -25,6 +26,7 @@
2526
from cassandra.cqlengine.models import Model
2627
from cassandra.util import Date, Time
2728

29+
from tests.integration import PROTOCOL_VERSION
2830
from tests.integration.cqlengine.base import BaseCassEngTestCase
2931

3032
class TestModel(Model):
@@ -148,40 +150,72 @@ class AllDatatypesModel(Model):
148150
b = columns.BigInt()
149151
c = columns.Blob()
150152
d = columns.Boolean()
151-
e = columns.Date()
152-
f = columns.DateTime()
153-
g = columns.Decimal()
154-
h = columns.Double()
155-
i = columns.Float(double_precision=False)
156-
j = columns.Inet()
157-
k = columns.Integer()
158-
l = columns.SmallInt()
159-
m = columns.Text()
160-
n = columns.Time()
161-
o = columns.TimeUUID()
162-
p = columns.TinyInt()
163-
q = columns.UUID()
164-
r = columns.VarInt()
153+
e = columns.DateTime()
154+
f = columns.Decimal()
155+
g = columns.Double()
156+
h = columns.Float(double_precision=False)
157+
i = columns.Inet()
158+
j = columns.Integer()
159+
k = columns.Text()
160+
l = columns.TimeUUID()
161+
m = columns.UUID()
162+
n = columns.VarInt()
165163

166164
sync_table(AllDatatypesModel)
167165

168-
input = ['ascii', 2 ** 63 - 1, bytearray(b'hello world'), True, Date(date(1970, 1, 1)),
169-
datetime.utcfromtimestamp(872835240), Decimal('12.3E+7'), 2.39,
170-
3.4028234663852886e+38, '123.123.123.123', 2147483647, 32523, 'text', Time(time(16, 47, 25, 7)),
171-
UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), 123, UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'),
166+
input = ['ascii', 2 ** 63 - 1, bytearray(b'hello world'), True, datetime.utcfromtimestamp(872835240),
167+
Decimal('12.3E+7'), 2.39, 3.4028234663852886e+38, '123.123.123.123', 2147483647, 'text',
168+
UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'),
172169
int(str(2147483647) + '000')]
173170

174-
AllDatatypesModel.create(id=0, a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True, e=date(1970, 1, 1),
175-
f=datetime.utcfromtimestamp(872835240), g=Decimal('12.3E+7'), h=2.39,
176-
i=3.4028234663852886e+38, j='123.123.123.123', k=2147483647, l=32523, m='text',
177-
n=time(16, 47, 25, 7), o=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
178-
p=123, q=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'),
179-
r=int(str(2147483647) + '000'))
171+
AllDatatypesModel.create(id=0, a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True,
172+
e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39,
173+
h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text',
174+
l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
175+
m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'))
180176

181177
self.assertEqual(1, AllDatatypesModel.objects.count())
182178
output = AllDatatypesModel.objects().first()
183179

184-
for i, i_char in enumerate(range(ord('a'), ord('a') + 15)):
180+
for i, i_char in enumerate(range(ord('a'), ord('a') + 14)):
181+
self.assertEqual(input[i], output[chr(i_char)])
182+
183+
def test_can_insert_model_with_all_protocol_v4_column_types(self):
184+
"""
185+
Test for inserting all protocol v4 column types into a Model
186+
187+
test_can_insert_model_with_all_protocol_v4_column_types tests that each cqlengine protocol v4 column type can be
188+
inserted into a Model. It first creates a Model that has each cqlengine protocol v4 column type. It then creates
189+
a Model instance where all the fields have corresponding data, which performs the insert into the Cassandra table.
190+
Finally, it verifies that each column read from the Model from Cassandra is the same as the input parameters.
191+
192+
@since 2.6.0
193+
@jira_ticket PYTHON-245
194+
@expected_result The Model is inserted with each protocol v4 column type, and the resulting read yields proper data for each column.
195+
196+
@test_category data_types:primitive
197+
"""
198+
199+
if PROTOCOL_VERSION < 4:
200+
raise unittest.SkipTest("Protocol v4 datatypes require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
201+
202+
class AllDatatypesModel(Model):
203+
id = columns.Integer(primary_key=True)
204+
a = columns.Date()
205+
b = columns.SmallInt()
206+
c = columns.Time()
207+
d = columns.TinyInt()
208+
209+
sync_table(AllDatatypesModel)
210+
211+
input = [Date(date(1970, 1, 1)), 32523, Time(time(16, 47, 25, 7)), 123]
212+
213+
AllDatatypesModel.create(id=0, a=date(1970, 1, 1), b=32523, c=time(16, 47, 25, 7), d=123)
214+
215+
self.assertEqual(1, AllDatatypesModel.objects.count())
216+
output = AllDatatypesModel.objects().first()
217+
218+
for i, i_char in enumerate(range(ord('a'), ord('a') + 3)):
185219
self.assertEqual(input[i], output[chr(i_char)])
186220

187221
def test_can_insert_double_and_float(self):
@@ -399,6 +433,9 @@ class TestQuerying(BaseCassEngTestCase):
399433

400434
@classmethod
401435
def setUpClass(cls):
436+
if PROTOCOL_VERSION < 4:
437+
raise unittest.SkipTest("Date query tests require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))
438+
402439
super(TestQuerying, cls).setUpClass()
403440
drop_table(TestQueryModel)
404441
sync_table(TestQueryModel)

0 commit comments

Comments
 (0)