<commit_before>package org.stepik.android.data.section

import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.reactivex.Completable
import io.reactivex.Maybe
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.runners.MockitoJUnitRunner
import org.stepik.android.data.section.repository.SectionRepositoryImpl
import org.stepik.android.data.section.source.SectionCacheDataSource
import org.stepik.android.data.section.source.SectionRemoteDataSource
import org.stepik.android.domain.base.DataSourceType
import org.stepik.android.model.Section
import java.io.IOException

@RunWith(MockitoJUnitRunner::class)
class SectionRepositoryTest {
    @Mock
    private lateinit var sectionRemoteDataSource: SectionRemoteDataSource

    @Mock
    private lateinit var sectionCacheDataSource: SectionCacheDataSource

    @Test
    fun sectionInCacheTest() {
        val sectionRepository = SectionRepositoryImpl(sectionCacheDataSource, sectionRemoteDataSource)

        val sectionId = 312L
        val section = Section(id = sectionId)

        whenever(sectionCacheDataSource.getSection(sectionId)) doReturn Maybe.just(section)
        whenever(sectionCacheDataSource.saveSection(section)) doReturn Completable.complete()
        whenever(sectionRemoteDataSource.getSection(sectionId)) doReturn Maybe.empty()

        sectionRepository
            .getSection(sectionId, primarySourceType = DataSourceType.CACHE)
            .test()
            .assertNoErrors()
            .assertComplete()
            .assertResult(section)
    }

    @Test
    fun sectionRemoteFallbackToCacheTest() {
        val sectionRepository = SectionRepositoryImpl(sectionCacheDataSource, sectionRemoteDataSource)

        val sectionId = 312L
        val section = Section(id = sectionId)

        whenever(sectionCacheDataSource.getSection(sectionId)) doReturn Maybe.just(section)
        whenever(sectionRemoteDataSource.getSection(sectionId)) doReturn Maybe.error(IOException(""))

        sectionRepository
            .getSection(sectionId, primarySourceType = DataSourceType.REMOTE)
            .test()
            .assertNoErrors()
            .assertComplete()
            .assertResult(section)
    }

    @Test
    fun sectionEmptyCacheSwitchToRemoteTest() {
        val sectionRepository = SectionRepositoryImpl(sectionCacheDataSource, sectionRemoteDataSource)

        val sectionId = 312L
        val section = Section(id = sectionId)

        whenever(sectionCacheDataSource.getSection(sectionId)) doReturn Maybe.empty()
        whenever(sectionCacheDataSource.saveSection(section)) doReturn Completable.complete()
        whenever(sectionRemoteDataSource.getSection(sectionId)) doReturn Maybe.just(section)

        sectionRepository
            .getSection(sectionId)
            .test()
            .assertNoErrors()
            .assertComplete()
            .assertResult(section)

        verify(sectionCacheDataSource).saveSection(section)
    }
}<commit_msg>remove unused mock<commit_after>package org.stepik.android.data.section

import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.reactivex.Completable
import io.reactivex.Maybe
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.runners.MockitoJUnitRunner
import org.stepik.android.data.section.repository.SectionRepositoryImpl
import org.stepik.android.data.section.source.SectionCacheDataSource
import org.stepik.android.data.section.source.SectionRemoteDataSource
import org.stepik.android.domain.base.DataSourceType
import org.stepik.android.model.Section
import java.io.IOException

@RunWith(MockitoJUnitRunner::class)
class SectionRepositoryTest {
    @Mock
    private lateinit var sectionRemoteDataSource: SectionRemoteDataSource

    @Mock
    private lateinit var sectionCacheDataSource: SectionCacheDataSource

    @Test
    fun sectionInCacheTest() {
        val sectionRepository = SectionRepositoryImpl(sectionCacheDataSource, sectionRemoteDataSource)

        val sectionId = 312L
        val section = Section(id = sectionId)

        whenever(sectionCacheDataSource.getSection(sectionId)) doReturn Maybe.just(section)
        whenever(sectionRemoteDataSource.getSection(sectionId)) doReturn Maybe.empty()

        sectionRepository
            .getSection(sectionId, primarySourceType = DataSourceType.CACHE)
            .test()
            .assertNoErrors()
            .assertComplete()
            .assertResult(section)
    }

    @Test
    fun sectionRemoteFallbackToCacheTest() {
        val sectionRepository = SectionRepositoryImpl(sectionCacheDataSource, sectionRemoteDataSource)

        val sectionId = 312L
        val section = Section(id = sectionId)

        whenever(sectionCacheDataSource.getSection(sectionId)) doReturn Maybe.just(section)
        whenever(sectionRemoteDataSource.getSection(sectionId)) doReturn Maybe.error(IOException(""))

        sectionRepository
            .getSection(sectionId, primarySourceType = DataSourceType.REMOTE)
            .test()
            .assertNoErrors()
            .assertComplete()
            .assertResult(section)
    }

    @Test
    fun sectionEmptyCacheSwitchToRemoteTest() {
        val sectionRepository = SectionRepositoryImpl(sectionCacheDataSource, sectionRemoteDataSource)

        val sectionId = 312L
        val section = Section(id = sectionId)

        whenever(sectionCacheDataSource.getSection(sectionId)) doReturn Maybe.empty()
        whenever(sectionCacheDataSource.saveSection(section)) doReturn Completable.complete()
        whenever(sectionRemoteDataSource.getSection(sectionId)) doReturn Maybe.just(section)

        sectionRepository
            .getSection(sectionId)
            .test()
            .assertNoErrors()
            .assertComplete()
            .assertResult(section)

        verify(sectionCacheDataSource).saveSection(section)
    }
}
{'fraction_non_alphanumeric': 0.07647931432338882, 'fraction_numerical': 0.003955826602933905, 'mean_word_length': 3.6109422492401215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535408', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1519, 'n_words': 271}