def test_schema_unpack_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])) # Note: It's up to the user to know - presumably using the defined schemas, that inner1 is a # Note: B3_COMPOSITE_DICT type, as the returned dict (outer_data) just has the encoded bytes in that field. outer_data = schema_unpack(OUTER_SCHEMA, test_outer_buf, 0, len(test_outer_buf)) inner_len = len(outer_data['inner1']) inner_data = schema_unpack(TEST_SCHEMA, outer_data['inner1'], 0, inner_len) assert inner_data == dict(bool1=False, number1=0, string1=u"")
def test_schema_unpack_unwanted_incoming_field(): bool2_buf = SBytes( "55 04 01 01") # a second B3_BOOL with key=4, len=1, value=True unwantfield_buf = test1_buf + bool2_buf out2_data = schema_unpack(TEST_SCHEMA, unwantfield_buf, 0, len(unwantfield_buf)) assert out2_data == test1
def test_schema_unpack_missing_incoming_field(): missing_fields_buf = SBytes( "97 01") # so only field 1 is present (and null) null_data = dict( bool1=None, number1=None, string1=None) # missing incoming fields get created and null-valued. assert schema_unpack(TEST_SCHEMA, missing_fields_buf, 0, len(missing_fields_buf)) == null_data
def test_schema_unpack_bytes_yield(): BYTES_SCHEMA = ((B3_BYTES, 'bytes1', 1), (B3_COMPOSITE_LIST, 'list1', 2)) bytes1_hex = "53 01 03 66 6f 6f" # b"foo" list1_hex = "52 02 03 66 6f 6f" # (actually just b"foo" as well, not an encoded list) test_buf = SBytes(" ".join([bytes1_hex, list1_hex])) test_data = dict(bytes1=b"foo", list1=b"foo") assert schema_unpack(BYTES_SCHEMA, test_buf, 0, len(test_buf)) == test_data
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_unpack_type_mismatch(): with pytest.raises(TypeError): mismatch_buf = SBytes( "17 01 14 02 14 03" ) # field 3 is a utf8 here (x14) when it should be a bool (x15) schema_unpack(TEST_SCHEMA, mismatch_buf, 0, len(mismatch_buf))
def test_schema_unpack_zero_data(): zero_buf = SBytes("17 01 14 02 15 03") zero_data = dict(bool1=False, number1=0, string1=u"") assert schema_unpack(TEST_SCHEMA, zero_buf, 0, len(zero_buf)) == zero_data
def test_schema_unpack_null_data(): null_buf = SBytes("97 01 94 02 95 03") null_data = dict(bool1=None, number1=None, string1=None) assert schema_unpack(TEST_SCHEMA, null_buf, 0, len(null_buf)) == null_data
def test_schema_unpack_dictcheck(): out1_data = schema_unpack(TEST_SCHEMA, test1_buf, 0, len(test1_buf)) assert isinstance(out1_data, dict)
def test_schema_unpack_nominal_data(): # "Happy path" out1_data = schema_unpack(TEST_SCHEMA, test1_buf, 0, len(test1_buf)) assert out1_data == test1