Search in sources :

Example 31 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class RelationsTest method name.

@Test
public void name() {
    SampleApp sampleApp = (SampleApp) RuntimeEnvironment.application;
    sampleApp.appComponent().storIOSQLite().put().objects(asList(Tweet.newTweet(1L, "artem_zin", "test tweet 1"), Tweet.newTweet(2L, "artem_zin", "test tweet 2"), Tweet.newTweet(3L, "nikitin-da", "test tweet 3"), User.newUser(1L, "artem_zin"), User.newUser(2L, "nikitin-da"))).prepare().executeAsBlocking();
    Relations relations = new Relations(sampleApp.appComponent().storIOSQLite());
    List<TweetWithUser> tweetsWithUsers = relations.getTweetWithUser();
    // Same as count of tweets, not users.
    assertThat(tweetsWithUsers).hasSize(3);
    assertThat(tweetsWithUsers.get(0)).isEqualTo(new TweetWithUser(Tweet.newTweet(1L, "artem_zin", "test tweet 1"), User.newUser(1L, "artem_zin")));
    assertThat(tweetsWithUsers.get(1)).isEqualTo(new TweetWithUser(Tweet.newTweet(2L, "artem_zin", "test tweet 2"), User.newUser(1L, "artem_zin")));
    assertThat(tweetsWithUsers.get(2)).isEqualTo(new TweetWithUser(Tweet.newTweet(3L, "nikitin-da", "test tweet 3"), User.newUser(2L, "nikitin-da")));
}
Also used : TweetWithUser(com.pushtorefresh.storio3.sample.db.entities.TweetWithUser) SampleApp(com.pushtorefresh.storio3.sample.SampleApp) Test(org.junit.Test)

Example 32 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultDeleteResolverTest method performDelete.

@Test
public void performDelete() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    final String testTable = "test_table";
    final Set<String> testTags = singleton("test_tag");
    final DeleteQuery deleteQuery = DeleteQuery.builder().table(testTable).affectsTags(testTags).build();
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    when(lowLevel.delete(deleteQuery)).thenReturn(1);
    final TestItem testItem = new TestItem();
    final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {

        @NonNull
        @Override
        public DeleteQuery mapToDeleteQuery(@NonNull TestItem testItem) {
            return deleteQuery;
        }
    };
    final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOSQLite, testItem);
    verify(lowLevel).delete(any(DeleteQuery.class));
    verify(lowLevel).delete(deleteQuery);
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(1);
    assertThat(deleteResult.affectedTables()).isEqualTo(singleton(testTable));
    assertThat(deleteResult.affectedTags()).isEqualTo(testTags);
}
Also used : NonNull(android.support.annotation.NonNull) DeleteQuery(com.pushtorefresh.storio3.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 33 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class PreparedDeleteByQueryTest method shouldWrapExceptionIntoStorIOExceptionCompletable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionCompletable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    // noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    when(deleteResolver.performDelete(same(storIOSQLite), any(DeleteQuery.class))).thenThrow(new IllegalStateException("test exception"));
    final TestObserver<DeleteResult> testObserver = new TestObserver<DeleteResult>();
    new PreparedDeleteByQuery.Builder(storIOSQLite, DeleteQuery.builder().table("test_table").build()).withDeleteResolver(deleteResolver).prepare().asRxCompletable().subscribe(testObserver);
    testObserver.awaitTerminalEvent();
    testObserver.assertNoValues();
    testObserver.assertError(StorIOException.class);
    // noinspection ThrowableResultOfMethodCallIgnored
    StorIOException expected = (StorIOException) testObserver.errors().get(0);
    IllegalStateException cause = (IllegalStateException) expected.getCause();
    assertThat(cause).hasMessage("test exception");
    verify(storIOSQLite).defaultRxScheduler();
    verify(deleteResolver).performDelete(same(storIOSQLite), any(DeleteQuery.class));
    verify(storIOSQLite).interceptors();
    verifyNoMoreInteractions(storIOSQLite, lowLevel, deleteResolver);
}
Also used : StorIOException(com.pushtorefresh.storio3.StorIOException) DeleteQuery(com.pushtorefresh.storio3.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 34 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class PreparedDeleteByQueryTest method shouldNotNotifyIfWasNotDeleted.

@Test
public void shouldNotNotifyIfWasNotDeleted() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    final DeleteQuery deleteQuery = DeleteQuery.builder().table("test_table").where("column1 = ?").whereArgs(1).build();
    // noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    // No items were deleted
    final DeleteResult expectedDeleteResult = DeleteResult.newInstance(0, deleteQuery.table());
    when(deleteResolver.performDelete(same(storIOSQLite), same(deleteQuery))).thenReturn(expectedDeleteResult);
    final DeleteResult actualDeleteResult = new PreparedDeleteByQuery.Builder(storIOSQLite, deleteQuery).withDeleteResolver(deleteResolver).prepare().executeAsBlocking();
    assertThat(actualDeleteResult).isEqualTo(expectedDeleteResult);
    verify(deleteResolver).performDelete(same(storIOSQLite), same(deleteQuery));
    verify(lowLevel, never()).notifyAboutChanges(any(Changes.class));
    verify(storIOSQLite).interceptors();
    verifyNoMoreInteractions(storIOSQLite, lowLevel, deleteResolver);
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) DeleteQuery(com.pushtorefresh.storio3.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 35 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class PreparedDeleteByQueryTest method shouldWrapExceptionIntoStorIOExceptionBlocking.

@Test
public void shouldWrapExceptionIntoStorIOExceptionBlocking() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    // noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    when(deleteResolver.performDelete(same(storIOSQLite), any(DeleteQuery.class))).thenThrow(new IllegalStateException("test exception"));
    try {
        new PreparedDeleteByQuery.Builder(storIOSQLite, DeleteQuery.builder().table("test_table").build()).withDeleteResolver(deleteResolver).prepare().executeAsBlocking();
        failBecauseExceptionWasNotThrown(StorIOException.class);
    } catch (StorIOException expected) {
        IllegalStateException cause = (IllegalStateException) expected.getCause();
        assertThat(cause).hasMessage("test exception");
        verify(deleteResolver).performDelete(same(storIOSQLite), any(DeleteQuery.class));
        verify(storIOSQLite).interceptors();
        verifyNoMoreInteractions(storIOSQLite, lowLevel, deleteResolver);
    }
}
Also used : StorIOException(com.pushtorefresh.storio3.StorIOException) DeleteQuery(com.pushtorefresh.storio3.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)34 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)32 NonNull (android.support.annotation.NonNull)18 StorIOException (com.pushtorefresh.storio3.StorIOException)18 ContentValues (android.content.ContentValues)13 Query (com.pushtorefresh.storio3.sqlite.queries.Query)9 Cursor (android.database.Cursor)8 TestObserver (io.reactivex.observers.TestObserver)8 DeleteQuery (com.pushtorefresh.storio3.sqlite.queries.DeleteQuery)7 TestSubscriber (io.reactivex.subscribers.TestSubscriber)7 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)4 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)4 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)4 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)4 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)3 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)3 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)3 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)3 User (com.pushtorefresh.storio3.sample.db.entities.User)2 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)2