diff --git a/internal/test/api.go b/internal/test/api.go index cc737ad5..7795e27d 100644 --- a/internal/test/api.go +++ b/internal/test/api.go @@ -3,12 +3,15 @@ package test import ( "context" "fmt" + "io" + "net/http" "testing" queryapi "queryorchestration/api/queryAPI" "queryorchestration/internal/serviceconfig" "github.com/docker/go-connections/nat" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -64,3 +67,11 @@ var QueryAPI = API{ var apis = []API{ QueryAPI, } + +func AssertStatus(t testing.TB, statusCode int, response *http.Response) { + if !assert.Equal(t, statusCode, response.StatusCode) && response.Body != nil { + response, err := io.ReadAll(response.Body) + require.NoError(t, err) + t.Log("response: ", string(response)) + } +} diff --git a/internal/test/api_test.go b/internal/test/api_test.go new file mode 100644 index 00000000..88efe489 --- /dev/null +++ b/internal/test/api_test.go @@ -0,0 +1,37 @@ +package test_test + +import ( + "io" + "net/http" + "strings" + "testing" + + "queryorchestration/internal/test" + + "github.com/stretchr/testify/assert" +) + +func TestAssertStatus(t *testing.T) { + t.Run("pass", func(t *testing.T) { + mockT := &testing.T{} + test.AssertStatus(mockT, http.StatusOK, &http.Response{ + StatusCode: 200, + }) + assert.False(t, mockT.Failed()) + }) + t.Run("fail with nil body", func(t *testing.T) { + mockT := &testing.T{} + test.AssertStatus(mockT, http.StatusOK, &http.Response{ + StatusCode: 500, + }) + assert.True(t, mockT.Failed()) + }) + t.Run("fail with body", func(t *testing.T) { + mockT := &testing.T{} + test.AssertStatus(mockT, http.StatusOK, &http.Response{ + StatusCode: 500, + Body: io.NopCloser(strings.NewReader("my body")), + }) + assert.True(t, mockT.Failed()) + }) +} diff --git a/internal/test/queryAPI/service.go b/internal/test/queryAPI/service.go index c8eb46ba..22cc7003 100644 --- a/internal/test/queryAPI/service.go +++ b/internal/test/queryAPI/service.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "queryorchestration/internal/test" queryapi "queryorchestration/pkg/queryAPI" "github.com/google/uuid" @@ -84,7 +85,7 @@ func CreateDependentQueries(t testing.TB, client *queryapi.ClientWithResponses) Type: queryapi.CONTEXTFULL, }) require.NoError(t, err) - require.Equal(t, http.StatusCreated, contextQueryRes.StatusCode()) + test.AssertStatus(t, http.StatusCreated, contextQueryRes.HTTPResponse) jcfg := `{"path":"keyone"}` jsonQueryRes, err := client.CreateQueryWithResponse(t.Context(), queryapi.QueryCreate{ Type: queryapi.JSONEXTRACTOR, @@ -92,7 +93,7 @@ func CreateDependentQueries(t testing.TB, client *queryapi.ClientWithResponses) RequiredQueries: &[]types.UUID{contextQueryRes.JSON201.Id}, }) require.NoError(t, err) - require.Equal(t, http.StatusCreated, jsonQueryRes.StatusCode()) + test.AssertStatus(t, http.StatusCreated, jsonQueryRes.HTTPResponse) return contextQueryRes.JSON201.Id, jsonQueryRes.JSON201.Id } diff --git a/scripts/tests.yml b/scripts/tests.yml index a9625a90..c687be4c 100644 --- a/scripts/tests.yml +++ b/scripts/tests.yml @@ -25,7 +25,7 @@ vars: echo "4" # Default fallback value fi TEST_PARALLEL: - sh: echo $(( {{.CPU_COUNT}} * 2)) + sh: echo $(( {{.CPU_COUNT}} )) # yamllint disable-line rule:line-length EXCLUDED_FILES: ".gen.go|internal/serviceconfig/observability/prometheus/generator/main.go|internal/cognitoauth/middleware.go|internal/cognitoauth/token.go|internal/cognitoauth/auth.go|internal/cognitoauth/handler.go|internal/cognitoauth/models.go|api/queryAPI/authHandlers.go|api/queryAPI/homehandler.go" diff --git a/test/process_test.go b/test/process_test.go index e9fe2921..968b5388 100644 --- a/test/process_test.go +++ b/test/process_test.go @@ -119,28 +119,39 @@ func TestProcess(t *testing.T) { "JSON_QUERY": "valuetwo", } - queryapitest.WaitForClientStatus(t, t.Context(), net.Client, clientId, queryapi.NOTSYNCED) queryapitest.WaitForClientStatus(t, t.Context(), net.Client, clientId, queryapi.INSYNC) - docRes, err = net.Client.GetDocumentWithResponse(t.Context(), doc.Id) - require.NoError(t, err) - require.NotNil(t, docRes) - require.NotNil(t, docRes.JSON200) - assert.EqualExportedValues(t, expectedDoc, *docRes.JSON200) + wg.Add(3) + go func(t testing.TB) { + docRes, err = net.Client.GetDocumentWithResponse(t.Context(), doc.Id) + require.NoError(t, err) + require.NotNil(t, docRes) + require.NotNil(t, docRes.JSON200) + assert.EqualExportedValues(t, expectedDoc, *docRes.JSON200) + wg.Done() + }(t) - testRes, err := net.Client.TestQueryWithResponse(t.Context(), jsonId, queryapi.QueryTestRequest{ - QueryVersion: 1, - DocumentId: doc.Id, - }) - require.NoError(t, err) - assert.Equal(t, "valueone", testRes.JSON200.Value) + go func(t testing.TB) { + testRes, err := net.Client.TestQueryWithResponse(t.Context(), jsonId, queryapi.QueryTestRequest{ + QueryVersion: 1, + DocumentId: doc.Id, + }) + require.NoError(t, err) + assert.Equal(t, "valueone", testRes.JSON200.Value) + wg.Done() + }(t) - testRes, err = net.Client.TestQueryWithResponse(t.Context(), jsonId, queryapi.QueryTestRequest{ - QueryVersion: 2, - DocumentId: doc.Id, - }) - require.NoError(t, err) - assert.Equal(t, "valuetwo", testRes.JSON200.Value) + go func(t testing.TB) { + testRes, err := net.Client.TestQueryWithResponse(t.Context(), jsonId, queryapi.QueryTestRequest{ + QueryVersion: 2, + DocumentId: doc.Id, + }) + require.NoError(t, err) + assert.Equal(t, "valuetwo", testRes.JSON200.Value) + wg.Done() + }(t) + + wg.Wait() } const pdfHelloWorld = `%PDF-1.4 diff --git a/test/queryAPI/client_test.go b/test/queryAPI/client_test.go index 912f480c..4fb54df2 100644 --- a/test/queryAPI/client_test.go +++ b/test/queryAPI/client_test.go @@ -26,7 +26,7 @@ func TestClient(t *testing.T) { clientErrRes, err := client.GetClientWithResponse(ctx, "INVALID") require.NoError(t, err) - assert.Equal(t, http.StatusBadRequest, clientErrRes.StatusCode()) + test.AssertStatus(t, http.StatusBadRequest, clientErrRes.HTTPResponse) assert.Equal(t, "Unable to get client: no rows in result set", clientErrRes.JSON400.Message) idRes, err := client.CreateClientWithResponse(ctx, queryapi.ClientCreate{ @@ -34,14 +34,14 @@ func TestClient(t *testing.T) { Id: "EXA", }) require.NoError(t, err) - require.Equal(t, http.StatusCreated, idRes.StatusCode()) + test.AssertStatus(t, http.StatusCreated, idRes.HTTPResponse) assert.NotNil(t, idRes.JSON201) assert.Equal(t, "EXA", idRes.JSON201.Id) id := idRes.JSON201.Id clientRes, err := client.GetClientWithResponse(ctx, id) require.NoError(t, err) - require.Equal(t, http.StatusOK, clientRes.StatusCode()) + test.AssertStatus(t, http.StatusOK, clientRes.HTTPResponse) assert.Equal(t, id, clientRes.JSON200.Id) assert.Equal(t, "example_name", clientRes.JSON200.Name) assert.False(t, clientRes.JSON200.CanSync) diff --git a/test/queryAPI/collectorservice_test.go b/test/queryAPI/collectorservice_test.go index e2d4238b..8c931bce 100644 --- a/test/queryAPI/collectorservice_test.go +++ b/test/queryAPI/collectorservice_test.go @@ -2,6 +2,7 @@ package endtoend_test import ( "context" + "net/http" "regexp" "testing" @@ -61,6 +62,7 @@ func TestCollectorService(t *testing.T) { collRes, err := client.GetCollectorByClientIdWithResponse(ctx, id) require.NoError(t, err) + test.AssertStatus(t, http.StatusOK, collRes.HTTPResponse) assert.Equal(t, id, collRes.JSON200.ClientId) assert.Equal(t, int32(0), collRes.JSON200.ActiveVersion) assert.Equal(t, int32(0), collRes.JSON200.LatestVersion) @@ -83,10 +85,11 @@ func TestCollectorService(t *testing.T) { Fields: &fields, }) require.NoError(t, err) - assert.Equal(t, 200, uRes.StatusCode()) + test.AssertStatus(t, http.StatusOK, uRes.HTTPResponse) collRes, err = client.GetCollectorByClientIdWithResponse(ctx, id) require.NoError(t, err) + test.AssertStatus(t, http.StatusOK, collRes.HTTPResponse) assert.Equal(t, id, collRes.JSON200.ClientId) assert.Equal(t, int32(1), collRes.JSON200.ActiveVersion) assert.Equal(t, int32(1), collRes.JSON200.LatestVersion)