ortools.math_opt.python.testing.compare_proto

Assertions to compare protocol buffers.

Two comparisons are provided:

  1. assert_protos_equal(): Checks that two messages are exactly the same (roughly speaking, they should produce the same binary representation).
  2. assert_protos_equiv(): Checks that two messages are equivalent in the MathOpt sense (roughly speaking, empty submessages being set/unset do not matter, details below).

(2) is reduced to (1) by recursively clearing empty messages via normalize.py. To implement (1), in open source we check that the text proto representations are the same (this is a slightly weaker test).

The exact contract for assert_protos_equiv() is as follows. Empty sub-messages (recursively) have no effect on equivalence, except for the Duration message, otherwise the same as proto equality. Oneof messages have their fields recursively cleared, but the oneof itself is not, to preserve the selection. This is similar to C++ EquivToProto, but this function cares about:

  • field presence for optional scalar fields,
  • field presence for Duration messages (e.g. Duration unset of time limit means +inf),
  • field presence for one_ofs, and C++ EquivToProto does not.

See normalize.py for details.

 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
51def assert_protos_equal(
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
60def assert_protos_equiv(
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
76    def assert_protos_equal(
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
84    def assert_protos_equiv(
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)
def assert_protos_equal( test: unittest.case.TestCase, actual: google.protobuf.message.Message, expected: google.protobuf.message.Message) -> None:
52def assert_protos_equal(
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))

Asserts the input protos are equal, see module doc for details.

def assert_protos_equiv( test: unittest.case.TestCase, actual: google.protobuf.message.Message, expected: google.protobuf.message.Message) -> None:
61def assert_protos_equiv(
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))

Asserts the input protos are equivalent, see module doc for details.

class MathOptProtoAssertions(unittest.case.TestCase):
74class MathOptProtoAssertions(unittest.TestCase):
75    """Provides a custom MathOpt proto equivalence assertion for tests."""
76
77    def assert_protos_equal(
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
85    def assert_protos_equiv(
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)

Provides a custom MathOpt proto equivalence assertion for tests.

def assert_protos_equal( self, actual: google.protobuf.message.Message, expected: google.protobuf.message.Message) -> None:
77    def assert_protos_equal(
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)

Asserts the input protos are equal, see module doc for details.

def assert_protos_equiv( self, actual: google.protobuf.message.Message, expected: google.protobuf.message.Message) -> None:
85    def assert_protos_equiv(
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)

Asserts the input protos are equivalent, see module doc for details.