<commit_before>package com.android.example.paging.pagingwithnetwork.reddit.repository

import androidx.paging.PagingSource.LoadParams.Refresh
import androidx.paging.PagingSource.LoadResult.Page
import com.android.example.paging.pagingwithnetwork.reddit.ui.SubRedditViewModel.Companion.DEFAULT_SUBREDDIT
import com.android.example.paging.pagingwithnetwork.reddit.repository.inMemory.byItem.ItemKeyedSubredditPagingSource
import com.android.example.paging.pagingwithnetwork.reddit.repository.inMemory.byPage.PageKeyedSubredditPagingSource
import com.android.example.paging.pagingwithnetwork.repository.FakeRedditApi
import com.android.example.paging.pagingwithnetwork.repository.PostFactory
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalCoroutinesApi::class)
class SubredditPagingSourceTest {
    private val postFactory = PostFactory()
    private val fakePosts = listOf(
        postFactory.createRedditPost(DEFAULT_SUBREDDIT),
        postFactory.createRedditPost(DEFAULT_SUBREDDIT),
        postFactory.createRedditPost(DEFAULT_SUBREDDIT)
    )
    private val fakeApi = FakeRedditApi().apply {
        fakePosts.forEach { post -> addPost(post) }
    }

    @Test
    fun itemKeyedSubredditPagingSource() = runBlockingTest {
        val pagingSource = ItemKeyedSubredditPagingSource(fakeApi, DEFAULT_SUBREDDIT)
        assertEquals(
            expected = Page(
                data = listOf(fakePosts[0]),
                prevKey = fakePosts[0].name,
                nextKey = fakePosts[0].name
            ),
            actual = pagingSource.load(
                Refresh(
                    key = null,
                    loadSize = 1,
                    placeholdersEnabled = false
                )
            ),
        )
    }

    @Test
    fun pageKeyedSubredditPagingSource() = runBlockingTest {
        val pagingSource = PageKeyedSubredditPagingSource(fakeApi, DEFAULT_SUBREDDIT)
        assertEquals(
            expected = Page(
                data = listOf(fakePosts[0]),
                prevKey = null,
                nextKey = fakePosts[0].name
            ),
            actual = pagingSource.load(
                Refresh(
                    key = null,
                    loadSize = 1,
                    placeholdersEnabled = false<commit_msg>Use loadSize = 2 in tests<commit_after>package com.android.example.paging.pagingwithnetwork.reddit.repository

import androidx.paging.PagingSource.LoadParams.Refresh
import androidx.paging.PagingSource.LoadResult.Page
import com.android.example.paging.pagingwithnetwork.reddit.ui.SubRedditViewModel.Companion.DEFAULT_SUBREDDIT
import com.android.example.paging.pagingwithnetwork.reddit.repository.inMemory.byItem.ItemKeyedSubredditPagingSource
import com.android.example.paging.pagingwithnetwork.reddit.repository.inMemory.byPage.PageKeyedSubredditPagingSource
import com.android.example.paging.pagingwithnetwork.repository.FakeRedditApi
import com.android.example.paging.pagingwithnetwork.repository.PostFactory
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalCoroutinesApi::class)
class SubredditPagingSourceTest {
    private val postFactory = PostFactory()
    private val fakePosts = listOf(
        postFactory.createRedditPost(DEFAULT_SUBREDDIT),
        postFactory.createRedditPost(DEFAULT_SUBREDDIT),
        postFactory.createRedditPost(DEFAULT_SUBREDDIT)
    )
    private val fakeApi = FakeRedditApi().apply {
        fakePosts.forEach { post -> addPost(post) }
    }

    @Test
    fun itemKeyedSubredditPagingSource() = runBlockingTest {
        val pagingSource = ItemKeyedSubredditPagingSource(fakeApi, DEFAULT_SUBREDDIT)
        assertEquals(
            expected = Page(
                data = listOf(fakePosts[0], fakePosts[1]),
                prevKey = fakePosts[0].name,
                nextKey = fakePosts[1].name
            ),
            actual = pagingSource.load(
                Refresh(
                    key = null,
                    loadSize = 2,
                    placeholdersEnabled = false
                )
            ),
        )
    }

    @Test
    fun pageKeyedSubredditPagingSource() = runBlockingTest {
        val pagingSource = PageKeyedSubredditPagingSource(fakeApi, DEFAULT_SUBREDDIT)
        assertEquals(
            expected = Page(
                data = listOf(fakePosts[0], fakePosts[1]),
                prevKey = null,
                nextKey = fakePosts[1].name
            ),
            actual = pagingSource.load(
                Refresh(
                    key = null,
                    loadSize = 2,
                    placeholdersEnabled = false
{'fraction_non_alphanumeric': 0.07531985142385472, 'fraction_numerical': 0.0035080478745356997, 'mean_word_length': 2.5903703703703704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3409658', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1348, 'n_words': 199}