def assertProtoPartiallyEquals( self, expected: Union[str, message.Message], actual: message.Message, ignored_fields: Optional[Iterable[str]] = None, ): """Asserts proto messages are equal except the ignored fields.""" if isinstance(expected, str): expected = text_format.Merge(expected, actual.__class__()) else: expected = copy.deepcopy(expected) actual = copy.deepcopy(actual) # Currently only supports one-level for ignored fields. for ignored_field in ignored_fields or []: expected.ClearField(ignored_field) actual.ClearField(ignored_field) return self.assertProtoEquals(expected, actual)
def assertProtoEqual(self, a: message.Message, b: message.Message, check_initialized: bool = True, normalize_numbers: bool = False, msg: Optional[str] = None) -> None: """Fails with a useful error if a and b aren't equal. Comparison of repeated fields matches the semantics of unittest.TestCase.assertEqual(), ie order and extra duplicates fields matter. Args: self: absltest.TestCase a: proto2 PB instance, or text string representing one. b: proto2 PB instance -- message.Message or subclass thereof. check_initialized: boolean, whether to fail if either a or b isn't initialized. normalize_numbers: boolean, whether to normalize types and precision of numbers before comparison. msg: if specified, is used as the error message on failure. """ pool = descriptor_pool.Default() if isinstance(a, str): a = text_format.Merge(a, b.__class__(), descriptor_pool=pool) for pb in a, b: if check_initialized: errors = pb.FindInitializationErrors() if errors: cast(absltest.TestCase, self).fail(f'Initialization errors: {errors}\n{pb}') if normalize_numbers: normalize_number_fields(pb) cast(absltest.TestCase, self).assertMultiLineEqual( text_format.MessageToString(a, descriptor_pool=pool), text_format.MessageToString(b, descriptor_pool=pool), msg=msg)