def test_dates(): types = AllTypes() # strong type types.date_property = datetime.fromtimestamp(123 / 1000.0, tz=timezone.utc) assert types.date_property == datetime.fromtimestamp(123 / 1000.0, tz=timezone.utc) # weak type types.any_property = datetime.fromtimestamp(999 / 1000.0, tz=timezone.utc) assert types.any_property == datetime.fromtimestamp(999 / 1000.0, tz=timezone.utc)
def test_collectionTypes(): types = AllTypes() # array types.array_property = ["Hello", "World"] assert types.array_property[1] == "World" # map map_ = {} map_["Foo"] = Number(123) types.map_property = map_
def test_creationOfNativeObjectsFromJavaScriptObjects(): """ See that we can create a native object, pass it JS and then unmarshal back without type information. """ types = AllTypes() js_obj = Number(44) types.any_property = js_obj unmarshalled_js_obj = types.any_property assert unmarshalled_js_obj.__class__ == Number native_obj = AddTen(10) types.any_property = native_obj result1 = types.any_property assert result1 is native_obj native_obj2 = MulTen(20) types.any_property = native_obj2 unmarshalled_native_obj = types.any_property assert unmarshalled_native_obj.__class__ == MulTen
def test_unionTypes(): types = AllTypes() # single valued property types.union_property = 1234 assert types.union_property == 1234 types.union_property = "Hello" assert types.union_property == "Hello" types.union_property = Multiply(Number(2), Number(12)) assert types.union_property.value == 24 # map map_ = {} map_["Foo"] = Number(99) types.union_map_property = map_ # TODO: No Assertion? # array types.union_array_property = [123, Number(33)] assert types.union_array_property[1].value == 33
def test_primitiveTypes(): types = AllTypes() # boolean types.boolean_property = True assert types.boolean_property # string types.string_property = "foo" assert types.string_property == "foo" # number types.number_property = 1234 assert types.number_property == 1234 # date types.date_property = datetime.fromtimestamp(123 / 1000.0, tz=timezone.utc) assert types.date_property == datetime.fromtimestamp(123 / 1000.0, tz=timezone.utc) # json types.json_property = { "Foo": { "bar": 123 } } assert types.json_property.get("Foo") == { "bar": 123 }
def test_dynamicTypes(): types = AllTypes() # boolean types.any_property = False assert not types.any_property # string types.any_property = "String" assert types.any_property == "String" # number types.any_property = 12 assert types.any_property == 12 # date types.any_property = datetime.fromtimestamp(1234 / 1000.0, tz=timezone.utc) assert types.any_property == datetime.fromtimestamp(1234 / 1000.0, tz=timezone.utc) # json (notice that when deserialized, it is deserialized as a map). types.any_property = {"Goo": ["Hello", {"World": 123}]} assert types.any_property.get("Goo")[1].get("World") == 123 # array types.any_property = ["Hello", "World"] assert types.any_property[0] == "Hello" assert types.any_property[1] == "World" # array of any types.any_array_property = ["Hybrid", Number(12), 123, False] assert types.any_array_property[2] == 123 # map map_ = {} map_["MapKey"] = "MapValue" types.any_property = map_ assert types.any_property.get("MapKey") == "MapValue" # map of any map_["Goo"] = 19_289_812 types.any_map_property = map_ types.any_map_property.get("Goo") == 19_289_812 # classes mult = Multiply(Number(10), Number(20)) types.any_property = mult assert types.any_property is mult assert isinstance(types.any_property, Multiply) assert types.any_property.value == 200