Skip to content

Commit 33e9a99

Browse files
committed
Remove unused CassandraType init, validate
1 parent 94b8f4d commit 33e9a99

3 files changed

Lines changed: 149 additions & 209 deletions

File tree

cassandra/cqltypes.py

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,9 @@ class _CassandraType(object):
213213
of EmptyValue) will be returned.
214214
"""
215215

216-
def __init__(self, val):
217-
self.val = self.validate(val)
218-
219216
def __repr__(self):
220217
return '<%s( %r )>' % (self.cql_parameterized_type(), self.val)
221218

222-
@staticmethod
223-
def validate(val):
224-
"""
225-
Called to transform an input value into one of a suitable type
226-
for this class. As an example, the BooleanType class uses this
227-
to convert an incoming value to True or False.
228-
"""
229-
return val
230-
231219
@classmethod
232220
def from_binary(cls, byts, protocol_version):
233221
"""
@@ -358,10 +346,6 @@ class BytesType(_CassandraType):
358346
typename = 'blob'
359347
empty_binary_ok = True
360348

361-
@staticmethod
362-
def validate(val):
363-
return bytearray(val)
364-
365349
@staticmethod
366350
def serialize(val, protocol_version):
367351
return six.binary_type(val)
@@ -370,10 +354,6 @@ def serialize(val, protocol_version):
370354
class DecimalType(_CassandraType):
371355
typename = 'decimal'
372356

373-
@staticmethod
374-
def validate(val):
375-
return Decimal(val)
376-
377357
@staticmethod
378358
def deserialize(byts, protocol_version):
379359
scale = int32_unpack(byts[:4])
@@ -412,10 +392,6 @@ def serialize(uuid, protocol_version):
412392
class BooleanType(_CassandraType):
413393
typename = 'boolean'
414394

415-
@staticmethod
416-
def validate(val):
417-
return bool(val)
418-
419395
@staticmethod
420396
def deserialize(byts, protocol_version):
421397
return bool(int8_unpack(byts))
@@ -556,14 +532,9 @@ class CounterColumnType(LongType):
556532
class DateType(_CassandraType):
557533
typename = 'timestamp'
558534

559-
@classmethod
560-
def validate(cls, val):
561-
if isinstance(val, six.string_types):
562-
val = cls.interpret_datestring(val)
563-
return val
564-
565535
@staticmethod
566536
def interpret_datestring(val):
537+
# not used internally. deprecate?
567538
if val[-5] in ('+', '-'):
568539
offset = (int(val[-4:-2]) * 3600 + int(val[-2:]) * 60) * int(val[-5] + '1')
569540
val = val[:-5]
@@ -579,9 +550,6 @@ def interpret_datestring(val):
579550
else:
580551
raise ValueError("can't interpret %r as a date" % (val,))
581552

582-
def my_timestamp(self):
583-
return self.val
584-
585553
@staticmethod
586554
def deserialize(byts, protocol_version):
587555
timestamp = int64_unpack(byts) / 1000.0
@@ -633,12 +601,6 @@ class SimpleDateType(_CassandraType):
633601
# range (2^31).
634602
EPOCH_OFFSET_DAYS = 2 ** 31
635603

636-
@classmethod
637-
def validate(cls, val):
638-
if not isinstance(val, util.Date):
639-
val = util.Date(val)
640-
return val
641-
642604
@staticmethod
643605
def deserialize(byts, protocol_version):
644606
days = uint32_unpack(byts) - SimpleDateType.EPOCH_OFFSET_DAYS
@@ -668,12 +630,6 @@ def serialize(byts, protocol_version):
668630
class TimeType(_CassandraType):
669631
typename = 'time'
670632

671-
@classmethod
672-
def validate(cls, val):
673-
if not isinstance(val, util.Time):
674-
val = util.Time(val)
675-
return val
676-
677633
@staticmethod
678634
def deserialize(byts, protocol_version):
679635
return util.Time(int64_unpack(byts))
@@ -709,11 +665,6 @@ class VarcharType(UTF8Type):
709665

710666

711667
class _ParameterizedType(_CassandraType):
712-
def __init__(self, val):
713-
if not self.subtypes:
714-
raise ValueError("%s type with no parameters can't be instantiated" % (self.typename,))
715-
_CassandraType.__init__(self, val)
716-
717668
@classmethod
718669
def deserialize(cls, byts, protocol_version):
719670
if not cls.subtypes:
@@ -730,11 +681,6 @@ def serialize(cls, val, protocol_version):
730681

731682

732683
class _SimpleParameterizedType(_ParameterizedType):
733-
@classmethod
734-
def validate(cls, val):
735-
subtype, = cls.subtypes
736-
return cls.adapter([subtype.validate(subval) for subval in val])
737-
738684
@classmethod
739685
def deserialize_safe(cls, byts, protocol_version):
740686
subtype, = cls.subtypes
@@ -787,11 +733,6 @@ class MapType(_ParameterizedType):
787733
typename = 'map'
788734
num_subtypes = 2
789735

790-
@classmethod
791-
def validate(cls, val):
792-
key_type, value_type = cls.subtypes
793-
return dict((key_type.validate(k), value_type.validate(v)) for (k, v) in six.iteritems(val))
794-
795736
@classmethod
796737
def deserialize_safe(cls, byts, protocol_version):
797738
key_type, value_type = cls.subtypes

tests/unit/test_types.py

Lines changed: 1 addition & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ def test_lookup_casstype(self):
105105

106106
self.assertRaises(ValueError, lookup_casstype, 'AsciiType~')
107107

108-
# TODO: Do a few more tests
109-
# "I would say some parameterized and nested types would be good to test,
110-
# like "MapType(AsciiType, IntegerType)" and "ReversedType(AsciiType)"
111-
self.assertEqual(str(lookup_casstype(BooleanType(True))), str(BooleanType(True)))
112-
113108
def test_casstype_parameterized(self):
114109
self.assertEqual(LongType.cass_parameterized_type_with(()), 'LongType')
115110
self.assertEqual(LongType.cass_parameterized_type_with((), full=True), 'org.apache.cassandra.db.marshal.LongType')
@@ -125,134 +120,7 @@ def test_datetype_from_string(self):
125120
# Ensure all formats can be parsed, without exception
126121
for format in cassandra.cqltypes.cql_timestamp_formats:
127122
date_string = str(datetime.datetime.now().strftime(format))
128-
cassandra.cqltypes.DateType(date_string)
129-
130-
def test_simpledate(self):
131-
"""
132-
Test cassandra.cqltypes.SimpleDateType() construction
133-
"""
134-
Date = cassandra.util.Date
135-
# from datetime
136-
expected_dt = datetime.datetime(1492, 10, 12, 1, 1)
137-
expected_date = Date(expected_dt)
138-
self.assertEqual(str(expected_date), '1492-10-12')
139-
140-
# from string
141-
sd = SimpleDateType('1492-10-12')
142-
self.assertEqual(sd.val, expected_date)
143-
sd = SimpleDateType('+1492-10-12')
144-
self.assertEqual(sd.val, expected_date)
145-
146-
# Date
147-
sd = SimpleDateType(expected_date)
148-
self.assertEqual(sd.val, expected_date)
149-
150-
# date
151-
sd = SimpleDateType(datetime.date(expected_dt.year, expected_dt.month, expected_dt.day))
152-
self.assertEqual(sd.val, expected_date)
153-
154-
# days
155-
sd = SimpleDateType(0)
156-
self.assertEqual(sd.val, Date(datetime.date(1970, 1, 1)))
157-
sd = SimpleDateType(-1)
158-
self.assertEqual(sd.val, Date(datetime.date(1969, 12, 31)))
159-
sd = SimpleDateType(1)
160-
self.assertEqual(sd.val, Date(datetime.date(1970, 1, 2)))
161-
# limits
162-
min_builtin = Date(datetime.date(1, 1, 1))
163-
max_builtin = Date(datetime.date(9999, 12, 31))
164-
self.assertEqual(SimpleDateType(min_builtin.days_from_epoch).val, min_builtin)
165-
self.assertEqual(SimpleDateType(max_builtin.days_from_epoch).val, max_builtin)
166-
# just proving we can construct with on offset outside buildin range
167-
self.assertEqual(SimpleDateType(min_builtin.days_from_epoch - 1).val.days_from_epoch,
168-
min_builtin.days_from_epoch - 1)
169-
self.assertEqual(SimpleDateType(max_builtin.days_from_epoch + 1).val.days_from_epoch,
170-
max_builtin.days_from_epoch + 1)
171-
172-
# no contruct
173-
self.assertRaises(ValueError, SimpleDateType, '-1999-10-10')
174-
self.assertRaises(TypeError, SimpleDateType, 1.234)
175-
176-
# str
177-
date_str = '2015-03-16'
178-
self.assertEqual(str(Date(date_str)), date_str)
179-
# out of range
180-
self.assertEqual(str(Date(2932897)), '2932897')
181-
self.assertEqual(repr(Date(1)), 'Date(1)')
182-
183-
# eq other types
184-
self.assertEqual(Date(1234), 1234)
185-
self.assertEqual(Date(1), datetime.date(1970, 1, 2))
186-
self.assertFalse(Date(2932897) == datetime.date(9999, 12, 31)) # date can't represent year > 9999
187-
self.assertEqual(Date(2932897), 2932897)
188-
189-
def test_time(self):
190-
"""
191-
Test cassandra.cqltypes.TimeType() construction
192-
"""
193-
Time = cassandra.util.Time
194-
one_micro = 1000
195-
one_milli = 1000 * one_micro
196-
one_second = 1000 * one_milli
197-
one_minute = 60 * one_second
198-
one_hour = 60 * one_minute
199-
200-
# from strings
201-
tt = TimeType('00:00:00.000000001')
202-
self.assertEqual(tt.val, 1)
203-
tt = TimeType('00:00:00.000001')
204-
self.assertEqual(tt.val, one_micro)
205-
tt = TimeType('00:00:00.001')
206-
self.assertEqual(tt.val, one_milli)
207-
tt = TimeType('00:00:01')
208-
self.assertEqual(tt.val, one_second)
209-
tt = TimeType('00:01:00')
210-
self.assertEqual(tt.val, one_minute)
211-
tt = TimeType('01:00:00')
212-
self.assertEqual(tt.val, one_hour)
213-
tt = TimeType('01:00:00.')
214-
self.assertEqual(tt.val, one_hour)
215-
216-
tt = TimeType('23:59:59.1')
217-
tt = TimeType('23:59:59.12')
218-
tt = TimeType('23:59:59.123')
219-
tt = TimeType('23:59:59.1234')
220-
tt = TimeType('23:59:59.12345')
221-
222-
tt = TimeType('23:59:59.123456')
223-
self.assertEqual(tt.val, 23 * one_hour + 59 * one_minute + 59 * one_second + 123 * one_milli + 456 * one_micro)
224-
225-
tt = TimeType('23:59:59.1234567')
226-
self.assertEqual(tt.val, 23 * one_hour + 59 * one_minute + 59 * one_second + 123 * one_milli + 456 * one_micro + 700)
227-
228-
tt = TimeType('23:59:59.12345678')
229-
self.assertEqual(tt.val, 23 * one_hour + 59 * one_minute + 59 * one_second + 123 * one_milli + 456 * one_micro + 780)
230-
231-
tt = TimeType('23:59:59.123456789')
232-
self.assertEqual(tt.val, 23 * one_hour + 59 * one_minute + 59 * one_second + 123 * one_milli + 456 * one_micro + 789)
233-
234-
# from int
235-
tt = TimeType(12345678)
236-
self.assertEqual(tt.val, 12345678)
237-
238-
# from time
239-
expected_time = datetime.time(12, 1, 2, 3)
240-
tt = TimeType(expected_time)
241-
self.assertEqual(tt.val, expected_time)
242-
243-
# util.Time self equality
244-
self.assertEqual(Time(1234), Time(1234))
245-
246-
# str
247-
time_str = '12:13:14.123456789'
248-
self.assertEqual(str(Time(time_str)), time_str)
249-
self.assertEqual(repr(Time(1)), 'Time(1)')
250-
251-
# no construct
252-
self.assertRaises(ValueError, TimeType, '1999-10-10 11:11:11.1234')
253-
self.assertRaises(TypeError, TimeType, 1.234)
254-
self.assertRaises(ValueError, TimeType, 123456789000000)
255-
self.assertRaises(TypeError, TimeType, datetime.datetime(2004, 12, 23, 11, 11, 1))
123+
cassandra.cqltypes.DateType.interpret_datestring(date_string)
256124

257125
def test_cql_typename(self):
258126
"""
@@ -309,12 +177,6 @@ class BarType(FooType):
309177
def test_empty_value(self):
310178
self.assertEqual(str(EmptyValue()), 'EMPTY')
311179

312-
def test_cassandratype_base(self):
313-
cassandra_type = _CassandraType('randomvaluetocheck')
314-
self.assertEqual(cassandra_type.val, 'randomvaluetocheck')
315-
self.assertEqual(cassandra_type.validate('randomvaluetocheck2'), 'randomvaluetocheck2')
316-
self.assertEqual(cassandra_type.val, 'randomvaluetocheck')
317-
318180
def test_datetype(self):
319181
now_time_seconds = time.time()
320182
now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)
@@ -325,14 +187,6 @@ def test_datetype(self):
325187
# same results serialized
326188
self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0))
327189

328-
# from timestamp
329-
date_type = DateType(now_timestamp)
330-
self.assertEqual(date_type.my_timestamp(), now_timestamp)
331-
332-
# from datetime object
333-
date_type = DateType(now_datetime)
334-
self.assertEqual(date_type.my_timestamp(), now_datetime)
335-
336190
# deserialize
337191
# epoc
338192
expected = 0
@@ -346,8 +200,6 @@ def test_datetype(self):
346200
expected = -770172256
347201
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))
348202

349-
self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
350-
351203
# work around rounding difference among Python versions (PYTHON-230)
352204
expected = 1424817268.274
353205
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))

0 commit comments

Comments
 (0)