Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 642bf51

Browse files
committed
Merge pull request #77 from square/jw/static-mapper-utils
Expose static mapping operators on Query.
2 parents def5c26 + cd0c8e7 commit 642bf51

5 files changed

Lines changed: 277 additions & 141 deletions

File tree

sqlbrite/src/androidTest/java/com/squareup/sqlbrite/BriteDatabaseTest.java

Lines changed: 3 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,26 @@
2929
import java.io.IOException;
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
32-
import java.util.Collection;
3332
import java.util.List;
3433
import java.util.concurrent.CountDownLatch;
3534
import java.util.concurrent.TimeUnit;
3635
import org.junit.After;
3736
import org.junit.Before;
38-
import org.junit.Ignore;
3937
import org.junit.Test;
4038
import org.junit.runner.RunWith;
4139
import rx.Observable;
4240
import rx.Subscription;
4341
import rx.functions.Action1;
44-
import rx.functions.Func1;
45-
import rx.observables.BlockingObservable;
4642

4743
import static android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE;
4844
import static com.google.common.truth.Truth.assertThat;
4945
import static com.squareup.sqlbrite.RecordingObserver.CursorAssert;
5046
import static com.squareup.sqlbrite.SqlBrite.Query;
51-
import static com.squareup.sqlbrite.TestDb.EmployeeTable.ID;
47+
import static com.squareup.sqlbrite.TestDb.BOTH_TABLES;
5248
import static com.squareup.sqlbrite.TestDb.EmployeeTable.NAME;
5349
import static com.squareup.sqlbrite.TestDb.EmployeeTable.USERNAME;
54-
import static com.squareup.sqlbrite.TestDb.ManagerTable.EMPLOYEE_ID;
55-
import static com.squareup.sqlbrite.TestDb.ManagerTable.MANAGER_ID;
50+
import static com.squareup.sqlbrite.TestDb.SELECT_EMPLOYEES;
51+
import static com.squareup.sqlbrite.TestDb.SELECT_MANAGER_LIST;
5652
import static com.squareup.sqlbrite.TestDb.TABLE_EMPLOYEE;
5753
import static com.squareup.sqlbrite.TestDb.TABLE_MANAGER;
5854
import static com.squareup.sqlbrite.TestDb.employee;
@@ -62,18 +58,6 @@
6258

6359
@RunWith(AndroidJUnit4.class)
6460
public final class BriteDatabaseTest {
65-
private static final Collection<String> BOTH_TABLES =
66-
Arrays.asList(TABLE_EMPLOYEE, TABLE_MANAGER);
67-
private static final String SELECT_EMPLOYEES =
68-
"SELECT " + USERNAME + ", " + NAME + " FROM " + TABLE_EMPLOYEE;
69-
private static final String SELECT_MANAGER_LIST = ""
70-
+ "SELECT e." + NAME + ", m." + NAME + " "
71-
+ "FROM " + TABLE_MANAGER + " AS manager "
72-
+ "JOIN " + TABLE_EMPLOYEE + " AS e "
73-
+ "ON manager." + EMPLOYEE_ID + " = e." + ID + " "
74-
+ "JOIN " + TABLE_EMPLOYEE + " as m "
75-
+ "ON manager." + MANAGER_ID + " = m." + ID;
76-
7761
private final List<String> logs = new ArrayList<>();
7862
private final RecordingObserver o = new RecordingObserver();
7963

@@ -134,34 +118,6 @@ public final class BriteDatabaseTest {
134118
new Employee("eve", "Eve Evenson"));
135119
}
136120

137-
@Test public void queryMapToListEmpty() {
138-
List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
139-
.mapToList(Employee.MAPPER)
140-
.toBlocking()
141-
.first();
142-
assertThat(employees).isEmpty();
143-
}
144-
145-
@Test public void queryMapToListMapperReturnNullThrows() {
146-
BlockingObservable<List<Employee>> employees =
147-
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
148-
.mapToList(new Func1<Cursor, Employee>() {
149-
private int count;
150-
151-
@Override public Employee call(Cursor cursor) {
152-
return count++ == 2 ? null : Employee.MAPPER.call(cursor);
153-
}
154-
}) //
155-
.toBlocking();
156-
try {
157-
employees.first();
158-
} catch (NullPointerException e) {
159-
assertThat(e).hasMessage("Mapper returned null for row 3");
160-
assertThat(e.getCause()).hasMessage(
161-
"OnError while emitting onNext value: SELECT username, name FROM employee");
162-
}
163-
}
164-
165121
@Test public void queryMapToOne() {
166122
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
167123
.mapToOne(Employee.MAPPER)
@@ -170,46 +126,6 @@ public final class BriteDatabaseTest {
170126
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
171127
}
172128

173-
@Ignore("How to test in black box way? Can't take(1).mapToOne() to trigger complete.") // TODO
174-
@Test public void queryMapToOneEmpty() {
175-
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
176-
.mapToOne(Employee.MAPPER)
177-
.toBlocking()
178-
.first();
179-
}
180-
181-
@Test public void queryMapToOneMapperReturnNullThrows() {
182-
BlockingObservable<Employee> employees =
183-
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
184-
.mapToOne(new Func1<Cursor, Employee>() {
185-
@Override public Employee call(Cursor cursor) {
186-
return null;
187-
}
188-
}) //
189-
.toBlocking();
190-
try {
191-
employees.first();
192-
} catch (NullPointerException e) {
193-
assertThat(e).hasMessage("Mapper returned null for row 1");
194-
assertThat(e.getCause()).hasMessage(
195-
"OnError while emitting onNext value: SELECT username, name FROM employee");
196-
}
197-
}
198-
199-
@Test public void queryMapToOneMultipleRowsThrows() {
200-
BlockingObservable<Employee> employees =
201-
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") //
202-
.mapToOne(Employee.MAPPER) //
203-
.toBlocking();
204-
try {
205-
employees.first();
206-
} catch (IllegalStateException e) {
207-
assertThat(e).hasMessage("Cursor returned more than 1 row");
208-
assertThat(e.getCause()).hasMessage(
209-
"OnError while emitting onNext value: SELECT username, name FROM employee LIMIT 2");
210-
}
211-
}
212-
213129
@Test public void queryMapToOneOrDefault() {
214130
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
215131
.mapToOneOrDefault(Employee.MAPPER, null)
@@ -218,54 +134,6 @@ public final class BriteDatabaseTest {
218134
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
219135
}
220136

221-
@Test public void queryMapToOneOrDefaultEmpty() {
222-
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
223-
.mapToOneOrDefault(Employee.MAPPER, new Employee("bob", "Bob Bobberson"))
224-
.toBlocking()
225-
.first();
226-
assertThat(employees).isEqualTo(new Employee("bob", "Bob Bobberson"));
227-
}
228-
229-
@Test public void queryMapToOneOrDefaultEmptyUsingNull() {
230-
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
231-
.mapToOneOrDefault(Employee.MAPPER, null)
232-
.toBlocking()
233-
.first();
234-
assertThat(employees).isNull();
235-
}
236-
237-
@Test public void queryMapToOneOrDefaultMapperReturnNullThrows() {
238-
BlockingObservable<Employee> employees =
239-
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
240-
.mapToOneOrDefault(new Func1<Cursor, Employee>() {
241-
@Override public Employee call(Cursor cursor) {
242-
return null;
243-
}
244-
}, null) //
245-
.toBlocking();
246-
try {
247-
employees.first();
248-
} catch (NullPointerException e) {
249-
assertThat(e).hasMessage("Mapper returned null for row 1");
250-
assertThat(e.getCause()).hasMessage(
251-
"OnError while emitting onNext value: SELECT username, name FROM employee");
252-
}
253-
}
254-
255-
@Test public void queryMapToOneOrDefaultMultipleRowsThrows() {
256-
BlockingObservable<Employee> employees =
257-
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") //
258-
.mapToOneOrDefault(Employee.MAPPER, null) //
259-
.toBlocking();
260-
try {
261-
employees.first();
262-
} catch (IllegalStateException e) {
263-
assertThat(e).hasMessage("Cursor returned more than 1 row");
264-
assertThat(e.getCause()).hasMessage(
265-
"OnError while emitting onNext value: SELECT username, name FROM employee LIMIT 2");
266-
}
267-
}
268-
269137
@Test public void badQueryCallsError() {
270138
db.createQuery(TABLE_EMPLOYEE, "SELECT * FROM missing").subscribe(o);
271139
o.assertErrorContains("no such table: missing");
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (C) 2015 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.sqlbrite;
17+
18+
import android.database.Cursor;
19+
import android.support.test.InstrumentationRegistry;
20+
import com.squareup.sqlbrite.SqlBrite.Query;
21+
import com.squareup.sqlbrite.TestDb.Employee;
22+
import java.util.List;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import rx.functions.Func1;
26+
import rx.observables.BlockingObservable;
27+
28+
import static com.google.common.truth.Truth.assertThat;
29+
import static com.squareup.sqlbrite.TestDb.SELECT_EMPLOYEES;
30+
import static com.squareup.sqlbrite.TestDb.TABLE_EMPLOYEE;
31+
32+
public final class QueryTest {
33+
private BriteDatabase db;
34+
35+
@Before public void setUp() {
36+
SqlBrite sqlBrite = SqlBrite.create();
37+
TestDb helper = new TestDb(InstrumentationRegistry.getContext());
38+
db = sqlBrite.wrapDatabaseHelper(helper);
39+
}
40+
41+
@Test public void mapToOne() {
42+
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
43+
.lift(Query.mapToOne(Employee.MAPPER))
44+
.toBlocking()
45+
.first();
46+
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
47+
}
48+
49+
@Test public void mapToOneThrowsOnMapperNull() {
50+
BlockingObservable<Employee> employees =
51+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
52+
.lift(Query.mapToOne(new Func1<Cursor, Employee>() {
53+
@Override public Employee call(Cursor cursor) {
54+
return null;
55+
}
56+
})) //
57+
.toBlocking();
58+
try {
59+
employees.first();
60+
} catch (NullPointerException e) {
61+
assertThat(e).hasMessage("Mapper returned null for row 1");
62+
assertThat(e.getCause()).hasMessage(
63+
"OnError while emitting onNext value: SELECT username, name FROM employee");
64+
}
65+
}
66+
67+
@Test public void mapToOneNoOpOnNoRows() {
68+
List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
69+
.take(1)
70+
.lift(Query.mapToOne(Employee.MAPPER))
71+
.toList()
72+
.toBlocking()
73+
.first();
74+
assertThat(employees).isEmpty();
75+
}
76+
77+
@Test public void mapToOneThrowsOnMultipleRows() {
78+
BlockingObservable<Employee> employees =
79+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") //
80+
.lift(Query.mapToOne(Employee.MAPPER)) //
81+
.toBlocking();
82+
try {
83+
employees.first();
84+
} catch (IllegalStateException e) {
85+
assertThat(e).hasMessage("Cursor returned more than 1 row");
86+
assertThat(e.getCause()).hasMessage(
87+
"OnError while emitting onNext value: SELECT username, name FROM employee LIMIT 2");
88+
}
89+
}
90+
91+
@Test public void mapToOneOrDefault() {
92+
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
93+
.lift(Query.mapToOneOrDefault(Employee.MAPPER, null))
94+
.toBlocking()
95+
.first();
96+
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
97+
}
98+
99+
@Test public void mapToOneOrDefaultReturnsDefaultWhenNoRows() {
100+
Employee defaultEmployee = new Employee("bob", "Bob Bobberson");
101+
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
102+
.lift(Query.mapToOneOrDefault(Employee.MAPPER, defaultEmployee))
103+
.toBlocking()
104+
.first();
105+
assertThat(employees).isSameAs(defaultEmployee);
106+
}
107+
108+
@Test public void mapToOneOrDefaultAllowsNullDefault() {
109+
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
110+
.lift(Query.mapToOneOrDefault(Employee.MAPPER, null))
111+
.toBlocking()
112+
.first();
113+
assertThat(employees).isNull();
114+
}
115+
116+
@Test public void mapToOneOrDefaultThrowsOnMapperNull() {
117+
BlockingObservable<Employee> employees =
118+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
119+
.lift(Query.mapToOneOrDefault(new Func1<Cursor, Employee>() {
120+
@Override public Employee call(Cursor cursor) {
121+
return null;
122+
}
123+
}, null)) //
124+
.toBlocking();
125+
try {
126+
employees.first();
127+
} catch (NullPointerException e) {
128+
assertThat(e).hasMessage("Mapper returned null for row 1");
129+
assertThat(e.getCause()).hasMessage(
130+
"OnError while emitting onNext value: SELECT username, name FROM employee");
131+
}
132+
}
133+
134+
@Test public void mapToOneOrDefaultThrowsOnMultipleRows() {
135+
BlockingObservable<Employee> employees =
136+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") //
137+
.lift(Query.mapToOneOrDefault(Employee.MAPPER, null)) //
138+
.toBlocking();
139+
try {
140+
employees.first();
141+
} catch (IllegalStateException e) {
142+
assertThat(e).hasMessage("Cursor returned more than 1 row");
143+
assertThat(e.getCause()).hasMessage(
144+
"OnError while emitting onNext value: SELECT username, name FROM employee LIMIT 2");
145+
}
146+
}
147+
148+
@Test public void mapToList() {
149+
List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
150+
.lift(Query.mapToList(Employee.MAPPER))
151+
.toBlocking()
152+
.first();
153+
assertThat(employees).containsExactly( //
154+
new Employee("alice", "Alice Allison"), //
155+
new Employee("bob", "Bob Bobberson"), //
156+
new Employee("eve", "Eve Evenson"));
157+
}
158+
159+
@Test public void mapToListEmptyWhenNoRows() {
160+
List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2")
161+
.lift(Query.mapToList(Employee.MAPPER))
162+
.toBlocking()
163+
.first();
164+
assertThat(employees).isEmpty();
165+
}
166+
167+
@Test public void mapToListThrowsOnMapperNull() {
168+
BlockingObservable<List<Employee>> employees =
169+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES) //
170+
.lift(Query.mapToList(new Func1<Cursor, Employee>() {
171+
private int count;
172+
173+
@Override public Employee call(Cursor cursor) {
174+
return count++ == 2 ? null : Employee.MAPPER.call(cursor);
175+
}
176+
})) //
177+
.toBlocking();
178+
try {
179+
employees.first();
180+
} catch (NullPointerException e) {
181+
assertThat(e).hasMessage("Mapper returned null for row 3");
182+
assertThat(e.getCause()).hasMessage(
183+
"OnError while emitting onNext value: SELECT username, name FROM employee");
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)