Skip to content

Commit

Permalink
Change createTest to return task ID (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
dturner authored Mar 27, 2023
1 parent 7313121 commit 04924f6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class StatisticsScreenTest {
repository.apply {
createTask("Title1", "Description1")
createTask("Title2", "Description2").also {
completeTask(it.id)
completeTask(it)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.example.android.architecture.blueprints.todoapp.HiltTestActivity
import com.example.android.architecture.blueprints.todoapp.data.Task
import com.example.android.architecture.blueprints.todoapp.data.TaskRepository
import com.google.accompanist.appcompattheme.AppCompatTheme
import dagger.hilt.android.testing.HiltAndroidRule
Expand Down Expand Up @@ -66,13 +65,13 @@ class TaskDetailScreenTest {
@Test
fun activeTaskDetails_DisplayedInUi() = runTest {
// GIVEN - Add active (incomplete) task to the DB
val activeTask = repository.createTask(
val activeTaskId = repository.createTask(
title = "Active Task",
description = "AndroidX Rocks"
)

// WHEN - Details screen is opened
setContent(activeTask)
setContent(activeTaskId)

// THEN - Task details are displayed on the screen
// make sure that the title/description are both shown and correct
Expand All @@ -85,11 +84,11 @@ class TaskDetailScreenTest {
@Test
fun completedTaskDetails_DisplayedInUi() = runTest {
// GIVEN - Add completed task to the DB
val completedTask = repository.createTask("Completed Task", "AndroidX Rocks")
repository.completeTask(completedTask.id)
val completedTaskId = repository.createTask("Completed Task", "AndroidX Rocks")
repository.completeTask(completedTaskId)

// WHEN - Details screen is opened
setContent(completedTask)
setContent(completedTaskId)

// THEN - Task details are displayed on the screen
// make sure that the title/description are both shown and correct
Expand All @@ -99,14 +98,14 @@ class TaskDetailScreenTest {
composeTestRule.onNode(isToggleable()).assertIsOn()
}

private fun setContent(activeTask: Task) {
private fun setContent(activeTaskId: String) {
composeTestRule.setContent {
AppCompatTheme {
Surface {
TaskDetailScreen(
viewModel = TaskDetailViewModel(
repository,
SavedStateHandle(mapOf("taskId" to activeTask.id))
SavedStateHandle(mapOf("taskId" to activeTaskId))
),
onEditTask = { /*TODO*/ },
onBack = { },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TasksScreenTest {
@Test
fun displayCompletedTask() = runTest {
repository.apply {
createTask("TITLE1", "DESCRIPTION1").also { completeTask(it.id) }
createTask("TITLE1", "DESCRIPTION1").also { completeTask(it) }
}

setContent()
Expand Down Expand Up @@ -134,7 +134,7 @@ class TasksScreenTest {
@Test
fun markTaskAsActive() = runTest {
repository.apply {
createTask("TITLE1", "DESCRIPTION1").also { completeTask(it.id) }
createTask("TITLE1", "DESCRIPTION1").also { completeTask(it) }
}

setContent()
Expand All @@ -156,7 +156,7 @@ class TasksScreenTest {
// Add one active task and one completed task
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it.id) }
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it) }
}

setContent()
Expand All @@ -173,7 +173,7 @@ class TasksScreenTest {
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2")
createTask("TITLE3", "DESCRIPTION3").also { completeTask(it.id) }
createTask("TITLE3", "DESCRIPTION3").also { completeTask(it) }
}

setContent()
Expand All @@ -190,8 +190,8 @@ class TasksScreenTest {
// Add one active task and 2 completed tasks
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it.id) }
createTask("TITLE3", "DESCRIPTION3").also { completeTask(it.id) }
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it) }
createTask("TITLE3", "DESCRIPTION3").also { completeTask(it) }
}

setContent()
Expand All @@ -208,7 +208,7 @@ class TasksScreenTest {
// Add one active task and one completed task
repository.apply {
createTask("TITLE1", "DESCRIPTION1")
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it.id) }
createTask("TITLE2", "DESCRIPTION2").also { completeTask(it) }
}

setContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class TasksTest {
// Add 1 completed task
val taskTitle = "ACTIVE"
repository.apply {
createTask(taskTitle, "DESCRIPTION").also { completeTask(it.id) }
createTask(taskTitle, "DESCRIPTION").also { completeTask(it) }
}

setContent()
Expand Down Expand Up @@ -249,7 +249,7 @@ class TasksTest {
// Add 1 completed task
val taskTitle = "COMP-ACT"
repository.apply {
createTask(taskTitle, "DESCRIPTION").also { completeTask(it.id) }
createTask(taskTitle, "DESCRIPTION").also { completeTask(it) }
}

setContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DefaultTaskRepository(
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default
) : TaskRepository {

override suspend fun createTask(title: String, description: String): Task {
override suspend fun createTask(title: String, description: String): String {
// ID creation might be a complex operation so it's executed using the supplied
// coroutine dispatcher
val taskId = withContext(coroutineDispatcher) {
Expand All @@ -53,7 +53,7 @@ class DefaultTaskRepository(
)
taskDao.upsert(task.toLocal())
saveTasksToNetwork()
return task
return taskId
}

override suspend fun updateTask(taskId: String, title: String, description: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface TaskRepository {

suspend fun refreshTask(taskId: String)

suspend fun createTask(title: String, description: String): Task
suspend fun createTask(title: String, description: String): String

suspend fun updateTask(taskId: String, title: String, description: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ class DefaultTaskRepositoryTest {
private val task1 = Task(id = "1", title = "Title1", description = "Description1")
private val task2 = Task(id = "2", title = "Title2", description = "Description2")
private val task3 = Task(id = "3", title = "Title3", description = "Description3")
private val newTask = Task(id = "new", title = "Title new", description = "Description new")
private val networkTasks = listOf(task1, task2).toNetwork().sortedBy { it.id }
private val localTasks = listOf(task3.toLocal()).sortedBy { it.id }

private val newTasks = listOf(newTask).sortedBy { it.id }
private val newTaskTitle = "Title new"
private val newTaskDescription = "Description new"
private val newTask = Task(id = "new", title = newTaskTitle, description = newTaskDescription)
private val newTasks = listOf(newTask)

private val networkTasks = listOf(task1, task2).toNetwork()
private val localTasks = listOf(task3.toLocal())

private lateinit var networkDataSource: FakeNetworkDataSource
private lateinit var localDataSource: FakeTaskDao

Expand Down Expand Up @@ -103,16 +107,12 @@ class DefaultTaskRepositoryTest {

@Test
fun saveTask_savesToLocalAndRemote() = runTest {
// Make sure newTask is not in the remote or local datasources
assertThat(networkDataSource.tasks).doesNotContain(newTask.toNetwork())
assertThat(localDataSource.tasks).doesNotContain(newTask.toLocal())

// When a task is saved to the tasks repository
val newTask = tasksRepository.createTask(newTask.title, newTask.description)
val newTaskId = tasksRepository.createTask(newTask.title, newTask.description)

// Then the remote and local sources are called
assertThat(networkDataSource.tasks).contains(newTask.toNetwork())
assertThat(localDataSource.tasks?.contains(newTask.toLocal()))
// Then the remote and local sources contain the new task
assertThat(networkDataSource.tasks?.map { it.id }?.contains(newTaskId))
assertThat(localDataSource.tasks?.map { it.id }?.contains(newTaskId))
}

@Test
Expand Down Expand Up @@ -179,32 +179,32 @@ class DefaultTaskRepositoryTest {
@Test
fun completeTask_completesTaskToServiceAPIUpdatesCache() = runTest {
// Save a task
val newTask = tasksRepository.createTask(newTask.title, newTask.description)
val newTaskId = tasksRepository.createTask(newTask.title, newTask.description)

// Make sure it's active
assertThat(tasksRepository.getTask(newTask.id)?.isCompleted).isFalse()
assertThat(tasksRepository.getTask(newTaskId)?.isCompleted).isFalse()

// Mark is as complete
tasksRepository.completeTask(newTask.id)
tasksRepository.completeTask(newTaskId)

// Verify it's now completed
assertThat(tasksRepository.getTask(newTask.id)?.isCompleted).isTrue()
assertThat(tasksRepository.getTask(newTaskId)?.isCompleted).isTrue()
}

@Test
fun completeTask_activeTaskToServiceAPIUpdatesCache() = runTest {
// Save a task
val newTask = tasksRepository.createTask(newTask.title, newTask.description)
tasksRepository.completeTask(newTask.id)
val newTaskId = tasksRepository.createTask(newTask.title, newTask.description)
tasksRepository.completeTask(newTaskId)

// Make sure it's completed
assertThat(tasksRepository.getTask(newTask.id)?.isActive).isFalse()
assertThat(tasksRepository.getTask(newTaskId)?.isActive).isFalse()

// Mark is as active
tasksRepository.activateTask(newTask.id)
tasksRepository.activateTask(newTaskId)

// Verify it's now activated
assertThat(tasksRepository.getTask(newTask.id)?.isActive).isTrue()
assertThat(tasksRepository.getTask(newTaskId)?.isActive).isTrue()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ class FakeTaskRepository : TaskRepository {
refreshTasks()
}

override suspend fun createTask(title: String, description: String): Task {
return Task(
title = title,
description = description,
id = generateTaskId()
).also {
override suspend fun createTask(title: String, description: String): String {
val taskId = generateTaskId()
Task(title = title, description = description, id = taskId).also {
saveTask(it)
}
return taskId
}

override fun getTasksStream(): Flow<List<Task>> = observableTasks
Expand Down

0 comments on commit 04924f6

Please sign in to comment.