Exemplo n.º 1
0
 def test_map_str_msg_leave_key_entirely(self):
     msg = test_proto_pb2.Msg(map_str_msg={
         'a': test_proto_pb2.Msg(num=1),
         'b': test_proto_pb2.Msg(num=2),
     },
                              num=1)
     self.trim(msg, 'map_str_msg.a')
     self.assertEqual(
         msg,
         test_proto_pb2.Msg(map_str_msg={'a': test_proto_pb2.Msg(num=1)}))
Exemplo n.º 2
0
 def test_map_str_msg_leave_key_partially(self):
     msg = test_proto_pb2.Msg(map_str_msg={
         'a': test_proto_pb2.Msg(num=1, str='a'),
         'b': test_proto_pb2.Msg(num=2, str='b'),
     },
                              num=1)
     self.trim(msg, 'map_str_msg.a.num')
     self.assertEqual(
         msg,
         test_proto_pb2.Msg(map_str_msg={'a': test_proto_pb2.Msg(num=1)}))
Exemplo n.º 3
0
 def test_works(self):
     msg = test_proto_pb2.Msg(
         str='s',
         strs=['s1', 's2'],
         num=1,
         nums=[1, 2],
         msg=test_proto_pb2.Msg(num=3),
         msgs=[test_proto_pb2.Msg(num=4),
               test_proto_pb2.Msg(num=5)])
     data = {
         'str': 'a',
         'strs': ['a', 'b'],
         'num': 2,
         'nums': [3, 4],
         'msg': {
             'num': 5,
         },
         'msgs': [{
             'num': 6,
         }],
     }
     protoutil.merge_dict(data, msg)
     self.assertEqual(
         msg,
         test_proto_pb2.Msg(str='a',
                            strs=['s1', 's2', 'a', 'b'],
                            num=2,
                            nums=[1, 2, 3, 4],
                            msg=test_proto_pb2.Msg(num=5),
                            msgs=[
                                test_proto_pb2.Msg(num=4),
                                test_proto_pb2.Msg(num=5),
                                test_proto_pb2.Msg(num=6),
                            ]))
Exemplo n.º 4
0
 def test_submessage_repeated_leave_entirely_trailing_star(self):
     msg = test_proto_pb2.Msg(
         msgs=[test_proto_pb2.Msg(
             num=1), test_proto_pb2.Msg(num=2)])
     expected = test_proto_pb2.Msg(
         msgs=[test_proto_pb2.Msg(
             num=1), test_proto_pb2.Msg(num=2)])
     self.trim(msg, 'msgs.*')
     self.assertEqual(msg, expected)
Exemplo n.º 5
0
 def test_multiline(self):
     poem = """\
           `Twas brillig, and the slithy toves
           Did gyre and gimble in the wabe:
           All mimsy were the borogoves,
           And the mome raths outgrabe."""
     content = textwrap.dedent("""
   str: <<EOP
   %s
   EOP
   num: 42
 """) % poem
     msg = test_proto_pb2.Msg()
     parsed_content = multiline_proto.parse_multiline(content)
     protobuf.text_format.Merge(parsed_content, msg)
     self.assertEqual(textwrap.dedent(poem), msg.str)
Exemplo n.º 6
0
 def test_submessage_repeated_leave_partially(self):
     msg = test_proto_pb2.Msg(msgs=[
         test_proto_pb2.Msg(num=1, str='x'),
         test_proto_pb2.Msg(num=2, str='y'),
     ])
     expected = test_proto_pb2.Msg(msgs=[
         test_proto_pb2.Msg(num=1),
         test_proto_pb2.Msg(num=2),
     ])
     self.trim(msg, 'msgs.*.num')
     self.assertEqual(msg, expected)
Exemplo n.º 7
0
 def test_submessage_repeated_trim(self):
     msg = test_proto_pb2.Msg(
         msgs=[test_proto_pb2.Msg(
             num=1), test_proto_pb2.Msg(num=2)])
     self.trim(msg, 'str')
     self.assertEqual(msg, test_proto_pb2.Msg())
Exemplo n.º 8
0
 def test_invalid_field_value_type(self):
     with self.assertRaises(TypeError):
         protoutil.merge_dict({'str': 0}, test_proto_pb2.Msg())
Exemplo n.º 9
0
 def test_scalar_trim(self):
     msg = test_proto_pb2.Msg(num=1)
     self.trim(msg, 'str')
     self.assertEqual(msg, test_proto_pb2.Msg())
Exemplo n.º 10
0
 def test_map_str_msg_trim(self):
     msg = test_proto_pb2.Msg(map_str_msg={'a': test_proto_pb2.Msg()})
     self.trim(msg, 'str')
     self.assertEqual(msg, test_proto_pb2.Msg())
Exemplo n.º 11
0
 def test_map_str_num_leave_key_with_int_key_invalid(self):
     msg = test_proto_pb2.Msg(map_str_num={'1': 1, '2': 2})
     with self.assertRaisesRegexp(ValueError, 'expected a string'):
         self.trim(msg, 'map_str_num.1')
Exemplo n.º 12
0
 def test_map_str_num_leave_key_with_int_key(self):
     msg = test_proto_pb2.Msg(map_str_num={'1': 1, '2': 2})
     self.trim(msg, 'map_str_num.`1`')
     self.assertEqual(msg, test_proto_pb2.Msg(map_str_num={'1': 1}))
Exemplo n.º 13
0
 def test_map_str_num_leave_key(self):
     msg = test_proto_pb2.Msg(map_str_num={'a': 1, 'b': 2})
     self.trim(msg, 'map_str_num.a')
     self.assertEqual(msg, test_proto_pb2.Msg(map_str_num={'a': 1}))
Exemplo n.º 14
0
 def test_map_str_num_trim(self):
     msg = test_proto_pb2.Msg(map_str_num={'1': 1, '2': 2})
     self.trim(msg, 'str')
     self.assertEqual(msg, test_proto_pb2.Msg())
Exemplo n.º 15
0
 def test_invalid_field_name(self):
     with self.assertRaises(TypeError):
         protoutil.merge_dict({'no_such_field': 0}, test_proto_pb2.Msg())
Exemplo n.º 16
0
 def test_scalar_leave(self):
     msg = test_proto_pb2.Msg(num=1)
     self.trim(msg, 'num')
     self.assertEqual(msg, test_proto_pb2.Msg(num=1))
Exemplo n.º 17
0
 def test_submessage_leave_partially(self):
     msg = test_proto_pb2.Msg(msg=test_proto_pb2.Msg(num=1, str='x'))
     self.trim(msg, 'msg.num')
     self.assertEqual(msg,
                      test_proto_pb2.Msg(msg=test_proto_pb2.Msg(num=1)))
Exemplo n.º 18
0
 def test_submessage_leave_entirely(self):
     msg = test_proto_pb2.Msg(msg=test_proto_pb2.Msg(num=1))
     self.trim(msg, 'msg')
     self.assertEqual(msg,
                      test_proto_pb2.Msg(msg=test_proto_pb2.Msg(num=1)))
Exemplo n.º 19
0
 def test_submessage_trim(self):
     msg = test_proto_pb2.Msg(msg=test_proto_pb2.Msg(num=1))
     self.trim(msg, 'str')
     self.assertEqual(msg, test_proto_pb2.Msg())
Exemplo n.º 20
0
 def test_scalar_repeated_leave(self):
     msg = test_proto_pb2.Msg(nums=[1, 2])
     self.trim(msg, 'nums')
     self.assertEqual(msg, test_proto_pb2.Msg(nums=[1, 2]))
Exemplo n.º 21
0
 def test_scalar_repeated_trim(self):
     msg = test_proto_pb2.Msg(nums=[1, 2])
     self.trim(msg, 'str')
     self.assertEqual(msg, test_proto_pb2.Msg())