Documentation
¶
Overview ¶
Package testconn holds a collection of common test procedures. They provide a framework to write mock tests.
Index ¶
- Constants
- func ComparatorPagination(serverURL string, actual *common.ReadResult, expected *common.ReadResult) *testutils.CompareResult
- func ComparatorSortedSubsetReadByIds(serverURL string, actual, expected []common.ReadResultRow) *testutils.CompareResult
- func ComparatorSubscriptionSuccess(_ string, actual, _ *common.SubscriptionResult) *testutils.CompareResult
- func ComparatorSubscriptionWithoutResult(_ string, actual, expected *common.SubscriptionResult) *testutils.CompareResult
- func ComparatorSubsetBatchWrite(_ string, actual, expected *common.BatchWriteResult) *testutils.CompareResult
- func ComparatorSubsetMetadata(_ string, actual, expected *common.ListObjectMetadataResult) *testutils.CompareResult
- func ComparatorSubsetRead(serverURL string, actual, expected *common.ReadResult) *testutils.CompareResult
- func ComparatorSubsetReadSorted(serverURL string, actual, expected *common.ReadResult) *testutils.CompareResult
- func ComparatorSubsetUpsertMetadata(_ string, actual, expected *common.UpsertMetadataResult) *testutils.CompareResult
- func ComparatorSubsetWrite(_ string, actual, expected *common.WriteResult) *testutils.CompareResult
- func ResolveTestServerURL(urlTemplate string, serverURL string) string
- type Comparator
- type ConnectorBuilder
- type DeleteSubscriptionParams
- type InputMutator
- type None
- type Proxy
- type ReadByIdsParams
- type SubscriptionEventExpected
- type SubscriptionEventExpectedData
- type SubscriptionEventExpectedErr
- type TestCase
- type TestCaseBatchWrite
- type TestCaseDelete
- type TestCaseDeleteMetadata
- type TestCaseDeleteSubscription
- type TestCaseGetPostAuthInfo
- type TestCaseGetRecordsByIds
- type TestCaseListObjectMetadata
- type TestCaseRead
- type TestCaseSearch
- type TestCaseSubscribe
- type TestCaseSubscriptionEvent
- type TestCaseUpdateSubscription
- type TestCaseUpsertMetadata
- type TestCaseVerifyWebhookMessage
- type TestCaseWrite
- type TestableBatchReader
- type TestableBatchWriter
- type TestableDeleter
- type TestableMetadataDeleter
- type TestableMetadataReader
- type TestableMetadataUpdater
- type TestablePostAuthMetadata
- type TestableReader
- type TestableSearcher
- type TestableSubscriptionCreator
- type TestableSubscriptionRemover
- type TestableSubscriptionUpdater
- type TestableWebhookMessageVerifier
- type TestableWriter
- type UpdateSubscriptionParams
- type WebhookMessageVerificationParams
Constants ¶
const URLTestServer = "{{testServerURL}}"
URLTestServer is an alias to mock server BaseURL. For usage please refer to ComparatorPagination.
Variables ¶
This section is empty.
Functions ¶
func ComparatorPagination ¶
func ComparatorPagination( serverURL string, actual *common.ReadResult, expected *common.ReadResult, ) *testutils.CompareResult
ComparatorPagination will check pagination related fields. Note: you may use an alias for Mock-Server-URL which will be dynamically resolved at runtime. Example:
common.ReadResult{
NextPage: testconn.URLTestServer + "/v3/contacts?cursor=bGltaXQ9MSZuZXh0PTI="
}
At runtime this may look as follows: http://127.0.0.1:38653/v3/contacts?cursor=bGltaXQ9MSZuZXh0PTI=. The query parameters in URL can be in different order, encoding could differ as soon as the URL content matches the check will conclude that pagination matches.
func ComparatorSortedSubsetReadByIds ¶
func ComparatorSortedSubsetReadByIds(serverURL string, actual, expected []common.ReadResultRow, ) *testutils.CompareResult
ComparatorSortedSubsetReadByIds compares two slices of ReadResultRow as a subset, ignoring order and focusing only on relevant fields: raw data, associations, and identifiers.
The `actual` slice is sorted by ID to ensure consistent output. The `expected` slice must be pre-sorted in the desired order. We intentionally do not sort `expected` so that mismatch logs can correctly report the index positions that differ.
Sort: Ascending order of common.ReadResultRow.Id.
func ComparatorSubscriptionSuccess ¶
func ComparatorSubscriptionSuccess( _ string, actual, _ *common.SubscriptionResult, ) *testutils.CompareResult
ComparatorSubscriptionSuccess verifies the operation returned a successful subscription result.
func ComparatorSubscriptionWithoutResult ¶
func ComparatorSubscriptionWithoutResult( _ string, actual, expected *common.SubscriptionResult, ) *testutils.CompareResult
ComparatorSubscriptionWithoutResult compares subscription results without inspecting the nested Result payload. It is useful when the Result field is irrelevant for the test case.
If Result data structure should be compared use ComparatorSubscriptionWithResult.
func ComparatorSubsetBatchWrite ¶
func ComparatorSubsetBatchWrite(_ string, actual, expected *common.BatchWriteResult) *testutils.CompareResult
ComparatorSubsetBatchWrite compares two BatchWriteResult objects, performing subset matching for individual WriteResult entries while ensuring batch-level metrics (Status, SuccessCount, FailureCount) match exactly.
Error comparison is normalized, allowing flexible matches between strings, Go errors, and mockutils.JSONErrorWrapper values—useful when top-level or per-record errors are represented as structs or JSON.
This enables expressive, stable tests that verify meaningful fields without enforcing strict structural equality across the entire batch.
func ComparatorSubsetMetadata ¶
func ComparatorSubsetMetadata(_ string, actual, expected *common.ListObjectMetadataResult) *testutils.CompareResult
ComparatorSubsetMetadata will check a subset of fields is present. Errors could be an exact match for each object or subset can be used as well. This must be done by specifying expected errors using mockutils.ExpectedSubsetErrors. Then errors.Is() will be applied for each error.
For if this is the case refer to the example below:
Errors: map[string]error{
"arsenal": mockutils.ExpectedSubsetErrors{ // Is doing a subset match.
common.ErrCaller,
errors.New(string(unsupportedResponse)),
},
"arsenal": common.NewHTTPError(http.StatusBadRequest, // Is doing exact match.
headers, body, fmt.Errorf("%w: %s", common.ErrCaller, string(unsupportedResponse))),
},
func ComparatorSubsetRead ¶
func ComparatorSubsetRead(serverURL string, actual, expected *common.ReadResult) *testutils.CompareResult
ComparatorSubsetRead ensures that a subset of fields or raw data is present in the response. This is convenient for cases where the returned data is large, allowing for a more concise test that still validates the desired behavior.
func ComparatorSubsetReadSorted ¶
func ComparatorSubsetReadSorted(serverURL string, actual, expected *common.ReadResult) *testutils.CompareResult
ComparatorSubsetReadSorted is similar to ComparatorSubsetRead but the actual Rows are sorted using the identifiers. This ensures that the rows returned by connector are in the same order for the testing purposes. The test expectation should follow this imposed order. This is important to preserve the indexes of the test reports.
Sort: Ascending order of common.ReadResultRow.Id.
func ComparatorSubsetUpsertMetadata ¶
func ComparatorSubsetUpsertMetadata(_ string, actual, expected *common.UpsertMetadataResult) *testutils.CompareResult
ComparatorSubsetUpsertMetadata compares two UpsertMetadataResult objects, ensuring structural equality for core result properties while allowing subset matching for metadata contents.
Comparison rules:
- Success must match exactly between actual and expected.
- The number of top-level field groups must match.
- For every expected property and field:
- The property and field must exist in the actual result.
- FieldName and Action must match exactly.
- Warnings must match exactly (DeepEqual comparison).
- Metadata is compared using subset semantics — all key/value pairs defined in expected.Metadata must be present in actual.Metadata, but actual may contain additional entries.
func ComparatorSubsetWrite ¶
func ComparatorSubsetWrite(_ string, actual, expected *common.WriteResult) *testutils.CompareResult
ComparatorSubsetWrite compares two WriteResult objects, allowing partial (subset) matching for Data fields while requiring exact matches for Success and RecordId.
It provides flexible error comparison logic:
- Errors are normalized before comparison, allowing strings, Go error types, and mockutils.JSONErrorWrapper values (for JSON-based or struct comparison) to be treated uniformly.
This comparator is typically used when only a subset of Data fields needs verification rather than a full equality check.
func ResolveTestServerURL ¶
Types ¶
type Comparator ¶
type Comparator[Output any] func(serverURL string, actual, expected Output) *testutils.CompareResult
Comparator is an equality function with custom rules for specific test scenarios. Takes server URL actual output, expected output, and returns detailed comparison result.
This package provides the most commonly used comparators like ComparatorSubsetRead, ComparatorPagination, ComparatorSubsetWrite, ComparatorSubsetMetadata for partial field matching in large API responses.
func ComparatorSubscriptionWithResult ¶
func ComparatorSubscriptionWithResult[R any]( resultComparator func(expectedResult, actualResult *R) *testutils.CompareResult, ) Comparator[*common.SubscriptionResult]
ComparatorSubscriptionWithResult returns a comparator for subscription results that first compares the common SubscriptionResult fields and then compares the nested Result values with the provided resultComparator.
The generic type parameter R represents the concrete Result payload type.
Both expected.Result and actual.Result must be pointers. Otherwise, this is not a valid connector implementation. If Result is of no importance use ComparatorSubscriptionWithoutResult.
func ComparatorSubsetMetadataWithMissingFields ¶
func ComparatorSubsetMetadataWithMissingFields( missingFields map[string][]string, ) Comparator[*common.ListObjectMetadataResult]
ComparatorSubsetMetadataWithMissingFields returns a comparator that checks metadata using ComparatorSubsetMetadata and also verifies that selected fields are absent from the returned object metadata.
Use missingFields to specify object names and the fields that must not be present in either Result[objectName].Fields. Result[objectName].FieldsMap are supported for backwards compatability.
type ConnectorBuilder ¶
ConnectorBuilder is a callback method to construct and configure connector for testing. This is a factory method called for every test suite.
type DeleteSubscriptionParams ¶
type DeleteSubscriptionParams struct {
Params common.SubscribeParams
PreviousResult *common.SubscriptionResult
}
type InputMutator ¶
InputMutator optionally transforms the test input before it is passed to the method under test.
It is useful when the input needs to depend on the mock server state, for example when embedding the server URL into request bodies, headers, or other fields that cannot be known until the test server is created.
If nil, the original Input value is used unchanged.
type Proxy ¶
type Proxy struct {
Name string
Builder ConnectorBuilder[connectors.ProxyConnector]
ExpectedProxy *connectors.ProxyConfig
ExpectedModuleProxy *connectors.ProxyConfig
}
Proxy is a test suite useful for testing connectors.ProxyConnector interface.
type ReadByIdsParams ¶
type SubscriptionEventExpected ¶
type SubscriptionEventExpected struct {
Data SubscriptionEventExpectedData
Err SubscriptionEventExpectedErr
}
type TestCase ¶
type TestCase[Input any, Output any] struct { // Name of the test suite. Name string // Input passed to the tested method. Input Input // InputMutator optionally transforms the input using the mock server before // the test executes. Call PrepareInput to obtain the final input with this // transformation applied. InputMutator InputMutator[Input] // Mock Server which connector will call. Server *httptest.Server // Custom Comparator of how expected output agrees with actual output. Comparator Comparator[Output] // Expected return value. Expected Output // ExpectedErrs is a list of errors that must be present in error output. ExpectedErrs []error }
TestCase describes major components that are used to test any Connector methods. It is universal and generic `Input` data type is what the tested method accepts, while `Output` value represents the data type of the expected output.
func (TestCase[Input, Output]) PrepareInput ¶
func (c TestCase[Input, Output]) PrepareInput() Input
type TestCaseBatchWrite ¶
type TestCaseBatchWrite batchWriteType
TestCaseBatchWrite is a test suite useful for testing connectors.BatchWriteConnector interface.
func (TestCaseBatchWrite) Run ¶
func (m TestCaseBatchWrite) Run(t *testing.T, builder ConnectorBuilder[TestableBatchWriter])
Run provides a procedure to test connectors.BatchWriteConnector
type TestCaseDelete ¶
type TestCaseDelete deleteType
TestCaseDelete is a test suite useful for testing connectors.DeleteConnector interface.
func (TestCaseDelete) Run ¶
func (d TestCaseDelete) Run(t *testing.T, builder ConnectorBuilder[TestableDeleter])
Run provides a procedure to test connectors.DeleteConnector
type TestCaseDeleteMetadata ¶
type TestCaseDeleteMetadata deleteMetadataType
TestCaseDeleteMetadata is a test suite useful for testing connectors.DeleteMetadataConnector interface.
func (TestCaseDeleteMetadata) Run ¶
func (m TestCaseDeleteMetadata) Run(t *testing.T, builder ConnectorBuilder[TestableMetadataDeleter])
Run provides a procedure to test connectors.DeleteMetadataConnector.
func (TestCaseDeleteMetadata) RunWithContext ¶
func (m TestCaseDeleteMetadata) RunWithContext(t *testing.T, ctx context.Context, builder ConnectorBuilder[TestableMetadataDeleter], )
RunWithContext provides a procedure to test connectors.DeleteMetadataConnector.
type TestCaseDeleteSubscription ¶
type TestCaseDeleteSubscription deleteSubscriptionType
TestCaseDeleteSubscription is a test suite useful for testing part of connectors.SubscribeConnector interface.
func (TestCaseDeleteSubscription) Run ¶
func (s TestCaseDeleteSubscription) Run(t *testing.T, builder ConnectorBuilder[TestableSubscriptionRemover])
Run provides a procedure to test connectors.SubscribeConnector
type TestCaseGetPostAuthInfo ¶
type TestCaseGetPostAuthInfo postAuthInfoType
TestCaseGetPostAuthInfo is a test suite useful for testing connectors.AuthMetadataConnector interface.
func (TestCaseGetPostAuthInfo) Run ¶
func (r TestCaseGetPostAuthInfo) Run(t *testing.T, builder ConnectorBuilder[TestablePostAuthMetadata])
Run provides a procedure to test connectors.AuthMetadataConnector
type TestCaseGetRecordsByIds ¶
type TestCaseGetRecordsByIds readByIdsType
TestCaseGetRecordsByIds is a test suite useful for testing connectors.BatchRecordReaderConnector interface.
func (TestCaseGetRecordsByIds) Run ¶
func (r TestCaseGetRecordsByIds) Run(t *testing.T, builder ConnectorBuilder[TestableBatchReader])
Run provides a procedure to test connectors.BatchRecordReaderConnector
type TestCaseListObjectMetadata ¶
type TestCaseListObjectMetadata metadataType
TestCaseListObjectMetadata is a test suite useful for testing connectors.ObjectMetadataConnector interface.
func (TestCaseListObjectMetadata) Run ¶
func (m TestCaseListObjectMetadata) Run(t *testing.T, builder ConnectorBuilder[TestableMetadataReader])
Run provides a procedure to test connectors.ObjectMetadataConnector
type TestCaseRead ¶
type TestCaseRead readType
TestCaseRead is a test suite useful for testing connectors.ReadConnector interface.
func (TestCaseRead) Run ¶
func (r TestCaseRead) Run(t *testing.T, builder ConnectorBuilder[TestableReader])
Run provides a procedure to test connectors.ReadConnector
type TestCaseSearch ¶
type TestCaseSearch searchType
TestCaseSearch is a test suite useful for testing connectors.SearchConnector interface.
func (TestCaseSearch) Run ¶
func (s TestCaseSearch) Run(t *testing.T, builder ConnectorBuilder[TestableSearcher])
Run provides a procedure to test connectors.SearchConnector
type TestCaseSubscribe ¶
type TestCaseSubscribe createSubscriptionType
TestCaseSubscribe is a test suite useful for testing part of connectors.SubscribeConnector interface.
func (TestCaseSubscribe) Run ¶
func (s TestCaseSubscribe) Run(t *testing.T, builder ConnectorBuilder[TestableSubscriptionCreator])
Run provides a procedure to test connectors.SubscribeConnector
type TestCaseSubscriptionEvent ¶
type TestCaseSubscriptionEvent struct {
Name string
Input common.Event
Expected []SubscriptionEventExpected
SubscriptionEventListErr error
}
func (TestCaseSubscriptionEvent) Run ¶
func (c TestCaseSubscriptionEvent) Run(t *testing.T)
type TestCaseUpdateSubscription ¶
type TestCaseUpdateSubscription updateSubscriptionType
TestCaseUpdateSubscription is a test suite useful for testing part of connectors.SubscribeConnector interface.
func (TestCaseUpdateSubscription) Run ¶
func (s TestCaseUpdateSubscription) Run(t *testing.T, builder ConnectorBuilder[TestableSubscriptionUpdater])
Run provides a procedure to test connectors.SubscribeConnector
type TestCaseUpsertMetadata ¶
type TestCaseUpsertMetadata upsertMetadataType
TestCaseUpsertMetadata is a test suite useful for testing connectors.UpsertMetadataConnector interface.
func (TestCaseUpsertMetadata) Run ¶
func (m TestCaseUpsertMetadata) Run(t *testing.T, builder ConnectorBuilder[TestableMetadataUpdater])
Run provides a procedure to test connectors.UpsertMetadataConnector.
func (TestCaseUpsertMetadata) RunWithContext ¶
func (m TestCaseUpsertMetadata) RunWithContext(t *testing.T, ctx context.Context, builder ConnectorBuilder[TestableMetadataUpdater], )
RunWithContext provides a procedure to test connectors.UpsertMetadataConnector.
type TestCaseVerifyWebhookMessage ¶
type TestCaseVerifyWebhookMessage webhookMessageVerificationType
TestCaseVerifyWebhookMessage is a test suite useful for testing connectors.WebhookVerifierConnector interface.
func (TestCaseVerifyWebhookMessage) Run ¶
func (r TestCaseVerifyWebhookMessage) Run(t *testing.T, builder ConnectorBuilder[TestableWebhookMessageVerifier], )
Run provides a procedure to test connectors.WebhookVerifierConnector
type TestCaseWrite ¶
type TestCaseWrite writeType
TestCaseWrite is a test suite useful for testing connectors.WriteConnector interface.
func (TestCaseWrite) Run ¶
func (w TestCaseWrite) Run(t *testing.T, builder ConnectorBuilder[TestableWriter])
Run provides a procedure to test connectors.WriteConnector
type TestableBatchReader ¶
type TestableBatchReader interface {
GetRecordsByIds(
ctx context.Context,
objectName string,
recordIds []string,
fields []string,
associations []string,
) ([]common.ReadResultRow, error)
}
TestableBatchReader is the minimal interface for a connector that can batch read records.
type TestableBatchWriter ¶
type TestableBatchWriter interface {
BatchWrite(ctx context.Context, params *common.BatchWriteParam) (*common.BatchWriteResult, error)
}
TestableBatchWriter is the minimal interface for a connector that can batch write records.
type TestableDeleter ¶
type TestableDeleter interface {
Delete(ctx context.Context, params common.DeleteParams) (*common.DeleteResult, error)
}
TestableDeleter is the minimal interface for a connector that can delete records.
type TestableMetadataDeleter ¶
type TestableMetadataDeleter interface {
DeleteMetadata(ctx context.Context, params *common.DeleteMetadataParams) (*common.DeleteMetadataResult, error)
}
TestableMetadataDeleter is the minimal interface for a connector that can delete metadata.
type TestableMetadataReader ¶
type TestableMetadataReader interface {
ListObjectMetadata(ctx context.Context, objectNames []string) (*common.ListObjectMetadataResult, error)
}
TestableMetadataReader is the minimal interface for a connector that can read metadata.
type TestableMetadataUpdater ¶
type TestableMetadataUpdater interface {
UpsertMetadata(ctx context.Context, params *common.UpsertMetadataParams) (*common.UpsertMetadataResult, error)
}
TestableMetadataUpdater is the minimal interface for a connector that can update metadata.
type TestablePostAuthMetadata ¶
type TestablePostAuthMetadata interface {
GetPostAuthInfo(ctx context.Context) (*common.PostAuthInfo, error)
}
TestablePostAuthMetadata is the minimal interface for a connector that returns post auth metadata.
type TestableReader ¶
type TestableReader interface {
Read(ctx context.Context, params common.ReadParams) (*common.ReadResult, error)
}
TestableReader is the minimal interface for a connector that can read records.
type TestableSearcher ¶
type TestableSearcher interface {
Search(ctx context.Context, params *common.SearchParams) (*common.SearchResult, error)
}
TestableSearcher is the minimal interface for a connector that can search records.
type TestableSubscriptionCreator ¶
type TestableSubscriptionCreator interface {
Subscribe(
ctx context.Context,
params common.SubscribeParams,
) (*common.SubscriptionResult, error)
}
TestableSubscriptionCreator is the minimal interface for a connector that can create subscriptions.
type TestableSubscriptionRemover ¶
type TestableSubscriptionRemover interface {
DeleteSubscription(
ctx context.Context,
previousResult common.SubscriptionResult,
) error
}
TestableSubscriptionRemover is the minimal interface for a connector that can delete subscriptions.
type TestableSubscriptionUpdater ¶
type TestableSubscriptionUpdater interface {
UpdateSubscription(
ctx context.Context,
params common.SubscribeParams,
previousResult *common.SubscriptionResult,
) (*common.SubscriptionResult, error)
}
TestableSubscriptionUpdater is the minimal interface for a connector that can update subscriptions.
type TestableWebhookMessageVerifier ¶
type TestableWebhookMessageVerifier interface {
VerifyWebhookMessage(
ctx context.Context,
request *common.WebhookRequest,
params *common.VerificationParams,
) (bool, error)
}
TestableWebhookMessageVerifier is the minimal interface for a connector that can verify webhook messages.
type TestableWriter ¶
type TestableWriter interface {
Write(ctx context.Context, params common.WriteParams) (*common.WriteResult, error)
}
TestableWriter is the minimal interface for a connector that can write records.
type UpdateSubscriptionParams ¶
type UpdateSubscriptionParams struct {
Params common.SubscribeParams
PreviousResult *common.SubscriptionResult
}
type WebhookMessageVerificationParams ¶
type WebhookMessageVerificationParams struct {
Request *common.WebhookRequest
Params *common.VerificationParams
}