This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializationTests.fs
158 lines (125 loc) · 5.78 KB
/
SerializationTests.fs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
module SerializationTests
open UnitTest
open GraphQLTransportWS
open System.Text.Json
open Xunit
open FSharp.Data.GraphQL.Ast
let getStdSerializerOptions () =
let serializerOptions = new JsonSerializerOptions()
serializerOptions.PropertyNameCaseInsensitive <- true
serializerOptions.Converters.Add(new ClientMessageConverter<Root>(TestSchema.executor))
serializerOptions.Converters.Add(new RawServerMessageConverter())
serializerOptions
[<Fact>]
let ``Deserializes ConnectionInit correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input = "{\"type\":\"connection_init\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ConnectionInit None -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)
[<Fact>]
let ``Deserializes ConnectionInit with payload correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input = "{\"type\":\"connection_init\", \"payload\":\"hello\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ConnectionInit _ -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)
[<Fact>]
let ``Deserializes ClientPing correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input = "{\"type\":\"ping\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ClientPing None -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value '%A'" other)
[<Fact>]
let ``Deserializes ClientPing with payload correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input = "{\"type\":\"ping\", \"payload\":\"ping!\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ClientPing _ -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value '%A" other)
[<Fact>]
let ``Deserializes ClientPong correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input = "{\"type\":\"pong\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ClientPong None -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)
[<Fact>]
let ``Deserializes ClientPong with payload correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input = "{\"type\":\"pong\", \"payload\": \"pong!\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ClientPong _ -> () // <-- expected
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)
[<Fact>]
let ``Deserializes ClientComplete correctly``() =
let serializerOptions = getStdSerializerOptions()
let input = "{\"id\": \"65fca2b5-f149-4a70-a055-5123dea4628f\", \"type\":\"complete\"}"
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| ClientComplete id ->
Assert.Equal("65fca2b5-f149-4a70-a055-5123dea4628f", id)
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A'" other)
[<Fact>]
let ``Deserializes client subscription correctly`` () =
let serializerOptions = getStdSerializerOptions()
let input =
"""{
"id": "b5d4d2ff-d262-4882-a7b9-d6aec5e4faa6",
"type": "subscribe",
"payload" : {
"query": "subscription { watchMoon(id: \"1\") { id name isMoon } }"
}
}
"""
let result = JsonSerializer.Deserialize<ClientMessage>(input, serializerOptions)
match result with
| Subscribe (id, payload) ->
Assert.Equal("b5d4d2ff-d262-4882-a7b9-d6aec5e4faa6", id)
Assert.Equal(1, payload.ExecutionPlan.Operation.SelectionSet.Length)
let watchMoonSelection = payload.ExecutionPlan.Operation.SelectionSet |> List.head
match watchMoonSelection with
| Field watchMoonField ->
Assert.Equal("watchMoon", watchMoonField.Name)
Assert.Equal(1, watchMoonField.Arguments.Length)
let watchMoonFieldArg = watchMoonField.Arguments |> List.head
Assert.Equal("id", watchMoonFieldArg.Name)
match watchMoonFieldArg.Value with
| StringValue theValue ->
Assert.Equal("1", theValue)
| other ->
Assert.Fail(sprintf "expected arg to be a StringValue, but it was: %A" other)
Assert.Equal(3, watchMoonField.SelectionSet.Length)
match watchMoonField.SelectionSet.[0] with
| Field firstField ->
Assert.Equal("id", firstField.Name)
| other ->
Assert.Fail(sprintf "expected field to be a Field, but it was: %A" other)
match watchMoonField.SelectionSet.[1] with
| Field secondField ->
Assert.Equal("name", secondField.Name)
| other ->
Assert.Fail(sprintf "expected field to be a Field, but it was: %A" other)
match watchMoonField.SelectionSet.[2] with
| Field thirdField ->
Assert.Equal("isMoon", thirdField.Name)
| other ->
Assert.Fail(sprintf "expected field to be a Field, but it was: %A" other)
| somethingElse ->
Assert.Fail(sprintf "expected it to be a field, but it was: %A" somethingElse)
| other ->
Assert.Fail(sprintf "unexpected actual value: '%A" other)