def json_to_packstream(cls, data): """ This converts from JSON format into PackStream prior to proper hydration. This code needs to die horribly in a freak yachting accident. """ # TODO: other partial hydration if "self" in data: if "type" in data: return Structure(ord(b"R"), cls._uri_to_id(data["self"]), cls._uri_to_id(data["start"]), cls._uri_to_id(data["end"]), data["type"], data["data"]) else: return Structure(ord(b"N"), cls._uri_to_id(data["self"]), data["metadata"]["labels"], data["data"]) elif "nodes" in data and "relationships" in data: nodes = [ Structure(ord(b"N"), i, None, None) for i in map(cls._uri_to_id, data["nodes"]) ] relps = [ Structure(ord(b"r"), i, None, None) for i in map(cls._uri_to_id, data["relationships"]) ] seq = [i // 2 + 1 for i in range(2 * len(data["relationships"]))] for i, direction in enumerate(data["directions"]): if direction == "<-": seq[2 * i] *= -1 return Structure(ord(b"P"), nodes, relps, seq) else: # from warnings import warn # warn("Map literals returned over the Neo4j HTTP interface are ambiguous " # "and may be unintentionally hydrated as graph objects") return data
def test_extra_large_struct(): fields = [0] * 16 s = Structure(0x7F, *fields) with raises(ValueError): pack_and_unpack(s)
def test_struct(): for n in range(16): fields = [0] * n s = Structure(0x7F, *fields) data = bytearray([0xB0 + n, 0x7F]) + b"\x00" * n assert_packable(s, data)
def test_3d_point(cls, srid): b, unpacked = pack_and_unpack(cls((0, 0, 0)), version=(2, 0)) assert b == b"\xB4Y" + pack(srid) + b"\x00\x00\x00" assert unpacked == Structure(ord(b"Y"), srid, 0, 0, 0)
def test_datetime_with_timezone_offset(cls): b, unpacked = pack_and_unpack(cls(1970, 1, 1, 0, 0, 0, tzinfo=FixedOffset(1)), version=(2, 0)) assert b == b"\xB3F\x00\x00\x3C" assert unpacked == Structure(ord(b"F"), 0, 0, 60)
def test_timedelta_and_duration(cls): b, unpacked = pack_and_unpack(cls(), version=(2, 0)) assert b == b"\xB4E\x00\x00\x00\x00" assert unpacked == Structure(ord(b"E"), 0, 0, 0, 0)
def test_datetime_with_named_timezone(cls): b, unpacked = pack_and_unpack(cls(1970, 1, 1, 0, 0, 0, tzinfo=utc), version=(2, 0)) assert b == b"\xB3f\x00\x00\x83UTC" assert unpacked == Structure(ord(b"f"), 0, 0, "UTC")
def test_naive_datetime(cls): b, unpacked = pack_and_unpack(cls(1970, 1, 1, 0, 0, 0), version=(2, 0)) assert b == b"\xB2d\x00\x00" assert unpacked == Structure(ord(b"d"), 0, 0)
def test_aware_time(cls): b, unpacked = pack_and_unpack(cls(0, 0, 0, tzinfo=utc), version=(2, 0)) assert b == b"\xB2T\x00\x00" assert unpacked == Structure(ord(b"T"), 0, 0)
def test_naive_time(cls): b, unpacked = pack_and_unpack(cls(0, 0, 0), version=(2, 0)) assert b == b"\xB1t\x00" assert unpacked == Structure(ord(b"t"), 0)
def test_date(cls): b, unpacked = pack_and_unpack(cls(1970, 1, 1), version=(2, 0)) assert b == b"\xB1D\x00" assert unpacked == Structure(ord(b"D"), 0)