Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Commit

Permalink
Upgrade to kotlin 1.3.10 and add unit test for #40
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Gutkowski committed Jan 8, 2019
1 parent d61a72a commit 780eda8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ group 'com.github.pgutkowski'
version '0.3.0'

buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_version = '1.3.10'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,62 @@ import com.github.pgutkowski.kgraphql.schema.scalar.StringScalarCoercion
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import java.time.LocalDate
import java.util.*

@Specification("3.1.1 Scalars")
class ScalarsSpecificationTest {

data class Person(val uuid : UUID, val name : String)
data class Person(val uuid: UUID, val name: String)

@Test
fun `type systems can add additional scalars with semantic meaning`(){
fun `type systems can add additional scalars with semantic meaning`() {
val uuid = UUID.randomUUID()
val testedSchema = KGraphQL.schema {
stringScalar<UUID> {
description = "unique identifier of object"

coercion = object : StringScalarCoercion<UUID>{
coercion = object : StringScalarCoercion<UUID> {
override fun serialize(instance: UUID): String = instance.toString()

override fun deserialize(raw: String): UUID = UUID.fromString(raw)
}
}
query("person"){
resolver{ -> Person(uuid, "John Smith")}
query("person") {
resolver { -> Person(uuid, "John Smith") }
}
mutation("createPerson") {
resolver{ uuid : UUID, name: String -> Person(uuid, name) }
resolver { uuid: UUID, name: String -> Person(uuid, name) }
}
}

val queryResponse = deserialize(testedSchema.execute("{person{uuid}}"))
assertThat(queryResponse.extract<String>("data/person/uuid"), equalTo(uuid.toString()))

val mutationResponse = deserialize(testedSchema.execute(
"{createPerson(uuid: \"$uuid\", name: \"John\"){uuid name}}"
"{createPerson(uuid: \"$uuid\", name: \"John\"){uuid name}}"
))
assertThat(mutationResponse.extract<String>("data/createPerson/uuid"), equalTo(uuid.toString()))
assertThat(mutationResponse.extract<String>("data/createPerson/name"), equalTo("John"))
}

@Test
fun `integer value represents a value grater than 2^-31 and less or equal to 2^31`(){
fun `integer value represents a value grater than 2^-31 and less or equal to 2^31`() {
val schema = KGraphQL.schema {
mutation("Int") {
resolver { int : Int -> int }
resolver { int: Int -> int }
}
}

expect<RequestException>("is not valid value of type Int"){
schema.execute("{Int(int: ${Integer.MAX_VALUE.toLong()+2L})}")
expect<RequestException>("is not valid value of type Int") {
schema.execute("{Int(int: ${Integer.MAX_VALUE.toLong() + 2L})}")
}
}

@Test
fun `when float is expected as an input type, both integer and float input values are accepted`(){
fun `when float is expected as an input type, both integer and float input values are accepted`() {
val schema = KGraphQL.schema {
mutation("float"){
mutation("float") {
resolver { float: Float -> float }
}
}
Expand All @@ -73,16 +74,16 @@ class ScalarsSpecificationTest {
}

@Test
fun `server can declare custom ID type`(){
fun `server can declare custom ID type`() {
val testedSchema = KGraphQL.schema {
stringScalar<UUID> {
name = "ID"
description = "unique identifier of object"
deserialize = { uuid : String -> UUID.fromString(uuid) }
deserialize = { uuid: String -> UUID.fromString(uuid) }
serialize = UUID::toString
}
query("personById"){
resolver{ id: UUID -> Person(id, "John Smith")}
query("personById") {
resolver { id: UUID -> Person(id, "John Smith") }
}
}

Expand All @@ -93,31 +94,31 @@ class ScalarsSpecificationTest {


@Test
fun `For numeric scalars, input string with numeric content must raise a query error indicating an incorrect type`(){
fun `For numeric scalars, input string with numeric content must raise a query error indicating an incorrect type`() {
val schema = KGraphQL.schema {
mutation("Int") {
resolver { int : Int -> int }
resolver { int: Int -> int }
}
}

expect<RequestException>(""){
expect<RequestException>("") {
schema.execute("{Int(int: \"223\")}")
}
}

data class Number(val int : Int)
data class Number(val int: Int)

@Test
fun `Schema may declare custom int scalar type`(){
fun `Schema may declare custom int scalar type`() {

val schema = KGraphQL.schema {
intScalar<Number> {
deserialize = ::Number
serialize = { (int) -> int }
}

query("number"){
resolver { number : Number -> number }
query("number") {
resolver { number: Number -> number }
}
}

Expand All @@ -129,16 +130,16 @@ class ScalarsSpecificationTest {
data class Bool(val boolean: Boolean)

@Test
fun `Schema may declare custom boolean scalar type`(){
fun `Schema may declare custom boolean scalar type`() {

val schema = KGraphQL.schema {
booleanScalar<Bool> {
deserialize = ::Bool
serialize = { (boolean) -> boolean }
}

query("boolean"){
resolver { boolean : Boolean -> boolean }
query("boolean") {
resolver { boolean: Boolean -> boolean }
}
}

Expand All @@ -155,16 +156,16 @@ class ScalarsSpecificationTest {
data class Multi(val boo: Boo, val str: String, val num: Num)

@Test
fun `Schema may declare custom double scalar type`(){
fun `Schema may declare custom double scalar type`() {

val schema = KGraphQL.schema {
floatScalar<Dob> {
deserialize = ::Dob
serialize = { (double) -> double }
}

query("double"){
resolver { double : Dob -> double }
query("double") {
resolver { double: Dob -> double }
}
}

Expand All @@ -174,7 +175,7 @@ class ScalarsSpecificationTest {
}

@Test
fun `Scalars within input variables`(){
fun `Scalars within input variables`() {
val schema = KGraphQL.schema {
booleanScalar<Boo> {
deserialize = ::Boo
Expand All @@ -197,11 +198,11 @@ class ScalarsSpecificationTest {
serialize = { (str) -> str }
}

query("boo") { resolver { boo : Boo -> boo } }
query("lon") { resolver { lon : Lon -> lon } }
query("dob") { resolver { dob : Dob -> dob } }
query("num") { resolver { num : Num -> num } }
query("str") { resolver { str : Str -> str } }
query("boo") { resolver { boo: Boo -> boo } }
query("lon") { resolver { lon: Lon -> lon } }
query("dob") { resolver { dob: Dob -> dob } }
query("num") { resolver { num: Num -> num } }
query("str") { resolver { str: Str -> str } }
query("multi") { resolver { -> Multi(Boo(false), "String", Num(25)) } }
}

Expand Down Expand Up @@ -244,4 +245,44 @@ class ScalarsSpecificationTest {
assertThat(response.extract<String>("data/multi/str"), equalTo("String"))
assertThat(response.extract<Int>("data/multi/num"), equalTo(25))
}

data class NewPart(val manufacturer: String, val name: String, val oem: Boolean, val addedDate: LocalDate)

@Test
fun `Schema may declare LocalDate custom scalar`() {
val schema = KGraphQL.schema {
stringScalar<LocalDate> {
serialize = { date -> date.toString() }
deserialize = { dateString -> LocalDate.parse(dateString) }
description = "Date in format yyyy-mm-dd"
}

mutation("addPart") {
description = "Adds a new part in the parts inventory database"
resolver { newPart: NewPart ->
println(newPart)

newPart
}
}

inputType<NewPart> {}
}

val manufacturer = """Joe Bloggs"""

val response = deserialize(schema.execute(
"mutation Mutation(\$newPart : NewPart!){ addPart(newPart: \$newPart) {manufacturer} }",
"""
{ "newPart" : {
"manufacturer":"$manufacturer",
"name":"Front bumper",
"oem":true,
"addedDate":"2001-09-01"
}}
""".trimIndent())
)

assertThat(response.extract<String>("data/addPart/manufacturer"), equalTo(manufacturer))
}
}

0 comments on commit 780eda8

Please sign in to comment.