def test_data_types_v3(): grid = hszinc.Grid(version=hszinc.VER_3_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'A NA', 'value': hszinc.NA, }, { 'comment': 'An empty list', 'value': [], }, { 'comment': 'A null value in a list', 'value': [None], }, { 'comment': 'A marker in a list', 'value': [hszinc.MARKER], }, { 'comment': 'Booleans', 'value': [True, False], }, { 'comment': 'References', 'value': [hszinc.Ref('a-ref'), hszinc.Ref('a-ref', 'a value')], }, { 'comment': 'A quantity', 'value': [hszinc.Quantity(500, 'miles')], }, { 'comment': 'A XStr', 'value': [hszinc.XStr("hex", 'deadbeef')], }, ]) grid_str = hszinc.dump(grid) ref_str = '''ver:"3.0" comment,value "A NA",NA "An empty list",[] "A null value in a list",[N] "A marker in a list",[M] "Booleans",[T,F] "References",[@a-ref,@a-ref "a value"] "A quantity",[500miles] "A XStr",[hex("deadbeef")] ''' assert grid_str == ref_str
def test_xstr_b64(): assert XStr("b64", '3q2+7w==').data == b'\xde\xad\xbe\xef' barray = bytearray(random.getrandbits(8) for _ in range(10)) assert barray == hszinc.XStr("b64", binascii.b2a_base64(barray)).data
def test_xstr_other(): assert (XStr("other", "hello word").data == "hello word") barray = bytearray(random.getrandbits(8) for _ in range(10)) assert barray == hszinc.XStr("other", barray).data
def test_xstr_hex(): assert XStr("hex", "deadbeef").data == b'\xde\xad\xbe\xef' barray = bytearray(random.getrandbits(8) for _ in range(10)) assert barray == hszinc.XStr("hex", binascii.hexlify(barray).decode("ascii")).data
def test_data_types_json_v3(): grid = hszinc.Grid(version=hszinc.VER_3_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'A Remove (3.0 version)', 'value': hszinc.REMOVE, }, { 'comment': 'A NA', 'value': hszinc.NA, }, { 'comment': 'An empty list', 'value': [], }, { 'comment': 'A null value in a list', 'value': [None], }, { 'comment': 'A marker in a list', 'value': [hszinc.MARKER], }, { 'comment': 'Booleans', 'value': [True, False], }, { 'comment': 'References', 'value': [hszinc.Ref('a-ref'), hszinc.Ref('a-ref', 'a value')], }, { 'comment': 'A quantity', 'value': [hszinc.Quantity(500, 'miles')], }, { 'comment': 'A XStr', 'value': [hszinc.XStr("hex", 'deadbeef')], }, ]) grid_json = json.loads(hszinc.dump(grid, mode=hszinc.MODE_JSON)) assert grid_json == { 'meta': { 'ver': '3.0' }, 'cols': [ { 'name': 'comment' }, { 'name': 'value' }, ], 'rows': [ { 'comment': 's:A Remove (3.0 version)', 'value': '-:' }, { 'comment': 's:A NA', 'value': 'z:' }, { 'comment': "s:An empty list", 'value': [] }, { 'comment': "s:A null value in a list", 'value': [None] }, { 'comment': "s:A marker in a list", 'value': ['m:'] }, { 'comment': "s:Booleans", 'value': [True, False] }, { 'comment': "s:References", 'value': ['r:a-ref', 'r:a-ref a value'] }, { 'comment': "s:A quantity", 'value': ['n:500.000000 miles'] # Python is more precise # than The Proclaimers }, { 'comment': "s:A XStr", 'value': ['x:hex:deadbeef'] } ] }