def test_kvfmt_single(self): expected_pb = b'\x08\x96\x01' raw_obj = {'value': 150} schema = (('value', 'V'),) w = minipb.Wire(schema) self.assertEqual(w.encode(raw_obj), expected_pb) self.assertEqual(w.decode(expected_pb), raw_obj)
def test_codec_vint_2sc_negative_force_32(self): expected_pb = b'\x08\xff\xff\xff\xff\x0f' fields = (-1,) w = minipb.Wire('t') w.vint_2sc_max_bits = 32 self.assertEqual(w.encode(*fields), expected_pb) self.assertEqual(w.decode(expected_pb), fields)
def test_badbehavior_missing_field_kvfmt(self): schema = ( ('field1', 'V'), ('field2', 'V'), ) w = minipb.Wire(schema) with self.assertRaises(minipb.CodecError) as details: w.encode({ 'field2': 123 }) self.assertIn('empty field field1 not padded with None', details.exception.args[0])
def test_kvfmt_complex(self): expected_pb = b'\x08\x7b\x12\x04\x74\x65\x73\x74\x1a\x0b\x0a\x06\x73\x74\x72\x69\x6e\x67\x10\xf8\x06' raw_obj = { 'number': 123, 'string': 'test', 'nested': { 'str2': 'string', 'num2': 888, } } schema = ( ('number', 'V'), ('string', 'U'), ('nested', (('str2', 'U'), ('num2', 'V'),)), ) w = minipb.Wire(schema) self.assertEqual(w.encode(raw_obj), expected_pb) self.assertEqual(w.decode(expected_pb), raw_obj)
def test_kvfmt_very_complex(self): expected_pb = b'\x08\x7b\x12\x04\x74\x65\x73\x74\x1a\x0b\x0a\x06\x73\x74\x72\x69\x6e\x67\x10\xf8\x06\x1a\x13\x0a\x0e\x61\x6e\x6f\x74\x68\x65\x72\x5f\x73\x74\x72\x69\x6e\x67\x10\xb9\x60' raw_obj = { 'number': 123, 'string': 'test', 'nested': ( { 'str2': 'string', 'num2': 888, }, { 'str2': 'another_string', 'num2': 12345, }, ), } schema = ( ('number', 'V'), ('string', 'U'), ('nested', '+[', (('str2', 'U'), ('num2', 'V'),)), ) w = minipb.Wire(schema) self.assertEqual(w.encode(raw_obj), expected_pb) self.assertEqual(w.decode(expected_pb), raw_obj)
def test_badbehavior_missing_field_fmtstr(self): w = minipb.Wire('V2') with self.assertRaises(minipb.CodecError) as details: w.encode(321) self.assertIn('empty field 2 not padded with None', details.exception.args[0])