def test_bad_pbtxt(self): with tempfile.NamedTemporaryFile(mode='w+', suffix='.pbtxt') as f: message = test_pb2.RepeatedScalar(values=[1.2, 3.4]) f.write(text_format.MessageToString(message)) f.flush() with self.assertRaisesRegex(ValueError, 'no field named "values"'): message_helpers.load_message(f.name, test_pb2.Nested)
def test_bad_json(self): with tempfile.NamedTemporaryFile(mode='w+') as f: message = test_pb2.RepeatedScalar(values=[1.2, 3.4]) f.write(json_format.MessageToJson(message)) f.flush() with self.assertRaisesRegex(ValueError, 'no field named "values"'): message_helpers.load_message(f.name, test_pb2.Nested, 'json')
def setUp(self): super().setUp() self.messages = [ test_pb2.Scalar(int32_value=3, float_value=4.5), test_pb2.RepeatedScalar(values=[1.2, 3.4]), test_pb2.Enum(value='FIRST'), test_pb2.RepeatedEnum(values=['FIRST', 'SECOND']), test_pb2.Nested(child=test_pb2.Nested.Child(value=1.2)), ]
def test_repeated_scalar(self): message = test_pb2.RepeatedScalar(values=[1.1, 2.2, 3.3]) record = proto_to_json.get_database_json(message) # NOTE(kearnes): Using individual tests here to avoid introducing a # dependency on numpy for assert_almost_equal. self.assertLen(record, 1) self.assertLen(record['values'], 3) self.assertAlmostEqual(record['values'][0], 1.1, places=3) self.assertAlmostEqual(record['values'][1], 2.2, places=3) self.assertAlmostEqual(record['values'][2], 3.3, places=3)
def test_bad_binary(self): with tempfile.NamedTemporaryFile(suffix='.pb') as f: message = test_pb2.RepeatedScalar(values=[1.2, 3.4]) f.write(message.SerializeToString()) f.flush() # NOTE(kearnes): The decoder is not perfect; for example, it will # not be able to distinguish from a message with the same tags and # types (e.g. test_pb2.Scalar and test_pb2.RepeatedScalar). with self.assertRaisesRegex(ValueError, 'Error parsing message'): message_helpers.load_message(f.name, test_pb2.Nested)
def test_bad_suffix(self): message = test_pb2.RepeatedScalar(values=[1.2, 3.4]) with self.assertRaisesRegex(ValueError, 'not a valid MessageFormat'): message_helpers.write_message(message, 'test.proto')
def test_bad_suffix(self): message = test_pb2.RepeatedScalar(values=[1.2, 3.4]) with self.assertRaisesRegex(ValueError, 'expected suffix'): message_helpers.write_message( message, 'test.pb', message_helpers.MessageFormats.PBTXT)