Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_matcher.py
Go to the documentation of this file.
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
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