11package org.dataloader
22
33import org.junit.jupiter.api.Test
4- import java.util.concurrent.CompletableFuture
5- import java.util.concurrent.CompletableFuture.*
4+ import reactor.core.publisher.Flux
5+ import java.util.concurrent.CompletableFuture.completedFuture
66
77/* *
88 * Some Kotlin code to prove that are JSpecify annotations work here
@@ -13,28 +13,83 @@ class KotlinExamples {
1313
1414 @Test
1515 fun `basic kotlin test of non nullable value types` () {
16- val dataLoader: DataLoader <String , String > = DataLoaderFactory .newDataLoader { keys -> completedFuture(keys.toList()) }
16+ val batchLoadFunction = BatchLoader <String , String >
17+ { keys -> completedFuture(keys.toList()) }
18+ val dataLoader: DataLoader <String , String > =
19+ DataLoaderFactory .newDataLoader(batchLoadFunction)
1720
1821 val cfA = dataLoader.load(" A" )
1922 val cfB = dataLoader.load(" B" )
2023
2124 dataLoader.dispatch()
2225
2326 assert (cfA.join().equals(" A" ))
24- assert (cfA .join().equals(" A " ))
27+ assert (cfB .join().equals(" B " ))
2528 }
2629
2730 @Test
2831 fun `basic kotlin test of nullable value types` () {
29- val dataLoader: DataLoader <String , String ?> = DataLoaderFactory .newDataLoader { keys -> completedFuture(keys.toList()) }
32+ val batchLoadFunction: BatchLoader <String , String ?> = BatchLoader { keys -> completedFuture(keys.toList()) }
33+ val dataLoader: DataLoader <String , String ?> = DataLoaderFactory .newDataLoader(batchLoadFunction)
34+
35+ standardNullableAsserts(dataLoader)
36+ }
37+
38+ @Test
39+ fun `basic kotlin test of nullable value types in mapped batch loader` () {
40+ val batchLoadFunction = MappedBatchLoader <String , String ?>
41+ { keys -> completedFuture(keys.associateBy({ it })) }
42+
43+ val dataLoader: DataLoader <String , String ?> = DataLoaderFactory .newMappedDataLoader(batchLoadFunction)
44+
45+ standardNullableAsserts(dataLoader)
46+ }
47+
48+ @Test
49+ fun `basic kotlin test of nullable value types in mapped batch loader with context` () {
50+ val batchLoadFunction = MappedBatchLoaderWithContext <String , String ?>
51+ { keys, env -> completedFuture(keys.associateBy({ it })) }
52+
53+ val dataLoader: DataLoader <String , String ?> = DataLoaderFactory .newMappedDataLoader(batchLoadFunction)
54+
55+ standardNullableAsserts(dataLoader)
56+ }
3057
58+ @Test
59+ fun `basic kotlin test of nullable value types in mapped batch publisher` () {
60+ val batchLoadFunction = MappedBatchPublisher <String , String ?>
61+ { keys, subscriber ->
62+ val map: Map <String , String ?> = keys.associateBy({ it })
63+ Flux .fromIterable(map.entries).subscribe(subscriber);
64+ }
65+
66+ val dataLoader: DataLoader <String , String ?> = DataLoaderFactory .newMappedPublisherDataLoader(batchLoadFunction)
67+
68+ standardNullableAsserts(dataLoader)
69+ }
70+
71+ @Test
72+ fun `basic kotlin test of nullable value types in mapped batch publisher with context` () {
73+ val batchLoadFunction = MappedBatchPublisherWithContext <String , String ?>
74+ { keys, subscriber, env ->
75+ val map: Map <String , String ?> = keys.associateBy({ it })
76+ Flux .fromIterable(map.entries).subscribe(subscriber);
77+ }
78+
79+ val dataLoader: DataLoader <String , String ?> = DataLoaderFactory .newMappedPublisherDataLoader(batchLoadFunction)
80+
81+ standardNullableAsserts(dataLoader)
82+ }
83+
84+ private fun standardNullableAsserts (dataLoader : DataLoader <String , String ?>) {
3185 val cfA = dataLoader.load(" A" )
3286 val cfB = dataLoader.load(" B" )
3387
3488 dataLoader.dispatch()
3589
3690 assert (cfA.join().equals(" A" ))
37- assert (cfA .join().equals(" A " ))
91+ assert (cfB .join().equals(" B " ))
3892 }
3993
94+
4095}
0 commit comments