-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
a12bc35
commit 906e2a5
Showing
9 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
shared/src/commonTest/kotlin/AddOrUpdateTodoUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} |
8 changes: 8 additions & 0 deletions
8
shared/src/jvmTest/kotlin/com/soulesidibe/domain/suspendTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} |