def test_pack(self): class Pet(Message): sound = String() protocol = JSONProtocol(Pet) self.assertEqual(protocol.pack(Pet()), b'{}') self.assertEqual(protocol.pack(Pet('hiss!')), b'{"sound":"hiss!"}')
def test_string_value(self): protocol = JSONProtocol(StringValue) self.assertEqual(protocol.encode(StringValue('hiss!')), 'hiss!') self.assertEqual(protocol.decode('hiss!'), StringValue('hiss!')) self.assertEqual(protocol.pack(StringValue()), b'""') self.assertEqual(protocol.pack(StringValue('hiss!')), b'"hiss!"') with self.assertRaises(ValidationError): protocol.decode(42)
def test_repeat(self): class Pet(Message): sounds: Repeat[str] protocol = JSONProtocol(Pet) self.assertEqual(protocol.encode(Pet(['hiss!', '(slither)'])), {'sounds': ['hiss!', '(slither)']}) self.assertEqual(protocol.decode({"sounds": ['hiss!']}), Pet(['hiss!'])) self.assertEqual(protocol.decode({}), Pet()) self.assertEqual(protocol.pack(Pet()), b'{}') self.assertEqual(protocol.pack(Pet([])), b'{"sounds":[]}') self.assertEqual(protocol.pack(Pet(['hiss!'])), b'{"sounds":["hiss!"]}') self.assertEqual(protocol.unpack(b'{}'), Pet()) self.assertEqual(protocol.unpack(b'{"sounds":["hiss!"]}'), Pet(['hiss!']))