-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fake data source and using dependency injection
- Loading branch information
1 parent
06eb48e
commit 4a39fc9
Showing
6 changed files
with
154 additions
and
18 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
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
42 changes: 42 additions & 0 deletions
42
...example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepositoryTest.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,42 @@ | ||
package com.example.android.architecture.blueprints.todoapp.data.source | ||
|
||
import com.example.android.architecture.blueprints.todoapp.data.Result | ||
import com.example.android.architecture.blueprints.todoapp.data.Task | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.hamcrest.core.IsEqual | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Before | ||
|
||
import org.junit.Test | ||
|
||
@ExperimentalCoroutinesApi | ||
class DefaultTasksRepositoryTest { | ||
private val task1 = Task("Title1", "Description1") | ||
private val task2 = Task("Title2", "Description2") | ||
private val task3 = Task("Title3", "Description3") | ||
private val remoteTasks = listOf(task1, task2).sortedBy { it.id } | ||
private val localTasks = listOf(task3).sortedBy { it.id } | ||
private val newTasks = listOf(task3).sortedBy { it.id } | ||
|
||
private lateinit var tasksRemoteDataSource: FakeDataSource | ||
private lateinit var tasksLocalDataSource: FakeDataSource | ||
// Class under test | ||
private lateinit var tasksRepository: DefaultTasksRepository | ||
|
||
@Before | ||
fun createRepository() { | ||
tasksRemoteDataSource = FakeDataSource(remoteTasks.toMutableList()) | ||
tasksLocalDataSource = FakeDataSource(localTasks.toMutableList()) | ||
tasksRepository = DefaultTasksRepository( | ||
tasksRemoteDataSource, tasksLocalDataSource, Dispatchers.Unconfined | ||
) | ||
} | ||
|
||
@Test | ||
fun getTasks_requestsAllTasksFromRemoteDataSource() = runBlockingTest { | ||
val tasks = tasksRepository.getTasks(true) as Result.Success | ||
assertThat(tasks.data, IsEqual(remoteTasks)) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...st/java/com/example/android/architecture/blueprints/todoapp/data/source/FakeDataSource.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,68 @@ | ||
package com.example.android.architecture.blueprints.todoapp.data.source | ||
|
||
import androidx.lifecycle.LiveData | ||
import com.example.android.architecture.blueprints.todoapp.data.Result | ||
import com.example.android.architecture.blueprints.todoapp.data.Result.Error | ||
import com.example.android.architecture.blueprints.todoapp.data.Result.Success | ||
import com.example.android.architecture.blueprints.todoapp.data.Task | ||
|
||
class FakeDataSource(var tasks: MutableList<Task> = mutableListOf()) : TasksDataSource { | ||
override fun observeTasks(): LiveData<Result<List<Task>>> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun getTasks(): Result<List<Task>> { | ||
tasks?.let { return Success(ArrayList(it)) } | ||
return Error( | ||
Exception("Tasks not found") | ||
) | ||
} | ||
|
||
override suspend fun refreshTasks() { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun observeTask(taskId: String): LiveData<Result<Task>> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun getTask(taskId: String): Result<Task> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun refreshTask(taskId: String) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun saveTask(task: Task) { | ||
tasks?.add(task) | ||
} | ||
|
||
override suspend fun completeTask(task: Task) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun completeTask(taskId: String) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun activateTask(task: Task) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun activateTask(taskId: String) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun clearCompletedTasks() { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun deleteAllTasks() { | ||
tasks?.clear() | ||
} | ||
|
||
override suspend fun deleteTask(taskId: String) { | ||
TODO("Not yet implemented") | ||
} | ||
} |
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