From 038c486b98766d4ab6a33875a8df930f9103cc34 Mon Sep 17 00:00:00 2001 From: Andrea Di Cesare Date: Fri, 27 Oct 2023 11:39:19 +0200 Subject: [PATCH] :white_check_mark: Add test scenarios for rootDoc support in GraphQL mappings --- .../rootDoc/app-definition-for-rootDoc.json | 15 +- .../graphql/rootDoc/authors-and-posts.json | 117 ++++++++++++-- .../java/karate/graphql/rootDoc/groups.json | 16 +- .../karate/graphql/rootDoc/rootDoc.feature | 152 ++++++++++++++++-- 4 files changed, 268 insertions(+), 32 deletions(-) diff --git a/core/src/test/java/karate/graphql/rootDoc/app-definition-for-rootDoc.json b/core/src/test/java/karate/graphql/rootDoc/app-definition-for-rootDoc.json index ee6bfe980..7d9279c8c 100644 --- a/core/src/test/java/karate/graphql/rootDoc/app-definition-for-rootDoc.json +++ b/core/src/test/java/karate/graphql/rootDoc/app-definition-for-rootDoc.json @@ -6,7 +6,7 @@ "enabled": true, "uri": "test-rootDoc" }, - "schema": "type Author { _id: String posts(visible: Boolean): [Post] group: Group} type Group {_id: String description: String} type Post { content: String } type Query { authors: [Author] }", + "schema": "type PropertiesWrapperPartialMapping { version: Int properties: Properties } type PropertiesWrapperNoMapping { properties: Properties } type Properties { group: Group genre: String } type Author { _id: String posts(visible: Boolean): [Post] properties: Properties propertiesWrapperNoMapping: PropertiesWrapperNoMapping propertiesWrapperPartialMapping: PropertiesWrapperPartialMapping } type Group {_id: String description: String tag: String} type Post { content: String } type Query { authors: [Author] }", "mappings": { "Author": { "posts": { @@ -14,24 +14,29 @@ "collection": "authors-and-posts", "stages": [ { "$match": { "_id": { "$arg": "rootDoc._id" } } }, - { "$unwind" : "$sub.posts" }, - { "$replaceRoot": {"newRoot": "$sub.posts"} }, + { "$unwind" : "$posts" }, + { "$replaceRoot": {"newRoot": "$posts"} }, { "$match": { "visible": { "$arg": "visible" } } } ] - }, + } + }, + "Properties": { "group": { "db": "test-graphql", "collection": "groups", "find": { "_id": { "$arg": "rootDoc.properties.group" } } } }, + "PropertiesWrapperPartialMapping": { + "version": "v" + }, "Query": { "authors": { "db": "test-graphql", "collection": "authors-and-posts", "limit": 10, "skip": 0, - "sort": {} + "sort": { "_id": 1 } } } } diff --git a/core/src/test/java/karate/graphql/rootDoc/authors-and-posts.json b/core/src/test/java/karate/graphql/rootDoc/authors-and-posts.json index 15c4bf286..38cbc9189 100644 --- a/core/src/test/java/karate/graphql/rootDoc/authors-and-posts.json +++ b/core/src/test/java/karate/graphql/rootDoc/authors-and-posts.json @@ -1,13 +1,104 @@ -{ - "_id": "bar", - "properties": { - "group": "coolest" - }, - "sub": { - "posts": [ - { "content": "ping", "visible": true }, - { "content": "pong", "visible": true }, - { "content": "invisible", "visible": false } - ] - } -} \ No newline at end of file +[ + { + "_id": "foo", + "properties": { + "group": "coolest", + "genre": "sci-fi" + }, + "posts": [ + { + "content": "ping", + "visible": true + }, + { + "content": "pong", + "visible": true + }, + { + "content": "invisible", + "visible": false + } + ], + "propertiesWrapperNoMapping": { + "properties": { + "group": "coolest", + "genre": "sci-fi" + } + }, + "propertiesWrapperPartialMapping": { + "v": 2, + "properties": { + "group": "weird", + "genre": "sci-fi" + } + } + }, + { + "_id": "bar", + "properties": { + "group": "coolest", + "genre": "crime" + }, + "posts": [ + { + "content": "ping bar", + "visible": true + }, + { + "content": "pong bar", + "visible": false + }, + { + "content": "invisible bar", + "visible": false + } + ], + "propertiesWrapperNoMapping": { + "properties": { + "group": "coolest", + "genre": "crime" + } + }, + "propertiesWrapperPartialMapping": { + "v": 1, + "properties": { + "group": "weird", + "genre": "crime" + } + } + }, + { + "_id": "zum", + "properties": { + "group": "weird", + "genre": "action" + }, + "posts": [ + { + "content": "ping zum", + "visible": true + }, + { + "content": "pong zum", + "visible": false + }, + { + "content": "invisible zum", + "visible": false + } + ], + "propertiesWrapperNoMapping": { + "properties": { + "group": "weird", + "genre": "action" + } + }, + "propertiesWrapperPartialMapping": { + "v": 1, + "properties": { + "group": "weird", + "genre": "action" + } + } + } +] \ No newline at end of file diff --git a/core/src/test/java/karate/graphql/rootDoc/groups.json b/core/src/test/java/karate/graphql/rootDoc/groups.json index 2dc8487f1..2c3c85fc5 100644 --- a/core/src/test/java/karate/graphql/rootDoc/groups.json +++ b/core/src/test/java/karate/graphql/rootDoc/groups.json @@ -1,4 +1,12 @@ -{ - "_id": "coolest", - "description": "the coolest authors" -} \ No newline at end of file +[ + { + "_id": "coolest", + "description": "the coolest authors", + "tag": "coolest tag" + }, + { + "_id": "weird", + "description": "authors somehow wierd", + "tag": "weird tag" + } +] \ No newline at end of file diff --git a/core/src/test/java/karate/graphql/rootDoc/rootDoc.feature b/core/src/test/java/karate/graphql/rootDoc/rootDoc.feature index 394e9bae8..1e4bfaa74 100644 --- a/core/src/test/java/karate/graphql/rootDoc/rootDoc.feature +++ b/core/src/test/java/karate/graphql/rootDoc/rootDoc.feature @@ -18,15 +18,18 @@ Background: * def setupData = callonce read('setup.feature') -Scenario: Should return two posts +Scenario: Should return three authors with visible posts and group description * text query = """ { authors{ _id - group { - description + properties { + group { + description + } + genre } posts(visible: true) { content @@ -40,18 +43,37 @@ Scenario: Should return two posts And request query When method POST Then status 200 - And match $..posts.length() == 2 - And match $.data.authors[0].group.description == 'the coolest authors' -Scenario: Should return one post + And match $.data.authors.length() == 3 + + And match $.data.authors[0]._id == 'bar' + And match $.data.authors[0].posts.length() == 1 + And match $.data.authors[0].properties.group.description == 'the coolest authors' + And match $.data.authors[0].properties.genre == 'crime' + + And match $.data.authors[1]._id == 'foo' + And match $.data.authors[1].posts.length() == 2 + And match $.data.authors[1].properties.group.description == 'the coolest authors' + And match $.data.authors[1].properties.genre == 'sci-fi' + + And match $.data.authors[2]._id == 'zum' + And match $.data.authors[2].posts.length() == 1 + And match $.data.authors[2].properties.group.description == 'authors somehow wierd' + And match $.data.authors[2].properties.genre == 'action' + + +Scenario: Should return three authors with invisible posts and group description * text query = """ { authors{ _id - group { - description + properties { + group { + description + } + genre } posts(visible: false) { content @@ -65,5 +87,115 @@ Scenario: Should return one post And request query When method POST Then status 200 - And match $..posts.length() == 1 - And match $.data.authors[0].group.description == 'the coolest authors' + + And match $.data.authors.length() == 3 + + And match $.data.authors[0]._id == 'bar' + And match $.data.authors[0].posts.length() == 2 + And match $.data.authors[0].properties.group.description == 'the coolest authors' + And match $.data.authors[0].properties.genre == 'crime' + + And match $.data.authors[1]._id == 'foo' + And match $.data.authors[1].posts.length() == 1 + And match $.data.authors[1].properties.group.description == 'the coolest authors' + And match $.data.authors[1].properties.genre == 'sci-fi' + + And match $.data.authors[2]._id == 'zum' + And match $.data.authors[2].posts.length() == 2 + And match $.data.authors[2].properties.group.description == 'authors somehow wierd' + And match $.data.authors[2].properties.genre == 'action' + +Scenario: rootDoc should work when a type without mappings is involved + + * text query = + """ + { + authors{ + _id + properties { + group { + description + } + genre + } + propertiesWrapperNoMapping { + properties { + group { + description + } + genre + } + } + } + } + """ + + Given header Content-Type = contTypeGraphQL + And header Authorization = admin + And request query + When method POST + Then status 200 + + And match $.data.authors.length() == 3 + + And match $.data.authors[0]._id == 'bar' + And match $.data.authors[0].properties.group.description == $.data.authors[0].propertiesWrapperNoMapping.properties.group.description + And match $.data.authors[0].properties.genre == $.data.authors[0].propertiesWrapperNoMapping.properties.genre + + And match $.data.authors[1]._id == 'foo' + And match $.data.authors[1].properties.group.description == $.data.authors[1].propertiesWrapperNoMapping.properties.group.description + And match $.data.authors[1].properties.genre == $.data.authors[1].propertiesWrapperNoMapping.properties.genre + + And match $.data.authors[2]._id == 'zum' + And match $.data.authors[2].properties.group.description == $.data.authors[2].propertiesWrapperNoMapping.properties.group.description + And match $.data.authors[2].properties.genre == $.data.authors[2].propertiesWrapperNoMapping.properties.genre + +Scenario: rootDoc should work when a type with partial mappings is involved + + * text query = + """ + { + authors{ + _id + properties { + group { + description + } + genre + } + propertiesWrapperPartialMapping { + properties { + group { + description + } + genre + } + version + } + } + } + """ + + Given header Content-Type = contTypeGraphQL + And header Authorization = admin + And request query + When method POST + Then status 200 + + And match $.data.authors.length() == 3 + + And match $.data.authors[0]._id == 'bar' + And match $.data.authors[0].propertiesWrapperPartialMapping.version == 1 + And match $.data.authors[0].properties.group.description == $.data.authors[0].propertiesWrapperPartialMapping.properties.group.description + And match $.data.authors[0].properties.genre == $.data.authors[0].propertiesWrapperPartialMapping.properties.genre + + And match $.data.authors[1]._id == 'foo' + And match $.data.authors[1].propertiesWrapperPartialMapping.version == 2 + And match $.data.authors[1].properties.group.description == $.data.authors[1].propertiesWrapperPartialMapping.properties.group.description + And match $.data.authors[1].properties.genre == $.data.authors[1].propertiesWrapperPartialMapping.properties.genre + + And match $.data.authors[2]._id == 'zum' + And match $.data.authors[2].propertiesWrapperPartialMapping.version == 1 + And match $.data.authors[2].properties.group.description == $.data.authors[2].propertiesWrapperPartialMapping.properties.group.description + And match $.data.authors[2].properties.genre == $.data.authors[2].propertiesWrapperPartialMapping.properties.genre +