This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Add methods for getting locally changed Posts and Pages #1303
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac6db0c
PostTestUtils: Accept title parameter for helper
shiki 571ad0d
Add PostSTore.getLocallyChangedPosts
shiki b64c92c
Add test for getLocallyChangedPosts
shiki 0824e6c
Rename PageStoreLocalDraftTest to *LocalChanges
shiki ba5a46f
Add PageStore.getLocallyChangedPages
shiki 60d040f
Add a test for PageStore.getLocallyChangedPages
shiki df6ad09
Rename getLocallyChanged* to get*WithLocalChanges
shiki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
example/src/test/java/org/wordpress/android/fluxc/page/PageStoreLocalChangesTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package org.wordpress.android.fluxc.page | ||
|
|
||
| import com.nhaarman.mockitokotlin2.mock | ||
| import com.yarolegovich.wellsql.WellSql | ||
| import kotlinx.coroutines.Dispatchers | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.RuntimeEnvironment | ||
| import org.wordpress.android.fluxc.SingleStoreWellSqlConfigForTests | ||
| import org.wordpress.android.fluxc.model.PostModel | ||
| import org.wordpress.android.fluxc.model.SiteModel | ||
| import org.wordpress.android.fluxc.model.post.PostStatus | ||
| import org.wordpress.android.fluxc.persistence.PostSqlUtils | ||
| import org.wordpress.android.fluxc.store.PageStore | ||
| import org.wordpress.android.fluxc.test | ||
| import java.util.UUID | ||
| import kotlin.random.Random | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class PageStoreLocalChangesTest { | ||
| private val postSqlUtils = PostSqlUtils() | ||
| private val pageStore = PageStore( | ||
| postStore = mock(), | ||
| dispatcher = mock(), | ||
| coroutineContext = Dispatchers.Default, | ||
| postSqlUtils = postSqlUtils | ||
| ) | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| val appContext = RuntimeEnvironment.application.applicationContext | ||
| val modelsToTest = listOf(PostModel::class.java) | ||
| val config = SingleStoreWellSqlConfigForTests(appContext, modelsToTest, "") | ||
| WellSql.init(config) | ||
| config.reset() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| WellSql.closeDb() | ||
| } | ||
|
|
||
| @Test | ||
| fun `getLocalDraftPages returns local draft pages only`() = test { | ||
| // Arrange | ||
| val site = SiteModel().apply { id = 3_000 } | ||
|
|
||
| val baseTitle = "Voluptatem harum repellendus" | ||
| val expectedPages = List(3) { | ||
| createLocalDraft(localSiteId = site.id, baseTitle = baseTitle, isPage = true) | ||
| } | ||
|
|
||
| val unexpectedPosts = listOf( | ||
| // local draft post | ||
| createLocalDraft(localSiteId = site.id, isPage = false), | ||
| // other site page | ||
| createLocalDraft(localSiteId = 4_000, isPage = true), | ||
| // uploaded page | ||
| PostModel().apply { | ||
| title = "Title" | ||
| localSiteId = site.id | ||
| setIsLocalDraft(false) | ||
| } | ||
| ) | ||
|
|
||
| expectedPages.plus(unexpectedPosts).forEach { postSqlUtils.insertPostForResult(it) } | ||
|
|
||
| // Act | ||
| val localDraftPages = pageStore.getLocalDraftPages(site) | ||
|
|
||
| // Assert | ||
| assertThat(localDraftPages).hasSize(3) | ||
| assertThat(localDraftPages).allMatch { it.title.startsWith(baseTitle) } | ||
| assertThat(localDraftPages.map { it.id }).isEqualTo(expectedPages.map { it.id }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getPagesWithLocalChanges returns local draft and locally changed pages only`() = test { | ||
| // Arrange | ||
| val site = SiteModel().apply { id = 3_000 } | ||
|
|
||
| val baseTitle = "Voluptatem harum repellendus" | ||
| val expectedPages = mutableListOf<PostModel>().apply { | ||
| addAll(List(3) { | ||
| createLocalDraft(localSiteId = site.id, baseTitle = baseTitle, isPage = true) | ||
| }) | ||
|
|
||
| addAll(List(5) { | ||
| createUploadedPost(localSiteId = site.id, baseTitle = baseTitle, isPage = true).apply { | ||
| setIsLocallyChanged(true) | ||
| } | ||
| }) | ||
|
|
||
| add(createUploadedPost(localSiteId = site.id, baseTitle = baseTitle, isPage = true).apply { | ||
| status = PostStatus.PUBLISHED.toString() | ||
| setIsLocallyChanged(true) | ||
| }) | ||
| }.toList() | ||
|
|
||
| val unexpectedPosts = listOf( | ||
| // local draft post | ||
| createLocalDraft(localSiteId = site.id, isPage = false), | ||
| // other site page | ||
| createLocalDraft(localSiteId = 4_000, isPage = true), | ||
| // uploaded post | ||
| createUploadedPost(localSiteId = site.id, isPage = false), | ||
| // uploaded post with changes | ||
| createUploadedPost(localSiteId = site.id, isPage = false).apply { setIsLocallyChanged(true) }, | ||
| // uploaded page with no changes | ||
| createUploadedPost(localSiteId = site.id, isPage = true), | ||
| // published page with no changes | ||
| createUploadedPost(localSiteId = site.id, isPage = true).apply { | ||
| status = PostStatus.PUBLISHED.toString() | ||
| } | ||
| ) | ||
|
|
||
| expectedPages.plus(unexpectedPosts).forEach { postSqlUtils.insertPostForResult(it) } | ||
|
|
||
| // Act | ||
| val locallyChangedPages = pageStore.getPagesWithLocalChanges(site) | ||
|
|
||
| // Assert | ||
| assertThat(locallyChangedPages).hasSize(expectedPages.size) | ||
| assertThat(locallyChangedPages).allMatch { it.title.startsWith(baseTitle) } | ||
| assertThat(locallyChangedPages.map { it.id }).isEqualTo(expectedPages.map { it.id }) | ||
| } | ||
|
|
||
| private fun createLocalDraft(localSiteId: Int, baseTitle: String = "Title", isPage: Boolean) = PostModel().apply { | ||
| this.localSiteId = localSiteId | ||
| title = "$baseTitle:${UUID.randomUUID()}" | ||
| setIsPage(isPage) | ||
| setIsLocalDraft(true) | ||
| status = PostStatus.DRAFT.toString() | ||
| } | ||
|
|
||
| private fun createUploadedPost(localSiteId: Int, baseTitle: String = "Title", isPage: Boolean) = PostModel().apply { | ||
| this.localSiteId = localSiteId | ||
| remotePostId = Random.nextLong() | ||
| title = "$baseTitle:${UUID.randomUUID()}" | ||
| setIsPage(isPage) | ||
| } | ||
| } | ||
88 changes: 0 additions & 88 deletions
88
example/src/test/java/org/wordpress/android/fluxc/page/PageStoreLocalDraftTest.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.