Search in sources :

Example 11 with GetResolver

use of com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method typeMappingShouldWorkWithSpecifiedTypeMappingFinder.

@Test
public void typeMappingShouldWorkWithSpecifiedTypeMappingFinder() {
    TypeMappingFinder typeMappingFinder = new TypeMappingFinderImpl();
    // noinspection unchecked
    SQLiteTypeMapping<ClassEntity> typeMapping = SQLiteTypeMapping.builder().putResolver(mock(PutResolver.class)).getResolver(mock(GetResolver.class)).deleteResolver(mock(DeleteResolver.class)).build();
    DefaultStorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).typeMappingFinder(typeMappingFinder).addTypeMapping(ClassEntity.class, typeMapping).build();
    assertThat(storIOSQLite.lowLevel().typeMapping(ClassEntity.class)).isEqualTo(typeMapping);
}
Also used : TypeMappingFinderImpl(com.pushtorefresh.storio3.internal.TypeMappingFinderImpl) TypeMappingFinder(com.pushtorefresh.storio3.TypeMappingFinder) GetResolver(com.pushtorefresh.storio3.sqlite.operations.get.GetResolver) Test(org.junit.Test)

Example 12 with GetResolver

use of com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver in project storio by pushtorefresh.

the class DbModule method provideStorIOSQLite.

// We suggest to keep one instance of StorIO (SQLite or ContentResolver)
// It's thread safe and so on, so just share it.
// But if you need you can have multiple instances of StorIO
// (SQLite or ContentResolver) with different settings such as type mapping, logging and so on.
// But keep in mind that different instances of StorIOSQLite won't share notifications!
@Provides
@NonNull
@Singleton
public StorIOSQLite provideStorIOSQLite(@NonNull SQLiteOpenHelper sqLiteOpenHelper) {
    final CarStorIOSQLitePutResolver carStorIOSQLitePutResolver = new CarStorIOSQLitePutResolver();
    final CarStorIOSQLiteGetResolver carStorIOSQLiteGetResolver = new CarStorIOSQLiteGetResolver();
    final PersonStorIOSQLitePutResolver personStorIOSQLitePutResolver = new PersonStorIOSQLitePutResolver();
    final PersonStorIOSQLiteGetResolver personStorIOSQLiteGetResolver = new PersonStorIOSQLiteGetResolver();
    final CarPersonRelationPutResolver carPersonRelationPutResolver = new CarPersonRelationPutResolver();
    return DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).addTypeMapping(Tweet.class, new TweetSQLiteTypeMapping()).addTypeMapping(User.class, new UserSQLiteTypeMapping()).addTypeMapping(TweetWithUser.class, SQLiteTypeMapping.<TweetWithUser>builder().putResolver(new TweetWithUserPutResolver()).getResolver(new TweetWithUserGetResolver()).deleteResolver(new TweetWithUserDeleteResolver()).build()).addTypeMapping(Person.class, SQLiteTypeMapping.<Person>builder().putResolver(new PersonRelationsPutResolver(carStorIOSQLitePutResolver, carPersonRelationPutResolver)).getResolver(new PersonRelationsGetResolver(carStorIOSQLiteGetResolver)).deleteResolver(new PersonRelationsDeleteResolver()).build()).addTypeMapping(Car.class, SQLiteTypeMapping.<Car>builder().putResolver(new CarRelationsPutResolver(personStorIOSQLitePutResolver, carPersonRelationPutResolver)).getResolver(new CarRelationsGetResolver(personStorIOSQLiteGetResolver)).deleteResolver(new CarRelationsDeleteResolver()).build()).build();
}
Also used : TweetWithUserDeleteResolver(com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserDeleteResolver) PersonStorIOSQLitePutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonStorIOSQLitePutResolver) CarPersonRelationPutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarPersonRelationPutResolver) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) TweetSQLiteTypeMapping(com.pushtorefresh.storio3.sample.db.entities.TweetSQLiteTypeMapping) CarStorIOSQLitePutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarStorIOSQLitePutResolver) CarRelationsDeleteResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsDeleteResolver) CarStorIOSQLiteGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.CarStorIOSQLiteGetResolver) TweetWithUser(com.pushtorefresh.storio3.sample.db.entities.TweetWithUser) TweetWithUserGetResolver(com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserGetResolver) PersonRelationsPutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.PersonRelationsPutResolver) Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) PersonStorIOSQLiteGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.PersonStorIOSQLiteGetResolver) PersonRelationsDeleteResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.PersonRelationsDeleteResolver) CarRelationsPutResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsPutResolver) CarRelationsGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.CarRelationsGetResolver) TweetWithUserPutResolver(com.pushtorefresh.storio3.sample.db.resolvers.TweetWithUserPutResolver) PersonRelationsGetResolver(com.pushtorefresh.storio3.sample.many_to_many_sample.resolvers.PersonRelationsGetResolver) UserSQLiteTypeMapping(com.pushtorefresh.storio3.sample.db.entities.UserSQLiteTypeMapping) Singleton(javax.inject.Singleton) NonNull(android.support.annotation.NonNull) Provides(dagger.Provides)

Example 13 with GetResolver

use of com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForBlocking.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForBlocking() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    // noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    try {
        new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(mock(Uri.class)).build()).withGetResolver(getResolver).prepare().executeAsBlocking();
        failBecauseExceptionWasNotThrown(StorIOException.class);
    } catch (StorIOException expected) {
        IllegalStateException cause = (IllegalStateException) expected.getCause();
        assertThat(cause).hasMessage("test exception");
    }
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 14 with GetResolver

use of com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver in project storio by pushtorefresh.

the class DefaultGetResolverTest method query.

@Test
public void query() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    final Query query = Query.builder().uri(mock(Uri.class)).build();
    final Cursor expectedCursor = mock(Cursor.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    when(lowLevel.query(query)).thenReturn(expectedCursor);
    final GetResolver<TestItem> defaultGetResolver = new DefaultGetResolver<TestItem>() {

        @NonNull
        @Override
        public TestItem mapFromCursor(@NonNull StorIOContentResolver storIOContentResolver, @NonNull Cursor cursor) {
            assertThat(cursor).isSameAs(expectedCursor);
            return new TestItem();
        }
    };
    final Cursor actualCursor = defaultGetResolver.performGet(storIOContentResolver, query);
    // only one request should occur
    verify(lowLevel, times(1)).query(any(Query.class));
    // and this request should be equals to original
    verify(lowLevel, times(1)).query(query);
    assertThat(actualCursor).isSameAs(expectedCursor);
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 15 with GetResolver

use of com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver in project storio by pushtorefresh.

the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForSingle.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForSingle() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    // noinspection unchecked
    final GetResolver<Cursor> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestObserver<Cursor> testObserver = new TestObserver<Cursor>();
    new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testObserver);
    testObserver.awaitTerminalEvent(60, SECONDS);
    testObserver.assertError(StorIOException.class);
    StorIOException storIOException = (StorIOException) testObserver.errors().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
}
Also used : Query(com.pushtorefresh.storio3.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 StorIOException (com.pushtorefresh.storio3.StorIOException)9 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)7 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)6 Query (com.pushtorefresh.storio3.sqlite.queries.Query)6 Cursor (android.database.Cursor)4 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)4 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)3 GetResolver (com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver)3 TypeMappingFinderImpl (com.pushtorefresh.storio3.internal.TypeMappingFinderImpl)3 TestObserver (io.reactivex.observers.TestObserver)3 TestSubscriber (io.reactivex.subscribers.TestSubscriber)3 ContentResolver (android.content.ContentResolver)2 Uri (android.net.Uri)2 NonNull (android.support.annotation.NonNull)2 BackpressureStrategy (io.reactivex.BackpressureStrategy)2 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)1 TweetSQLiteTypeMapping (com.pushtorefresh.storio3.sample.db.entities.TweetSQLiteTypeMapping)1 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)1 UserSQLiteTypeMapping (com.pushtorefresh.storio3.sample.db.entities.UserSQLiteTypeMapping)1