|
34 | 34 | import java.util.concurrent.TimeUnit; |
35 | 35 | import org.junit.After; |
36 | 36 | import org.junit.Before; |
| 37 | +import org.junit.Ignore; |
37 | 38 | import org.junit.Test; |
38 | 39 | import org.junit.runner.RunWith; |
39 | 40 | import rx.Observable; |
40 | 41 | import rx.Subscription; |
| 42 | +import rx.functions.Func1; |
| 43 | +import rx.observables.BlockingObservable; |
41 | 44 |
|
42 | 45 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE; |
43 | 46 | import static com.google.common.truth.Truth.assertThat; |
@@ -124,11 +127,134 @@ public final class BriteDatabaseTest { |
124 | 127 | .toBlocking() |
125 | 128 | .first(); |
126 | 129 | assertThat(employees).containsExactly( // |
127 | | - new Employee("alice", "Alice Allison"), |
128 | | - new Employee("bob", "Bob Bobberson"), |
| 130 | + new Employee("alice", "Alice Allison"), // |
| 131 | + new Employee("bob", "Bob Bobberson"), // |
129 | 132 | new Employee("eve", "Eve Evenson")); |
130 | 133 | } |
131 | 134 |
|
| 135 | + @Test public void queryMapToListEmpty() { |
| 136 | + List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2") |
| 137 | + .mapToList(Employee.MAPPER) |
| 138 | + .toBlocking() |
| 139 | + .first(); |
| 140 | + assertThat(employees).isEmpty(); |
| 141 | + } |
| 142 | + |
| 143 | + @Test public void queryMapToListMapperReturnNullThrows() { |
| 144 | + BlockingObservable<List<Employee>> employees = |
| 145 | + db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) // |
| 146 | + .mapToList(new Func1<Cursor, Employee>() { |
| 147 | + private int count; |
| 148 | + @Override public Employee call(Cursor cursor) { |
| 149 | + return count++ == 2 ? null : Employee.MAPPER.call(cursor); |
| 150 | + } |
| 151 | + }) // |
| 152 | + .toBlocking(); |
| 153 | + try { |
| 154 | + employees.first(); |
| 155 | + } catch (NullPointerException e) { |
| 156 | + assertThat(e).hasMessage("Mapper returned null for row 3"); |
| 157 | + assertThat(e.getCause()).hasMessage( |
| 158 | + "OnError while emitting onNext value: SELECT username, name FROM employee"); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test public void queryMapToOne() { |
| 163 | + Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1") |
| 164 | + .mapToOne(Employee.MAPPER) |
| 165 | + .toBlocking() |
| 166 | + .first(); |
| 167 | + assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison")); |
| 168 | + } |
| 169 | + |
| 170 | + @Ignore("How to test in black box way? Can't take(1).mapToOne() to trigger complete.") // TODO |
| 171 | + @Test public void queryMapToOneEmpty() { |
| 172 | + db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2") |
| 173 | + .mapToOne(Employee.MAPPER) |
| 174 | + .toBlocking() |
| 175 | + .first(); |
| 176 | + } |
| 177 | + |
| 178 | + @Test public void queryMapToOneMapperReturnNullThrows() { |
| 179 | + BlockingObservable<Employee> employees = |
| 180 | + db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) // |
| 181 | + .mapToOne(new Func1<Cursor, Employee>() { |
| 182 | + @Override public Employee call(Cursor cursor) { |
| 183 | + return null; |
| 184 | + } |
| 185 | + }) // |
| 186 | + .toBlocking(); |
| 187 | + try { |
| 188 | + employees.first(); |
| 189 | + } catch (NullPointerException e) { |
| 190 | + assertThat(e).hasMessage("Mapper returned null for row 1"); |
| 191 | + assertThat(e.getCause()).hasMessage( |
| 192 | + "OnError while emitting onNext value: SELECT username, name FROM employee"); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Test public void queryMapToOneMultipleRowsThrows() { |
| 197 | + BlockingObservable<Employee> employees = |
| 198 | + db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") // |
| 199 | + .mapToOne(Employee.MAPPER) // |
| 200 | + .toBlocking(); |
| 201 | + try { |
| 202 | + employees.first(); |
| 203 | + } catch (IllegalStateException e) { |
| 204 | + assertThat(e).hasMessage("Cursor returned more than 1 row"); |
| 205 | + assertThat(e.getCause()).hasMessage( |
| 206 | + "OnError while emitting onNext value: SELECT username, name FROM employee LIMIT 2"); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Test public void queryMapToOneOrNull() { |
| 211 | + Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1") |
| 212 | + .mapToOneOrNull(Employee.MAPPER) |
| 213 | + .toBlocking() |
| 214 | + .first(); |
| 215 | + assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison")); |
| 216 | + } |
| 217 | + |
| 218 | + @Test public void queryMapToOneOrNullEmpty() { |
| 219 | + Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2") |
| 220 | + .mapToOneOrNull(Employee.MAPPER) |
| 221 | + .toBlocking() |
| 222 | + .first(); |
| 223 | + assertThat(employees).isNull(); |
| 224 | + } |
| 225 | + |
| 226 | + @Test public void queryMapToOneOrNullMapperReturnNullThrows() { |
| 227 | + BlockingObservable<Employee> employees = |
| 228 | + db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) // |
| 229 | + .mapToOneOrNull(new Func1<Cursor, Employee>() { |
| 230 | + @Override public Employee call(Cursor cursor) { |
| 231 | + return null; |
| 232 | + } |
| 233 | + }) // |
| 234 | + .toBlocking(); |
| 235 | + try { |
| 236 | + employees.first(); |
| 237 | + } catch (NullPointerException e) { |
| 238 | + assertThat(e).hasMessage("Mapper returned null for row 1"); |
| 239 | + assertThat(e.getCause()).hasMessage( |
| 240 | + "OnError while emitting onNext value: SELECT username, name FROM employee"); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + @Test public void queryMapToOneOrNullMultipleRowsThrows() { |
| 245 | + BlockingObservable<Employee> employees = |
| 246 | + db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") // |
| 247 | + .mapToOneOrNull(Employee.MAPPER) // |
| 248 | + .toBlocking(); |
| 249 | + try { |
| 250 | + employees.first(); |
| 251 | + } catch (IllegalStateException e) { |
| 252 | + assertThat(e).hasMessage("Cursor returned more than 1 row"); |
| 253 | + assertThat(e.getCause()).hasMessage( |
| 254 | + "OnError while emitting onNext value: SELECT username, name FROM employee LIMIT 2"); |
| 255 | + } |
| 256 | + } |
| 257 | + |
132 | 258 | @Test public void badQueryCallsError() { |
133 | 259 | db.createQuery(TABLE_EMPLOYEE, "SELECT * FROM missing").subscribe(o); |
134 | 260 | o.assertErrorContains("no such table: missing"); |
|
0 commit comments