Skip to content

Commit

Permalink
Improve tests and names
Browse files Browse the repository at this point in the history
  • Loading branch information
hantonelli committed Mar 3, 2019
1 parent c493d1b commit db7a03b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions example/fileupload/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ func TestFileUpload(t *testing.T) {
resolver := &Resolver{
MultipleUploadFunc: func(ctx context.Context, files []graphql.Upload) ([]model.File, error) {
require.Len(t, files, 2)
var contents []string
for i := range files {
require.NotNil(t, files[i].File)
content, err := ioutil.ReadAll(files[i].File)
require.Nil(t, err)
require.Equal(t, string(content), "test")
contents = append(contents, string(content))
}
require.ElementsMatch(t, []string{"test1", "test2"}, contents)
return []model.File{
{ID: 1},
{ID: 2},
Expand All @@ -132,11 +134,11 @@ func TestFileUpload(t *testing.T) {
require.NoError(t, err)
w0, err := bodyWriter.CreateFormFile("0", "a.txt")
require.NoError(t, err)
_, err = w0.Write([]byte("test"))
_, err = w0.Write([]byte("test1"))
require.NoError(t, err)
w1, err := bodyWriter.CreateFormFile("1", "b.txt")
require.NoError(t, err)
_, err = w1.Write([]byte("test"))
_, err = w1.Write([]byte("test2"))
require.NoError(t, err)
err = bodyWriter.Close()
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions handler/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ func TestFileUpload(t *testing.T) {
})

t.Run("valid single file upload with payload", func(t *testing.T) {
stub := &executableSchemaMock{
mock := &executableSchemaMock{
MutationFunc: func(ctx context.Context, op *ast.OperationDefinition) *graphql.Response {
require.Equal(t, len(op.VariableDefinitions), 1)
require.Equal(t, op.VariableDefinitions[0].Variable, "req")
return &graphql.Response{Data: []byte(`{"singleUploadWithPayload":{"id":1}}`)}
},
}
handler := GraphQL(stub)
handler := GraphQL(mock)

bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
Expand Down Expand Up @@ -206,11 +206,11 @@ func TestFileUpload(t *testing.T) {
require.NoError(t, err)
w0, err := bodyWriter.CreateFormFile("0", "a.txt")
require.NoError(t, err)
_, err = w0.Write([]byte("test"))
_, err = w0.Write([]byte("test1"))
require.NoError(t, err)
w1, err := bodyWriter.CreateFormFile("1", "b.txt")
require.NoError(t, err)
_, err = w1.Write([]byte("test"))
_, err = w1.Write([]byte("test2"))
require.NoError(t, err)
err = bodyWriter.Close()
require.NoError(t, err)
Expand Down Expand Up @@ -243,11 +243,11 @@ func TestFileUpload(t *testing.T) {
require.NoError(t, err)
w0, err := bodyWriter.CreateFormFile("0", "a.txt")
require.NoError(t, err)
_, err = w0.Write([]byte("test"))
_, err = w0.Write([]byte("test1"))
require.NoError(t, err)
w1, err := bodyWriter.CreateFormFile("1", "b.txt")
require.NoError(t, err)
_, err = w1.Write([]byte("test"))
_, err = w1.Write([]byte("test2"))
require.NoError(t, err)
err = bodyWriter.Close()
require.NoError(t, err)
Expand Down

0 comments on commit db7a03b

Please sign in to comment.