ortools.math_opt.python.testing.proto_matcher

A matcher that tests if protos meet MathOpts definition of equivalence.

This is designed to be used with unittest.mock, which is canonical for mocking in Google Python (e.g., see stubby codelabs).

See normalize.py for technical definitions.

The matcher can be used as a replacement for google3.net.proto2.contrib.pyutil.matcher.Proto2Matcher but supports a much smaller set of features.

 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"""A matcher that tests if protos meet MathOpts definition of equivalence.
15
16This is designed to be used with unittest.mock, which is canonical for mocking
17in Google Python (e.g., see stubby codelabs).
18
19See normalize.py for technical definitions.
20
21The matcher can be used as a replacement for
22google3.net.proto2.contrib.pyutil.matcher.Proto2Matcher
23but supports a much smaller set of features.
24"""
25
26import copy
27
28from google.protobuf import message
29from ortools.math_opt.python import normalize
30
31
32class MathOptProtoEquivMatcher:
33    """Matcher that checks if protos are equivalent in the MathOpt sense.
34
35    See normalize.py for technical definitions.
36    """
37
38    def __init__(self, expected: message.Message):
39        self._expected = copy.deepcopy(expected)
40        normalize.math_opt_normalize_proto(self._expected)
41
42    def __eq__(self, actual: message.Message) -> bool:
43        actual = copy.deepcopy(actual)
44        normalize.math_opt_normalize_proto(actual)
45        return str(actual) == str(self._expected)
46
47    def __ne__(self, other: message.Message) -> bool:
48        return not self == other
class MathOptProtoEquivMatcher:
33class MathOptProtoEquivMatcher:
34    """Matcher that checks if protos are equivalent in the MathOpt sense.
35
36    See normalize.py for technical definitions.
37    """
38
39    def __init__(self, expected: message.Message):
40        self._expected = copy.deepcopy(expected)
41        normalize.math_opt_normalize_proto(self._expected)
42
43    def __eq__(self, actual: message.Message) -> bool:
44        actual = copy.deepcopy(actual)
45        normalize.math_opt_normalize_proto(actual)
46        return str(actual) == str(self._expected)
47
48    def __ne__(self, other: message.Message) -> bool:
49        return not self == other

Matcher that checks if protos are equivalent in the MathOpt sense.

See normalize.py for technical definitions.

MathOptProtoEquivMatcher(expected: google.protobuf.message.Message)
39    def __init__(self, expected: message.Message):
40        self._expected = copy.deepcopy(expected)
41        normalize.math_opt_normalize_proto(self._expected)