mirror of
https://github.com/jlengrand/supabase-mock-demo-kotlin.git
synced 2026-03-10 00:31:18 +00:00
Adding all other test methods
This commit is contained in:
@@ -34,6 +34,8 @@ dependencies {
|
||||
exclude(group = "org.slf4j", module = "slf4j-api")
|
||||
}
|
||||
|
||||
testImplementation("io.mockk:mockk:1.13.7")
|
||||
testImplementation("io.ktor:ktor-client-mock:2.3.5")
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import io.github.jan.supabase.SupabaseClient
|
||||
import io.github.jan.supabase.createSupabaseClient
|
||||
import io.github.jan.supabase.postgrest.Postgrest
|
||||
import io.github.jan.supabase.postgrest.postgrest
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@@ -25,18 +28,42 @@ data class ResultPerson (
|
||||
fun main() {
|
||||
println("Hello World!")
|
||||
// Application goes here
|
||||
val supabaseClient = createSupabaseClient(
|
||||
supabaseUrl = "",
|
||||
supabaseKey = ""
|
||||
) {
|
||||
install(Postgrest)
|
||||
}
|
||||
|
||||
runBlocking {
|
||||
savePerson(listOf(Person("Jan", 30), Person("Jane", 42)), supabaseClient)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getPerson(client: SupabaseClient): List<ResultPerson> {
|
||||
return client
|
||||
.postgrest["person"]
|
||||
.select().decodeList()
|
||||
.select().decodeList<ResultPerson>()
|
||||
.filter { it.age > 18 }
|
||||
}
|
||||
|
||||
|
||||
suspend fun savePerson(persons: List<Person>, client: SupabaseClient): List<ResultPerson> {
|
||||
val adults = persons.filter { it.age > 18 }
|
||||
|
||||
return client
|
||||
.postgrest["person"]
|
||||
.insert(persons)
|
||||
.insert(adults)
|
||||
.decodeList<ResultPerson>()
|
||||
}
|
||||
|
||||
// Used in MainKtTestSubclass
|
||||
class DatabaseClient(private val client: SupabaseClient){
|
||||
suspend fun savePerson(persons: List<Person>): List<ResultPerson> {
|
||||
val adults = persons.filter { it.age > 18 }
|
||||
|
||||
return client
|
||||
.postgrest["person"]
|
||||
.insert(adults)
|
||||
.decodeList<ResultPerson>()
|
||||
}
|
||||
}
|
||||
49
src/test/kotlin/MainKtTestMock.kt
Normal file
49
src/test/kotlin/MainKtTestMock.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
import io.github.jan.supabase.SupabaseClient
|
||||
import io.github.jan.supabase.createSupabaseClient
|
||||
import io.github.jan.supabase.postgrest.Postgrest
|
||||
import io.github.jan.supabase.postgrest.postgrest
|
||||
import io.github.jan.supabase.postgrest.query.PostgrestBuilder
|
||||
import io.github.jan.supabase.postgrest.query.PostgrestResult
|
||||
import io.ktor.http.*
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
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
|
||||
import java.nio.file.Path
|
||||
import kotlin.test.BeforeTest
|
||||
|
||||
class MainKtTestMock {
|
||||
|
||||
private lateinit var supabaseClient : SupabaseClient
|
||||
|
||||
@BeforeTest
|
||||
fun setUp() {
|
||||
|
||||
supabaseClient = mockk<SupabaseClient>()
|
||||
val postgrest = mockk<Postgrest>()
|
||||
val postgrestBuilder = mockk<PostgrestBuilder>()
|
||||
val postgrestResult = PostgrestResult(body = null, headers = Headers.Empty)
|
||||
|
||||
every { supabaseClient.postgrest } returns postgrest
|
||||
every { postgrest["path"] } returns postgrestBuilder
|
||||
coEvery { postgrestBuilder.insert(values = any<List<Path>>()) } returns postgrestResult
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSavePerson(){
|
||||
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() })
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/test/kotlin/MainKtTestMockEngine.kt
Normal file
27
src/test/kotlin/MainKtTestMockEngine.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
import io.github.jan.supabase.SupabaseClient
|
||||
import io.github.jan.supabase.createSupabaseClient
|
||||
import io.ktor.client.engine.mock.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class MainKtTestMockEngine {
|
||||
|
||||
private val supabaseClient : SupabaseClient = createSupabaseClient("", "",) {
|
||||
httpEngine = MockEngine { _ ->
|
||||
respond(Json.encodeToString(Person.serializer(), Person("name_1", 16)))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSavePerson(){
|
||||
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() })
|
||||
}
|
||||
}
|
||||
}
|
||||
28
src/test/kotlin/MainKtTestSubclass.kt
Normal file
28
src/test/kotlin/MainKtTestSubclass.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.BeforeTest
|
||||
|
||||
class MainKtTestSubclass {
|
||||
|
||||
private lateinit var client : DatabaseClient
|
||||
|
||||
@BeforeTest
|
||||
fun setUp() {
|
||||
client = mockk<DatabaseClient>()
|
||||
coEvery { client.savePerson(any<List<Person>>()) } returns listOf(ResultPerson(2, "name_2", 2, "timestamp_2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSavePerson(){
|
||||
val fakePersons = listOf(Person("name_1", 16), Person("name_2", 28))
|
||||
|
||||
runBlocking {
|
||||
val result = client.savePerson(fakePersons)
|
||||
assertEquals(2, result.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user