Search in sources :

Example 6 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method defaultSchedulerReturnsNullIfSpecifiedSchedulerNull.

@Test
public void defaultSchedulerReturnsNullIfSpecifiedSchedulerNull() {
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).defaultScheduler(null).build();
    assertThat(storIOContentResolver.defaultScheduler()).isNull();
}
Also used : StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) ContentResolver(android.content.ContentResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 7 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method defaultSchedulerReturnsIOSchedulerIfNotSpecified.

@Test
public void defaultSchedulerReturnsIOSchedulerIfNotSpecified() {
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).build();
    assertThat(storIOContentResolver.defaultScheduler()).isSameAs(io());
}
Also used : StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 8 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method typeMappingShouldWorkWithSpecifiedTypeMappingFinder.

@Test
public void typeMappingShouldWorkWithSpecifiedTypeMappingFinder() {
    TypeMappingFinder typeMappingFinder = new TypeMappingFinderImpl();
    //noinspection unchecked
    ContentResolverTypeMapping<ClassEntity> typeMapping = ContentResolverTypeMapping.builder().putResolver(mock(PutResolver.class)).getResolver(mock(GetResolver.class)).deleteResolver(mock(DeleteResolver.class)).build();
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).typeMappingFinder(typeMappingFinder).addTypeMapping(ClassEntity.class, typeMapping).build();
    assertThat(storIOContentResolver.lowLevel().typeMapping(ClassEntity.class)).isEqualTo(typeMapping);
}
Also used : TypeMappingFinderImpl(com.pushtorefresh.storio.internal.TypeMappingFinderImpl) TypeMappingFinder(com.pushtorefresh.storio.TypeMappingFinder) GetResolver(com.pushtorefresh.storio.contentresolver.operations.get.GetResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 9 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultDeleteResolverTest method performDelete.

@Test
public void performDelete() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    final int expectedNumberOfRowsDeleted = 1;
    when(lowLevel.delete(any(DeleteQuery.class))).thenReturn(expectedNumberOfRowsDeleted);
    final Uri expectedUri = mock(Uri.class);
    final DeleteQuery expectedDeleteQuery = DeleteQuery.builder().uri(expectedUri).where("test where clause").whereArgs("test").build();
    final TestItem testItem = TestItem.newInstance();
    final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {

        @NonNull
        @Override
        protected DeleteQuery mapToDeleteQuery(@NonNull TestItem object) {
            assertThat(object).isSameAs(testItem);
            return expectedDeleteQuery;
        }
    };
    // Performing Delete Operation
    final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOContentResolver, testItem);
    // checks that required delete was performed
    verify(lowLevel, times(1)).delete(expectedDeleteQuery);
    // only one delete should be performed
    verify(lowLevel, times(1)).delete(any(DeleteQuery.class));
    // delete result checks
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(expectedNumberOfRowsDeleted);
    assertThat(deleteResult.affectedUris()).hasSize(1);
    assertThat(deleteResult.affectedUris()).contains(expectedUri);
}
Also used : NonNull(android.support.annotation.NonNull) DeleteQuery(com.pushtorefresh.storio.contentresolver.queries.DeleteQuery) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Example 10 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultPutResolverTest method shouldCloseCursorIfInsertThrowsException.

@Test
public void shouldCloseCursorIfInsertThrowsException() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// No results -> insert should be performed
    0);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    when(lowLevel.insert(eq(expectedInsertQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            return TestItem.MAP_TO_CONTENT_VALUES.call(object);
        }
    };
    try {
        putResolver.performPut(storIOContentResolver, testItem);
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Fake exception from ContentResolver");
        verify(storIOContentResolver).lowLevel();
        // Checks that it asks actual ContentResolver for results
        verify(lowLevel).query(eq(expectedQuery));
        verify(cursor).getCount();
        // Cursor must be closed in case of exception!
        verify(cursor).close();
        verify(lowLevel).insert(eq(expectedInsertQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio.contentresolver.queries.Query) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Aggregations

StorIOContentResolver (com.pushtorefresh.storio.contentresolver.StorIOContentResolver)23 Test (org.junit.Test)20 Query (com.pushtorefresh.storio.contentresolver.queries.Query)11 NonNull (android.support.annotation.NonNull)9 ContentResolver (android.content.ContentResolver)8 Cursor (android.database.Cursor)6 Uri (android.net.Uri)6 ContentValues (android.content.ContentValues)5 StorIOException (com.pushtorefresh.storio.StorIOException)5 InsertQuery (com.pushtorefresh.storio.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio.contentresolver.queries.UpdateQuery)5 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)4 GetResolver (com.pushtorefresh.storio.contentresolver.operations.get.GetResolver)3 WorkerThread (android.support.annotation.WorkerThread)2 ContentResolverTypeMapping (com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping)2 TypeMappingFinderImpl (com.pushtorefresh.storio.internal.TypeMappingFinderImpl)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 HashMap (java.util.HashMap)2 TestSubscriber (rx.observers.TestSubscriber)2 Handler (android.os.Handler)1