Skip to content

Commit 384176f

Browse files
committed
Added value object tests
1 parent 231150f commit 384176f

File tree

7 files changed

+422
-9
lines changed

7 files changed

+422
-9
lines changed

Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@
7373
<PackageReference Update="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.*" />
7474
<PackageReference Update="Microsoft.NETCore.Platforms" Version="6.0.*" />
7575
<PackageReference Update="Newtonsoft.Json" Version="13.*" />
76+
<PackageReference Update="Validus" Version="4.1.*" />
7677
</ItemGroup>
7778
</Project>

tests/FSharp.Data.GraphQL.Tests/ErrorHelpers.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ let ensureRequestError (result : GQLExecutionResult) (onRequestError : GQLProble
2626
| RequestError errors -> onRequestError errors
2727
| response -> fail $"Expected RequestError GQLResponse but got {Environment.NewLine}{response}"
2828

29-
let ensureValidationError (message : string) (path : FieldPath) (error) =
29+
let ensureValidationError (message : string) (path : FieldPath) (error : GQLProblemDetails) =
3030
equals message error.Message
3131
equals (Include path) error.Path
3232
match error.Extensions with
3333
| Skip -> fail "Expected extensions to be present"
3434
| Include extensions ->
3535
equals Validation (unbox extensions[CustomErrorFields.Kind])
3636

37-
let ensureExecutionError (message : string) (path : FieldPath) (error) =
37+
let ensureExecutionError (message : string) (path : FieldPath) (error : GQLProblemDetails) =
3838
equals message error.Message
3939
equals (Include path) error.Path
4040
match error.Extensions with
4141
| Skip -> fail "Expected extensions to be present"
4242
| Include extensions ->
4343
equals Execution (unbox extensions[CustomErrorFields.Kind])
4444

45-
let ensureInputCoercionError (errorSource : ErrorSource) (message : string) (``type`` : string) (error) =
45+
let ensureInputCoercionError (errorSource : ErrorSource) (message : string) (``type`` : string) (error : GQLProblemDetails) =
4646
equals message error.Message
4747
match error.Extensions with
4848
| Skip -> fail "Expected extensions to be present"
@@ -56,7 +56,7 @@ let ensureInputCoercionError (errorSource : ErrorSource) (message : string) (``t
5656
equals name (unbox extensions[CustomErrorFields.ArgumentName])
5757
equals ``type`` (unbox extensions[CustomErrorFields.ArgumentType])
5858

59-
let ensureInputObjectFieldCoercionError (errorSource : ErrorSource) (message : string) (inputObjectPath : FieldPath) (objectType : string) (fieldType : string) (error) =
59+
let ensureInputObjectFieldCoercionError (errorSource : ErrorSource) (message : string) (inputObjectPath : FieldPath) (objectType : string) (fieldType : string) (error : GQLProblemDetails) =
6060
equals message error.Message
6161
match error.Extensions with
6262
| Skip -> fail "Expected extensions to be present"
@@ -70,7 +70,7 @@ let ensureInputObjectFieldCoercionError (errorSource : ErrorSource) (message : s
7070
equals objectType (unbox extensions[CustomErrorFields.ObjectType])
7171
equals fieldType (unbox extensions[CustomErrorFields.FieldType])
7272

73-
let ensureInputObjectValidationError (errorSource : ErrorSource) (message : string) (inputObjectPath : FieldPath) (objectType : string) (error) =
73+
let ensureInputObjectValidationError (errorSource : ErrorSource) (message : string) (inputObjectPath : FieldPath) (objectType : string) (error : GQLProblemDetails) =
7474
equals message error.Message
7575
match error.Extensions with
7676
| Skip -> fail "Expected extensions to be present"

tests/FSharp.Data.GraphQL.Tests/FSharp.Data.GraphQL.Tests.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1919
<PackageReference Include="BenchmarkDotNet" />
20+
<PackageReference Include="Validus" />
2021
<PackageReference Include="xunit" />
2122
<PackageReference Include="xunit.runner.utility" />
2223
<PackageReference Include="xunit.runner.visualstudio" />
@@ -51,6 +52,8 @@
5152
<Compile Include="MutationTests.fs" />
5253
<Compile Include="ResolveTests.fs" />
5354
<Compile Include="Variables and Inputs\CoercionTests.fs" />
55+
<Compile Include="Variables and Inputs\OptionalsNormalizationTests.ValidString.fs" />
56+
<Compile Include="Variables and Inputs\OptionalsNormalizationTests.fs" />
5457
<Compile Include="Variables and Inputs\InputRecordTests.fs" />
5558
<Compile Include="Variables and Inputs\InputObjectValidatorTests.fs" />
5659
<Compile Include="Variables and Inputs\InputScalarAndAutoFieldScalarTests.fs" />

tests/FSharp.Data.GraphQL.Tests/Helpers.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ let asts query =
8989
["defer"; "stream"]
9090
|> Seq.map (query >> parse)
9191

92-
let set (mre : ManualResetEvent) =
92+
let setEvent (mre : ManualResetEvent) =
9393
mre.Set() |> ignore
9494

95-
let reset (mre : ManualResetEvent) =
95+
let resetEvent (mre : ManualResetEvent) =
9696
mre.Reset() |> ignore
9797

98-
let wait (mre : ManualResetEvent) errorMsg =
98+
let waitEvent (mre : ManualResetEvent) errorMsg =
9999
if TimeSpan.FromSeconds(float 30) |> mre.WaitOne |> not
100100
then fail errorMsg
101101

tests/FSharp.Data.GraphQL.Tests/Variables and Inputs/CoercionTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// The MIT License (MIT)
2-
/// Copyright (c) 2015-Mar 2016 Kevin Thompson @kthompson
2+
// Copyright (c) 2015-Mar 2016 Kevin Thompson @kthompson
33
// Copyright (c) 2016 Bazinga Technologies Inc
44

55
[<UseInvariantCulture>]
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// The MIT License (MIT)
2+
3+
namespace FSharp.Data.GraphQL.Tests.OptionalsNormalizationTests
4+
5+
open System
6+
open FSharp.Data.GraphQL
7+
8+
[<Struct>]
9+
type ValidString<'t> = internal ValidString of string
10+
with
11+
static member internal CreateVOption<'t> (value: string option) : ValidString<'t> voption =
12+
value |> ValueOption.ofOption |> ValueOption.map ValidString
13+
14+
module ValidString =
15+
16+
let value (ValidString text) = text
17+
18+
let vOption strOption : string voption = strOption |> ValueOption.map value
19+
20+
let vOptionValue strOption : string = strOption |> ValueOption.map value |> ValueOption.toObj
21+
22+
open Validus
23+
open Validus.Operators
24+
25+
module String =
26+
27+
let notStartsWithWhiteSpace fieldName (s: string) =
28+
if s.StartsWith ' ' then
29+
Error
30+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot start with whitespace" ]
31+
else
32+
Ok <| s
33+
34+
let notEndsWithWhiteSpace fieldName (s: string) =
35+
if s.EndsWith ' ' then
36+
Error
37+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot end with whitespace" ]
38+
else
39+
Ok <| s
40+
41+
let notContainsWhiteSpace fieldName (s: string) =
42+
if s.Contains ' ' then
43+
Error
44+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain whitespace" ]
45+
else
46+
Ok <| s
47+
48+
let notContainsBacktick fieldName (s: string) =
49+
if s.Contains '`' then
50+
Error
51+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain backtick" ]
52+
else
53+
Ok <| s
54+
55+
let notContainsTilde fieldName (s: string) =
56+
if s.Contains '~' then
57+
Error
58+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain tilde" ]
59+
else
60+
Ok <| s
61+
62+
let notContainsDash fieldName (s: string) =
63+
if s.Contains '-' then
64+
Error
65+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain dash: '-'" ]
66+
else
67+
Ok <| s
68+
69+
let notContainsUmlauts fieldName (s: string) =
70+
let umlauts = [ 'ä'; 'ö'; 'ü'; 'ß'; 'Ä'; 'Ö'; 'Ü' ] |> set
71+
72+
let contains = s |> Seq.exists (fun c -> umlauts |> Set.contains c)
73+
74+
if contains then
75+
Error
76+
<| ValidationErrors.create fieldName [
77+
$"'%s{fieldName}' field cannot contain umlauts: ä, ö, ü, ß, Ä, Ö, Ü"
78+
]
79+
else
80+
Ok <| s
81+
82+
open Validus.Operators
83+
84+
let allowEmpty = ValueOption.ofObj >> ValueOption.filter (not << String.IsNullOrWhiteSpace)
85+
86+
let validateStringCharacters =
87+
notStartsWithWhiteSpace
88+
<+> notEndsWithWhiteSpace
89+
<+> notContainsTilde
90+
<+> notContainsUmlauts
91+
<+> notContainsBacktick
92+
93+
module Uri =
94+
95+
let isValid fieldName uriString =
96+
if Uri.IsWellFormedUriString(uriString, UriKind.Absolute) then
97+
Ok uriString
98+
else
99+
Error
100+
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field is not a valid URI" ]
101+
102+
103+
//module VOptionString =
104+
105+
// let allow (validator : Validator<string, string>) : Validator<string voption, string voption> =
106+
// fun fieldName (value : string voption) ->
107+
// match value with
108+
// | ValueNone -> Ok ValueNone
109+
// | ValueSome str -> (validator *|* ValueSome) fieldName str
110+
111+
// let toValidationResult _ value : ValidationResult<string voption> =
112+
// let valueOption =
113+
// value
114+
// |> ValueOption.ofObj
115+
// |> ValueOption.filter (not << String.IsNullOrWhiteSpace)
116+
// match valueOption with
117+
// | ValueSome str -> ValueSome str |> Ok
118+
// | ValueNone -> ValueNone |> Ok
119+
120+
module ValidationErrors =
121+
122+
let toIGQLErrors (errors: ValidationErrors) : IGQLError list =
123+
errors
124+
|> ValidationErrors.toList
125+
|> List.map (fun e -> { new IGQLError with member _.Message = e })
126+
127+
module Operators =
128+
129+
let vOption (v1: 'a -> 'a voption) (v2: Validator<'a, 'b>) : Validator<'a, 'b voption> =
130+
fun x y ->
131+
let value = v1 y
132+
match value with
133+
| ValueSome value -> (v2 *|* ValueSome) x y
134+
| ValueNone -> Ok ValueNone
135+
136+
let (?=>) v1 v2 = vOption v1 v2
137+
let (?=<) v2 v1 = vOption v1 v2
138+
139+
module Scalars =
140+
141+
open System.Text.Json
142+
open FSharp.Data.GraphQL.Ast
143+
open FSharp.Data.GraphQL.Types
144+
open FSharp.Data.GraphQL.Types.SchemaDefinitions.Errors
145+
146+
type Define with
147+
148+
static member ValidStringScalar<'t>(typeName, createValid : Validator<string, 't voption>, ?description: string) =
149+
let createValid = createValid typeName
150+
Define.WrappedScalar
151+
(name = typeName,
152+
coerceInput =
153+
(function
154+
| Variable e when e.ValueKind = JsonValueKind.String -> e.GetString() |> createValid |> Result.mapError ValidationErrors.toIGQLErrors
155+
| InlineConstant (StringValue s) -> s |> createValid |> Result.mapError ValidationErrors.toIGQLErrors
156+
| Variable e -> e.GetDeserializeError typeName
157+
| InlineConstant value -> value.GetCoerceError typeName),
158+
coerceOutput =
159+
(function
160+
| :? ('t voption) as x -> x |> string |> Some
161+
| :? 't as x -> Some (string x)
162+
| :? string as s -> s |> Some
163+
| null -> None
164+
| _ -> raise <| System.NotSupportedException ()),
165+
?description = description)

0 commit comments

Comments
 (0)