|
| 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 | +"""Test MathOpt's custom proto test assertions.""" |
| 15 | + |
| 16 | +from absl.testing import absltest |
| 17 | +from ortools.math_opt import model_pb2 |
| 18 | +from ortools.math_opt.python import normalize |
| 19 | +from ortools.math_opt.python.testing import compare_proto |
| 20 | + |
| 21 | + |
| 22 | +class MathOptProtoAssertionsTest( |
| 23 | + absltest.TestCase, compare_proto.MathOptProtoAssertions |
| 24 | +): |
| 25 | + |
| 26 | + def test_assertions_match_but_not_equal(self) -> None: |
| 27 | + model_with_empty_vars = model_pb2.ModelProto() |
| 28 | + model_with_empty_vars.variables.SetInParent() |
| 29 | + empty = model_pb2.ModelProto() |
| 30 | + with self.assertRaisesRegex(AssertionError, ".*variables.*"): |
| 31 | + self.assert_protos_equal(model_with_empty_vars, empty) |
| 32 | + with self.assertRaisesRegex(AssertionError, ".*variables.*"): |
| 33 | + self.assert_protos_equal(empty, model_with_empty_vars) |
| 34 | + |
| 35 | + self.assert_protos_equiv(model_with_empty_vars, empty) |
| 36 | + self.assert_protos_equiv(empty, model_with_empty_vars) |
| 37 | + |
| 38 | + normalize.math_opt_normalize_proto(model_with_empty_vars) |
| 39 | + self.assert_protos_equal(model_with_empty_vars, empty) |
| 40 | + self.assert_protos_equal(empty, model_with_empty_vars) |
| 41 | + self.assert_protos_equiv(model_with_empty_vars, empty) |
| 42 | + self.assert_protos_equiv(empty, model_with_empty_vars) |
| 43 | + |
| 44 | + def test_do_not_match(self) -> None: |
| 45 | + with_maximize = model_pb2.ModelProto() |
| 46 | + with_maximize.objective.maximize = True |
| 47 | + empty = model_pb2.ModelProto() |
| 48 | + |
| 49 | + with self.assertRaisesRegex(AssertionError, ".*maximize.*"): |
| 50 | + self.assert_protos_equal(with_maximize, empty) |
| 51 | + with self.assertRaisesRegex(AssertionError, ".*maximize.*"): |
| 52 | + self.assert_protos_equal(empty, with_maximize) |
| 53 | + |
| 54 | + with self.assertRaisesRegex(AssertionError, ".*maximize.*"): |
| 55 | + self.assert_protos_equiv(with_maximize, empty) |
| 56 | + with self.assertRaisesRegex(AssertionError, ".*maximize.*"): |
| 57 | + self.assert_protos_equiv(empty, with_maximize) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + absltest.main() |
0 commit comments