leftmost bit decimal bug
MODIFY
fastavro/six.py
fastavro/six.py
@@ -32,8 +32,8 @@ if sys.version_info >= (3, 0):
def py3_mk_bits(bits):
return bytes([bits & 0xff])
- def py3_identity(datum):
- return datum
+ def py3_bytes2ints(datum):
+ return list(datum)
def py3_fstint(datum):
return datum[0]
@@ -77,7 +77,7 @@ if sys.version_info >= (3, 0):
iteritems = py3_iteritems
is_str = py3_is_str
mk_bits = py3_mk_bits
- str2ints = py3_identity
+ str2ints = py3_bytes2ints
fstint = py3_fstint
else:
btou = py2_btou

MODIFY
tests/test_logical_types.py
tests/test_logical_types.py
@@ -2,9 +2,12 @@ import datetime
from decimal import Decimal
from io import BytesIO
-import fastavro
from nose.tools import raises
+import fastavro
+from fastavro.reader import read_bytes_decimal
+
+
schema = {
"fields": [
{
@@ -91,10 +94,41 @@ schema_top = {
def test_top():
data1 = Decimal("123.456")
binary = serialize(schema_top, data1)
+
+ data2 = deserialize(schema_top, binary)
+ assert (data1 == data2)
+
+
+def test_negative():
+ data1 = Decimal("-2.90")
+ binary = serialize(schema_top, data1)
+ data2 = deserialize(schema_top, binary)
+ assert (data1 == data2)
+
+
+def test_zero():
+ data1 = Decimal("0.0")
+ binary = serialize(schema_top, data1)
data2 = deserialize(schema_top, binary)
assert (data1 == data2)
+schema_leftmost = {
+ "name": "n",
+ "namespace": "namespace",
+ "type": "bytes",
+ "logicalType": "decimal",
+ "precision": 18,
+ "scale": 6,
+}
+
+
+def test_leftmost():
+ binary = b'\xd5F\x80'
+ res = read_bytes_decimal(binary, schema_leftmost)
+ assert (Decimal("-2.80") == res)
+
+
@raises(AssertionError)
def test_scale():
data1 = Decimal("123.456678") # does not fit scale

leftmost bit decimal bug