Skip to content

Commit

Permalink
Simplified test names and formatted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Chebukin committed Aug 7, 2023
1 parent 21fdb55 commit 0c8b3b6
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 302 deletions.
338 changes: 160 additions & 178 deletions tests/FSharp.Data.GraphQL.Tests/AbstractionTests.fs

Large diffs are not rendered by default.

146 changes: 80 additions & 66 deletions tests/FSharp.Data.GraphQL.Tests/AstExtensionsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,71 @@ open FSharp.Data.GraphQL.Parser
open FSharp.Data.GraphQL.Ast.Extensions

/// Converts line breaks to a single standard to avoid different SO line break termination issues.
let normalize (str : string) = str.Replace("\r\n", "\n")
let normalize (str : string) = str.Replace ("\r\n", "\n")

/// Generates an Ast.Document from a query string, prints it to another
/// query string and expects it to be equal. Input query must be formatted (with line breaks and identation).
/// Identation unit is two empty spaces.
let private printAndAssert (query : string) =
let document = parse query
let expected = normalize query
let actual = normalize <| document.ToQueryString()
let actual = normalize <| document.ToQueryString ()
actual |> equals expected

[<Fact>]
let ``Should be able to print a simple query`` () =
printAndAssert """query q {
let ``Can print a simple query`` () =
printAndAssert
"""query q {
hero {
name
}
}"""

[<Fact>]
let ``Should be able to print a simple query with 2 fields`` () =
printAndAssert """query q {
let ``Can print a simple query with 2 fields`` () =
printAndAssert
"""query q {
hero {
id
name
}
}"""

[<Fact>]
let ``Should be able to print a query with variables`` () =
printAndAssert """query q($id: String!) {
let ``Can print a query with variables`` () =
printAndAssert
"""query q($id: String!) {
hero(id: $id) {
id
name
}
}"""

[<Fact>]
let ``Should be able to parse a query with an object input in the internal method`` () =
printAndAssert """mutation q($id: String!, $name: String!) {
let ``Can parse a query with an object input in the internal method`` () =
printAndAssert
"""mutation q($id: String!, $name: String!) {
addHero(input: { id: $id, label: $name })
}"""

[<Fact>]
let ``Should be able to parse a query with an object having array properties input in the internal method`` () =
printAndAssert """mutation q($id: String!, $name: String!, $friend1: String!) {
let ``Can parse a query with an object having array properties input in the internal method`` () =
printAndAssert
"""mutation q($id: String!, $name: String!, $friend1: String!) {
addHero(input: { friends: [ $friend1 ], id: $id, label: $name })
}"""

[<Fact>]
let ``Should be able to parse a query with an object having multi-element array input in the internal method`` () =
printAndAssert """mutation q($id: String!, $name: String!) {
let ``Can parse a query with an object having multi-element array input in the internal method`` () =
printAndAssert
"""mutation q($id: String!, $name: String!) {
addHero(input: { friends: [ 7, 5, -3 ], id: $id, label: $name })
}"""

[<Fact>]
let ``Should be able print ObjectValue names properly`` () =
printAndAssert """query GetCampaigns {
printAndAssert
"""query GetCampaigns {
campaigns(params: { limit: 100, offset: 0 }) {
campaigns {
code
Expand All @@ -74,8 +81,9 @@ let ``Should be able print ObjectValue names properly`` () =
}"""

[<Fact>]
let ``Should be able to print a query with aliases`` () =
printAndAssert """query q($myId: String!, $hisId: String!) {
let ``Can print a query with aliases`` () =
printAndAssert
"""query q($myId: String!, $hisId: String!) {
myHero: hero(id: $myId) {
id
name
Expand All @@ -90,8 +98,9 @@ let ``Should be able to print a query with aliases`` () =
}"""

[<Fact>]
let ``Should be able to print a query with fragment spreads`` () =
printAndAssert """query q($myId: String!, $hisId: String!) {
let ``Can print a query with fragment spreads`` () =
printAndAssert
"""query q($myId: String!, $hisId: String!) {
myHero: hero(id: $myId) {
id
name
Expand Down Expand Up @@ -120,24 +129,27 @@ fragment friend on Character {
}"""

[<Fact>]
let ``Should be able to print a short hand format query`` () =
printAndAssert """{
let ``Can print a short hand format query`` () =
printAndAssert
"""{
field1
field2
}"""

[<Fact>]
let ``Should not print query without name in short hand format`` () =
printAndAssert """query ($rId: Int) {
printAndAssert
"""query ($rId: Int) {
answer(id: $rId) {
id
answer
}
}"""

[<Fact>]
let ``Should be able to print a query with inline fragments`` () =
printAndAssert """query q($myId: String!, $hisId: String!) {
let ``Can print a query with inline fragments`` () =
printAndAssert
"""query q($myId: String!, $hisId: String!) {
myHero: hero(id: $myId) {
id
name
Expand Down Expand Up @@ -174,8 +186,9 @@ fragment friend on Character {
}"""

[<Fact>]
let ``Should be able to print arguments inside fragment spreads and default variable values`` () =
printAndAssert """query HeroComparison($first: Int = 3) {
let ``Can print arguments inside fragment spreads and default variable values`` () =
printAndAssert
"""query HeroComparison($first: Int = 3) {
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
Expand All @@ -197,8 +210,9 @@ fragment comparisonFields on Character {
}"""

[<Fact>]
let ``Should be able to print directives`` () =
printAndAssert """query Hero($episode: Episode, $withFriends: Boolean!) {
let ``Can print directives`` () =
printAndAssert
"""query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
Expand All @@ -208,8 +222,9 @@ let ``Should be able to print directives`` () =
}"""

[<Fact>]
let ``Should be able to print multiple directives and arguments`` () =
printAndAssert """query q($skip: Boolean!) {
let ``Can print multiple directives and arguments`` () =
printAndAssert
"""query q($skip: Boolean!) {
hero(id: "1000") {
name
friends(first: 1, name_starts_with: "D") @defer @skip(if: $skip) {
Expand All @@ -226,26 +241,30 @@ let ``Should be able to print multiple directives and arguments`` () =
}"""

[<Fact>]
let ``Should be able to print a mutation`` () =
printAndAssert """mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
let ``Can print a mutation`` () =
printAndAssert
"""mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}"""

[<Fact>]
let ``Should be able to print a subscription`` () =
printAndAssert """subscription onCommentAdded($repoFullName: String!) {
let ``Can print a subscription`` () =
printAndAssert
"""subscription onCommentAdded($repoFullName: String!) {
commentAdded(repoFullName: $repoFullName) {
id
content
}
}"""

[<Fact>]
let ``Should be able to print type name meta field`` () =
let expected = normalize """query q {
let ``Can print type name meta field`` () =
let expected =
normalize
"""query q {
hero(id: "1000") {
name
friends {
Expand All @@ -265,7 +284,9 @@ let ``Should be able to print type name meta field`` () =
}
__typename
}"""
let query = """query q {

let query =
"""query q {
hero(id: "1000") {
name
friends {
Expand All @@ -281,13 +302,15 @@ let ``Should be able to print type name meta field`` () =
}
}
"""

let document = parse query
let actual = normalize <| document.ToQueryString(QueryStringPrintingOptions.IncludeTypeNames)
let actual = normalize <| document.ToQueryString (QueryStringPrintingOptions.IncludeTypeNames)
actual |> equals expected

[<Fact>]
let ``Should generate information map correctly`` () =
let query = """query q {
let query =
"""query q {
hero(id: "1000") {
name
friends {
Expand All @@ -304,31 +327,22 @@ let ``Should generate information map correctly`` () =
}
"""
let document = parse query
let actual = document.GetInfoMap() |> Map.toList
let expected = [(Some "q", [TypeField
{Name = "hero";
Alias = None;
Fields =
[TypeField
{Name = "friends";
Alias = None;
Fields =
[FragmentField {Name = "primaryFunction";
Alias = None;
TypeCondition = "Droid";
Fields = [];};
FragmentField {Name = "id";
Alias = None;
TypeCondition = "Droid";
Fields = [];};
FragmentField {Name = "homePlanet";
Alias = None;
TypeCondition = "Human";
Fields = [];};
FragmentField {Name = "id";
Alias = None;
TypeCondition = "Human";
Fields = [];}];}; TypeField {Name = "name";
Alias = None;
Fields = [];}];}])]
let actual = document.GetInfoMap () |> Map.toList

let expected =
[ (Some "q",
[ TypeField
{ Name = "hero"
Alias = None
Fields =
[ TypeField
{ Name = "friends"
Alias = None
Fields =
[ FragmentField { Name = "primaryFunction"; Alias = None; TypeCondition = "Droid"; Fields = [] }
FragmentField { Name = "id"; Alias = None; TypeCondition = "Droid"; Fields = [] }
FragmentField { Name = "homePlanet"; Alias = None; TypeCondition = "Human"; Fields = [] }
FragmentField { Name = "id"; Alias = None; TypeCondition = "Human"; Fields = [] } ] }
TypeField { Name = "name"; Alias = None; Fields = [] } ] } ]) ]

actual |> equals expected
10 changes: 5 additions & 5 deletions tests/FSharp.Data.GraphQL.Tests/IntrospectionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let inputFieldQuery = """{
"""

[<Fact>]
let ``Input field should be marked as nullable when defaultValue is provided`` () =
let ``Input field must be marked as nullable when defaultValue is provided`` () =
let root = Define.Object("Query", [
Define.Field("onlyField", StringType, "The only field", [
Define.Input("in", StringType, defaultValue = "1")
Expand Down Expand Up @@ -75,7 +75,7 @@ let ``Input field should be marked as nullable when defaultValue is provided`` (
| _ -> fail "Expected Direct GQResponse"

[<Fact>]
let ``Input field should be marked as non-nullable when defaultValue is not provided`` () =
let ``Input field must be marked as non-nullable when defaultValue is not provided`` () =
let root = Define.Object("Query", [
Define.Field("onlyField", StringType, "The only field", [
Define.Input("in", StringType)
Expand Down Expand Up @@ -109,7 +109,7 @@ let ``Input field should be marked as non-nullable when defaultValue is not prov
| _ -> fail "Expected Direct GQResponse"

[<Fact>]
let ``Input field should be marked as nullable when its type is nullable`` () =
let ``Input field must be marked as nullable when its type is nullable`` () =
let root = Define.Object("Query", [
Define.Field("onlyField", StringType, "The only field", [
Define.Input("in", Nullable StringType)
Expand Down Expand Up @@ -143,7 +143,7 @@ let ``Input field should be marked as nullable when its type is nullable`` () =
| _ -> fail "Expected Direct GQResponse"

[<Fact>]
let ``Input field should be marked as nullable when its type is nullable and have default value provided`` () =
let ``Input field must be marked as nullable when its type is nullable and have default value provided`` () =
let root = Define.Object("Query", [
Define.Field("onlyField", StringType, "The only field", [
Define.Input("in", Nullable StringType, defaultValue = Some "1")
Expand Down Expand Up @@ -177,7 +177,7 @@ let ``Input field should be marked as nullable when its type is nullable and hav
| _ -> fail "Expected Direct GQResponse"

[<Fact>]
let ``Introspection schema should be serializable back and forth using json`` () =
let ``Introspection schema must be serializable back and forth using json`` () =
let root = Define.Object("Query", [ Define.Field("onlyField", StringType) ])
let schema = Schema(root)
let query = """query IntrospectionQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let ``ofSeq should call OnComplete and return items in expected order`` () =
sub.Received |> seqEquals source

[<Fact>]
let `` bind should call OnComplete and return items in expected order`` () =
let ``bind should call OnComplete and return items in expected order`` () =
let source = seq { for x in 1 .. 5 do yield x }
let obs = Observable.ofSeq source |> Observable.bind (fun x -> Observable.ofSeq [x; x])
use sub = Observer.create obs
Expand Down
Loading

0 comments on commit 0c8b3b6

Please sign in to comment.