11package com.expedia.graphql.generator.types
22
3+ import com.expedia.graphql.SchemaGeneratorConfig
34import com.expedia.graphql.annotations.GraphQLDescription
45import com.expedia.graphql.annotations.GraphQLDirective
56import com.expedia.graphql.annotations.GraphQLID
6- import com.expedia.graphql.generator.extensions.getValidProperties
7+ import com.expedia.graphql.generator.SchemaGenerator
8+ import com.expedia.graphql.hooks.NoopSchemaGeneratorHooks
79import graphql.Scalars
810import graphql.introspection.Introspection
11+ import graphql.schema.DataFetcher
12+ import graphql.schema.DataFetcherFactory
913import graphql.schema.GraphQLNonNull
14+ import graphql.schema.PropertyDataFetcher
15+ import io.mockk.every
16+ import io.mockk.mockk
1017import org.junit.jupiter.api.Test
1118import kotlin.test.assertEquals
19+ import kotlin.test.assertFalse
20+ import kotlin.test.assertNull
1221import kotlin.test.assertTrue
1322
1423@Suppress(" Detekt.UnusedPrivateClass" )
15- internal class PropertyTypeTest : TypeTestHelper () {
24+ internal class PropertyTypeBuilderTest : TypeTestHelper () {
1625
1726 @GraphQLDirective(locations = [Introspection .DirectiveLocation .FIELD ])
1827 internal annotation class PropertyDirective (val arg : String )
@@ -25,6 +34,8 @@ internal class PropertyTypeTest : TypeTestHelper() {
2534
2635 @Deprecated(" Healthy food is deprecated" , replaceWith = ReplaceWith (" cake" ))
2736 lateinit var healthyFood: String
37+
38+ var nullableCake: String? = null
2839 }
2940
3041 private data class DataClassWithProperties (
@@ -43,15 +54,15 @@ internal class PropertyTypeTest : TypeTestHelper() {
4354
4455 @Test
4556 fun `Test naming` () {
46- val prop = ClassWithProperties ::class .getValidProperties(hooks)[ 0 ]
57+ val prop = ClassWithProperties ::cake
4758 val result = builder.property(prop, ClassWithProperties ::class )
4859
4960 assertEquals(" cake" , result.name)
5061 }
5162
5263 @Test
5364 fun `Test deprecation` () {
54- val prop = ClassWithProperties ::class .getValidProperties(hooks)[ 0 ]
65+ val prop = ClassWithProperties ::cake
5566 val result = builder.property(prop, ClassWithProperties ::class )
5667
5768 assertTrue(result.isDeprecated)
@@ -60,7 +71,7 @@ internal class PropertyTypeTest : TypeTestHelper() {
6071
6172 @Test
6273 fun `Test deprecation with replacement` () {
63- val prop = ClassWithProperties ::class .getValidProperties(hooks)[ 1 ]
74+ val prop = ClassWithProperties ::healthyFood
6475 val result = builder.property(prop, ClassWithProperties ::class )
6576
6677 assertTrue(result.isDeprecated)
@@ -69,31 +80,31 @@ internal class PropertyTypeTest : TypeTestHelper() {
6980
7081 @Test
7182 fun `Test description` () {
72- val prop = ClassWithProperties ::class .getValidProperties(hooks)[ 0 ]
83+ val prop = ClassWithProperties ::cake
7384 val result = builder.property(prop, ClassWithProperties ::class )
7485
7586 assertEquals(" The truth" , result.description)
7687 }
7788
7889 @Test
7990 fun `Test description on data class` () {
80- val prop = DataClassWithProperties ::class .getValidProperties(hooks)[ 0 ]
91+ val prop = DataClassWithProperties ::fooBar
8192 val result = builder.property(prop, DataClassWithProperties ::class )
8293
8394 assertEquals(" A great description" , result.description)
8495 }
8596
8697 @Test
8798 fun `Test graphql id on data class` () {
88- val prop = DataClassWithProperties ::class .getValidProperties(hooks)[ 1 ]
99+ val prop = DataClassWithProperties ::myId
89100 val result = builder.property(prop, DataClassWithProperties ::class )
90101
91102 assertEquals(" ID" , (result.type as ? GraphQLNonNull )?.wrappedType?.name)
92103 }
93104
94105 @Test
95106 fun `Test custom directive` () {
96- val prop = ClassWithProperties ::class .getValidProperties(hooks)[ 0 ]
107+ val prop = ClassWithProperties ::cake
97108 val result = builder.property(prop, ClassWithProperties ::class )
98109
99110 assertEquals(1 , result.directives.size)
@@ -107,4 +118,33 @@ internal class PropertyTypeTest : TypeTestHelper() {
107118 setOf (Introspection .DirectiveLocation .FIELD )
108119 )
109120 }
121+
122+ @Test
123+ fun `Test nullable property` () {
124+ val prop = ClassWithProperties ::nullableCake
125+ val result = builder.property(prop, ClassWithProperties ::class )
126+
127+ assertNull(result.description)
128+ assertTrue(result.type !is GraphQLNonNull )
129+ assertTrue(result.dataFetcher is PropertyDataFetcher )
130+ }
131+
132+ @Test
133+ fun `Test lateinit, non-null property with datafetcher factory` () {
134+ val localConfig: SchemaGeneratorConfig = mockk()
135+ every { localConfig.hooks } returns NoopSchemaGeneratorHooks ()
136+ every { localConfig.supportedPackages } returns emptyList()
137+ val mockDataDatafetcher: DataFetcher <* > = mockk()
138+ val dataFetcherFactory: DataFetcherFactory <* > = mockk()
139+ every { dataFetcherFactory.get(any()) } returns mockDataDatafetcher
140+ every { localConfig.dataFetcherFactory } returns dataFetcherFactory
141+ val localGenerator = SchemaGenerator (localConfig)
142+ val localBuilder = PropertyTypeBuilder (localGenerator)
143+
144+ val prop = ClassWithProperties ::cake
145+ val result = localBuilder.property(prop, ClassWithProperties ::class )
146+
147+ assertFalse(result.dataFetcher is PropertyDataFetcher )
148+ assertEquals(expected = mockDataDatafetcher, actual = result.dataFetcher)
149+ }
110150}
0 commit comments