def test_dict_field_encode(self): class GrabBag(messages.Message): item_count = messages.IntegerField() items = messages.DictField() gb = GrabBag() self.assertEquals('{}', to_json.encode_message(gb)) gb = GrabBag() gb.item_count = 123 self.assertEquals('{"item_count": 123}', to_json.encode_message(gb)) gb = GrabBag() gb.item_count = 123 gb.items = {} self.assertEquals('{"items": {}, "item_count": 123}', to_json.encode_message(gb)) gb = GrabBag() gb.item_count = 123 gb.items = {'a': 'b', 'foo': 'bar'} self.assertEquals('{"items": {"a": "b", "foo": "bar"}, "item_count": 123}', to_json.encode_message(gb)) gb = GrabBag() gb.items = {'a': datetime(2010, 11, 13, 14, 15, 16), 'b': date(2009, 10, 11), 'c': time(1, 2, 3)} self.assertEquals('{"items": {"a": "2010-11-13T14:15:16", "c": "01:02:03", "b": "2009-10-11"}}', to_json.encode_message(gb)) gb = GrabBag() gb.items = {'nested': {'a': datetime(2010, 11, 13, 14, 15, 16), 'b': date(2009, 10, 11), 'c': time(1, 2, 3)}} self.assertEquals('{"items": {"nested": {"a": "2010-11-13T14:15:16", "c": "01:02:03", "b": "2009-10-11"}}}', to_json.encode_message(gb))
def test_assigned_values_rendered(self): class Animals(messages.Message): bird = messages.StringField(repeated=True) cow = messages.StringField() a = Animals() self.assertEqual('{}', to_json.encode_message(a)) a = Animals() a.cow = "moo" self.assertEqual('{"cow": "moo"}', to_json.encode_message(a)) a = Animals() a.cow = None self.assertEqual('{"cow": null}', to_json.encode_message(a)) a = Animals() a.bird = [] self.assertEqual('{"bird": []}', to_json.encode_message(a)) a = Animals() a.bird = ["quack", "cheep", "honk"] self.assertEqual('{"bird": ["quack", "cheep", "honk"]}', to_json.encode_message(a)) a = Animals() a.cow = "moo" a.bird = ["quack", "cheep", "honk"] self.assertEqual(json.loads('{"bird": ["quack", "cheep", "honk"], "cow": "moo"}'), json.loads(to_json.encode_message(a)))
def test_dict_field_encode(self): class GrabBag(messages.Message): item_count = messages.IntegerField() items = messages.DictField() gb = GrabBag() self.assertEqual('{}', to_json.encode_message(gb)) gb = GrabBag() gb.item_count = 123 self.assertEqual('{"item_count": 123}', to_json.encode_message(gb)) gb = GrabBag() gb.item_count = 123 gb.items = {} self.assertEqual(json.loads('{"items": {}, "item_count": 123}'), json.loads(to_json.encode_message(gb))) gb = GrabBag() gb.item_count = 123 gb.items = {'a': 'b', 'foo': 'bar'} self.assertEqual(json.loads('{"items": {"a": "b", "foo": "bar"}, "item_count": 123}'), json.loads(to_json.encode_message(gb))) gb = GrabBag() gb.items = {'a': datetime(2010, 11, 13, 14, 15, 16), 'b': date(2009, 10, 11), 'c': time(1, 2, 3)} self.assertEqual(json.loads('{"items": {"a": "2010-11-13T14:15:16", "c": "01:02:03", "b": "2009-10-11"}}'), json.loads(to_json.encode_message(gb))) gb = GrabBag() gb.items = {'nested': {'a': datetime(2010, 11, 13, 14, 15, 16), 'b': date(2009, 10, 11), 'c': time(1, 2, 3)}} self.assertEqual(json.loads('{"items": {"nested": {"a": "2010-11-13T14:15:16", "c": "01:02:03", "b": "2009-10-11"}}}'), json.loads(to_json.encode_message(gb)))
def test_untyped_field_repeated_encode(self): class Foo(messages.Message): bar = messages.UntypedField(repeated=True) f = Foo() f.bar = [123, "woof", 1.23, True] self.assertEquals('{"bar": [123, "woof", 1.23, true]}', to_json.encode_message(f))
def test_untyped_field_repeated_encode(self): class Foo(messages.Message): bar = messages.UntypedField(repeated=True) f = Foo() f.bar = [123, "woof", 1.23, True] self.assertEqual('{"bar": [123, "woof", 1.23, true]}', to_json.encode_message(f))
def test_uuid_field_encode_repeated(self): from uuid import UUID class Foo(messages.Message): bar = messages.UUIDField(repeated=True) f = Foo(bar=[UUID("11115e84-2872-4914-8c5d-3ed07d2a2f16"), UUID("22225e84-2872-4914-8c5d-3ed07d2a2f16")]) self.assertEqual(to_json.encode_message(f), '{"bar": ["11115e84-2872-4914-8c5d-3ed07d2a2f16", "22225e84-2872-4914-8c5d-3ed07d2a2f16"]}')
def test_uuid_field_encode(self): from uuid import UUID class Foo(messages.Message): bar = messages.UUIDField() f = Foo(bar=UUID("06335e84-2872-4914-8c5d-3ed07d2a2f16")) self.assertEqual('{"bar": "06335e84-2872-4914-8c5d-3ed07d2a2f16"}', to_json.encode_message(f))
def test_untyped_field_encode(self): class Foo(messages.Message): bar = messages.UntypedField() f = Foo() self.assertEqual('{}', to_json.encode_message(f)) f = Foo() f.bar = 123 self.assertEqual('{"bar": 123}', to_json.encode_message(f)) f = Foo() f.bar = "meow" self.assertEqual('{"bar": "meow"}', to_json.encode_message(f)) f = Foo() f.bar = True self.assertEqual('{"bar": true}', to_json.encode_message(f)) f = Foo() f.bar = 1.23 self.assertEqual('{"bar": 1.23}', to_json.encode_message(f)) f = Foo() f.bar = None self.assertEqual('{"bar": null}', to_json.encode_message(f)) f = Foo() f.bar = [[123, 1.23, "woof", True], "meow"] self.assertEqual('{"bar": [[123, 1.23, "woof", true], "meow"]}', to_json.encode_message(f))
def test_untyped_field_encode(self): class Foo(messages.Message): bar = messages.UntypedField() f = Foo() self.assertEquals('{}', to_json.encode_message(f)) f = Foo() f.bar = 123 self.assertEquals('{"bar": 123}', to_json.encode_message(f)) f = Foo() f.bar = "meow" self.assertEquals('{"bar": "meow"}', to_json.encode_message(f)) f = Foo() f.bar = True self.assertEquals('{"bar": true}', to_json.encode_message(f)) f = Foo() f.bar = 1.23 self.assertEquals('{"bar": 1.23}', to_json.encode_message(f)) f = Foo() f.bar = None self.assertEquals('{"bar": null}', to_json.encode_message(f)) f = Foo() f.bar = [[123, 1.23, "woof", True], "meow"] self.assertEquals('{"bar": [[123, 1.23, "woof", true], "meow"]}', to_json.encode_message(f))
def testEncodeDateTimeMsInteger(self): for datetime_int, datetime_vals in ( (1349019110262, (2012, 9, 30, 15, 31, 50, 262000)), (1349019110262, (2012, 9, 30, 15, 31, 50, 262123)), (1349019110000, (2012, 9, 30, 15, 31, 50, 0))): decoded_message = to_json.encode_message( MyMessage(a_datetime_ms_integer=datetime(*datetime_vals))) expected_decoding = '{"a_datetime_ms_integer": %d}' % datetime_int self.CompareEncoded(expected_decoding, decoded_message)
def testEncodeDateTimeISO8601(self): for datetime_string, datetime_vals in ( ('2012-09-30T15:31:50.262000', (2012, 9, 30, 15, 31, 50, 262000)), ('2012-09-30T15:31:50.262123', (2012, 9, 30, 15, 31, 50, 262123)), ('2012-09-30T15:31:50', (2012, 9, 30, 15, 31, 50, 0))): decoded_message = to_json.encode_message( MyMessage(a_datetime_iso8601=datetime(*datetime_vals))) expected_decoding = '{"a_datetime_iso8601": "%s"}' % datetime_string self.CompareEncoded(expected_decoding, decoded_message)
def test_assigned_values_rendered(self): class Animals(messages.Message): bird = messages.StringField(repeated=True) cow = messages.StringField() a = Animals() self.assertEquals('{}', to_json.encode_message(a)) a = Animals() a.cow = "moo" self.assertEquals('{"cow": "moo"}', to_json.encode_message(a)) a = Animals() a.cow = None self.assertEquals('{"cow": null}', to_json.encode_message(a)) a = Animals() a.bird = [] self.assertEquals('{"bird": []}', to_json.encode_message(a)) a = Animals() a.bird = ["quack", "cheep", "honk"] self.assertEquals('{"bird": ["quack", "cheep", "honk"]}', to_json.encode_message(a)) a = Animals() a.cow = "moo" a.bird = ["quack", "cheep", "honk"] self.assertEquals('{"bird": ["quack", "cheep", "honk"], "cow": "moo"}', to_json.encode_message(a))
def test_explicit_field_name(self): class Foo(messages.Message): bob = messages.StringField(name="robert") ryan = messages.StringField() m = to_json.decode_message(Foo, '{"robert": "smith", "ryan": "morlok"}') self.assertEquals("smith", m.bob) self.assertEquals("morlok", m.ryan) f = Foo() f.bob = "smith" f.ryan = "morlok" self.assertEquals('{"robert": "smith", "ryan": "morlok"}', to_json.encode_message(f))
def testEncodeCustom(self): decoded_message = to_json.encode_message(MyMessage(a_custom=1)) self.CompareEncoded('{"a_custom": 1}', decoded_message)
def test_explicit_field_name(self): class Foo(messages.Message): bob = messages.StringField(name="robert") ryan = messages.StringField() m = to_json.decode_message(Foo, '{"robert": "smith", "ryan": "morlok"}') self.assertEqual("smith", m.bob) self.assertEqual("morlok", m.ryan) f = Foo() f.bob = "smith" f.ryan = "morlok" self.assertEqual(json.loads('{"robert": "smith", "ryan": "morlok"}'), json.loads(to_json.encode_message(f)))