Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit bb089a6

Browse files
authored
Merge pull request #1243 from wordpress-mobile/feature/add-get-local-drafts
Add PostStore.getLocalDrafts
2 parents 4abb99c + 5aad0a2 commit bb089a6

File tree

6 files changed

+155
-5
lines changed

6 files changed

+155
-5
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.wordpress.android.fluxc.page
2+
3+
import com.nhaarman.mockitokotlin2.mock
4+
import com.yarolegovich.wellsql.WellSql
5+
import kotlinx.coroutines.Dispatchers
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.junit.After
8+
import org.junit.Before
9+
import org.junit.Test
10+
import org.junit.runner.RunWith
11+
import org.robolectric.RobolectricTestRunner
12+
import org.robolectric.RuntimeEnvironment
13+
import org.wordpress.android.fluxc.SingleStoreWellSqlConfigForTests
14+
import org.wordpress.android.fluxc.model.PostModel
15+
import org.wordpress.android.fluxc.model.SiteModel
16+
import org.wordpress.android.fluxc.model.post.PostStatus
17+
import org.wordpress.android.fluxc.persistence.PostSqlUtils
18+
import org.wordpress.android.fluxc.store.PageStore
19+
import org.wordpress.android.fluxc.test
20+
import java.util.UUID
21+
22+
@RunWith(RobolectricTestRunner::class)
23+
class PageStoreLocalDraftTest {
24+
private val pageStore = PageStore(postStore = mock(), dispatcher = mock(), coroutineContext = Dispatchers.Default)
25+
26+
@Before
27+
fun setUp() {
28+
val appContext = RuntimeEnvironment.application.applicationContext
29+
val modelsToTest = listOf(PostModel::class.java)
30+
val config = SingleStoreWellSqlConfigForTests(appContext, modelsToTest, "")
31+
WellSql.init(config)
32+
config.reset()
33+
}
34+
35+
@After
36+
fun tearDown() {
37+
WellSql.closeDb()
38+
}
39+
40+
@Test
41+
fun `getLocalDraftPages returns local draft pages only`() = test {
42+
// Arrange
43+
val site = SiteModel().apply { id = 3_000 }
44+
45+
val baseTitle = "Voluptatem harum repellendus"
46+
val expectedPages = List(3) {
47+
createLocalDraft(localSiteId = site.id, baseTitle = baseTitle).apply {
48+
setIsPage(true)
49+
}
50+
}
51+
52+
val unexpectedPosts = listOf(
53+
// local draft post
54+
createLocalDraft(localSiteId = site.id).apply { setIsPage(false) },
55+
// other site page
56+
createLocalDraft(localSiteId = 4_000),
57+
// uploaded page
58+
PostModel().apply {
59+
title = "Title"
60+
localSiteId = site.id
61+
setIsLocalDraft(false)
62+
}
63+
)
64+
65+
expectedPages.plus(unexpectedPosts).forEach { PostSqlUtils.insertPostForResult(it) }
66+
67+
// Act
68+
val localDraftPages = pageStore.getLocalDraftPages(site)
69+
70+
// Assert
71+
assertThat(localDraftPages).hasSize(3)
72+
assertThat(localDraftPages).allMatch { it.title.startsWith(baseTitle) }
73+
assertThat(localDraftPages.map { it.pageId }).isEqualTo(expectedPages.map { it.id })
74+
}
75+
76+
private fun createLocalDraft(localSiteId: Int, baseTitle: String = "Title") = PostModel().apply {
77+
this.localSiteId = localSiteId
78+
title = "$baseTitle:${UUID.randomUUID()}"
79+
setIsLocalDraft(true)
80+
status = PostStatus.DRAFT.toString()
81+
}
82+
}

example/src/test/java/org/wordpress/android/fluxc/post/PostStoreUnitTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.Date;
3535
import java.util.List;
36+
import java.util.UUID;
3637

3738
import static junit.framework.Assert.assertFalse;
3839
import static junit.framework.Assert.assertNotNull;
@@ -435,9 +436,43 @@ public void testDeleteLocalRevisionOfAPostOrPage() {
435436
assertNull(mPostStore.getLocalRevision(site, postModel));
436437
}
437438

439+
@Test
440+
public void testGetLocalDraftPostsMethodOnlyReturnsLocalDrafts() {
441+
// Arrange
442+
final String baseTitle = "Alexandrine Thiel";
443+
for (int i = 0; i < 3; i++) {
444+
final String compoundTitle = baseTitle.concat(":").concat(UUID.randomUUID().toString());
445+
final PostModel post = PostTestUtils.generateSampleLocalDraftPost(compoundTitle);
446+
PostSqlUtils.insertPostForResult(post);
447+
}
448+
449+
final PostModel localDraftPage = PostTestUtils.generateSampleLocalDraftPost();
450+
localDraftPage.setIsPage(true);
451+
PostSqlUtils.insertPostForResult(localDraftPage);
452+
453+
final PostModel uploadedPost = PostTestUtils.generateSampleUploadedPost();
454+
PostSqlUtils.insertPostForResult(uploadedPost);
455+
456+
final SiteModel site = new SiteModel();
457+
site.setId(PostTestUtils.DEFAULT_LOCAL_SITE_ID);
458+
459+
// Act
460+
final List<PostModel> localDraftPosts = mPostStore.getLocalDraftPosts(site);
461+
462+
// Assert
463+
assertEquals(3, localDraftPosts.size());
464+
for (PostModel localDraftPost : localDraftPosts) {
465+
assertTrue(localDraftPost.isLocalDraft());
466+
assertTrue(localDraftPost.getTitle().startsWith(baseTitle));
467+
468+
assertNotEquals(uploadedPost.getId(), localDraftPost.getId());
469+
assertNotEquals(localDraftPage.getId(), localDraftPost.getId());
470+
}
471+
}
472+
438473
/**
439474
* Tests that getPostsByLocalOrRemotePostIds works correctly in various situations.
440-
*
475+
* <p>
441476
* Normally it's not a good idea to combine multiple tests like this, however due to Java's verbosity the tests
442477
* are combined to avoid having too much boilerplate code.
443478
*/

example/src/test/java/org/wordpress/android/fluxc/post/PostTestUtils.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.wordpress.android.fluxc.post;
22

3+
import android.support.annotation.NonNull;
4+
35
import com.yarolegovich.wellsql.WellSql;
46

57
import org.wordpress.android.fluxc.model.PostModel;
@@ -14,13 +16,15 @@ public class PostTestUtils {
1416
public static final double EXAMPLE_LATITUDE = 44.8378;
1517
public static final double EXAMPLE_LONGITUDE = -0.5792;
1618

19+
static final int DEFAULT_LOCAL_SITE_ID = 6;
20+
1721
public static PostModel generateSampleUploadedPost() {
1822
return generateSampleUploadedPost("text");
1923
}
2024

2125
public static PostModel generateSampleUploadedPost(String postFormat) {
2226
PostModel example = new PostModel();
23-
example.setLocalSiteId(6);
27+
example.setLocalSiteId(DEFAULT_LOCAL_SITE_ID);
2428
example.setRemotePostId(5);
2529
example.setTitle("A test post");
2630
example.setContent("Bunch of content here");
@@ -29,17 +33,21 @@ public static PostModel generateSampleUploadedPost(String postFormat) {
2933
}
3034

3135
public static PostModel generateSampleLocalDraftPost() {
36+
return generateSampleLocalDraftPost("A test post");
37+
}
38+
39+
static PostModel generateSampleLocalDraftPost(@NonNull String title) {
3240
PostModel example = new PostModel();
33-
example.setLocalSiteId(6);
34-
example.setTitle("A test post");
41+
example.setLocalSiteId(DEFAULT_LOCAL_SITE_ID);
42+
example.setTitle(title);
3543
example.setContent("Bunch of content here");
3644
example.setIsLocalDraft(true);
3745
return example;
3846
}
3947

4048
public static PostModel generateSampleLocallyChangedPost() {
4149
PostModel example = new PostModel();
42-
example.setLocalSiteId(6);
50+
example.setLocalSiteId(DEFAULT_LOCAL_SITE_ID);
4351
example.setRemotePostId(7);
4452
example.setTitle("A test post");
4553
example.setContent("Bunch of content here");

fluxc/src/main/java/org/wordpress/android/fluxc/persistence/PostSqlUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public static List<PostModel> getUploadedPostsForSite(SiteModel site, boolean ge
125125
.getAsModel();
126126
}
127127

128+
public static List<PostModel> getLocalDrafts(@NonNull Integer localSiteId, boolean isPage) {
129+
return WellSql.select(PostModel.class)
130+
.where()
131+
.beginGroup()
132+
.equals(PostModelTable.LOCAL_SITE_ID, localSiteId)
133+
.equals(PostModelTable.IS_LOCAL_DRAFT, true)
134+
.equals(PostModelTable.IS_PAGE, isPage)
135+
.endGroup()
136+
.endWhere()
137+
.getAsModel();
138+
}
139+
128140
public static List<PostModel> getPostsByRemoteIds(@Nullable List<Long> remoteIds, int localSiteId) {
129141
if (remoteIds != null && remoteIds.size() > 0) {
130142
return WellSql.select(PostModel.class)

fluxc/src/main/java/org/wordpress/android/fluxc/store/PageStore.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ class PageStore @Inject constructor(
182182
fetchPages(site, false)
183183
}
184184

185+
suspend fun getLocalDraftPages(site: SiteModel): List<PageModel> = withContext(coroutineContext) {
186+
return@withContext PostSqlUtils.getLocalDrafts(site.id, true).map {
187+
PageModel(post = it, site = site)
188+
}
189+
}
190+
185191
private fun fetchPages(site: SiteModel, loadMore: Boolean) {
186192
val payload = FetchPostsPayload(site, loadMore, PAGE_TYPES)
187193
dispatcher.dispatch(PostActionBuilder.newFetchPagesAction(payload))

fluxc/src/main/java/org/wordpress/android/fluxc/store/PostStore.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ public int getUploadedPagesCountForSite(SiteModel site) {
422422
return getUploadedPagesForSite(site).size();
423423
}
424424

425+
/**
426+
* Returns all posts and pages that are local drafts for the given site.
427+
*/
428+
public List<PostModel> getLocalDraftPosts(@NonNull SiteModel site) {
429+
return PostSqlUtils.getLocalDrafts(site.getId(), false);
430+
}
431+
425432
/**
426433
* Given a local ID for a post, returns that post as a {@link PostModel}.
427434
*/

0 commit comments

Comments
 (0)