/
MainKtTestTestContainers.kt
63 lines (53 loc) · 2.21 KB
/
MainKtTestTestContainers.kt
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
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.postgrest.Postgrest
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.testcontainers.containers.ComposeContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import java.io.File
@Testcontainers
class MainKtTestTestContainers {
// The jwt token is calculated manually (https://jwt.io/) based on the private key in the docker-compose.yml file, and a payload of {"role":"postgres"} to match the user in the database
private val jwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicG9zdGdyZXMifQ.88jCdmcEuy2McbdwKPmuazNRD-dyD65WYeKIONDXlxg"
private lateinit var supabaseClient: SupabaseClient
@Container
var environment: ComposeContainer =
ComposeContainer(File("src/test/resources/docker-compose.yml"))
.withExposedService("postgrest-db", 5432)
.withExposedService("postgrest", 3000)
.withExposedService("nginx", 80)
@BeforeEach
fun setUp() {
val fakeSupabaseUrl = environment.getServiceHost("nginx", 80) +
":" + environment.getServicePort("nginx", 80)
supabaseClient = createSupabaseClient(
supabaseUrl = "http://$fakeSupabaseUrl",
supabaseKey = jwtToken
) {
install(Postgrest)
}
}
@Test
fun testEmptyPersonTable(){
runBlocking {
val result = getPerson(supabaseClient)
assertEquals(0, result.size)
}
}
@Test
fun testSavePersonAndRetrieve(){
val randomPersons = listOf(Person("Jan", 30), Person("Jane", 42))
runBlocking {
val result = savePerson(randomPersons, supabaseClient)
assertEquals(2, result.size)
assertEquals(randomPersons, result.map { it.toPerson() })
val fetchResult = getPerson(supabaseClient)
assertEquals(2, fetchResult.size)
assertEquals(randomPersons, fetchResult.map { it.toPerson() })
}
}
}