def test_proto_coder(self): # For instructions on how these test proto message were generated, # see coders_test.py ma = test_message.MessageA() mab = ma.field2.add() mab.field1 = True ma.field1 = u'hello world' mb = test_message.MessageA() mb.field1 = u'beam' proto_coder = coders.ProtoCoder(ma.__class__) self.check_coder(proto_coder, ma) self.check_coder(coders.TupleCoder((proto_coder, coders.BytesCoder())), (ma, 'a'), (mb, 'b'))
def test_deterministic_coder(self): coder = coders.FastPrimitivesCoder() deterministic_coder = coders.DeterministicFastPrimitivesCoder(coder, 'step') self.check_coder(deterministic_coder, *self.test_values_deterministic) for v in self.test_values_deterministic: self.check_coder(coders.TupleCoder((deterministic_coder, )), (v, )) self.check_coder( coders.TupleCoder( (deterministic_coder, ) * len(self.test_values_deterministic)), tuple(self.test_values_deterministic)) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, dict()) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, [1, dict()]) self.check_coder( coders.TupleCoder((deterministic_coder, coder)), (1, dict()), ('a', [dict()])) self.check_coder(deterministic_coder, test_message.MessageA(field1='value')) if dataclasses is not None: self.check_coder( deterministic_coder, [FrozenDataClass(1, 2), MyNamedTuple(1, 2)]) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, UnFrozenDataClass(1, 2)) with self.assertRaises(TypeError): self.check_coder( deterministic_coder, FrozenDataClass(UnFrozenDataClass(1, 2), 3)) with self.assertRaises(TypeError): self.check_coder( deterministic_coder, MyNamedTuple(UnFrozenDataClass(1, 2), 3))
def test_deterministic_coder(self): coder = coders.FastPrimitivesCoder() deterministic_coder = coders.DeterministicFastPrimitivesCoder( coder, 'step') self.check_coder(deterministic_coder, *self.test_values_deterministic) for v in self.test_values_deterministic: self.check_coder(coders.TupleCoder((deterministic_coder, )), (v, )) self.check_coder( coders.TupleCoder( (deterministic_coder, ) * len(self.test_values_deterministic)), tuple(self.test_values_deterministic)) self.check_coder(deterministic_coder, {}) self.check_coder(deterministic_coder, {2: 'x', 1: 'y'}) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, {1: 'x', 'y': 2}) self.check_coder(deterministic_coder, [1, {}]) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, [1, {1: 'x', 'y': 2}]) self.check_coder(coders.TupleCoder((deterministic_coder, coder)), (1, {}), ('a', [{}])) self.check_coder(deterministic_coder, test_message.MessageA(field1='value')) self.check_coder( deterministic_coder, [MyNamedTuple(1, 2), MyTypedNamedTuple(1, 'a')]) if dataclasses is not None: self.check_coder(deterministic_coder, FrozenDataClass(1, 2)) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, UnFrozenDataClass(1, 2)) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, FrozenDataClass(UnFrozenDataClass(1, 2), 3)) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, MyNamedTuple(UnFrozenDataClass(1, 2), 3)) self.check_coder(deterministic_coder, list(MyEnum)) self.check_coder(deterministic_coder, list(MyIntEnum)) self.check_coder(deterministic_coder, list(MyIntFlag)) self.check_coder(deterministic_coder, list(MyFlag)) self.check_coder( deterministic_coder, [DefinesGetAndSetState(1), DefinesGetAndSetState((1, 2, 3))]) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, DefinesGetState(1)) with self.assertRaises(TypeError): self.check_coder(deterministic_coder, DefinesGetAndSetState({ 1: 'x', 'y': 2 }))
def test_proto_coder(self): ma = test_message.MessageA() mb = ma.field2.add() mb.field1 = True ma.field1 = u'hello world' expected_coder = coders.ProtoCoder(ma.__class__) real_coder = coders_registry.get_coder(ma.__class__) self.assertEqual(expected_coder, real_coder) self.assertEqual(real_coder.encode(ma), expected_coder.encode(ma)) self.assertEqual(ma, real_coder.decode(real_coder.encode(ma)))
def test_deterministic_proto_coder(self): ma = test_message.MessageA() mb = ma.field2.add() mb.field1 = True ma.field1 = u'hello world' expected_coder = coders.DeterministicProtoCoder(ma.__class__) real_coder = (coders_registry.get_coder( ma.__class__).as_deterministic_coder(step_label='unused')) self.assertTrue(real_coder.is_deterministic()) self.assertEqual(expected_coder, real_coder) self.assertEqual(real_coder.encode(ma), expected_coder.encode(ma)) self.assertEqual(ma, real_coder.decode(real_coder.encode(ma)))