def test_schema_nested_errors(): # Nested containers e.g. dict1 and list1 need to be explicitely packed to bytes first. # schema_pack should raise an error if it sees un-packed python dicts or lists in the fields. NEST_SCHEMA = ( (B3_COMPOSITE_DICT, 'dict1', 1), (B3_COMPOSITE_LIST, 'list1', 2), ) data = dict(dict1={1: 2, 3: 4}, list1=[7, 8, 9]) with pytest.raises(TypeError): schema_pack(NEST_SCHEMA, data)
def test_schema_pack_nesting(): # Testing this buffer... bytes1_hex = "53 01 0a 6f 75 74 65 72 62 79 74 65 73" # header + 'outerbytes' signed1_hex = "58 02 02 a3 13" # header + encode_svarint(-1234) inner_buf_hex = "51 03 06 17 01 14 02 15 03" # header + buffer output from the zeroval test test_outer_buf = SBytes(" ".join([bytes1_hex, signed1_hex, inner_buf_hex])) # ...against this data inner_data = dict(bool1=False, number1=0, string1=u"") inner1 = schema_pack(TEST_SCHEMA, inner_data) outer_data = dict(bytes1=b"outerbytes", signed1=-1234, inner1=inner1) outer_buf = schema_pack(OUTER_SCHEMA, outer_data) assert outer_buf == test_outer_buf
def test_schema_pack_zeroval(): # Testing this buffer... number1_zero_header = "17 01" string1_zero_header = "14 02" bool1_zero_header = "15 03" buf_zv_hex = " ".join( [number1_zero_header, string1_zero_header, bool1_zero_header]) buf_zv = SBytes(buf_zv_hex) # ...against this data test_zv_data = dict(bool1=False, number1=0, string1=u"") buf = schema_pack(TEST_SCHEMA, test_zv_data) assert buf_zv == buf
def test_schema_pack_field_missing(): # Testing this buffer... bool1_header_null_value = u"95 03" # encode_header(B3_BOOL, key=3, is_null=True) bool1_nulled_hex = " ".join([ number1_header, number1_data, string1_header, string1_data, bool1_header_null_value ]) # note no data for bool1 bool1_nulled_buf = SBytes(bool1_nulled_hex) # ...against this data test2 = copy.copy(test1) del test2[ 'bool1'] # Missing field should be sent out as present but with a null value. buf = schema_pack(TEST_SCHEMA, test2) assert buf == bool1_nulled_buf
def test_schema_alltypes_roundtrip(): import time, math from decimal import Decimal from datetime import datetime ALLTYPES_SCHEMA = ( (B3_BYTES, 'bytes1', 3), (B3_UTF8, 'string1', 4), (B3_BOOL, 'bool1', 5), (B3_INT64, 'int641', 6), (B3_UVARINT, 'uvint1', 7), (B3_SVARINT, 'svint1', 8), (B3_FLOAT64, 'float1', 9), (B3_DECIMAL, 'deci1', 10), (B3_STAMP64, 'stamp1', 11), (B3_SCHED, 'date1', 12), (B3_COMPLEX, 'cplx1', 13), ) now_float = time.time( ) # the STAMP64 encoder takes time.time() floats OR unixnano integers now_ns = math.trunc( now_float * 1e9) # but it's decoder always yields unixnano integers. data = dict(bytes1=b"foo", string1=u"bar", bool1=True, int641=123, uvint1=456, svint1=-789, float1=13.37, deci1=Decimal("13.37"), stamp1=now_ns, date1=datetime.now(), cplx1=33j) buf = schema_pack(ALLTYPES_SCHEMA, data) out = schema_unpack(ALLTYPES_SCHEMA, buf, 0, len(buf)) assert data == out
def test_schema_pack_field_unwanted_strict(): test2 = copy.copy(test1) test2['unwanted_field'] = "hello" with pytest.raises(KeyError): schema_pack(TEST_SCHEMA, test2, strict=True)
def test_schema_pack_field_unwanted_ignore(): test2 = copy.copy(test1) test2['unwanted_field'] = "hello" buf = schema_pack(TEST_SCHEMA, test2) # aka strict=False assert buf == test1_buf # ensure unwanted field is not in result data.
def test_schema_pack_dictcheck(): with pytest.raises(TypeError): schema_pack(TEST_SCHEMA, [])
def test_schema_pack_nomimal_data(): # "Happy path" out1_buf = schema_pack(TEST_SCHEMA, test1) assert out1_buf == test1_buf