forked from fsprojects/FSharp.Data.GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTests.fs
More file actions
122 lines (107 loc) · 4.36 KB
/
Copy pathFileTests.fs
File metadata and controls
122 lines (107 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module FSharp.Data.GraphQL.Tests.FileTests
open System.Collections.Immutable
open System.IO
open System.Text
open System.Text.Json
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Parser
open FSharp.Data.GraphQL.Types
open Xunit
type Root = { File : Stream }
let QueryType =
Define.Object<unit> (
name = "QueryType",
fields =
[ Define.Field ("dummy", StringType, fun _ _ -> "dummy")]
)
type Input = {
File : FileData
File2 : FileData option
}
let private getFullInfo fileData =
use reader = new StreamReader(fileData.Stream, Encoding.UTF8, true)
let fileContent = reader.ReadToEnd()
fileContent + fileData.ContentType
let InputObject = Define.InputObject<Input>(
name = "Input",
fields =
[
Define.Input("file", FileType)
Define.Input("file2", Nullable FileType)
])
let MutationType =
Define.Object<unit> (
name = "MutationType",
fields =
[ Define.Field ("uploadFile", StringType, "", [ Define.Input ("input", FileType) ],
(fun ctx () ->
let fileData = ctx.Arg<FileData> "input"
getFullInfo fileData
));
Define.Field ("uploadFileComplex", StringType, "", [ Define.Input ("input", InputObject) ],
(fun ctx () ->
let input = ctx.Arg<Input> "input"
let fileString = getFullInfo input.File
let file2String = match input.File2 with
| Some file2 ->
getFullInfo file2
| None -> ""
fileString + file2String
))
]
)
let schema = Schema (QueryType, MutationType)
let executor = Executor (schema, [])
let execute (query : string) = executor.AsyncExecute (query, getMockInputContext) |> sync
let executeWithVariables ( query : string, variables : ImmutableDictionary<string, JsonElement>) =
executor.AsyncExecute (ast = parse query, getInputContext = getMockInputContext, variables = variables) |> sync
let mutationWithVariable = """mutation uploadFile ($file : FileType!) {
uploadFile (input : $file)
}
"""
let mutationWithConstant = """mutation uploadFile () {
uploadFile (input : "fileKey")
}"""
let mutationComplexObject = """mutation uploadFile () {
uploadFileComplex (input : { file : "fileKey" })
}"""
let mutationComplexObjectWithTwoFiles = """mutation uploadFile () {
uploadFileComplex (input : {file : "fileKey", file2: "fileKey2" })
}"""
[<Fact>]
let ``File type: Must upload file as input scalar using inline string as a file name`` () =
let expected = NameValueLookup.ofList [ "uploadFile", MockInputContext.mockFileTextAndContentType ]
let result = execute mutationWithConstant
ensureDirect result <| fun data errors ->
empty errors
data |> equals (upcast expected)
()
[<Fact>]
let ``File type: Must upload a file as input scalar using a variable`` () =
let expected = NameValueLookup.ofList [ "uploadFile", MockInputContext.mockFileTextAndContentType ]
let jsonVariable = "\"fileKey\"" |> JsonDocument.Parse |> _.RootElement
let variables = ImmutableDictionary<string, JsonElement>.Empty.Add ("file", jsonVariable)
let result = executeWithVariables (mutationWithVariable, variables)
ensureDirect result <| fun data errors ->
empty errors
data |> equals (upcast expected)
()
[<Fact>]
let ``File type: Must upload a file as input object field using inline string a file name`` () =
let expected = NameValueLookup.ofList [ "uploadFileComplex", MockInputContext.mockFileTextAndContentType ]
let result = execute mutationComplexObject
ensureDirect result <| fun data errors ->
empty errors
data |> equals (upcast expected)
()
[<Fact>]
let ``File type: Must upload two files as input object field using inline string a file name`` () =
let expectedContent = MockInputContext.mockFileTextAndContentType + MockInputContext.mockFileText2AndContentType
let expected = NameValueLookup.ofList [
"uploadFileComplex", expectedContent
]
let result = execute mutationComplexObjectWithTwoFiles
ensureDirect result <| fun data errors ->
empty errors
data |> equals (upcast expected)
()