Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions internal/assert/assertbson/assertbson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) MongoDB, Inc. 2025-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package assertbson

import (
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/internal/assert"
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
)

type tHelper interface {
Helper()
}

// EqualDocument asserts that the expected and actual BSON documents are equal.
// If the documents are not equal, it prints both the binary diff and Extended
// JSON representation of the BSON documents.
func EqualDocument(t assert.TestingT, expected, actual []byte) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return assert.Equal(t,
expected,
actual,
`expected and actual BSON documents do not match
As Extended JSON:
Expected: %s
Actual : %s`,
bson.Raw(expected),
bson.Raw(actual))
}

// EqualValue asserts that the expected and actual BSON values are equal. If the
// values are not equal, it prints both the binary diff and Extended JSON
// representation of the BSON values.
func EqualValue[T bson.RawValue | bsoncore.Value](t assert.TestingT, expected, actual T) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return assert.Equal(t,
expected,
actual,
`expected and actual BSON values do not match
As Extended JSON:
Expected: %s
Actual : %s`,
expected,
actual)
}
249 changes: 249 additions & 0 deletions internal/assert/assertbson/assertbson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Copyright (C) MongoDB, Inc. 2025-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package assertbson

import (
"testing"

"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
)

func TestEqualDocument(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
expected []byte
actual []byte
want bool
}{
{
name: "equal bson.Raw",
expected: bson.Raw{5, 0, 0, 0, 0},
actual: bson.Raw{5, 0, 0, 0, 0},
want: true,
},
{
name: "different bson.Raw",
expected: bson.Raw{8, 0, 0, 0, 10, 120, 0, 0},
actual: bson.Raw{5, 0, 0, 0, 0},
want: false,
},
{
name: "invalid bson.Raw",
expected: bson.Raw{99, 99, 99, 99},
actual: bson.Raw{5, 0, 0, 0, 0},
want: false,
},
{
name: "nil bson.Raw",
expected: bson.Raw(nil),
actual: bson.Raw(nil),
want: true,
},
}

for _, tc := range testCases {
tc := tc // Capture range variable.

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got := EqualDocument(new(testing.T), tc.expected, tc.actual)
if got != tc.want {
t.Errorf("EqualDocument(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
}
})
}
}

func TestEqualValue(t *testing.T) {
t.Parallel()

t.Run("bson.RawValue", func(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
expected bson.RawValue
actual bson.RawValue
want bool
}{
{
name: "equal",
expected: bson.RawValue{
Type: bson.TypeInt32,
Value: []byte{1, 0, 0, 0},
},
actual: bson.RawValue{
Type: bson.TypeInt32,
Value: []byte{1, 0, 0, 0},
},
want: true,
},
{
name: "same type, different value",
expected: bson.RawValue{
Type: bson.TypeInt32,
Value: []byte{1, 0, 0, 0},
},
actual: bson.RawValue{
Type: bson.TypeInt32,
Value: []byte{1, 1, 1, 1},
},
want: false,
},
{
name: "same value, different type",
expected: bson.RawValue{
Type: bson.TypeDouble,
Value: []byte{1, 0, 0, 0, 0, 0, 0, 0},
},
actual: bson.RawValue{
Type: bson.TypeInt64,
Value: []byte{1, 0, 0, 0, 0, 0, 0, 0},
},
want: false,
},
{
name: "different value, different type",
expected: bson.RawValue{
Type: bson.TypeInt32,
Value: []byte{1, 0, 0, 0},
},
actual: bson.RawValue{
Type: bson.TypeString,
Value: []byte{1, 1, 1, 1},
},
want: false,
},
{
name: "invalid",
expected: bson.RawValue{
Type: bson.TypeInt64,
Value: []byte{1, 0, 0, 0},
},
actual: bson.RawValue{
Type: bson.TypeInt32,
Value: []byte{1, 0, 0, 0},
},
want: false,
},
{
name: "empty",
expected: bson.RawValue{},
actual: bson.RawValue{},
want: true,
},
}

for _, tc := range testCases {
tc := tc // Capture range variable.

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got := EqualValue(new(testing.T), tc.expected, tc.actual)
if got != tc.want {
t.Errorf("EqualValue(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
}
})
}
})

t.Run("bsoncore.Value", func(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
expected bsoncore.Value
actual bsoncore.Value
want bool
}{
{
name: "equal",
expected: bsoncore.Value{
Type: bsoncore.TypeInt32,
Data: []byte{1, 0, 0, 0},
},
actual: bsoncore.Value{
Type: bsoncore.TypeInt32,
Data: []byte{1, 0, 0, 0},
},
want: true,
},
{
name: "same type, different value",
expected: bsoncore.Value{
Type: bsoncore.TypeInt32,
Data: []byte{1, 0, 0, 0},
},
actual: bsoncore.Value{
Type: bsoncore.TypeInt32,
Data: []byte{1, 1, 1, 1},
},
want: false,
},
{
name: "same value, different type",
expected: bsoncore.Value{
Type: bsoncore.TypeDouble,
Data: []byte{1, 0, 0, 0, 0, 0, 0, 0},
},
actual: bsoncore.Value{
Type: bsoncore.TypeInt64,
Data: []byte{1, 0, 0, 0, 0, 0, 0, 0},
},
want: false,
},
{
name: "different value, different type",
expected: bsoncore.Value{
Type: bsoncore.TypeInt32,
Data: []byte{1, 0, 0, 0},
},
actual: bsoncore.Value{
Type: bsoncore.TypeString,
Data: []byte{1, 1, 1, 1},
},
want: false,
},
{
name: "invalid",
expected: bsoncore.Value{
Type: bsoncore.TypeInt64,
Data: []byte{1, 0, 0, 0},
},
actual: bsoncore.Value{
Type: bsoncore.TypeInt32,
Data: []byte{1, 0, 0, 0},
},
want: false,
},
{
name: "empty",
expected: bsoncore.Value{},
actual: bsoncore.Value{},
want: true,
},
}

for _, tc := range testCases {
tc := tc // Capture range variable.

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got := EqualValue(new(testing.T), tc.expected, tc.actual)
if got != tc.want {
t.Errorf("EqualValue(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
}
})
}
})
}
21 changes: 0 additions & 21 deletions internal/assert/assertion_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package assert

import (
"context"
"fmt"
"reflect"
"time"
"unsafe"
Expand Down Expand Up @@ -71,26 +70,6 @@ func DifferentAddressRanges(t TestingT, a, b []byte) (ok bool) {
return false
}

// EqualBSON asserts that the expected and actual BSON binary values are equal.
// If the values are not equal, it prints both the binary and Extended JSON diff
// of the BSON values. The provided BSON value types must implement the
// fmt.Stringer interface.
func EqualBSON(t TestingT, expected, actual interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return Equal(t,
expected,
actual,
`expected and actual BSON values do not match
As Extended JSON:
Expected: %s
Actual : %s`,
expected.(fmt.Stringer).String(),
actual.(fmt.Stringer).String())
}

// Soon runs the provided callback and fails the passed-in test if the callback
// does not complete within timeout. The provided callback should respect the
// passed-in context and cease execution when it has expired.
Expand Down
Loading
Loading