Skip to content

Commit

Permalink
Add GraphQL endpoint tests for sorting with pagination (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi authored Sep 15, 2023
1 parent 8f87179 commit 48fa9e5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import org.apache.http.HttpStatus

import io.restassured.RestAssured
import io.restassured.response.Response
import jakarta.validation.constraints.NotNull
import jakarta.ws.rs.core.MediaType
import spock.lang.Specification

abstract class AbstractITSpec extends Specification {
Expand Down Expand Up @@ -194,8 +196,8 @@ abstract class AbstractITSpec extends Specification {
expect: "database is initially empty"
RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
query(
Expand All @@ -218,8 +220,8 @@ abstract class AbstractITSpec extends Specification {
when: "an entity is POSTed via GraphQL API"
Response response = RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
mutation(
Expand Down Expand Up @@ -264,8 +266,8 @@ abstract class AbstractITSpec extends Specification {
then: "we can retrieve that entity next"
RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
query(
Expand Down Expand Up @@ -300,8 +302,8 @@ abstract class AbstractITSpec extends Specification {
when: "we update that entity"
RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
mutation(
Expand Down Expand Up @@ -340,8 +342,8 @@ abstract class AbstractITSpec extends Specification {
then: "we can retrieve that entity with updated attribute"
RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
query(
Expand Down Expand Up @@ -376,8 +378,8 @@ abstract class AbstractITSpec extends Specification {
when: "the entity is deleted"
RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
mutation(
Expand All @@ -403,8 +405,8 @@ abstract class AbstractITSpec extends Specification {
then: "that entity is not found in database anymore"
RestAssured
.given()
.contentType("application/json")
.accept("application/json")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
query(
Expand All @@ -424,4 +426,86 @@ abstract class AbstractITSpec extends Specification {
.statusCode(200)
.body(equalTo("""{"data":{"book":{"edges":[]}}}"""))
}

def "GraphQL API can sort and paginate (effectively fetching 1 record with some min/max attribute)"() {
given: "3 entities are inserted into the database"
createBook(new Book(id: 1, title: "Pride & Prejudice"))
createBook(new Book(id: 2, title: "Effective Java"))
createBook(new Book(id: 3, title: "Critiques of Pure Reason"))
expect: "sorting by ID in descending order and paginating to get the firsts result returns Kant's work"
RestAssured
.given()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: """
{
book(sort: "-id", first: "1", after: "0") {
edges {
node {
id
title
}
}
pageInfo {
totalRecords
startCursor
endCursor
hasNextPage
}
}
}
"""
)
.when().post().then()
.statusCode(200)
.body("", equalTo(
data: [
book: [
edges:[[
node: [
id: "3",
title:"Critiques of Pure Reason"
]
]],
pageInfo: [
totalRecords: 3,
startCursor: "0",
endCursor: "1",
hasNextPage:true
]
]
] as HashMap
))
}
static Response createBook(@NotNull final Book book) {
RestAssured
.given()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(
query: document(
mutation(
selection(
field(
"book",
arguments(
argument("op", "UPSERT"),
argument("data", book)
),
selections(
field("id"),
field("title")
)
)
)
)
).toQuery()
)
.when()
.post()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,8 @@
*/
package com.qubitpi.ws.jersey.template.application

import static com.yahoo.elide.test.graphql.GraphQLDSL.argument
import static com.yahoo.elide.test.graphql.GraphQLDSL.arguments
import static com.yahoo.elide.test.graphql.GraphQLDSL.document
import static com.yahoo.elide.test.graphql.GraphQLDSL.field
import static com.yahoo.elide.test.graphql.GraphQLDSL.mutation
import static com.yahoo.elide.test.graphql.GraphQLDSL.query
import static com.yahoo.elide.test.graphql.GraphQLDSL.selection
import static com.yahoo.elide.test.graphql.GraphQLDSL.selections
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.attr
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.attributes
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.data
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.datum
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.id
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.resource
import static com.yahoo.elide.test.jsonapi.JsonApiDSL.type
import static org.hamcrest.Matchers.equalTo

import com.yahoo.elide.jsonapi.JsonApi

import com.qubitpi.ws.jersey.template.models.Book
import com.qubitpi.ws.jersey.template.web.filters.OAuthFilter

import org.apache.http.HttpStatus
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
Expand All @@ -47,7 +26,6 @@ import org.testcontainers.spock.Testcontainers

import io.restassured.RestAssured
import io.restassured.builder.RequestSpecBuilder
import io.restassured.response.Response
import spock.lang.Shared

@Testcontainers
Expand Down

0 comments on commit 48fa9e5

Please sign in to comment.