2929import java .io .IOException ;
3030import java .util .ArrayList ;
3131import java .util .Arrays ;
32- import java .util .Collection ;
3332import java .util .List ;
3433import java .util .concurrent .CountDownLatch ;
3534import java .util .concurrent .TimeUnit ;
3635import org .junit .After ;
3736import org .junit .Before ;
38- import org .junit .Ignore ;
3937import org .junit .Test ;
4038import org .junit .runner .RunWith ;
4139import rx .Observable ;
4240import rx .Subscription ;
4341import rx .functions .Action1 ;
44- import rx .functions .Func1 ;
45- import rx .observables .BlockingObservable ;
4642
4743import static android .database .sqlite .SQLiteDatabase .CONFLICT_IGNORE ;
4844import static com .google .common .truth .Truth .assertThat ;
4945import static com .squareup .sqlbrite .RecordingObserver .CursorAssert ;
5046import static com .squareup .sqlbrite .SqlBrite .Query ;
51- import static com .squareup .sqlbrite .TestDb .EmployeeTable . ID ;
47+ import static com .squareup .sqlbrite .TestDb .BOTH_TABLES ;
5248import static com .squareup .sqlbrite .TestDb .EmployeeTable .NAME ;
5349import 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 ;
5652import static com .squareup .sqlbrite .TestDb .TABLE_EMPLOYEE ;
5753import static com .squareup .sqlbrite .TestDb .TABLE_MANAGER ;
5854import static com .squareup .sqlbrite .TestDb .employee ;
6258
6359@ RunWith (AndroidJUnit4 .class )
6460public 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" );
0 commit comments