16
16
17
17
package com .example .android .architecture .blueprints .todoapp .data ;
18
18
19
+ import android .content .ContentValues ;
20
+ import android .database .Cursor ;
19
21
import android .support .test .InstrumentationRegistry ;
20
22
import android .support .test .runner .AndroidJUnit4 ;
21
23
import android .test .suitebuilder .annotation .LargeTest ;
22
24
23
25
import com .example .android .architecture .blueprints .todoapp .data .source .TasksDataSource ;
24
26
import com .example .android .architecture .blueprints .todoapp .data .source .local .TasksLocalDataSource ;
27
+ import com .example .android .architecture .blueprints .todoapp .data .source .local .TasksPersistenceContract ;
25
28
29
+ import java .util .ArrayList ;
26
30
import java .util .List ;
27
31
28
32
import org .junit .After ;
@@ -77,7 +81,7 @@ public void saveTask_retrievesTask() {
77
81
mLocalDataSource .saveTask (newTask );
78
82
79
83
// Then the task can be retrieved from the persistent repository
80
- assertThat (mLocalDataSource .getTask (newTask .getId ()), is (newTask ));
84
+ assertThat (Task . from ( mLocalDataSource .getTask (newTask .getId () )), is (newTask ));
81
85
}
82
86
83
87
@ Test
@@ -86,27 +90,45 @@ public void completeTask_retrievedTaskIsComplete() {
86
90
TasksDataSource .GetTaskCallback callback = mock (TasksDataSource .GetTaskCallback .class );
87
91
// Given a new task in the persistent repository
88
92
final Task newTask = new Task (TITLE , "" );
93
+
94
+ // And it's representative content values
95
+ ContentValues values = new ContentValues ();
96
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_ENTRY_ID , newTask .getId ());
97
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_TITLE , newTask .getTitle ());
98
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_DESCRIPTION , newTask .getDescription ());
99
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_COMPLETED , newTask .isCompleted () ? 1 : 0 );
100
+
101
+ String [] selectionArgs = {newTask .getId ()};
102
+
89
103
mLocalDataSource .saveTask (newTask );
90
104
91
105
// When completed in the persistent repository
92
- mLocalDataSource .completeTask ( newTask );
106
+ mLocalDataSource .updateTask ( values , selectionArgs );
93
107
94
- // Then the task can be retrieved from the persistent repository and is complete
95
-
96
- assertThat (mLocalDataSource .getTask (newTask .getId ()), is (newTask ));
97
- assertThat (mLocalDataSource .getTask (newTask .getId ()).isCompleted (), is (true ));
108
+ // Then the task can be retrieved from the persistent repository and is completed
109
+ assertThat (Task .from (mLocalDataSource .getTask (newTask .getId ())), is (newTask ));
110
+ assertThat (Task .from (mLocalDataSource .getTask (newTask .getId ())).isCompleted (), is (true ));
98
111
}
99
112
100
113
@ Test
101
114
public void activateTask_retrievedTaskIsActive () {
102
-
103
115
// Given a new completed task in the persistent repository
104
116
final Task newTask = new Task (TITLE , "" );
117
+
118
+ // And it's representative content values
119
+ ContentValues values = new ContentValues ();
120
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_ENTRY_ID , newTask .getId ());
121
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_TITLE , newTask .getTitle ());
122
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_DESCRIPTION , newTask .getDescription ());
123
+ values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_COMPLETED , newTask .isCompleted () ? 1 : 0 );
124
+
125
+ String [] selectionArgs = {newTask .getId ()};
126
+
105
127
mLocalDataSource .saveTask (newTask );
106
- mLocalDataSource .completeTask ( newTask );
128
+ mLocalDataSource .updateTask ( values , selectionArgs );
107
129
108
130
// When activated in the persistent repository
109
- mLocalDataSource .activateTask ( newTask );
131
+ mLocalDataSource .updateTask ( values , selectionArgs );
110
132
111
133
// Then the task can be retrieved from the persistent repository and is active
112
134
mLocalDataSource .getTask (newTask .getId ());
@@ -118,16 +140,41 @@ public void activateTask_retrievedTaskIsActive() {
118
140
public void clearCompletedTask_taskNotRetrievable () {
119
141
// Given 2 new completed tasks and 1 active task in the persistent repository
120
142
final Task newTask1 = new Task (TITLE , "" );
121
- mLocalDataSource .saveTask (newTask1 );
122
- mLocalDataSource .completeTask (newTask1 );
143
+
144
+ // And it's representative content values
145
+ ContentValues task1Values = new ContentValues ();
146
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_ENTRY_ID , newTask1 .getId ());
147
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_TITLE , newTask1 .getTitle ());
148
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_DESCRIPTION , newTask1 .getDescription ());
149
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_COMPLETED , newTask1 .isCompleted () ? 1 : 0 );
150
+
151
+ String [] task1SelectionArgs = {newTask1 .getId ()};
152
+
123
153
final Task newTask2 = new Task (TITLE2 , "" );
124
- mLocalDataSource .saveTask (newTask2 );
125
- mLocalDataSource .completeTask (newTask2 );
154
+ // And it's representative content values
155
+ ContentValues task2Values = new ContentValues ();
156
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_ENTRY_ID , newTask2 .getId ());
157
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_TITLE , newTask2 .getTitle ());
158
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_DESCRIPTION , newTask2 .getDescription ());
159
+ task1Values .put (TasksPersistenceContract .TaskEntry .COLUMN_NAME_COMPLETED , newTask2 .isCompleted () ? 1 : 0 );
160
+
161
+ String [] task2SelectionArgs = {newTask2 .getId ()};
162
+
126
163
final Task newTask3 = new Task (TITLE3 , "" );
164
+
165
+ mLocalDataSource .saveTask (newTask1 );
166
+ mLocalDataSource .updateTask (task1Values , task1SelectionArgs );
167
+
168
+ mLocalDataSource .saveTask (newTask2 );
169
+ mLocalDataSource .updateTask (task2Values , task2SelectionArgs );
170
+
127
171
mLocalDataSource .saveTask (newTask3 );
128
172
129
173
// When completed tasks are cleared in the repository
130
- mLocalDataSource .clearCompletedTasks ();
174
+ String selection = TasksPersistenceContract .TaskEntry .COLUMN_NAME_COMPLETED + " LIKE ?" ;
175
+ String [] selectionArgs = {"1" };
176
+
177
+ mLocalDataSource .clearCompletedTasks (selection , selectionArgs );
131
178
132
179
// Then the completed tasks cannot be retrieved and the active one can
133
180
assertThat (mLocalDataSource .getTask (newTask1 .getId ()), is (isNull ()));
@@ -145,8 +192,9 @@ public void deleteAllTasks_emptyListOfRetrievedTask() {
145
192
mLocalDataSource .deleteAllTasks ();
146
193
147
194
// Then the retrieved tasks is an empty list
148
- List <Task > tasks = mLocalDataSource .getTasks ();
149
- assertEquals (tasks .size (), 0 );
195
+ Cursor cursor = mLocalDataSource .getTasks (null , null );
196
+ assertNotNull (cursor );
197
+ assertEquals (cursor .getCount (), 0 );
150
198
}
151
199
152
200
@ Test
@@ -158,9 +206,17 @@ public void getTasks_retrieveSavedTasks() {
158
206
mLocalDataSource .saveTask (newTask2 );
159
207
160
208
// Then the tasks can be retrieved from the persistent repository
161
- List <Task > tasks = mLocalDataSource .getTasks ();
162
- assertNotNull (tasks );
163
- assertTrue (tasks .size () >= 2 );
209
+ Cursor cursor = mLocalDataSource .getTasks (null , null );
210
+ assertNotNull (cursor );
211
+ assertTrue (cursor .getCount () >= 2 );
212
+
213
+ List <Task > tasks = new ArrayList <>();
214
+ if (cursor .moveToFirst ()) {
215
+ do {
216
+ Task task = Task .from (cursor );
217
+ tasks .add (task );
218
+ } while (cursor .moveToNext ());
219
+ }
164
220
165
221
boolean newTask1IdFound = false ;
166
222
boolean newTask2IdFound = false ;
0 commit comments