Skip to content

Commit 9ebcadc

Browse files
committed
Fixes database creation and some minor unit test fixes
1 parent 8d69b8f commit 9ebcadc

File tree

5 files changed

+50
-37
lines changed

5 files changed

+50
-37
lines changed

app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepository.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.example.android.architecture.blueprints.todoapp.data.source
1717

1818
import com.example.android.architecture.blueprints.todoapp.data.Result
19+
import com.example.android.architecture.blueprints.todoapp.data.Result.Error
1920
import com.example.android.architecture.blueprints.todoapp.data.Result.Success
2021
import com.example.android.architecture.blueprints.todoapp.data.Task
2122
import com.example.android.architecture.blueprints.todoapp.util.EspressoIdlingResource
@@ -61,16 +62,16 @@ class DefaultTasksRepository(
6162
(newTasks as? Success)?.let { refreshCache(it.data) }
6263

6364
cachedTasks?.values?.let { tasks ->
64-
return@withContext Result.Success(tasks.sortedBy { it.id })
65+
return@withContext Success(tasks.sortedBy { it.id })
6566
}
6667

6768
(newTasks as? Success)?.let {
6869
if (it.data.isEmpty()) {
69-
return@withContext Result.Success(it.data)
70+
return@withContext Success(it.data)
7071
}
7172
}
7273

73-
return@withContext Result.Error(Exception("Illegal state"))
74+
return@withContext Error(Exception("Illegal state"))
7475
}
7576
}
7677
}
@@ -79,8 +80,8 @@ class DefaultTasksRepository(
7980
// Remote first
8081
val remoteTasks = tasksRemoteDataSource.getTasks()
8182
when (remoteTasks) {
82-
is Result.Error -> Timber.w("Remote data source fetch failed")
83-
is Result.Success -> {
83+
is Error -> Timber.w("Remote data source fetch failed")
84+
is Success -> {
8485
refreshLocalDataSource(remoteTasks.data)
8586
return remoteTasks
8687
}
@@ -89,13 +90,13 @@ class DefaultTasksRepository(
8990

9091
// Don't read from local if it's forced
9192
if (forceUpdate) {
92-
return Result.Error(Exception("Can't force refresh: remote data source is unavailable"))
93+
return Error(Exception("Can't force refresh: remote data source is unavailable"))
9394
}
9495

9596
// Local if remote fails
9697
val localTasks = tasksLocalDataSource.getTasks()
97-
if (localTasks is Result.Success) return localTasks
98-
return Result.Error(Exception("Error fetching from remote and local"))
98+
if (localTasks is Success) return localTasks
99+
return Error(Exception("Error fetching from remote and local"))
99100
}
100101

101102
/**
@@ -131,8 +132,8 @@ class DefaultTasksRepository(
131132
// Remote first
132133
val remoteTask = tasksRemoteDataSource.getTask(taskId)
133134
when (remoteTask) {
134-
is Result.Error -> Timber.w("Remote data source fetch failed")
135-
is Result.Success -> {
135+
is Error -> Timber.w("Remote data source fetch failed")
136+
is Success -> {
136137
refreshLocalDataSource(remoteTask.data)
137138
return remoteTask
138139
}
@@ -141,13 +142,13 @@ class DefaultTasksRepository(
141142

142143
// Don't read from local if it's forced
143144
if (forceUpdate) {
144-
return Result.Error(Exception("Refresh failed"))
145+
return Error(Exception("Refresh failed"))
145146
}
146147

147148
// Local if remote fails
148149
val localTasks = tasksLocalDataSource.getTask(taskId)
149-
if (localTasks is Result.Success) return localTasks
150-
return Result.Error(Exception("Error fetching from remote and local"))
150+
if (localTasks is Success) return localTasks
151+
return Error(Exception("Error fetching from remote and local"))
151152
}
152153

153154
override suspend fun saveTask(task: Task) {
@@ -226,7 +227,6 @@ class DefaultTasksRepository(
226227
}
227228

228229
cachedTasks?.remove(taskId)
229-
Unit // Force return type
230230
}
231231

232232
private fun refreshCache(tasks: List<Task>) {

app/src/mock/java/com/example/android/architecture/blueprints/todoapp/ServiceLocator.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.annotation.VisibleForTesting
2020
import androidx.room.Room
2121
import com.example.android.architecture.blueprints.todoapp.data.FakeTasksRemoteDataSource
2222
import com.example.android.architecture.blueprints.todoapp.data.source.DefaultTasksRepository
23+
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
2324
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
2425
import com.example.android.architecture.blueprints.todoapp.data.source.local.TasksLocalDataSource
2526
import com.example.android.architecture.blueprints.todoapp.data.source.local.ToDoDatabase
@@ -44,16 +45,21 @@ object ServiceLocator {
4445
}
4546

4647
private fun createTasksRepository(context: Context): TasksRepository {
47-
database = Room.databaseBuilder(
48+
return DefaultTasksRepository(FakeTasksRemoteDataSource, createTaskLocalDataSource(context))
49+
}
50+
51+
private fun createTaskLocalDataSource(context: Context): TasksDataSource {
52+
val database = database ?: createDataBase(context)
53+
return TasksLocalDataSource(database.taskDao())
54+
}
55+
56+
private fun createDataBase(context: Context): ToDoDatabase {
57+
val result = Room.databaseBuilder(
4858
context.applicationContext,
4959
ToDoDatabase::class.java, "Tasks.db"
50-
)
51-
.build()
52-
53-
return DefaultTasksRepository(
54-
FakeTasksRemoteDataSource,
55-
TasksLocalDataSource(database!!.taskDao())
56-
)
60+
).build()
61+
database = result
62+
return result
5763
}
5864

5965
@VisibleForTesting
@@ -67,6 +73,7 @@ object ServiceLocator {
6773
clearAllTables()
6874
close()
6975
}
76+
database = null
7077
tasksRepository = null
7178
}
7279
}

app/src/prod/java/com/example/android/architecture/blueprints/todoapp/ServiceLocator.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.Context
2020
import androidx.annotation.VisibleForTesting
2121
import androidx.room.Room
2222
import com.example.android.architecture.blueprints.todoapp.data.source.DefaultTasksRepository
23+
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
2324
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
2425
import com.example.android.architecture.blueprints.todoapp.data.source.local.TasksLocalDataSource
2526
import com.example.android.architecture.blueprints.todoapp.data.source.local.ToDoDatabase
@@ -45,16 +46,21 @@ object ServiceLocator {
4546
}
4647

4748
private fun createTasksRepository(context: Context): TasksRepository {
48-
database = Room.databaseBuilder(
49+
return DefaultTasksRepository(TasksRemoteDataSource, createTaskLocalDataSource(context))
50+
}
51+
52+
private fun createTaskLocalDataSource(context: Context): TasksDataSource {
53+
val database = database ?: createDataBase(context)
54+
return TasksLocalDataSource(database.taskDao())
55+
}
56+
57+
private fun createDataBase(context: Context): ToDoDatabase {
58+
val result = Room.databaseBuilder(
4959
context.applicationContext,
5060
ToDoDatabase::class.java, "Tasks.db"
51-
)
52-
.build()
53-
54-
return DefaultTasksRepository(
55-
TasksRemoteDataSource,
56-
TasksLocalDataSource(database!!.taskDao())
57-
)
61+
).build()
62+
database = result
63+
return result
5864
}
5965

6066
@VisibleForTesting
@@ -68,6 +74,7 @@ object ServiceLocator {
6874
clearAllTables()
6975
close()
7076
}
77+
database = null
7178
tasksRepository = null
7279
}
7380
}

app/src/test/java/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepositoryTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.junit.Test
2828
/**
2929
* Unit tests for the implementation of the in-memory repository with cache.
3030
*/
31+
@ExperimentalCoroutinesApi
3132
class DefaultTasksRepositoryTest {
3233

3334
private val task1 = Task("Title1", "Description1")
@@ -157,7 +158,7 @@ class DefaultTasksRepositoryTest {
157158

158159
@Test
159160
fun getTasks_refreshesLocalDataSource() = runBlockingTest {
160-
val initialLocal = tasksLocalDataSource.tasks
161+
val initialLocal = tasksLocalDataSource.tasks!!.toList()
161162

162163
// First load will fetch from remote
163164
val newTasks = (tasksRepository.getTasks() as Success).data
@@ -219,7 +220,7 @@ class DefaultTasksRepositoryTest {
219220
fun getTask_repositoryCachesAfterFirstApiCall() = runBlockingTest {
220221
// Trigger the repository to load data, which loads from remote
221222
tasksRemoteDataSource.tasks = mutableListOf(task1)
222-
val initial = tasksRepository.getTask(task1.id)
223+
tasksRepository.getTask(task1.id)
223224

224225
// Configure the remote data source to store a different task
225226
tasksRemoteDataSource.tasks = mutableListOf(task2)
@@ -236,7 +237,7 @@ class DefaultTasksRepositoryTest {
236237
fun getTask_forceRefresh() = runBlockingTest {
237238
// Trigger the repository to load data, which loads from remote and caches
238239
tasksRemoteDataSource.tasks = mutableListOf(task1)
239-
val initial = tasksRepository.getTask(task1.id)
240+
tasksRepository.getTask(task1.id)
240241

241242
// Configure the remote data source to return a different task
242243
tasksRemoteDataSource.tasks = mutableListOf(task2)

app/src/test/java/com/example/android/architecture/blueprints/todoapp/data/source/FakeDataSource.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import com.example.android.architecture.blueprints.todoapp.data.Result.Error
2121
import com.example.android.architecture.blueprints.todoapp.data.Result.Success
2222
import com.example.android.architecture.blueprints.todoapp.data.Task
2323

24-
class FakeDataSource(var tasks: MutableList<Task>? = mutableListOf()) :
25-
TasksDataSource {
26-
24+
class FakeDataSource(var tasks: MutableList<Task>? = mutableListOf()) : TasksDataSource {
2725
override suspend fun getTasks(): Result<List<Task>> {
2826
tasks?.let { return Success(it) }
2927
return Error(
@@ -63,7 +61,7 @@ class FakeDataSource(var tasks: MutableList<Task>? = mutableListOf()) :
6361
}
6462

6563
override suspend fun deleteAllTasks() {
66-
tasks = mutableListOf()
64+
tasks?.clear()
6765
}
6866

6967
override suspend fun deleteTask(taskId: String) {

0 commit comments

Comments
 (0)