-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathIntrospectionUpdateTests.fs
More file actions
85 lines (72 loc) · 3.42 KB
/
Copy pathIntrospectionUpdateTests.fs
File metadata and controls
85 lines (72 loc) · 3.42 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
module FSharp.Data.GraphQL.IntegrationTests.IntrospectionUpdateTests
open System
open System.IO
open System.Net.Http.Json
open System.Text.Json
open System.Threading
open Xunit
let introspectionFilePath =
Path.Combine (__SOURCE_DIRECTORY__, "integration-introspection.json")
|> Path.GetFullPath
let normalizeJsonDocument options (document : JsonDocument) =
use buffer = new MemoryStream ()
use writer = new Utf8JsonWriter (buffer, options)
document.WriteTo writer
writer.Flush ()
buffer.Seek (0L, SeekOrigin.Begin) |> ignore
JsonDocument.Parse buffer
let parseAndNormalizeJsonAsync ct options stream = task {
let! document = JsonDocument.ParseAsync (stream, cancellationToken = ct)
return normalizeJsonDocument options document
}
let areSchemasEqual (document1 : JsonDocument) (document2 : JsonDocument) =
let schema1 = document1.RootElement.GetProperty("data").GetProperty ("__schema")
let schema2 = document2.RootElement.GetProperty("data").GetProperty ("__schema")
schema1.GetRawText () = schema2.GetRawText ()
let readDestinationDocumentAsync ct (stream : FileStream) = task {
try
let! document = JsonDocument.ParseAsync (stream, cancellationToken = ct)
return ValueSome document
with :? JsonException ->
return ValueNone
}
let updateIntrospectionFileAsync ct sourceStream = task {
use destinationStream =
new FileStream (introspectionFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)
let options = JsonWriterOptions (Indented = true)
let! sourceDocument = parseAndNormalizeJsonAsync ct options sourceStream
destinationStream.Seek (0L, SeekOrigin.Begin) |> ignore
let! destinationDocument = readDestinationDocumentAsync ct destinationStream
let shouldUpdate =
match destinationDocument with
| ValueNone -> true
| ValueSome document -> not (areSchemasEqual document sourceDocument)
if shouldUpdate then
destinationStream.Seek (0L, SeekOrigin.Begin) |> ignore
destinationStream.SetLength 0
use writer = new Utf8JsonWriter (destinationStream, options)
sourceDocument.WriteTo writer
writer.Flush ()
return shouldUpdate
}
[<Fact>]
let ``Get GraphQL introspection response returns schema`` () = task {
use httpClient = TestHosts.createIntegrationHttpClient ()
let! response = httpClient.GetFromJsonAsync<JsonElement> ("/", CancellationToken.None)
let schema = response.GetProperty("data").GetProperty ("__schema")
Assert.NotEqual (Unchecked.defaultof<JsonElement>, schema)
let hasErrors, _ = response.TryGetProperty "errors"
Assert.False hasErrors
}
[<Fact>]
let ``Update integration introspection file when schema changes`` () = task {
use httpClient = TestHosts.createIntegrationHttpClient ()
let! sourceStream = httpClient.GetStreamAsync ("/")
let! wasUpdated = updateIntrospectionFileAsync CancellationToken.None sourceStream
Assert.True (File.Exists introspectionFilePath)
if wasUpdated then
let! sourceStreamSecondRun = httpClient.GetStreamAsync ("/")
use sourceStreamForVerification = sourceStreamSecondRun
let! wasUpdatedSecondRun = updateIntrospectionFileAsync CancellationToken.None sourceStreamForVerification
Assert.False wasUpdatedSecondRun
}