Skip to content

Commit

Permalink
tests are not working yet
Browse files Browse the repository at this point in the history
The tools we use on a jvm target dont work on common
We need to use tools like the kotlin test library that will vring @ttest and the assertions
we can also use karmok for all things mock and turbine if we want to test flows
for now I'll not deep on this one
  • Loading branch information
soulesidibe committed Mar 31, 2022
1 parent a12bc35 commit 906e2a5
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 4 deletions.
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ object Deps {
const val mockito = "org.mockito:mockito-core:3.9.0"
const val mockk = "io.mockk:mockk:1.12.0"
const val koin_test = "io.insert-koin:koin-test:${Versions.koin}"

val commonTest = "org.jetbrains.kotlin:kotlin-test-common:${Versions.kotlin}"
val commonAnnotations = "org.jetbrains.kotlin:kotlin-test-annotations-common:${Versions.kotlin}"
}
10 changes: 6 additions & 4 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ kotlin {
}
}

// val commonTest by getting {
// dependencies {
val commonTest by getting {
dependencies {
implementation(Deps.commonTest)
implementation(Deps.commonAnnotations)
// implementation(Deps.junit)
// implementation(Deps.coroutines_test)
// implementation(Deps.hamcrest)
// implementation(Deps.mockito)
// implementation(Deps.mockk)
// implementation(Deps.koin_test)
// }
// }
}
}
}
}
40 changes: 40 additions & 0 deletions shared/src/commonTest/kotlin/AddOrUpdateTodoUseCaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.soulesidibe.domain

import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.CannotAddOrUpdateException
import com.soulesidibe.domain.repository.TodoRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import kotlin.test.Test
import kotlin.test.assertEquals

@ExperimentalCoroutinesApi
class AddOrUpdateTodoUseCaseTest {


@Test
fun `should add or update a todo`() = suspendTest {
val todo = TodoEntity("12345", "test todo")
val repository = mock(TodoRepository::class.java)
`when`(repository.addOrUpdate(todo)).thenReturn(true)

val usecase = AddOrUpdateTodoUseCase(repository)
val responseResult = usecase.execute(todo)
assertEquals(Response.success(true), responseResult)
}

@Test
fun `should return a fail response`() = suspendTest {
val todo = TodoEntity("12345", "test todo")
val repository = mock(TodoRepository::class.java)
`when`(repository.addOrUpdate(todo)).thenThrow(CannotAddOrUpdateException)

val useCase = AddOrUpdateTodoUseCase(repository)
val responseResult = useCase.execute(todo)
assertEquals(
Response.Error<CannotAddOrUpdateException>(CannotAddOrUpdateException),
responseResult
)
}
}
44 changes: 44 additions & 0 deletions shared/src/commonTest/kotlin/GetTodoUseCaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.soulesidibe.domain

import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.NoTodoFoundException
import com.soulesidibe.domain.repository.TodoRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import kotlin.test.Test
import kotlin.test.assertEquals

@ExperimentalCoroutinesApi
class GetTodoUseCaseTest {

@Test
fun `should get a todo`() = suspendTest {
val fakeId = "12345"
val repository = mock(TodoRepository::class.java)
`when`(repository.byId(fakeId)).thenReturn(TodoEntity("12345", "Test"))

val usecase = GetTodoUseCase(repository)

val response = usecase.execute(fakeId)
assertEquals(
Response.success(TodoEntity("12345", "Test")),
response
)
}

@Test
fun `should get an error when no todo id do no exist`() = suspendTest {
val fakeId = "12345"
val repository = mock(TodoRepository::class.java)
`when`(repository.byId(fakeId)).thenReturn(null)

val usecase = GetTodoUseCase(repository)

val response = usecase.execute(fakeId)
assertEquals(
Response.failure<NoTodoFoundException>(NoTodoFoundException),
response
)
}
}
45 changes: 45 additions & 0 deletions shared/src/commonTest/kotlin/GetTodosUseCaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.soulesidibe.domain

import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.NoTodosFoundException
import com.soulesidibe.domain.repository.TodoRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flowOf
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import kotlin.test.Test
import kotlin.test.assertEquals

@ExperimentalCoroutinesApi
class GetTodosUseCaseTest {

@Test
fun `should get the list of todos`() = suspendTest {
val repository = mock(TodoRepository::class.java)
`when`(repository.get()).thenReturn(flowOf(Response.success(listOf())))

val useCase: GetTodosUseCase = GetTodosUseCase(repository)
val result = useCase.execute(None())

assertEquals(
Response.Success<List<TodoEntity>>(listOf()),
result.firstOrNull()
)
}


@Test
fun `should get a fail response`() = suspendTest {
val repository = mock(TodoRepository::class.java)
`when`(repository.get()).thenReturn(flowOf(Response.failure(NoTodosFoundException)))

val useCase: GetTodosUseCase = GetTodosUseCase(repository)
val result = useCase.execute(None())

assertEquals(
Response.Error<NoTodosFoundException>(NoTodosFoundException),
result.firstOrNull()
)
}
}
37 changes: 37 additions & 0 deletions shared/src/commonTest/kotlin/RemoveTodoUseCaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.soulesidibe.domain

import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.NoTodoFoundException
import com.soulesidibe.domain.repository.TodoRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalCoroutinesApi::class)
class RemoveTodoUseCaseTest {


@Test
fun `should remove the todo if exsist`() = suspendTest {
val repository = mock(TodoRepository::class.java)
`when`(repository.byId("12345")).thenReturn(TodoEntity("12345", "test"))
`when`(repository.remove(TodoEntity("12345", "test"))).thenReturn(true)


val usecase = RemoveTodoUseCase(repository)
val response = usecase.execute("12345")
assertEquals(Response.success(true), response)
}

@Test
fun `should not remove the todo if not exsist`() = suspendTest {
val repository = mock(TodoRepository::class.java)
`when`(repository.byId("12345")).thenReturn(null)

val usecase = RemoveTodoUseCase(repository)
val response = usecase.execute("12345")
assertEquals(Response.Error<NoTodoFoundException>(NoTodoFoundException), response)
}
}
5 changes: 5 additions & 0 deletions shared/src/commonTest/kotlin/test_utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.soulesidibe.domain

import kotlinx.coroutines.CoroutineScope

expect fun suspendTest(body: suspend CoroutineScope.() -> Unit)
8 changes: 8 additions & 0 deletions shared/src/iosTest/kotlin/suspendTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.soulesidibe.domain

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking

actual fun suspendTest(body: suspend CoroutineScope.() -> Unit) {
runBlocking { body() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.soulesidibe.domain

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking

actual fun suspendTest(body: suspend CoroutineScope.() -> Unit) {
runBlocking { body() }
}

0 comments on commit 906e2a5

Please sign in to comment.