Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check blogging reminders presence #2038

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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