Skip to content

Commit f8e22e4

Browse files
committed
chore: add kotlin type mapper tests
1 parent e86575b commit f8e22e4

File tree

1 file changed

+327
-0
lines changed
  • graphql-kotlin-toolkit-codegen/src/test/kotlin/com/auritylab/graphql/kotlin/toolkit/codegen/mapper

1 file changed

+327
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
package com.auritylab.graphql.kotlin.toolkit.codegen.mapper
2+
3+
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
4+
import com.squareup.kotlinpoet.ANY
5+
import com.squareup.kotlinpoet.BOOLEAN
6+
import com.squareup.kotlinpoet.BYTE
7+
import com.squareup.kotlinpoet.CHAR
8+
import com.squareup.kotlinpoet.COLLECTION
9+
import com.squareup.kotlinpoet.ClassName
10+
import com.squareup.kotlinpoet.DOUBLE
11+
import com.squareup.kotlinpoet.INT
12+
import com.squareup.kotlinpoet.LIST
13+
import com.squareup.kotlinpoet.LONG
14+
import com.squareup.kotlinpoet.ParameterizedTypeName
15+
import com.squareup.kotlinpoet.SHORT
16+
import com.squareup.kotlinpoet.STRING
17+
import com.squareup.kotlinpoet.TypeName
18+
import com.squareup.kotlinpoet.asTypeName
19+
import graphql.Scalars
20+
import graphql.schema.GraphQLArgument
21+
import graphql.schema.GraphQLDirective
22+
import graphql.schema.GraphQLEnumType
23+
import graphql.schema.GraphQLFieldDefinition
24+
import graphql.schema.GraphQLInputObjectType
25+
import graphql.schema.GraphQLInterfaceType
26+
import graphql.schema.GraphQLList
27+
import graphql.schema.GraphQLObjectType
28+
import graphql.schema.GraphQLType
29+
import graphql.schema.GraphQLUnionType
30+
import org.junit.jupiter.api.Assertions
31+
import org.junit.jupiter.api.DisplayName
32+
import org.junit.jupiter.api.Nested
33+
import org.junit.jupiter.api.Test
34+
import org.junit.jupiter.api.TestInstance
35+
import org.junit.jupiter.params.ParameterizedTest
36+
import org.junit.jupiter.params.provider.Arguments
37+
import org.junit.jupiter.params.provider.MethodSource
38+
import java.math.BigDecimal
39+
import java.math.BigInteger
40+
import java.util.stream.Stream
41+
42+
internal class KotlinTypeMapperTest {
43+
@Nested
44+
@DisplayName("getKotlinType()")
45+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
46+
inner class GetKotlinType_Scalars {
47+
val kotlinTypeMapper = KotlinTypeMapper(TestObject.options.copy(generateAll = true), TestObject.generatedMapper)
48+
49+
@ParameterizedTest
50+
@MethodSource(provideScalarTestTypesPointer)
51+
fun `should generate types for scalars correctly`(type: GraphQLType, expected: TypeName) {
52+
// Assert against each given type and expect the type to be the given expected type name.
53+
Assertions.assertEquals(expected, kotlinTypeMapper.getKotlinType(type))
54+
}
55+
56+
@Test
57+
fun `should generate input object type correctly`() {
58+
val testInputObject = GraphQLInputObjectType.newInputObject()
59+
.name("TestInput")
60+
.build()
61+
62+
// Generate the type name reference.
63+
val result = kotlinTypeMapper.getKotlinType(testInputObject)
64+
Assertions.assertNotNull(result)
65+
66+
// Cast to a ClassName to access additional properties.
67+
result as ClassName
68+
69+
Assertions.assertEquals("TestInput", result.simpleName)
70+
Assertions.assertTrue(result.isNullable)
71+
}
72+
73+
@Test
74+
fun `should generate enum type correctly`() {
75+
val testEnumType = GraphQLEnumType.newEnum()
76+
.name("TestEnum")
77+
.value("FIRST")
78+
.value("SECOND")
79+
.value("THIRD")
80+
.build()
81+
82+
// Generate the type name reference.
83+
val result = kotlinTypeMapper.getKotlinType(testEnumType)
84+
Assertions.assertNotNull(result)
85+
86+
// Cast to a ClassName to access additional properties.
87+
result as ClassName
88+
89+
Assertions.assertEquals("TestEnum", result.simpleName)
90+
Assertions.assertTrue(result.isNullable)
91+
}
92+
93+
@Test
94+
fun `should generate object type with representation correctly`() {
95+
val testObjectType = GraphQLObjectType.newObject()
96+
.name("TestObjectType")
97+
.withDirective(
98+
GraphQLDirective.newDirective()
99+
.name("kRepresentation")
100+
.argument(
101+
GraphQLArgument.newArgument()
102+
.name("class")
103+
.type(Scalars.GraphQLString)
104+
.value("kotlin.String")
105+
)
106+
)
107+
.build()
108+
109+
val result = kotlinTypeMapper.getKotlinType(testObjectType)
110+
Assertions.assertNotNull(result)
111+
Assertions.assertEquals(STRING.copy(true), result)
112+
}
113+
114+
@Test
115+
fun `should generate object type with generated correctly`() {
116+
val testObjectType = GraphQLObjectType.newObject()
117+
.name("TestObjectType")
118+
.build()
119+
120+
val result = kotlinTypeMapper.getKotlinType(testObjectType)
121+
Assertions.assertNotNull(result)
122+
123+
result as ClassName
124+
125+
Assertions.assertEquals("TestObjectType", result.simpleName)
126+
Assertions.assertTrue(result.isNullable)
127+
}
128+
129+
@Test
130+
fun `should generate interface type with representation correctly`() {
131+
val testInterfaceType = GraphQLInterfaceType.newInterface()
132+
.name("TestInterfaceType")
133+
.withDirective(
134+
GraphQLDirective.newDirective()
135+
.name("kRepresentation")
136+
.argument(
137+
GraphQLArgument.newArgument()
138+
.name("class")
139+
.type(Scalars.GraphQLString)
140+
.value("kotlin.String")
141+
)
142+
)
143+
.build()
144+
145+
val result = kotlinTypeMapper.getKotlinType(testInterfaceType)
146+
Assertions.assertNotNull(result)
147+
Assertions.assertEquals(STRING.copy(true), result)
148+
}
149+
150+
@Test
151+
fun `should generate interface type with generated correctly`() {
152+
val testInterfaceType = GraphQLInterfaceType.newInterface()
153+
.name("TestInterfaceType")
154+
.withDirective(getRepresentationDirective())
155+
.build()
156+
157+
val result = kotlinTypeMapper.getKotlinType(testInterfaceType)
158+
Assertions.assertNotNull(result)
159+
160+
Assertions.assertEquals(STRING.copy(nullable = true), result)
161+
Assertions.assertTrue(result.isNullable)
162+
}
163+
164+
@Test
165+
fun `should generate union type with same types correctly`() {
166+
val representationDirective = getRepresentationDirective()
167+
val firstPossibleType = getDummyObjectType("FirstObjectType", representationDirective)
168+
val secondPossibleType = getDummyObjectType("SecondObjectType", representationDirective)
169+
val thirdPossibleType = getDummyObjectType("ThirdObjectType", representationDirective)
170+
171+
val testUnionType = GraphQLUnionType.newUnionType()
172+
.name("TestUnion")
173+
.possibleTypes(firstPossibleType, secondPossibleType, thirdPossibleType)
174+
.build()
175+
176+
val result = kotlinTypeMapper.getKotlinType(testUnionType)
177+
Assertions.assertNotNull(result)
178+
Assertions.assertEquals(STRING.copy(nullable = true), result)
179+
}
180+
181+
@Test
182+
fun `should generate union type with no representation correctly`() {
183+
val firstPossibleType = getDummyObjectType("FirstObjectType")
184+
val secondPossibleType = getDummyObjectType("SecondObjectType")
185+
val thirdPossibleType = getDummyObjectType("ThirdObjectType")
186+
187+
val testUnionType = GraphQLUnionType.newUnionType()
188+
.name("TestUnion")
189+
.possibleTypes(firstPossibleType, secondPossibleType, thirdPossibleType)
190+
.build()
191+
192+
val result = kotlinTypeMapper.getKotlinType(testUnionType)
193+
Assertions.assertNotNull(result)
194+
Assertions.assertEquals(ANY.copy(nullable = true), result)
195+
}
196+
197+
@Test
198+
fun `should generate union type with different representations correctly`() {
199+
val firstPossibleType = getDummyObjectType("FirstObjectType")
200+
val secondPossibleType = getDummyObjectType("SecondObjectType", getRepresentationDirective("kotlin.String"))
201+
val thirdPossibleType = getDummyObjectType("ThirdObjectType")
202+
203+
val testUnionType = GraphQLUnionType.newUnionType()
204+
.name("TestUnion")
205+
.possibleTypes(firstPossibleType, secondPossibleType, thirdPossibleType)
206+
.build()
207+
208+
val result = kotlinTypeMapper.getKotlinType(testUnionType)
209+
Assertions.assertNotNull(result)
210+
Assertions.assertEquals(ANY.copy(nullable = true), result)
211+
}
212+
213+
@Test
214+
fun `should generate doubleNull type correctly`() {
215+
val type = Scalars.GraphQLString
216+
217+
val field = GraphQLFieldDefinition.newFieldDefinition()
218+
.name("simple")
219+
.type(type)
220+
.withDirective(getDoubleNullDirective())
221+
.build()
222+
223+
val result = kotlinTypeMapper.getKotlinType(type, field)
224+
Assertions.assertNotNull(result)
225+
226+
val rawType = (result as ParameterizedTypeName).rawType
227+
228+
Assertions.assertEquals("V", rawType.simpleName)
229+
Assertions.assertTrue(result.isNullable)
230+
231+
Assertions.assertEquals(1, result.typeArguments.size)
232+
Assertions.assertEquals(STRING.copy(nullable = true), result.typeArguments[0])
233+
}
234+
235+
@Test
236+
fun `should generate standard list type correctly`() {
237+
val innerType = Scalars.GraphQLString
238+
val type = GraphQLList(innerType)
239+
240+
val result = kotlinTypeMapper.getKotlinType(type)
241+
Assertions.assertNotNull(result)
242+
243+
val rawType = (result as ParameterizedTypeName).rawType
244+
245+
Assertions.assertEquals(COLLECTION, rawType)
246+
Assertions.assertTrue(result.isNullable)
247+
248+
Assertions.assertEquals(1, result.typeArguments.size)
249+
Assertions.assertEquals(STRING.copy(true), result.typeArguments[0])
250+
}
251+
252+
@Test
253+
fun `should generate custom list type correctly` () {
254+
val innerType = Scalars.GraphQLString
255+
val type = GraphQLList(innerType)
256+
257+
val result = kotlinTypeMapper.getKotlinType(type, listType = LIST)
258+
Assertions.assertNotNull(result)
259+
260+
val rawType = (result as ParameterizedTypeName).rawType
261+
262+
Assertions.assertEquals(LIST, rawType)
263+
Assertions.assertTrue(result.isNullable)
264+
265+
Assertions.assertEquals(1, result.typeArguments.size)
266+
Assertions.assertEquals(STRING.copy(true), result.typeArguments[0])
267+
}
268+
}
269+
270+
private fun getDoubleNullDirective(): GraphQLDirective =
271+
GraphQLDirective.newDirective()
272+
.name("kDoubleNull")
273+
.build()
274+
275+
private fun getGenerateDirective(): GraphQLDirective =
276+
GraphQLDirective.newDirective()
277+
.name("kGenerate")
278+
.build()
279+
280+
/**
281+
* Will build a 'kRepresentation' directive with the given [clazz] as value for the class argument.
282+
*/
283+
private fun getRepresentationDirective(clazz: String = "kotlin.String"): GraphQLDirective =
284+
GraphQLDirective.newDirective()
285+
.name("kRepresentation")
286+
.argument(
287+
GraphQLArgument.newArgument()
288+
.name("class")
289+
.type(Scalars.GraphQLString)
290+
.value(clazz)
291+
)
292+
.build()
293+
294+
/**
295+
* Will create a new dummy ObjectType with the givne [name] and the given [directives].
296+
*/
297+
private fun getDummyObjectType(
298+
name: String = "TestObjectType",
299+
vararg directives: GraphQLDirective
300+
): GraphQLObjectType =
301+
GraphQLObjectType.newObject()
302+
.name(name)
303+
.withDirectives(*directives)
304+
.build()
305+
306+
companion object {
307+
const val provideScalarTestTypesPointer =
308+
"com.auritylab.graphql.kotlin.toolkit.codegen.mapper.KotlinTypeMapperTest#provideScalarTestTypes"
309+
310+
@JvmStatic
311+
fun provideScalarTestTypes(): Stream<Arguments> {
312+
return Stream.of(
313+
Arguments.of(Scalars.GraphQLInt, INT.copy(true)),
314+
Arguments.of(Scalars.GraphQLFloat, DOUBLE.copy(true)),
315+
Arguments.of(Scalars.GraphQLString, STRING.copy(true)),
316+
Arguments.of(Scalars.GraphQLBoolean, BOOLEAN.copy(true)),
317+
Arguments.of(Scalars.GraphQLID, STRING.copy(true)),
318+
Arguments.of(Scalars.GraphQLLong, LONG.copy(true)),
319+
Arguments.of(Scalars.GraphQLShort, SHORT.copy(true)),
320+
Arguments.of(Scalars.GraphQLByte, BYTE.copy(true)),
321+
Arguments.of(Scalars.GraphQLBigInteger, BigInteger::class.asTypeName().copy(true)),
322+
Arguments.of(Scalars.GraphQLBigDecimal, BigDecimal::class.asTypeName().copy(true)),
323+
Arguments.of(Scalars.GraphQLChar, CHAR.copy(true))
324+
)
325+
}
326+
}
327+
}

0 commit comments

Comments
 (0)