Skip to content

Commit 5fe130c

Browse files
committed
fixing database tests
1 parent d8fbed3 commit 5fe130c

File tree

9 files changed

+136
-303
lines changed

9 files changed

+136
-303
lines changed

todoapp/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/data/TasksLocalDataSourceTest.java

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636

3737
import static junit.framework.Assert.*;
3838
import static org.hamcrest.Matchers.is;
39-
import static org.hamcrest.Matchers.notNullValue;
4039
import static org.junit.Assert.assertThat;
41-
import static org.mockito.Matchers.isNull;
4240

4341
/**
4442
* Integration test for the {@link TasksDataSource}, which uses the {@link com.example.android.architecture.blueprints.todoapp.data.source.TasksProvider}.
@@ -81,6 +79,10 @@ public void saveTask_retrievesTask() {
8179

8280
// Then the task can be retrieved from the persistent repository
8381
Cursor savedTaskCursor = mLocalDataSource.getTask(newTask.getId());
82+
savedTaskCursor.moveToFirst();
83+
assertNotNull(savedTaskCursor);
84+
assertTrue(savedTaskCursor.getCount() >= 1);
85+
8486
Task savedTask = Task.from(savedTaskCursor);
8587
assertThat(savedTask, is(newTask));
8688
}
@@ -89,24 +91,19 @@ public void saveTask_retrievesTask() {
8991
public void completeTask_retrievedTaskIsComplete() {
9092
// Given a new task in the persistent repository
9193
final Task newTask = new Task(TITLE, "");
92-
93-
// And it's representative content values
94-
ContentValues values = new ContentValues();
95-
values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_ENTRY_ID, newTask.getId());
96-
values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_TITLE, newTask.getTitle());
97-
values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_DESCRIPTION, newTask.getDescription());
98-
values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_COMPLETED, newTask.isCompleted() ? 1 : 0);
99-
100-
String[] selectionArgs = {newTask.getId()};
101-
10294
mLocalDataSource.saveTask(newTask);
10395

10496
// When completed in the persistent repository
105-
mLocalDataSource.updateTask(values, selectionArgs);
97+
mLocalDataSource.completeTask(newTask);
10698

10799
// Then the task can be retrieved from the persistent repository and is completed
108-
assertThat(Task.from(mLocalDataSource.getTask(newTask.getId())), is(newTask));
109-
assertThat(Task.from(mLocalDataSource.getTask(newTask.getId())).isCompleted(), is(true));
100+
Cursor taskCursor = mLocalDataSource.getTask(newTask.getId());
101+
taskCursor.moveToFirst();
102+
103+
Task completedTask = Task.from(taskCursor);
104+
105+
assertThat(completedTask, is(newTask));
106+
assertThat(completedTask.isCompleted(), is(true));
110107
}
111108

112109
@Test
@@ -121,64 +118,47 @@ public void activateTask_retrievedTaskIsActive() {
121118
values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_DESCRIPTION, newTask.getDescription());
122119
values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_COMPLETED, newTask.isCompleted() ? 1 : 0);
123120

124-
String[] selectionArgs = {newTask.getId()};
125-
126121
mLocalDataSource.saveTask(newTask);
127-
mLocalDataSource.updateTask(values, selectionArgs);
122+
mLocalDataSource.completeTask(newTask);
128123

129124
// When activated in the persistent repository
130-
mLocalDataSource.updateTask(values, selectionArgs);
125+
mLocalDataSource.activateTask(newTask);
131126

132127
// Then the task can be retrieved from the persistent repository and is active
133-
mLocalDataSource.getTask(newTask.getId());
128+
Cursor taskCursor = mLocalDataSource.getTask(newTask.getId());
129+
taskCursor.moveToFirst();
130+
131+
Task activeTask = Task.from(taskCursor);
134132

135-
assertThat(newTask.isCompleted(), is(false));
133+
assertThat(activeTask.isCompleted(), is(false));
136134
}
137135

138136
@Test
139137
public void clearCompletedTask_taskNotRetrievable() {
140138
// Given 2 new completed tasks and 1 active task in the persistent repository
141139
final Task newTask1 = new Task(TITLE, "");
142-
143-
// And it's representative content values
144-
ContentValues task1Values = new ContentValues();
145-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_ENTRY_ID, newTask1.getId());
146-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_TITLE, newTask1.getTitle());
147-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_DESCRIPTION, newTask1.getDescription());
148-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_COMPLETED, newTask1.isCompleted() ? 1 : 0);
149-
150-
String[] task1SelectionArgs = {newTask1.getId()};
151-
152140
final Task newTask2 = new Task(TITLE2, "");
153-
// And it's representative content values
154-
ContentValues task2Values = new ContentValues();
155-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_ENTRY_ID, newTask2.getId());
156-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_TITLE, newTask2.getTitle());
157-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_DESCRIPTION, newTask2.getDescription());
158-
task1Values.put(TasksPersistenceContract.TaskEntry.COLUMN_NAME_COMPLETED, newTask2.isCompleted() ? 1 : 0);
159-
160-
String[] task2SelectionArgs = {newTask2.getId()};
161-
162141
final Task newTask3 = new Task(TITLE3, "");
163142

164143
mLocalDataSource.saveTask(newTask1);
165-
mLocalDataSource.updateTask(task1Values, task1SelectionArgs);
166-
167144
mLocalDataSource.saveTask(newTask2);
168-
mLocalDataSource.updateTask(task2Values, task2SelectionArgs);
169-
170145
mLocalDataSource.saveTask(newTask3);
171146

172-
// When completed tasks are cleared in the repository
173-
String selection = TasksPersistenceContract.TaskEntry.COLUMN_NAME_COMPLETED + " LIKE ?";
174-
String[] selectionArgs = {"1"};
147+
mLocalDataSource.completeTask(newTask1);
148+
mLocalDataSource.completeTask(newTask2);
175149

176-
mLocalDataSource.clearCompletedTasks(selection, selectionArgs);
150+
// When completed tasks are cleared in the repository
151+
mLocalDataSource.clearCompletedTasks();
177152

178153
// Then the completed tasks cannot be retrieved and the active one can
179-
assertThat(mLocalDataSource.getTask(newTask1.getId()), is(isNull()));
180-
assertThat(mLocalDataSource.getTask(newTask2.getId()), is(isNull()));
181-
assertThat(mLocalDataSource.getTask(newTask3.getId()), notNullValue());
154+
Cursor task1Cursor = mLocalDataSource.getTask(newTask1.getId());
155+
assertFalse(task1Cursor.moveToFirst());
156+
157+
Cursor task2Cursor = mLocalDataSource.getTask(newTask2.getId());
158+
assertFalse(task2Cursor.moveToFirst());
159+
160+
Cursor task3Cursor = mLocalDataSource.getTask(newTask3.getId());
161+
assertTrue(task3Cursor.moveToFirst());
182162
}
183163

184164
@Test

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public Loader<Cursor> createFilteredTasksLoader(TasksFilterType filterType) {
4747
}
4848

4949
public Loader<Cursor> createTaskLoader(String taskId) {
50-
return new CursorLoader(mContext, TasksPersistenceContract.TaskEntry.buildTasksUri(),
51-
TasksPersistenceContract.TaskEntry.TASKS_COLUMNS,
52-
TasksPersistenceContract.TaskEntry.COLUMN_NAME_ENTRY_ID + " = ? ",
50+
return new CursorLoader(mContext, TasksPersistenceContract.TaskEntry.buildTasksUriWith(taskId),
51+
null,
52+
null,
5353
new String[]{String.valueOf(taskId)}, null
5454
);
5555
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
147147

148148
@Override
149149
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
150-
if (data != null) {
150+
if (data != null && data.getCount() > 0) {
151151
callback.onDataLoaded(data);
152152
} else {
153153
callback.onDataNotAvailable();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public String getType(Uri uri) {
4747
switch (match) {
4848
case TASK:
4949
return TasksPersistenceContract.CONTENT_TASK_TYPE;
50+
case TASK_ITEM:
51+
return TasksPersistenceContract.CONTENT_TASK_ITEM_TYPE;
5052
default:
5153
throw new UnsupportedOperationException("Unknown uri: " + uri);
5254
}
@@ -74,7 +76,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
7476

7577
} else if (selectionArgs.equals("1")) {
7678
mTasksRemoteDataSource.clearCompletedTasks();
77-
rowsDeleted = mTasksLocalDataSource.clearCompletedTasks(selection, selectionArgs);
79+
rowsDeleted = mTasksLocalDataSource.clearCompletedTasks();
7880

7981
Iterator<Map.Entry<String, Task>> it = mCachedTasks.entrySet().iterator();
8082
while (it.hasNext()) {
@@ -86,7 +88,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
8688
} else {
8789
String taskId = selectionArgs[0];
8890
mTasksRemoteDataSource.deleteTask(taskId);
89-
rowsDeleted = mTasksLocalDataSource.deleteTask(selectionArgs);
91+
rowsDeleted = mTasksLocalDataSource.deleteTask(taskId);
9092

9193
Iterator<Map.Entry<String, Task>> it = mCachedTasks.entrySet().iterator();
9294
while (it.hasNext()) {
@@ -106,13 +108,15 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
106108
@Override
107109
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
108110
Task newTask = Task.from(values);
111+
int rowsUpdated;
109112
if (newTask.isCompleted()) {
110113
mTasksRemoteDataSource.completeTask(newTask);
114+
rowsUpdated = mTasksLocalDataSource.completeTask(newTask);
111115
} else {
112116
mTasksRemoteDataSource.activateTask(newTask);
117+
rowsUpdated = mTasksLocalDataSource.activateTask(newTask);
113118
}
114119

115-
int rowsUpdated = mTasksLocalDataSource.updateTask(values, selectionArgs);
116120
// mCachedTasks.put(newTask.getId(), newTask);
117121

118122
if (rowsUpdated != 0) {

0 commit comments

Comments
 (0)