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
4 changes: 2 additions & 2 deletions providers/hubspot/internal/crm/associations/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ func (c batchCreateTestCase) Run(t *testing.T, builder testroutines.ConnectorBui
testCaseTypeBatchCreate(c).Validate(t, err, output)
}

func batchCreateComparator(_ string, actual, expected *BatchCreateResult) *mockutils.CompareResult {
result := mockutils.NewCompareResult()
func batchCreateComparator(_ string, actual, expected *BatchCreateResult) *testutils.CompareResult {
result := testutils.NewCompareResult()
result.Assert("Success", expected.Success, actual.Success)
result.Merge(mockutils.ErrorNormalizedComparator.EachErrorEquals(
goutils.ToAnySlice(expected.Errors),
Expand Down
3 changes: 1 addition & 2 deletions providers/phoneburner/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/amp-labs/connectors"
"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/test/utils/mockutils"
"github.com/amp-labs/connectors/test/utils/mockutils/mockcond"
"github.com/amp-labs/connectors/test/utils/mockutils/mockserver"
"github.com/amp-labs/connectors/test/utils/testroutines"
Expand Down Expand Up @@ -348,7 +347,7 @@ func TestRead(t *testing.T) { //nolint:funlen,gocognit,cyclop

func comparatorSubsetReadOrderByFolderID(
serverURL string, actual, expected *common.ReadResult,
) *mockutils.CompareResult {
) *testutils.CompareResult {
sort.Slice(actual.Data, func(i, j int) bool {
ai, _ := actual.Data[i].Fields["folder_id"].(string)
aj, _ := actual.Data[j].Fields["folder_id"].(string)
Expand Down
13 changes: 6 additions & 7 deletions providers/salesforce/bulk-info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/test/utils/mockutils"
"github.com/amp-labs/connectors/test/utils/mockutils/mockcond"
"github.com/amp-labs/connectors/test/utils/mockutils/mockserver"
"github.com/amp-labs/connectors/test/utils/testroutines"
Expand Down Expand Up @@ -248,18 +247,18 @@ func TestGetBulkQueryResults(t *testing.T) { // nolint:dupl
}
}

func statusCodeComparator(serverURL string, actual, expected *http.Response) *mockutils.CompareResult {
result := mockutils.NewCompareResult()
func statusCodeComparator(serverURL string, actual, expected *http.Response) *testutils.CompareResult {
result := testutils.NewCompareResult()

result.Assert("StatusCode", expected.StatusCode, actual.StatusCode)

return result
}

func testJobResultsComparator(serverURL string, actual, expected *JobResults) *mockutils.CompareResult {
func testJobResultsComparator(serverURL string, actual, expected *JobResults) *testutils.CompareResult {
actual.JobInfo = nil // ignore JobInfo when comparing

result := mockutils.NewCompareResult()
result := testutils.NewCompareResult()

result.Assert("JobResults", expected, actual)

Expand All @@ -268,8 +267,8 @@ func testJobResultsComparator(serverURL string, actual, expected *JobResults) *m

func testConciseJobInfoComparator(
serverURL string, actual *GetJobInfoResult, expected *GetJobInfoResult,
) *mockutils.CompareResult {
result := mockutils.NewCompareResult()
) *testutils.CompareResult {
result := testutils.NewCompareResult()

result.Assert("Id", expected.Id, actual.Id)
result.Assert("Object", expected.Object, actual.Object)
Expand Down
5 changes: 2 additions & 3 deletions providers/salesforce/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/amp-labs/connectors"
"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/test/utils/mockutils"
"github.com/amp-labs/connectors/test/utils/mockutils/mockcond"
"github.com/amp-labs/connectors/test/utils/mockutils/mockserver"
"github.com/amp-labs/connectors/test/utils/testroutines"
Expand Down Expand Up @@ -358,8 +357,8 @@ func TestListObjectMetadataPardot(t *testing.T) { // nolint:funlen,gocognit,cycl
}.Server(),
Comparator: func(
serverURL string, actual, expected *common.ListObjectMetadataResult,
) *mockutils.CompareResult {
result := mockutils.NewCompareResult()
) *testutils.CompareResult {
result := testutils.NewCompareResult()
// Usual subset comparison.
result.Merge(testroutines.ComparatorSubsetMetadata(serverURL, actual, expected))

Expand Down
13 changes: 8 additions & 5 deletions test/utils/mockutils/batchWriteResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/test/utils/testutils"
)

// BatchWriteResultComparator provides utility methods for comparing BatchWriteResult structures in tests.
Expand All @@ -21,10 +22,12 @@ type batchWriteResultComparator struct{}
// - Subset equality for the Data field of each WriteResult (only expected keys/values are checked).
// - Normalized equality for Errors, supporting struct/JSON, string, or golang error comparison.
// - Exact equality for the Success and RecordId fields.
func (batchWriteResultComparator) SubsetWriteResults(actual, expected *common.BatchWriteResult) *CompareResult {
result := NewCompareResult()
func (batchWriteResultComparator) SubsetWriteResults(
actual, expected *common.BatchWriteResult,
) *testutils.CompareResult {
result := testutils.NewCompareResult()
if len(actual.Results) != len(expected.Results) {
result.AddDiff(fmt.Sprintf("expected %d batch results, got %d", len(expected.Results), len(actual.Results)))
result.AddDiff("expected %d batch results, got %d", len(expected.Results), len(actual.Results))
return result
}

Expand All @@ -37,11 +40,11 @@ func (batchWriteResultComparator) SubsetWriteResults(actual, expected *common.Ba
errorComparison := ErrorNormalizedComparator.EachErrorEquals(actualResult.Errors, expectedResult.Errors)

for _, diff := range dataComparison.Diff {
result.AddDiff(fmt.Sprintf("Result[%d] %s", i, diff))
result.AddDiff("Result[%d] %s", i, diff)
}

for _, diff := range errorComparison.Diff {
result.AddDiff(fmt.Sprintf("Result[%d] %s", i, diff))
result.AddDiff("Result[%d] %s", i, diff)
}

result.Assert(fmt.Sprintf("Result[%d].Success", i), expectedResult.Success, actualResult.Success)
Expand Down
73 changes: 0 additions & 73 deletions test/utils/mockutils/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,9 @@ package mockutils
import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/go-test/deep"
)

// CompareResult holds the result of a comparison operation between actual and expected values.
// It tracks whether the comparison passed (OK) and collects detailed failure messages (Diff)
// for precise test failure diagnostics.
type CompareResult struct {
OK bool // True if comparison passed completely, false otherwise
Diff []string // List of human-readable failure descriptions, empty if OK
}

// NewCompareResult creates a successful comparison result instance.
func NewCompareResult() *CompareResult {
return &CompareResult{OK: true}
}

// AddDiff marks the comparison as failed and appends a custom failure message.
//
// This is the primary way to report simple failures like row count mismatches
// or pagination URL differences.
func (r *CompareResult) AddDiff(diff string) {
r.OK = false
r.Diff = append(r.Diff, diff)
}

// Assert compares two data structures using github.com/go-test/deep.Equal
// and records a formatted mismatch report for the specified data name.
// Returns true if mismatch found (and recorded), false if exact match.
//
// Example output:
//
// Data[0].Fields[stagename] mismatch:
// ❌ Prospecting != PROSPECTING
//
// Data[0].Raw[OpportunityContactRoles] mismatch:
// ❌ map[totalSize]: 2 != 3
//
// No-op (returns false) if structures match exactly.
func (r *CompareResult) Assert(dataName string, expectedData, gotData any) bool {
diff := deep.Equal(gotData, expectedData)
if len(diff) == 0 {
return false
}

list := make([]string, len(diff))
for index, text := range diff {
// Tabulated list of mismatches.
list[index] = fmt.Sprintf("\t❌ %v", text)
}

message := fmt.Sprintf("%v mismatch:\n%v", dataName, strings.Join(list, "\n"))

r.Diff = append(r.Diff, message)
r.OK = false

return true
}

// Merge combines another CompareResult into the receiver.
//
// Updates OK status (true only if both were true) and concatenates all Diff messages.
// Ignores nil other results. Used for chaining multiple sub-comparisons.
func (r *CompareResult) Merge(other *CompareResult) {
if other == nil {
return
}

// Success requires both to succeed.
// Fail if either failed.
r.OK = r.OK && other.OK
r.Diff = append(r.Diff, other.Diff...)
}

func DoesObjectCorrespondToString(object any, correspondent string) bool {
if object == nil && len(correspondent) == 0 {
return true
Expand Down
21 changes: 13 additions & 8 deletions test/utils/mockutils/metadataResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import (
"fmt"

"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/test/utils/testutils"
)

var MetadataResultComparator = metadataResultComparator{}

type metadataResultComparator struct{}

// SubsetFields checks that expected ListObjectMetadataResult fields are a subset of actual metadata result.
func (metadataResultComparator) SubsetFields(actual, expected *common.ListObjectMetadataResult) *CompareResult {
result := NewCompareResult()
func (metadataResultComparator) SubsetFields(
actual, expected *common.ListObjectMetadataResult,
) *testutils.CompareResult {
result := testutils.NewCompareResult()
for objectName, expectedMetadata := range expected.Result {
actualMetadata, ok := actual.Result[objectName]
if !ok {
result.AddDiff(fmt.Sprintf("Result[%s] missing", objectName))
result.AddDiff("Result[%s] missing", objectName)
continue
}

Expand All @@ -27,7 +30,7 @@ func (metadataResultComparator) SubsetFields(actual, expected *common.ListObject
for k, expectedValue := range expectedMetadata.Fields {
actualValue, ok := actualMetadata.Fields[k]
if !ok {
result.AddDiff(fmt.Sprintf("Result[%s].Fields[%s] missing", objectName, k))
result.AddDiff("Result[%s].Fields[%s] missing", objectName, k)
continue
}

Expand All @@ -39,7 +42,7 @@ func (metadataResultComparator) SubsetFields(actual, expected *common.ListObject
for k, expectedValue := range expectedMetadata.FieldsMap {
actualValue, ok := actualMetadata.FieldsMap[k]
if !ok {
result.AddDiff(fmt.Sprintf("Result[%s].FieldsMap[%s] missing", objectName, k))
result.AddDiff("Result[%s].FieldsMap[%s] missing", objectName, k)
continue
}

Expand All @@ -51,12 +54,14 @@ func (metadataResultComparator) SubsetFields(actual, expected *common.ListObject
return result
}

func (metadataResultComparator) SubsetErrors(actual, expected *common.ListObjectMetadataResult) *CompareResult {
result := NewCompareResult()
func (metadataResultComparator) SubsetErrors(
actual, expected *common.ListObjectMetadataResult,
) *testutils.CompareResult {
result := testutils.NewCompareResult()
for objectName, expectedError := range expected.Errors {
actualError, ok := actual.Errors[objectName]
if !ok {
result.AddDiff(fmt.Sprintf("Errors[%s] missing", objectName))
result.AddDiff("Errors[%s] missing", objectName)
continue
}

Expand Down
16 changes: 9 additions & 7 deletions test/utils/mockutils/normalizedErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"reflect"
"strings"

"github.com/amp-labs/connectors/test/utils/testutils"
)

// ErrorNormalizedComparator provides helper methods to compare error values
Expand All @@ -27,8 +29,8 @@ type errorNormalizedComparator struct{}
// 4. Fallback: string comparison via fmt.Sprintf("%v").
//
// It returns true if the two values are considered equivalent under these rules.
func (errorNormalizedComparator) ErrorEquals(actualErr, expectedErr any) *CompareResult {
result := NewCompareResult()
func (errorNormalizedComparator) ErrorEquals(actualErr, expectedErr any) *testutils.CompareResult {
result := testutils.NewCompareResult()
// 1. Direct equality first.
if reflect.DeepEqual(actualErr, expectedErr) {
return result // good
Expand All @@ -51,7 +53,7 @@ func (errorNormalizedComparator) ErrorEquals(actualErr, expectedErr any) *Compar
if expectedJSON, ok := expectedErr.(JSONErrorWrapper); ok {
aJSON, err := json.Marshal(actualErr)
if err != nil {
result.AddDiff(fmt.Sprintf("failed to marshal actual error to JSON: %v", err))
result.AddDiff("failed to marshal actual error to JSON: %v", err)
return result
}

Expand All @@ -77,18 +79,18 @@ func (errorNormalizedComparator) ErrorEquals(actualErr, expectedErr any) *Compar
// according to ErrorEquals.
//
// Order and slice length must match exactly.
func (c errorNormalizedComparator) EachErrorEquals(actual, expected []any) *CompareResult {
result := NewCompareResult()
func (c errorNormalizedComparator) EachErrorEquals(actual, expected []any) *testutils.CompareResult {
result := testutils.NewCompareResult()
if len(actual) != len(expected) {
result.AddDiff(fmt.Sprintf("expected %d errors, got %d", len(expected), len(actual)))
result.AddDiff("expected %d errors, got %d", len(expected), len(actual))
return result
}

for i := range len(actual) {
res := c.ErrorEquals(actual[i], expected[i])

for _, diff := range res.Diff {
result.AddDiff(fmt.Sprintf("Errors[%d] %s", i, diff))
result.AddDiff("Errors[%d] %s", i, diff)
}
}

Expand Down
Loading
Loading