Skip to content

Commit

Permalink
✅ Add test scenarios for rootDoc support in GraphQL mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Oct 27, 2023
1 parent 82ff30f commit 038c486
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,37 @@
"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": {
"db": "test-graphql",
"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 }
}
}
}
Expand Down
117 changes: 104 additions & 13 deletions core/src/test/java/karate/graphql/rootDoc/authors-and-posts.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,104 @@
{
"_id": "bar",
"properties": {
"group": "coolest"
},
"sub": {
"posts": [
{ "content": "ping", "visible": true },
{ "content": "pong", "visible": true },
{ "content": "invisible", "visible": false }
]
}
}
[
{
"_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"
}
}
}
]
16 changes: 12 additions & 4 deletions core/src/test/java/karate/graphql/rootDoc/groups.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"_id": "coolest",
"description": "the coolest authors"
}
[
{
"_id": "coolest",
"description": "the coolest authors",
"tag": "coolest tag"
},
{
"_id": "weird",
"description": "authors somehow wierd",
"tag": "weird tag"
}
]
152 changes: 142 additions & 10 deletions core/src/test/java/karate/graphql/rootDoc/rootDoc.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 038c486

Please sign in to comment.