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