ortools.math_opt.python.testing.compare_proto

Assertions to test that protos are equivalent in MathOpt's sense.

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-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
37def assert_protos_equiv(
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
51    def assert_protos_equiv(
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)
def assert_protos_equiv( test: unittest.case.TestCase, actual: google.protobuf.message.Message, expected: google.protobuf.message.Message) -> None:
38def assert_protos_equiv(
39    test: unittest.TestCase, actual: message.Message, expected: message.Message
40) -> None:
41    """Asserts the input protos are equivalent, see module doc for details."""
42    normalized_actual = copy.deepcopy(actual)
43    normalize.math_opt_normalize_proto(normalized_actual)
44    normalized_expected = copy.deepcopy(expected)
45    normalize.math_opt_normalize_proto(normalized_expected)
46    test.assertEqual(str(normalized_actual), str(normalized_expected))

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

class MathOptProtoAssertions(unittest.case.TestCase):
49class MathOptProtoAssertions(unittest.TestCase):
50    """Provides a custom MathOpt proto equivalence assertion for tests."""
51
52    def assert_protos_equiv(
53        self, actual: message.Message, expected: message.Message
54    ) -> None:
55        """Asserts the input protos are equivalent, see module doc for details."""
56        return assert_protos_equiv(self, actual, expected)

Provides a custom MathOpt proto equivalence assertion for tests.

def assert_protos_equiv( self, actual: google.protobuf.message.Message, expected: google.protobuf.message.Message) -> None:
52    def assert_protos_equiv(
53        self, actual: message.Message, expected: message.Message
54    ) -> None:
55        """Asserts the input protos are equivalent, see module doc for details."""
56        return assert_protos_equiv(self, actual, expected)

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

Inherited Members
unittest.case.TestCase
TestCase
failureException
longMessage
maxDiff
addTypeEqualityFunc
addCleanup
enterContext
addClassCleanup
enterClassContext
setUp
tearDown
setUpClass
tearDownClass
countTestCases
defaultTestResult
shortDescription
id
subTest
run
doCleanups
doClassCleanups
debug
skipTest
fail
assertFalse
assertTrue
assertRaises
assertWarns
assertLogs
assertNoLogs
assertEqual
assertNotEqual
assertAlmostEqual
assertNotAlmostEqual
assertSequenceEqual
assertListEqual
assertTupleEqual
assertSetEqual
assertIn
assertNotIn
assertIs
assertIsNot
assertDictEqual
assertCountEqual
assertMultiLineEqual
assertLess
assertLessEqual
assertGreater
assertGreaterEqual
assertIsNone
assertIsNotNone
assertIsInstance
assertNotIsInstance
assertRaisesRegex
assertWarnsRegex
assertRegex
assertNotRegex