Skip to content

Commit

Permalink
refactor: fix testifylint.go-require lint issues (#3107)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored May 27, 2024
1 parent b18d028 commit d2a6bd5
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 95 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ linters-settings:
testifylint:
disable-all: true
enable:
- blank-import
- bool-compare
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- float-compare
- go-require
- len
- negative-positive
- nil-compare
- require-error
- useless-assert

linters:
disable-all: true
Expand Down
27 changes: 17 additions & 10 deletions _examples/chat/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"testing"

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

"github.com/99designs/gqlgen/client"
Expand Down Expand Up @@ -38,9 +39,11 @@ func TestChatSubscriptions(t *testing.T) {
}

msg.err = sub.Next(&msg.resp)
require.NoError(t, msg.err, "sub.Next")
require.Equal(t, "You've joined the room", msg.resp.MessageAdded.Text)
require.Equal(t, "system", msg.resp.MessageAdded.CreatedBy)
if !assert.NoError(t, msg.err, "sub.Next") {
return
}
assert.Equal(t, "You've joined the room", msg.resp.MessageAdded.Text)
assert.Equal(t, "system", msg.resp.MessageAdded.CreatedBy)

go func() {
var resp any
Expand All @@ -49,18 +52,22 @@ func TestChatSubscriptions(t *testing.T) {
b:post(text:"Hello Vektah!", roomName:"#gophers%d", username:"andrey") { id }
c:post(text:"Whats up?", roomName:"#gophers%d", username:"vektah") { id }
}`, i, i, i), &resp)
require.NoError(t, err)
assert.NoError(t, err)
}()

msg.err = sub.Next(&msg.resp)
require.NoError(t, msg.err, "sub.Next")
require.Equal(t, "Hello!", msg.resp.MessageAdded.Text)
require.Equal(t, "vektah", msg.resp.MessageAdded.CreatedBy)
if !assert.NoError(t, msg.err, "sub.Next") {
return
}
assert.Equal(t, "Hello!", msg.resp.MessageAdded.Text)
assert.Equal(t, "vektah", msg.resp.MessageAdded.CreatedBy)

msg.err = sub.Next(&msg.resp)
require.NoError(t, msg.err, "sub.Next")
require.Equal(t, "Whats up?", msg.resp.MessageAdded.Text)
require.Equal(t, "vektah", msg.resp.MessageAdded.CreatedBy)
if !assert.NoError(t, msg.err, "sub.Next") {
return
}
assert.Equal(t, "Whats up?", msg.resp.MessageAdded.Text)
assert.Equal(t, "vektah", msg.resp.MessageAdded.CreatedBy)
}(i)
// wait for goroutines to finish every N tests to not starve on CPU
if (i+1)%batchSize == 0 {
Expand Down
6 changes: 3 additions & 3 deletions _examples/starwars/starwars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func TestStarwars(t *testing.T) {
c.MustPost(`{ human(id:"1000") { starships { name length(unit:FOOT) } } }`, &resp)

require.Equal(t, "X-Wing", resp.Human.Starships[0].Name)
require.Equal(t, 41.0105, resp.Human.Starships[0].Length)
require.InDelta(t, 41.0105, resp.Human.Starships[0].Length, 0.0001)

require.Equal(t, "Imperial shuttle", resp.Human.Starships[1].Name)
require.Equal(t, 65.6168, resp.Human.Starships[1].Length)
require.InDelta(t, 65.6168, resp.Human.Starships[1].Length, 0.0001)
})

t.Run("hero height", func(t *testing.T) {
Expand All @@ -81,7 +81,7 @@ func TestStarwars(t *testing.T) {
}
c.MustPost(`{ hero(episode:EMPIRE) { ... on Human { height(unit:METER) } } }`, &resp)

require.Equal(t, 1.72, resp.Hero.Height)
require.InDelta(t, 1.72, resp.Hero.Height, 0.001)
})

t.Run("default hero episode", func(t *testing.T) {
Expand Down
63 changes: 36 additions & 27 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/99designs/gqlgen/client"
Expand All @@ -21,15 +22,16 @@ import (
func TestClient(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Equal(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))

err = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"name": "bob",
},
})
require.NoError(t, err)
if assert.NoError(t, err) {
assert.Equal(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))

err = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"name": "bob",
},
})
assert.NoError(t, err)
}
})

c := client.New(h)
Expand All @@ -46,14 +48,17 @@ func TestClient(t *testing.T) {
func TestClientMultipartFormData(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="operations"`)
require.Contains(t, string(bodyBytes), `{"query":"mutation ($input: Input!) {}","variables":{"file":{}}`)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="map"`)
require.Contains(t, string(bodyBytes), `{"0":["variables.file"]}`)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="0"; filename="example.txt"`)
require.Contains(t, string(bodyBytes), `Content-Type: text/plain`)
require.Contains(t, string(bodyBytes), `Hello World`)
if !assert.NoError(t, err) {
return
}

assert.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="operations"`)
assert.Contains(t, string(bodyBytes), `{"query":"mutation ($input: Input!) {}","variables":{"file":{}}`)
assert.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="map"`)
assert.Contains(t, string(bodyBytes), `{"0":["variables.file"]}`)
assert.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="0"; filename="example.txt"`)
assert.Contains(t, string(bodyBytes), `Content-Type: text/plain`)
assert.Contains(t, string(bodyBytes), `Hello World`)

w.Write([]byte(`{}`))
})
Expand Down Expand Up @@ -83,7 +88,7 @@ func TestClientMultipartFormData(t *testing.T) {

func TestAddHeader(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "ASDF", r.Header.Get("Test-Key"))
assert.Equal(t, "ASDF", r.Header.Get("Test-Key"))

w.Write([]byte(`{}`))
})
Expand All @@ -98,7 +103,7 @@ func TestAddHeader(t *testing.T) {

func TestAddClientHeader(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "ASDF", r.Header.Get("Test-Key"))
assert.Equal(t, "ASDF", r.Header.Get("Test-Key"))

w.Write([]byte(`{}`))
})
Expand All @@ -112,9 +117,9 @@ func TestAddClientHeader(t *testing.T) {
func TestBasicAuth(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
require.True(t, ok)
require.Equal(t, "user", user)
require.Equal(t, "pass", pass)
assert.True(t, ok)
assert.Equal(t, "user", user)
assert.Equal(t, "pass", pass)

w.Write([]byte(`{}`))
})
Expand All @@ -130,8 +135,10 @@ func TestBasicAuth(t *testing.T) {
func TestAddCookie(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("foo")
require.NoError(t, err)
require.Equal(t, "value", c.Value)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, "value", c.Value)

w.Write([]byte(`{}`))
})
Expand All @@ -147,14 +154,16 @@ func TestAddCookie(t *testing.T) {
func TestAddExtensions(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Equal(t, `{"query":"user(id:1){name}","extensions":{"persistedQuery":{"sha256Hash":"ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7","version":1}}}`, string(b))
if !assert.NoError(t, err) {
return
}
assert.Equal(t, `{"query":"user(id:1){name}","extensions":{"persistedQuery":{"sha256Hash":"ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7","version":1}}}`, string(b))
err = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"Name": "Bob",
},
})
require.NoError(t, err)
assert.NoError(t, err)
})

c := client.New(h)
Expand Down
Loading

0 comments on commit d2a6bd5

Please sign in to comment.