def test_json_all_kinds_of(): """ Serialize a JSON payload of all kinds of fields. """ lookup = MultiPackageLookup(CachedDarFile(dars.AllKindsOf).archives()) tt = con(lookup.data_type_name("AllKindsOf:OneOfEverything")) ctx = Context(JsonEncoder(), lookup) expected = { "operator": "Operator", "someBoolean": True, "someInteger": 10, "someDecimal": "10", "someMaybe": 10, "someMaybeNot": None, "someText": "text", "someDate": "2000-01-01", "someDatetime": "2000-01-01T00:00:00Z", "someSimpleList": [1], "someSimplePair": {"left": 4, "right": 5}, "someNestedPair": {"left": {"left": 4, "right": 5}, "right": {"left": 10, "right": 4}}, "someUglyNesting": { "tag": "Both", "value": { "tag": "Right", "value": {"left": {"left": 10, "right": 20}, "right": {"left": 30, "right": 40}}, }, }, "someMeasurement": "5", "someEnum": "Green", "theUnit": {}, } actual = ctx.convert( tt, { "operator": "Operator", "someBoolean": True, "someInteger": 10, "someDecimal": 10, "someMaybe": 10, "someMaybeNot": None, "someText": "text", "someDate": date(2000, 1, 1), "someDatetime": datetime(2000, 1, 1), "someSimpleList": [1], "someSimplePair": {"left": 4, "right": 5}, "someNestedPair": {"left": {"left": 4, "right": 5}, "right": {"left": 10, "right": 4}}, "someUglyNesting": { "Both": { "Right": {"left": {"left": 10, "right": 20}, "right": {"left": 30, "right": 40}} } }, "someMeasurement": 5, "someEnum": "Green", "theUnit": {}, }, ) assert expected == actual
def test_values_canonical_bool_random_string(): ctx = Context(CanonicalMapper()) try: ctx.convert(daml.Bool, "blahblahblah") assert False, "we were supposed to fail!" except ValueError: pass
def test_basic_json_text(): ctx = Context(JsonDecoder()) actual = ctx.convert(daml.Text, "some-text") assert actual == "some-text"
def decode_context(lookup): return Context(ProtobufDecoder(), lookup)
def encode_context(lookup): return Context(ProtobufEncoder(), lookup)
def test_values_party(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Party, "some-party") assert actual == Party("some-party")
def test_values_optional_none(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Optional(daml.Text), None) assert None is actual
def test_values_optional_some_empty_string(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Optional(daml.Text), "") assert "" == actual
def test_values_canonical_str_that_looks_like_a_date(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Text, "2020-01-01") assert "2020-01-01" == actual
def test_values_canonical_date_str(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Date, "2020-01-01") assert date(2020, 1, 1) == actual
def test_values_canonical_bool_false_string(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Bool, "false") assert not actual
def test_values_canonical_bool(): ctx = Context(CanonicalMapper()) actual = ctx.convert(daml.Bool, False) assert not actual