|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from absl.testing import absltest |
| 16 | +from google.protobuf import descriptor_pool |
| 17 | +from koladata.functions import proto_conversions |
| 18 | +from koladata.functions.tests import test_pb2 |
| 19 | +from koladata.testing import testing |
| 20 | +from koladata.types import data_slice |
| 21 | +from google.protobuf import any_pb2 |
| 22 | + |
| 23 | +ds = data_slice.DataSlice.from_vals |
| 24 | + |
| 25 | + |
| 26 | +class ToProtoAnyTest(absltest.TestCase): |
| 27 | + |
| 28 | + def test_zero_items(self): |
| 29 | + x = ds([]) |
| 30 | + res = proto_conversions.to_proto_any(x) |
| 31 | + self.assertEqual(res, []) |
| 32 | + |
| 33 | + def test_single_item(self): |
| 34 | + m = test_pb2.MessageA(some_text='thing 1') |
| 35 | + x = proto_conversions.from_proto(m) |
| 36 | + res = proto_conversions.to_proto_any(x) |
| 37 | + self.assertIsInstance(res, any_pb2.Any) |
| 38 | + unpacked_m = test_pb2.MessageA() |
| 39 | + res.Unpack(unpacked_m) |
| 40 | + self.assertEqual(unpacked_m, m) |
| 41 | + |
| 42 | + def test_single_none(self): |
| 43 | + x = ds(None) |
| 44 | + res = proto_conversions.to_proto_any(x) |
| 45 | + self.assertIsNone(res) |
| 46 | + |
| 47 | + def test_list_with_none(self): |
| 48 | + m1 = test_pb2.MessageA(some_text='thing 1') |
| 49 | + x = proto_conversions.from_proto([m1, None]) |
| 50 | + res = proto_conversions.to_proto_any(x) |
| 51 | + self.assertIsInstance(res, list) |
| 52 | + self.assertLen(res, 2) |
| 53 | + self.assertIsInstance(res[0], any_pb2.Any) |
| 54 | + self.assertIsNone(res[1]) |
| 55 | + unpacked_m1 = test_pb2.MessageA() |
| 56 | + res[0].Unpack(unpacked_m1) |
| 57 | + self.assertEqual(unpacked_m1, m1) |
| 58 | + |
| 59 | + def test_multiple_messages_same_type(self): |
| 60 | + m1 = test_pb2.MessageA(some_text='thing 1') |
| 61 | + m2 = test_pb2.MessageA(some_text='thing 2') |
| 62 | + x = proto_conversions.from_proto([m1, m2]) |
| 63 | + res = proto_conversions.to_proto_any(x) |
| 64 | + self.assertIsInstance(res, list) |
| 65 | + self.assertLen(res, 2) |
| 66 | + unpacked_m1 = test_pb2.MessageA() |
| 67 | + res[0].Unpack(unpacked_m1) |
| 68 | + self.assertEqual(unpacked_m1, m1) |
| 69 | + unpacked_m2 = test_pb2.MessageA() |
| 70 | + res[1].Unpack(unpacked_m2) |
| 71 | + self.assertEqual(unpacked_m2, m2) |
| 72 | + |
| 73 | + def test_multiple_messages_different_types(self): |
| 74 | + m1 = test_pb2.MessageA(some_text='thing 1') |
| 75 | + m2 = test_pb2.MessageB(text='thing 2') |
| 76 | + # Need to use from_proto_any to get them into the same DataSlice |
| 77 | + any_m1 = any_pb2.Any() |
| 78 | + any_m1.Pack(m1) |
| 79 | + any_m2 = any_pb2.Any() |
| 80 | + any_m2.Pack(m2) |
| 81 | + x = proto_conversions.from_proto_any([any_m1, any_m2]) |
| 82 | + |
| 83 | + res = proto_conversions.to_proto_any(x) |
| 84 | + self.assertIsInstance(res, list) |
| 85 | + self.assertLen(res, 2) |
| 86 | + unpacked_m1 = test_pb2.MessageA() |
| 87 | + res[0].Unpack(unpacked_m1) |
| 88 | + self.assertEqual(unpacked_m1, m1) |
| 89 | + unpacked_m2 = test_pb2.MessageB() |
| 90 | + res[1].Unpack(unpacked_m2) |
| 91 | + self.assertEqual(unpacked_m2, m2) |
| 92 | + |
| 93 | + def test_nested_list_input(self): |
| 94 | + m1 = test_pb2.MessageA(some_text='1') |
| 95 | + any_m1 = any_pb2.Any() |
| 96 | + any_m1.Pack(m1) |
| 97 | + m2 = test_pb2.MessageA(some_text='2') |
| 98 | + any_m2 = any_pb2.Any() |
| 99 | + any_m2.Pack(m2) |
| 100 | + m3 = test_pb2.MessageB(text='3') |
| 101 | + any_m3 = any_pb2.Any() |
| 102 | + any_m3.Pack(m3) |
| 103 | + |
| 104 | + x = proto_conversions.from_proto_any([[any_m1, None, any_m2], [], [any_m3]]) |
| 105 | + res = proto_conversions.to_proto_any(x) |
| 106 | + testing.assert_equivalent(proto_conversions.from_proto_any(res), x) |
| 107 | + |
| 108 | + def test_not_from_proto(self): |
| 109 | + x = ds([1, 2, 3]).implode() |
| 110 | + with self.assertRaisesRegex( |
| 111 | + ValueError, |
| 112 | + 'to_proto_any expects entities converted from proto messages', |
| 113 | + ): |
| 114 | + proto_conversions.to_proto_any(x) |
| 115 | + |
| 116 | + def test_empty_descriptor_pool(self): |
| 117 | + m = test_pb2.MessageA(some_text='thing 1') |
| 118 | + x = proto_conversions.from_proto(m) |
| 119 | + pool = descriptor_pool.DescriptorPool() |
| 120 | + with self.assertRaisesRegex(KeyError, 'MessageA'): |
| 121 | + proto_conversions.to_proto_any(x, descriptor_pool=pool) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + absltest.main() |
0 commit comments