def test_basic_partial_equality(self):
     expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
     actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
     opts = compare.ProtoComparisonOptions(
         scope=compare.ProtoComparisonScope.PARTIAL)
     self.assertProtoCompareToBe(
         compare.proto_compare(actual, expected, opts=opts), True)
    def test_ignore_field_multiple(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        del actual.bars[:]
        actual.baz.status = test_pb2.Baz.OK

        opts = compare.ProtoComparisonOptions(ignore_field_paths={('bars', )})
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), False)
        opts = compare.ProtoComparisonOptions(ignore_field_paths={('baz', )})
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), False)

        opts = compare.ProtoComparisonOptions(
            ignore_field_paths={('bars', ), ('baz', )})
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)
    def test_ignore_field_single(self):
        expected = text_format.Parse('baz { status: ERROR }', test_pb2.Foo())
        actual = text_format.Parse('', test_pb2.Foo())
        self.assertProtoCompareToBe(compare.proto_compare(actual, expected),
                                    False)

        opts = compare.ProtoComparisonOptions(ignore_field_paths={('baz', )})
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)
    def test_repeated_field_partial_inequality(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        expected.bars.add().progress = 0.1

        opts = compare.ProtoComparisonOptions(
            scope=compare.ProtoComparisonScope.PARTIAL)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), False)
    def test_partial_inequality_missing_field(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual.baz.Clear()

        opts = compare.ProtoComparisonOptions(
            scope=compare.ProtoComparisonScope.PARTIAL)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), False)
    def test_ignore_field_nested(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual.bars[0].size = 2
        self.assertProtoCompareToBe(compare.proto_compare(actual, expected),
                                    False)

        opts = compare.ProtoComparisonOptions(ignore_field_paths={('bars',
                                                                   'size')})
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)
    def test_within_fraction_or_margin_float(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        expected.bars[0].progress = 100.0
        actual.bars[0].progress = 109.9
        self.assertProtoCompareToBe(compare.proto_compare(actual, expected),
                                    False)

        # fraction and margin do not matter when |float_comp| is EXACT.
        opts = compare.ProtoComparisonOptions(float_fraction=0.0,
                                              float_margin=10.0)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), False)

        opts = compare.ProtoComparisonOptions(
            float_fraction=0.0,
            float_margin=10.0,
            float_comp=compare.ProtoFloatComparison.APPROXIMATE)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)

        opts = compare.ProtoComparisonOptions(
            float_fraction=0.2,
            float_margin=0.0,
            float_comp=compare.ProtoFloatComparison.APPROXIMATE)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)

        opts = compare.ProtoComparisonOptions(
            float_fraction=0.01,
            float_margin=0.0,
            float_comp=compare.ProtoFloatComparison.APPROXIMATE)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), False)

        opts = compare.ProtoComparisonOptions(
            float_fraction=0.10,
            float_margin=10.0,
            float_comp=compare.ProtoFloatComparison.APPROXIMATE)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)
    def test_aproximate_modified_equality_double(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        expected.bars[0].precision = 2.3 + 1.1e-15
        actual.bars[0].precision = 2.3 + 1.2e-15
        self.assertProtoCompareToBe(compare.proto_compare(actual, expected),
                                    False)

        opts = compare.ProtoComparisonOptions(
            float_comp=compare.ProtoFloatComparison.APPROXIMATE)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)
    def test_compare_proto_repeated_fields_ignoring_order(self):
        expected = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        actual = text_format.Parse(_TEST_PROTO, test_pb2.Foo())
        reversed_bars = actual.bars[::-1]
        del actual.bars[:]
        actual.bars.extend(reversed_bars)
        self.assertProtoCompareToBe(compare.proto_compare(actual, expected),
                                    False)

        opts = compare.ProtoComparisonOptions(
            repeated_field_comp=compare.RepeatedFieldComparison.AS_SET)
        self.assertProtoCompareToBe(
            compare.proto_compare(actual, expected, opts=opts), True)