Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnu-deepsource committed Feb 7, 2024
1 parent 1d89f60 commit 4105ad7
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 17 deletions.
35 changes: 18 additions & 17 deletions command/report/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package report
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -67,20 +68,20 @@ func (c *Client) CompressionEnabled() (bool, error) {
return false, nil
}

// const createArtifactMutationNew = `mutation($input: CreateArtifactInput!) {
// createArtifact(input: $input) {
// ok
// message
// error
// }
// }`

// const createArtifactMutationOld = `mutation($input: CreateArtifactInput!) {
// createArtifact(input: $input) {
// ok
// error
// }
// }`
const createArtifactMutationNew = `mutation($input: CreateArtifactInput!) {
createArtifact(input: $input) {
ok
message
error
}
}`

const createArtifactMutationOld = `mutation($input: CreateArtifactInput!) {
createArtifact(input: $input) {
ok
error
}
}`

type CreateArtifactResponse struct {
CreateArtifact CreateArtifact `json:"createArtifact"`
Expand All @@ -105,7 +106,7 @@ type CreateArtifactInput struct {
}

func (c *Client) SendReportNew(input *CreateArtifactInput) (*CreateArtifactResponse, error) {
req := graphql.NewRequest(reportGraphqlQuery)
req := graphql.NewRequest(createArtifactMutationNew)
req.Var("input", input)

resp := CreateArtifactResponse{}
Expand All @@ -114,12 +115,12 @@ func (c *Client) SendReportNew(input *CreateArtifactInput) (*CreateArtifactRespo
if err != nil {
return nil, err
}

fmt.Println(resp)
return &resp, nil
}

func (c *Client) SendReportOld(input *CreateArtifactInput) (*CreateArtifactResponse, error) {
req := graphql.NewRequest(reportGraphqlQueryOld)
req := graphql.NewRequest(createArtifactMutationOld)
req.Var("input", input)

resp := CreateArtifactResponse{}
Expand Down
168 changes: 168 additions & 0 deletions command/report/client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package report

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClient_CompressionEnabled(t *testing.T) {
Expand Down Expand Up @@ -60,3 +66,165 @@ func TestClient_CompressionEnabled(t *testing.T) {
})
}
}

type artifactRequest struct {
Query string `json:"query"`
Variables struct {
Input *CreateArtifactInput `json:"input"`
}
}

type artifactResponse struct {
Data struct {
CreateArtifact CreateArtifact `json:"createArtifact"`
} `json:"data"`
}

func TestClient_SendReportNew(t *testing.T) {
data, err := os.ReadFile("./tests/golden_files/report_graphql_request_body.json")
require.NoError(t, err, "failed to read test data")

request := artifactRequest{}
err = json.Unmarshal(data, &request)
require.NoError(t, err, "failed to unmarshal test data")

input := &CreateArtifactInput{
AccessToken: request.Variables.Input.AccessToken,
CommitOID: request.Variables.Input.CommitOID,
ReporterName: request.Variables.Input.ReporterName,
ReporterVersion: request.Variables.Input.ReporterVersion,
Data: request.Variables.Input.Data,
AnalyzerShortcode: request.Variables.Input.AnalyzerShortcode,
AnalyzerType: request.Variables.Input.AnalyzerType,
Metadata: request.Variables.Input.Metadata,
}

t.Run("server success", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "failed to read request body")

payload := artifactRequest{}
err = json.Unmarshal(body, &payload)
require.NoError(t, err, "failed to unmarshal request body")

assert.Equal(t, createArtifactMutationNew, payload.Query)

response := artifactResponse{
Data: struct {
CreateArtifact CreateArtifact `json:"createArtifact"`
}{
CreateArtifact: CreateArtifact{
Ok: true,
},
},
}

body, err = json.Marshal(response)
require.NoError(t, err, "failed to marshal response")
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(body); err != nil {
t.Error(err)
}
}))

client := NewGraphQLClient(server.URL, false)
response, err := client.SendReportNew(input)

require.NoError(t, err, "failed to send report")
assert.True(t, response.CreateArtifact.Ok, "report failed")
})

t.Run("server error", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, err, "failed to marshal response")
w.WriteHeader(http.StatusInternalServerError)

}))

client := NewGraphQLClient(server.URL, false)
response, err := client.SendReportNew(input)

require.Error(t, err, "expected error")
require.Nil(t, response, "expected nil response")
})

}

func TestClient_SendReportOld(t *testing.T) {
data, err := os.ReadFile("./tests/golden_files/report_graphql_request_body.json")
require.NoError(t, err, "failed to read test data")

request := artifactRequest{}
err = json.Unmarshal(data, &request)
require.NoError(t, err, "failed to unmarshal test data")

input := &CreateArtifactInput{
AccessToken: request.Variables.Input.AccessToken,
CommitOID: request.Variables.Input.CommitOID,
ReporterName: request.Variables.Input.ReporterName,
ReporterVersion: request.Variables.Input.ReporterVersion,
Data: request.Variables.Input.Data,
AnalyzerShortcode: request.Variables.Input.AnalyzerShortcode,
AnalyzerType: request.Variables.Input.AnalyzerType,
Metadata: request.Variables.Input.Metadata,
}

t.Run("server success", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "failed to read request body")

payload := artifactRequest{}
err = json.Unmarshal(body, &payload)
require.NoError(t, err, "failed to unmarshal request body")

assert.Equal(t, createArtifactMutationOld, payload.Query)

response := artifactResponse{
Data: struct {
CreateArtifact CreateArtifact `json:"createArtifact"`
}{
CreateArtifact: CreateArtifact{
Ok: true,
Message: "test-message",
Error: "test-error",
},
},
}

body, err = json.Marshal(response)
require.NoError(t, err, "failed to marshal response")

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(body); err != nil {
t.Error(err)
}
}))

client := NewGraphQLClient(server.URL, false)
response, err := client.SendReportOld(input)

require.NoError(t, err, "failed to send report")
assert.True(t, response.CreateArtifact.Ok, "report failed")
assert.Equal(t, "test-message", response.CreateArtifact.Message, "unexpected message")
assert.Equal(t, "test-error", response.CreateArtifact.Error, "unexpected error")
})

t.Run("server error", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, err, "failed to marshal response")
w.WriteHeader(http.StatusInternalServerError)

}))

client := NewGraphQLClient(server.URL, false)
response, err := client.SendReportOld(input)

require.Error(t, err, "expected error")
require.Nil(t, response, "expected nil response")
})

}

0 comments on commit 4105ad7

Please sign in to comment.