def test_slice(self) -> None: encoder = Serializer() val0 = slice(2) assert encoder.encode(val0) == SliceRep(type="slice", start=None, stop=2, step=None) val1 = slice(0, 2) assert encoder.encode(val1) == SliceRep(type="slice", start=0, stop=2, step=None) val2 = slice(0, 10, 2) assert encoder.encode(val2) == SliceRep(type="slice", start=0, stop=10, step=2) val3 = slice(0, None, 2) assert encoder.encode(val3) == SliceRep(type="slice", start=0, stop=None, step=2) val4 = slice(None, None, None) assert encoder.encode(val4) == SliceRep(type="slice", start=None, stop=None, step=None)
def test_list_circular(self) -> None: val: Sequence[Any] = [1, 2, 3] val.insert(2, val) encoder = Serializer() with pytest.raises(SerializationError): encoder.encode(val)
def test_dict_circular(self) -> None: val: Dict[Any, Any] = {float("nan"): [1, 2]} val[float("inf")] = val encoder = Serializer() with pytest.raises(SerializationError): encoder.encode(val)
def test_dataclass_Model_nested(self) -> None: v0 = SomeModel(p0=3, p1="b", p2=[4, 5, 6]) val = SomeDataClass(f0=2, f1=[1, 2, 3], f5=v0) encoder = Serializer() rep = encoder.encode(val) assert rep == ObjectRep( type="object", name="test_serialization.SomeDataClass", attributes=dict( f0=2, f1=[1, 2, 3], f2=None, f5=ObjectRefRep( type="object", name="test_serialization.SomeModel", id=v0.id, attributes=dict( p0=3, p1="b", p2=[4, 5, 6], ), ), ), ) assert encoder.buffers == []
def test_to_serializable(self) -> None: doc = Document() e = bde.TitleChangedEvent(doc, "title", "setter", "invoker") s = Serializer() r = s.encode(e) assert r == dict(kind=e.kind, title="title") assert s.buffers == []
def test_to_serializable(self) -> None: doc = Document() ref1 = OtherModel() ref2 = OtherModel() m = SomeModel(ref1=ref1, ref2=ref2) e = bde.RootAddedEvent(doc, m, "setter", "invoker") s = Serializer() r = s.encode(e) assert r == dict( kind=e.kind, model=ObjectRefRep( type="object", name="test_events__document.SomeModel", id=m.id, attributes=dict( ref1=ObjectRefRep( type="object", name="test_events__document.OtherModel", id=ref1.id, ), ref2=ObjectRefRep( type="object", name="test_events__document.OtherModel", id=ref2.id, ), ), ), ) assert s.buffers == []
def test_Model_circular(self) -> None: val0 = SomeModel(p0=10) val1 = SomeModel(p0=20, p3=val0) val2 = SomeModel(p0=30, p3=val1) val0.p3 = val2 encoder = Serializer() rep = encoder.encode(val2) assert rep == ObjectRefRep( type="object", name="test_serialization.SomeModel", id=val2.id, attributes=dict( p0=30, p3=ObjectRefRep( type="object", name="test_serialization.SomeModel", id=val1.id, attributes=dict( p0=20, p3=ObjectRefRep( type="object", name="test_serialization.SomeModel", id=val0.id, attributes=dict( p0=10, p3=Ref(id=val2.id), ), ), ), ), ), ) assert encoder.buffers == []
def test_ndarray_int64_uint64(self) -> None: val0 = np.array([-2**16], dtype="int64") val1 = np.array([2**16], dtype="uint64") val2 = np.array([-2**36], dtype="int64") val3 = np.array([2**36], dtype="uint64") encoder = Serializer() rep0 = encoder.encode(val0) rep1 = encoder.encode(val1) rep2 = encoder.encode(val2) rep3 = encoder.encode(val3) assert len(encoder.buffers) == 2 [buf0, buf1] = encoder.buffers assert rep0 == NDArrayRep( type="ndarray", array=BytesRep(type="bytes", data=buf0), order=sys.byteorder, shape=[1], dtype="int32", ) assert rep1 == NDArrayRep( type="ndarray", array=BytesRep(type="bytes", data=buf1), order=sys.byteorder, shape=[1], dtype="uint32", ) assert rep2 == NDArrayRep( type="ndarray", array=[-2**36], order=sys.byteorder, shape=[1], dtype="object", ) assert rep3 == NDArrayRep( type="ndarray", array=[2**36], order=sys.byteorder, shape=[1], dtype="object", )
def test_dict_empty(self) -> None: val = {} encoder = Serializer() rep = encoder.encode(val) assert rep == MapRep(type="map", entries=[]) assert encoder.buffers == []
def test_list_empty(self) -> None: val = [] encoder = Serializer() rep = encoder.encode(val) assert rep == [] assert encoder.buffers == []
def test_to_serializable(self) -> None: doc = Document() m = SomeModel() e = bde.RootRemovedEvent(doc, m, "setter", "invoker") s = Serializer() r = s.encode(e) assert r == dict(kind=e.kind, model=m.ref) assert s.buffers == []
def test_max_int(self, capsys: Capture) -> None: encoder = Serializer() rep = encoder.encode(2**64) assert rep == 2.0**64 assert isinstance(rep, float) out, err = capsys.readouterr() assert out == "" assert err == "" # "out of range integer may result in loss of precision"
def test_to_serializable(self) -> None: doc = Document() m = SomeModel() e = bde.ColumnsPatchedEvent(doc, m, "data", [1, 2], "setter", "invoker") s = Serializer() r = s.encode(e) assert r == dict(kind=e.kind, model=m.ref, attr="data", patches=[1, 2]) assert s.buffers == []
def test_bytes_base64(self) -> None: encoder = Serializer(deferred=False) val = bytes([0xFF, 0x00, 0x17, 0xFE, 0x00]) rep = encoder.encode(val) assert rep == BytesRep( type="bytes", data="/wAX/gA=", ) assert encoder.buffers == []
def to_json_rep(obj: Model) -> Dict[str, AnyRep]: serializer = Serializer() properties = obj.properties_with_values(include_defaults=True) attributes = { key: serializer.encode(val) for key, val in properties.items() } return dict(id=obj.id, **attributes)
def test_bytes(self) -> None: encoder = Serializer() val = bytes([0xFF, 0x00, 0x17, 0xFE, 0x00]) rep = encoder.encode(val) assert len(encoder.buffers) == 1 [buf] = encoder.buffers assert buf.data == val assert rep == BytesRep(type="bytes", data=buf)
def test_list(self) -> None: v0 = SomeProps(p0=2, p1="a", p2=[1, 2, 3]) v1 = SomeModel(p0=3, p1="b", p2=[4, 5, 6]) v2 = SomeDataClass(f0=2, f1=[1, 2, 3]) val = [ None, False, True, "abc", 1, 1.17, nan, -inf, +inf, v0, v1, v2, [nan] ] encoder = Serializer() rep = encoder.encode(val) assert rep == [ None, False, True, "abc", 1, 1.17, NumberRep(type="number", value="nan"), NumberRep(type="number", value="-inf"), NumberRep(type="number", value="+inf"), ObjectRep( type="object", name="test_serialization.SomeProps", attributes=dict( p0=2, p1="a", p2=[1, 2, 3], ), ), ObjectRefRep( type="object", name="test_serialization.SomeModel", id=v1.id, attributes=dict( p0=3, p1="b", p2=[4, 5, 6], ), ), ObjectRep( type="object", name="test_serialization.SomeDataClass", attributes=dict( f0=2, f1=[1, 2, 3], f2=None, ), ), [NumberRep(type="number", value="nan")], ] assert encoder.buffers == []
def test_to_serializable(self) -> None: doc = Document() m = SomeModel() e = bde.ColumnsStreamedEvent(doc, m, "data", dict(foo=1), 200, "setter", "invoker") s = Serializer() r = s.encode(e) assert r == dict(kind=e.kind, model=m.ref, attr="data", data=MapRep(type="map", entries=[("foo", 1)]), rollover=200) assert s.buffers == []
def test_dataclass(self) -> None: val = SomeDataClass(f0=2, f1=[1, 2, 3]) encoder = Serializer() rep = encoder.encode(val) assert rep == ObjectRep( type="object", name="test_serialization.SomeDataClass", attributes=dict( f0=2, f1=[1, 2, 3], f2=None, ), ) assert encoder.buffers == []
def test_HasProps(self) -> None: val = SomeProps(p0=2, p1="a", p2=[1, 2, 3]) encoder = Serializer() rep = encoder.encode(val) assert rep == ObjectRep( type="object", name="test_serialization.SomeProps", attributes=dict( p0=2, p1="a", p2=[1, 2, 3], ), ) assert encoder.buffers == []
def test_typed_array_base64(self) -> None: encoder = Serializer(deferred=False) val = TypedArray("i", [0, 1, 2, 3, 4, 5]) rep = encoder.encode(val) assert rep == TypedArrayRep( type="typed_array", array=BytesRep( type="bytes", data="AAAAAAEAAAACAAAAAwAAAAQAAAAFAAAA", ), order=sys.byteorder, dtype="int32", ) assert encoder.buffers == []
def test_ndarray_base64(self) -> None: encoder = Serializer(deferred=False) val = np.array([0, 1, 2, 3, 4, 5], dtype="int32") rep = encoder.encode(val) assert rep == NDArrayRep( type="ndarray", array=BytesRep( type="bytes", data="AAAAAAEAAAACAAAAAwAAAAQAAAAFAAAA", ), order=sys.byteorder, shape=[6], dtype="int32", ) assert encoder.buffers == []
def test_Model(self) -> None: val = SomeModel(p0=3, p1="b", p2=[4, 5, 6]) encoder = Serializer() rep = encoder.encode(val) assert rep == ObjectRefRep( type="object", name="test_serialization.SomeModel", id=val.id, attributes=dict( p0=3, p1="b", p2=[4, 5, 6], ), ) assert encoder.buffers == []
def test_primitive(self) -> None: encoder = Serializer() assert encoder.encode(None) is None assert encoder.encode(False) == False assert encoder.encode(True) == True assert encoder.encode("abc") == "abc" assert encoder.encode(1) == 1 assert encoder.encode(1.17) == 1.17 assert encoder.encode(nan) == NumberRep(type="number", value="nan") assert encoder.encode(-inf) == NumberRep(type="number", value="-inf") assert encoder.encode(+inf) == NumberRep(type="number", value="+inf") assert encoder.buffers == []
def test_pd_series(self, pd) -> None: encoder = Serializer() val = pd.Series([0, 1, 2, 3, 4, 5], dtype="int32") rep = encoder.encode(val) assert len(encoder.buffers) == 1 [buf] = encoder.buffers assert rep == NDArrayRep( type="ndarray", array=BytesRep(type="bytes", data=buf), order=sys.byteorder, shape=[6], dtype="int32", )
def test_typed_array(self) -> None: encoder = Serializer() val = TypedArray("i", [0, 1, 2, 3, 4, 5]) rep = encoder.encode(val) assert len(encoder.buffers) == 1 [buf] = encoder.buffers assert bytes(buf.data) == val.tobytes() assert rep == TypedArrayRep( type="typed_array", array=BytesRep(type="bytes", data=buf), order=sys.byteorder, dtype="int32", )
def test_to_serializable(self) -> None: doc = Document() m = SomeModel(data={"col0": [1], "col1": [1, 2], "col2": [1, 2, 3]}) e = bde.ColumnDataChangedEvent(doc, m, "data", None, ["col1", "col2"], "setter", "invoker") s = Serializer() r = s.encode(e) assert r == dict( kind=e.kind, model=m.ref, attr="data", data=MapRep(type="map", entries=[("col1", [1, 2]), ("col2", [1, 2, 3])]), cols=["col1", "col2"], ) assert s.buffers == []
def test_ndarray(self) -> None: encoder = Serializer() val = np.array([0, 1, 2, 3, 4, 5], dtype="int32") rep = encoder.encode(val) assert len(encoder.buffers) == 1 [buf] = encoder.buffers assert bytes(buf.data) == val.tobytes() assert rep == NDArrayRep( type="ndarray", array=BytesRep(type="bytes", data=buf), order=sys.byteorder, shape=[6], dtype="int32", )
def test_ndarray_object(self) -> None: @dataclass class X: f: int = 0 g: str = "a" val = np.array([[X()], [X(1)], [X(2, "b")]]) encoder = Serializer() rep = encoder.encode(val) assert rep == NDArrayRep( type="ndarray", array=[ ObjectRep( type="object", name= "test_serialization.TestSerializer.test_ndarray_object.X", attributes=dict(f=0, g="a"), ), ObjectRep( type="object", name= "test_serialization.TestSerializer.test_ndarray_object.X", attributes=dict(f=1, g="a"), ), ObjectRep( type="object", name= "test_serialization.TestSerializer.test_ndarray_object.X", attributes=dict(f=2, g="b"), ), ], order=sys.byteorder, shape=[3, 1], dtype="object", ) assert encoder.buffers == []
def test_dataclass_HasProps_nested(self) -> None: v0 = SomeProps(p0=2, p1="a", p2=[1, 2, 3]) val = SomeDataClass(f0=2, f1=[1, 2, 3], f4=v0) encoder = Serializer() rep = encoder.encode(val) assert rep == ObjectRep( type="object", name="test_serialization.SomeDataClass", attributes=dict( f0=2, f1=[1, 2, 3], f2=None, f4=ObjectRep( type="object", name="test_serialization.SomeProps", attributes=dict( p0=2, p1="a", p2=[1, 2, 3], ), ), ), ) assert encoder.buffers == []