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