Skip to content

Commit

Permalink
Merge pull request #2038 from wordpress-mobile/feature/check-blogging…
Browse files Browse the repository at this point in the history
…-reminders-presence

Check blogging reminders presence
  • Loading branch information
renanferrari authored Jun 25, 2021
2 parents c96baf1 + c9f6c62 commit 2f9ee2a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class BloggingRemindersStoreTest {
fun `maps items emitted from dao`() = test {
val dbEntity = BloggingReminders(siteId, monday = true)
val domainModel = BloggingRemindersModel(siteId, setOf(MONDAY))
whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(flowOf(dbEntity))
whenever(bloggingRemindersDao.liveGetBySiteId(siteId)).thenReturn(flowOf(dbEntity))
whenever(mapper.toDomainModel(dbEntity)).thenReturn(domainModel)

assertThat(store.bloggingRemindersModel(siteId).single()).isEqualTo(domainModel)
}

@Test
fun `maps null value to empty model emitted from dao`() = test {
whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(flowOf(null))
whenever(bloggingRemindersDao.liveGetBySiteId(siteId)).thenReturn(flowOf(null))

assertThat(store.bloggingRemindersModel(siteId).single()).isEqualTo(BloggingRemindersModel(siteId))
}
Expand All @@ -57,4 +57,19 @@ class BloggingRemindersStoreTest {

verify(bloggingRemindersDao).insert(dbEntity)
}

@Test
fun `has modified blogging reminders when DAO returns data`() = test {
val dbEntity = BloggingReminders(siteId, monday = true)
whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(listOf(dbEntity))

assertThat(store.hasModifiedBloggingReminders(siteId)).isTrue()
}

@Test
fun `does not have modified blogging reminders when DAO returns no data`() = test {
whenever(bloggingRemindersDao.getBySiteId(siteId)).thenReturn(listOf())

assertThat(store.hasModifiedBloggingReminders(siteId)).isFalse()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "4f29ca40a8cdc7081cbff3a88c7eaa87",
"entities": [
{
"tableName": "BloggingReminders",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`localSiteId` INTEGER NOT NULL, `monday` INTEGER NOT NULL, `tuesday` INTEGER NOT NULL, `wednesday` INTEGER NOT NULL, `thursday` INTEGER NOT NULL, `friday` INTEGER NOT NULL, `saturday` INTEGER NOT NULL, `sunday` INTEGER NOT NULL, PRIMARY KEY(`localSiteId`))",
"fields": [
{
"fieldPath": "localSiteId",
"columnName": "localSiteId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "monday",
"columnName": "monday",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "tuesday",
"columnName": "tuesday",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "wednesday",
"columnName": "wednesday",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "thursday",
"columnName": "thursday",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "friday",
"columnName": "friday",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "saturday",
"columnName": "saturday",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "sunday",
"columnName": "sunday",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"localSiteId"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4f29ca40a8cdc7081cbff3a88c7eaa87')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ abstract class BloggingRemindersDao {
abstract fun getAll(): Flow<List<BloggingReminders>>

@Query("SELECT * FROM BloggingReminders WHERE localSiteId = :siteId")
abstract fun getBySiteId(siteId: Int): Flow<BloggingReminders?>
abstract fun liveGetBySiteId(siteId: Int): Flow<BloggingReminders?>

@Query("SELECT * FROM BloggingReminders WHERE localSiteId = :siteId")
abstract suspend fun getBySiteId(siteId: Int): List<BloggingReminders>

@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(type: BloggingReminders): Long
abstract suspend fun insert(type: BloggingReminders): Long

@Entity(tableName = "BloggingReminders")
data class BloggingReminders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ class BloggingRemindersStore
private val coroutineEngine: CoroutineEngine
) {
fun bloggingRemindersModel(siteId: Int): Flow<BloggingRemindersModel> {
return bloggingRemindersDao.getBySiteId(siteId).map {
return bloggingRemindersDao.liveGetBySiteId(siteId).map {
it?.let { dbModel -> mapper.toDomainModel(dbModel) } ?: BloggingRemindersModel(siteId)
}
}

suspend fun hasModifiedBloggingReminders(siteId: Int) =
coroutineEngine.withDefaultContext(T.SETTINGS, this, "Has blogging reminders") {
bloggingRemindersDao.getBySiteId(siteId).isNotEmpty()
}

suspend fun updateBloggingReminders(model: BloggingRemindersModel) =
coroutineEngine.withDefaultContext(T.SETTINGS, this, "Updating blogging reminders") {
bloggingRemindersDao.insert(mapper.toDatabaseModel(model))
Expand Down

0 comments on commit 2f9ee2a

Please sign in to comment.