Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
compare_proto.py
Go to the documentation of this file.
1# Copyright 2010-2024 Google LLC
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14"""Assertions to test that protos are equivalent in MathOpt's sense.
15
16Empty sub-messages (recursively) have no effect on equivalence, except for the
17Duration message, otherwise the same as proto equality. Oneof messages have
18their fields recursively cleared, but the oneof itself is not, to preserve the
19selection. This is similar to C++ EquivToProto, but this function cares about:
20 * field presence for optional scalar fields,
21 * field presence for Duration messages (e.g. Duration unset of time limit
22 means +inf),
23 * field presence for one_ofs,
24and C++ EquivToProto does not.
25
26See normalize.py for details.
27"""
28
29import copy
30import unittest
31
32from google.protobuf import message
33
34from ortools.math_opt.python import normalize
35
36
38 test: unittest.TestCase, actual: message.Message, expected: message.Message
39) -> None:
40 """Asserts the input protos are equivalent, see module doc for details."""
41 normalized_actual = copy.deepcopy(actual)
42 normalize.math_opt_normalize_proto(normalized_actual)
43 normalized_expected = copy.deepcopy(expected)
44 normalize.math_opt_normalize_proto(normalized_expected)
45 test.assertEqual(str(normalized_actual), str(normalized_expected))
46
47
48class MathOptProtoAssertions(unittest.TestCase):
49 """Provides a custom MathOpt proto equivalence assertion for tests."""
50
52 self, actual: message.Message, expected: message.Message
53 ) -> None:
54 """Asserts the input protos are equivalent, see module doc for details."""
55 return assert_protos_equiv(self, actual, expected)
None assert_protos_equiv(self, message.Message actual, message.Message expected)
None assert_protos_equiv(unittest.TestCase test, message.Message actual, message.Message expected)