Skip to content

Commit 676d874

Browse files
committed
Decimal serlz accepts primitive numeric types and tuple
PYTHON-468
1 parent 5542112 commit 676d874

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

cassandra/cqltypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def serialize(dec, protocol_version):
369369
try:
370370
sign, digits, exponent = dec.as_tuple()
371371
except AttributeError:
372-
raise TypeError("Non-Decimal type received for Decimal value")
372+
sign, digits, exponent = Decimal(dec).as_tuple()
373373
unscaled = int(''.join([str(digit) for digit in digits]))
374374
if sign:
375375
unscaled *= -1

tests/unit/test_marshalling.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
from cassandra.marshal import bitlength
15+
from cassandra.protocol import MAX_SUPPORTED_VERSION
1516

1617
try:
1718
import unittest2 as unittest
@@ -139,3 +140,11 @@ def test_bitlength(self):
139140
def test_date(self):
140141
# separate test because it will deserialize as datetime
141142
self.assertEqual(DateType.from_binary(DateType.to_binary(date(2015, 11, 2), 1), 1), datetime(2015, 11, 2))
143+
144+
def test_decimal(self):
145+
# testing implicit numeric conversion
146+
# int, float, tuple(sign, digits, exp)
147+
for proto_ver in range(1, MAX_SUPPORTED_VERSION + 1):
148+
for n in (10001, 100.1, (0, (1, 0, 0, 0, 0, 1), -3)):
149+
expected = Decimal(n)
150+
self.assertEqual(DecimalType.from_binary(DecimalType.to_binary(n, proto_ver), proto_ver), expected)

0 commit comments

Comments
 (0)