Google OR-Tools v9.14
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-2025 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 compare protocol buffers.
15
16Two comparisons are provided:
17
181. assert_protos_equal(): Checks that two messages are exactly the same (roughly
19speaking, they should produce the same binary representation).
202. assert_protos_equiv(): Checks that two messages are equivalent in the MathOpt
21sense (roughly speaking, empty submessages being set/unset do not matter,
22details below).
23
24(2) is reduced to (1) by recursively clearing empty messages via normalize.py.
25To implement (1),
26in open source we check that the text proto representations are the same (this
27is a slightly weaker test).
28
29The exact contract for assert_protos_equiv() is as follows.
30Empty sub-messages (recursively) have no effect on equivalence, except for the
31Duration message, otherwise the same as proto equality. Oneof messages have
32their fields recursively cleared, but the oneof itself is not, to preserve the
33selection. This is similar to C++ EquivToProto, but this function cares about:
34 * field presence for optional scalar fields,
35 * field presence for Duration messages (e.g. Duration unset of time limit
36 means +inf),
37 * field presence for one_ofs,
38and C++ EquivToProto does not.
39
40See normalize.py for details.
41"""
42
43import copy
44import unittest
45
46from google.protobuf import message
47
48from ortools.math_opt.python import normalize
49
50
52 test: unittest.TestCase,
53 actual: message.Message,
54 expected: message.Message,
55) -> None:
56 """Asserts the input protos are equal, see module doc for details."""
57 test.assertEqual(str(actual), str(expected))
58
59
61 test: unittest.TestCase,
62 actual: message.Message,
63 expected: message.Message,
64) -> None:
65 """Asserts the input protos are equivalent, see module doc for details."""
66 normalized_actual = copy.deepcopy(actual)
67 normalize.math_opt_normalize_proto(normalized_actual)
68 normalized_expected = copy.deepcopy(expected)
69 normalize.math_opt_normalize_proto(normalized_expected)
70 test.assertEqual(str(normalized_actual), str(normalized_expected))
71
72
73class MathOptProtoAssertions(unittest.TestCase):
74 """Provides a custom MathOpt proto equivalence assertion for tests."""
75
77 self,
78 actual: message.Message,
79 expected: message.Message,
80 ) -> None:
81 """Asserts the input protos are equal, see module doc for details."""
82 return assert_protos_equal(self, actual, expected)
83
85 self,
86 actual: message.Message,
87 expected: message.Message,
88 ) -> None:
89 """Asserts the input protos are equivalent, see module doc for details."""
90 return assert_protos_equiv(self, actual, expected)
None assert_protos_equal(self, message.Message actual, message.Message expected)
None assert_protos_equiv(self, message.Message actual, message.Message expected)
None assert_protos_equal(unittest.TestCase test, message.Message actual, message.Message expected)
None assert_protos_equiv(unittest.TestCase test, message.Message actual, message.Message expected)