-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathKotlinExamples.kt
More file actions
95 lines (70 loc) · 3.25 KB
/
KotlinExamples.kt
File metadata and controls
95 lines (70 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.dataloader
import org.junit.jupiter.api.Test
import reactor.core.publisher.Flux
import java.util.concurrent.CompletableFuture.completedFuture
/**
* Some Kotlin code to prove that are JSpecify annotations work here
* as expected in Kotlin land. We don't intend to ue Kotlin in our tests
* or to deliver Kotlin code in the java
*/
class KotlinExamples {
@Test
fun `basic kotlin test of non nullable value types`() {
val batchLoadFunction = BatchLoader<String, String>
{ keys -> completedFuture(keys.toList()) }
val dataLoader: DataLoader<String, String> =
DataLoaderFactory.newDataLoader(batchLoadFunction)
val cfA = dataLoader.load("A")
val cfB = dataLoader.load("B")
dataLoader.dispatch()
assert(cfA.join().equals("A"))
assert(cfB.join().equals("B"))
}
@Test
fun `basic kotlin test of nullable value types`() {
val batchLoadFunction: BatchLoader<String, String?> = BatchLoader { keys -> completedFuture(keys.toList()) }
val dataLoader: DataLoader<String, String?> = DataLoaderFactory.newDataLoader(batchLoadFunction)
standardNullableAsserts(dataLoader)
}
@Test
fun `basic kotlin test of nullable value types in mapped batch loader`() {
val batchLoadFunction = MappedBatchLoader<String, String?>
{ keys -> completedFuture(keys.associateBy({ it })) }
val dataLoader: DataLoader<String, String?> = DataLoaderFactory.newMappedDataLoader(batchLoadFunction)
standardNullableAsserts(dataLoader)
}
@Test
fun `basic kotlin test of nullable value types in mapped batch loader with context`() {
val batchLoadFunction = MappedBatchLoaderWithContext<String, String?>
{ keys, env -> completedFuture(keys.associateBy({ it })) }
val dataLoader: DataLoader<String, String?> = DataLoaderFactory.newMappedDataLoader(batchLoadFunction)
standardNullableAsserts(dataLoader)
}
@Test
fun `basic kotlin test of nullable value types in mapped batch publisher`() {
val batchLoadFunction = MappedBatchPublisher<String, String?>
{ keys, subscriber ->
val map: Map<String, String?> = keys.associateBy({ it })
Flux.fromIterable(map.entries).subscribe(subscriber);
}
val dataLoader: DataLoader<String, String?> = DataLoaderFactory.newMappedPublisherDataLoader(batchLoadFunction)
standardNullableAsserts(dataLoader)
}
@Test
fun `basic kotlin test of nullable value types in mapped batch publisher with context`() {
val batchLoadFunction = MappedBatchPublisherWithContext<String, String?>
{ keys, subscriber, env ->
val map: Map<String, String?> = keys.associateBy({ it })
Flux.fromIterable(map.entries).subscribe(subscriber);
}
val dataLoader: DataLoader<String, String?> = DataLoaderFactory.newMappedPublisherDataLoader(batchLoadFunction)
standardNullableAsserts(dataLoader)
}
private fun standardNullableAsserts(dataLoader: DataLoader<String, String?>) {
val cfA = dataLoader.load("A")
val cfB = dataLoader.load("B")
dataLoader.dispatch()
assert(cfA.join().equals("A"))
assert(cfB.join().equals("B"))
}
}