github.com/mstephano/gqlgen-schemagen@v0.0.0-20230113041936-dd2cd4ea46aa/graphql/handler_test.go (about) 1 package graphql 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestAddUploadToOperations(t *testing.T) { 11 key := "0" 12 13 t.Run("fail missing all variables", func(t *testing.T) { 14 file, _ := os.Open("path/to/file") 15 params := &RawParams{} 16 17 upload := Upload{ 18 File: file, 19 Filename: "a.txt", 20 Size: int64(5), 21 ContentType: "text/plain", 22 } 23 path := "variables.req.0.file" 24 err := params.AddUpload(upload, key, path) 25 require.NotNil(t, err) 26 require.Equal(t, "input: path is missing \"variables.\" prefix, key: 0, path: variables.req.0.file", err.Error()) 27 }) 28 29 t.Run("valid variable", func(t *testing.T) { 30 file, _ := os.Open("path/to/file") 31 request := &RawParams{ 32 Variables: map[string]interface{}{ 33 "file": nil, 34 }, 35 } 36 37 upload := Upload{ 38 File: file, 39 Filename: "a.txt", 40 Size: int64(5), 41 ContentType: "text/plain", 42 } 43 44 expected := &RawParams{ 45 Variables: map[string]interface{}{ 46 "file": upload, 47 }, 48 } 49 50 path := "variables.file" 51 err := request.AddUpload(upload, key, path) 52 require.Nil(t, err) 53 54 require.Equal(t, request, expected) 55 }) 56 57 t.Run("valid nested variable", func(t *testing.T) { 58 file, _ := os.Open("path/to/file") 59 request := &RawParams{ 60 Variables: map[string]interface{}{ 61 "req": []interface{}{ 62 map[string]interface{}{ 63 "file": nil, 64 }, 65 }, 66 }, 67 } 68 69 upload := Upload{ 70 File: file, 71 Filename: "a.txt", 72 Size: int64(5), 73 ContentType: "text/plain", 74 } 75 76 expected := &RawParams{ 77 Variables: map[string]interface{}{ 78 "req": []interface{}{ 79 map[string]interface{}{ 80 "file": upload, 81 }, 82 }, 83 }, 84 } 85 86 path := "variables.req.0.file" 87 err := request.AddUpload(upload, key, path) 88 require.Nil(t, err) 89 90 require.Equal(t, request, expected) 91 }) 92 }