def test_dict_empty(self) -> None: val = {} encoder = Serializer() rep = encoder.encode(val) assert rep == MapRep(type="map", entries=[]) 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_dict(self) -> None: val = { float("nan"): { 1: [2, 3] }, "bcd": None, "abc": True, None: float("inf") } encoder = Serializer() rep = encoder.encode(val) assert rep == MapRep( type="map", entries=[ (NumberRep(type="number", value="nan"), MapRep(type="map", entries=[(1, [2, 3])])), ("bcd", None), ("abc", True), (None, NumberRep(type="number", value="+inf")), ], ) assert encoder.buffers == []
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_serialization_data_models(self) -> None: #obj0 = SomeDataModel() #obj1 = DerivedDataModel(prop6=obj0) #obj2 = CDSDerivedDataModel() #obj3 = CDSDerivedDerivedDataModel() doc = document.Document() #doc.add_root(obj0) #doc.add_root(obj1) #doc.add_root(obj2) #doc.add_root(obj3) json = doc.to_json() assert json["defs"] == [ ModelDef( type="model", name="test_document.SomeDataModel", properties=[ PropertyDef(name="prop0", kind="Any", default=0), PropertyDef(name="prop1", kind="Any", default=111), PropertyDef(name="prop2", kind="Any", default=[1, 2, 3]), ], ), ModelDef( type="model", name="test_document.DerivedDataModel", extends=Ref(id=ID("test_document.SomeDataModel")), properties=[ PropertyDef(name="prop3", kind="Any", default=0), PropertyDef(name="prop4", kind="Any", default=112), PropertyDef(name="prop5", kind="Any", default=[1, 2, 3, 4]), PropertyDef(name="prop6", kind="Any"), PropertyDef(name="prop7", kind="Any", default=None), ], overrides=[ OverrideDef(name="prop2", default=[4, 5, 6]), ], ), ModelDef( type="model", name="test_document.CDSDerivedDataModel", extends=Ref(id=ID("ColumnDataSource")), properties=[ PropertyDef(name="prop0", kind="Any", default=0), PropertyDef(name="prop1", kind="Any", default=111), PropertyDef(name="prop2", kind="Any", default=[1, 2, 3]), ], overrides=[ OverrideDef(name="data", default=MapRep(type="map", entries=[("default_column", [4, 5, 6])])), ], ), ModelDef( type="model", name="test_document.CDSDerivedDerivedDataModel", extends=Ref(id=ID("test_document.CDSDerivedDataModel")), properties=[ PropertyDef( name="prop3", kind="Any", default=ObjectRefRep( type="object", name="test_document.SomeDataModel", id=CDSDerivedDerivedDataModel.prop3.property. _default.ref["id"], attributes=dict(prop0=-1), ), ), ], overrides=[ OverrideDef(name="data", default=MapRep(type="map", entries=[("default_column", [7, 8, 9])])), ], ), ]