Skip to content

Commit

Permalink
Implemented Instrumentation test for NotificationDatastore.kt
Browse files Browse the repository at this point in the history
NotificationDatastoreTest tests all the possible edge cases which might cause problem in working of new notification system.
  • Loading branch information
prateek-singh-3212 committed Oct 26, 2022
1 parent abc3f30 commit 391acf2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.ichi2.anki

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.ichi2.anki.model.DeckNotification
import kotlinx.coroutines.runBlocking
import org.intellij.lang.annotations.Language
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.random.Random
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class NotificationDatastoreTest {
private lateinit var notificationDatastore: NotificationDatastore

@Before
fun setup() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
notificationDatastore = NotificationDatastore.getInstance(context)
}

@Test
fun deckSchedDataReadWriteTest() {
val deckNotification = DeckNotification()

runBlocking {
val isSuccess = notificationDatastore.setDeckSchedData(
deckNotification.did,
deckNotification
)
assertTrue(
isSuccess,
"Unable to save the deck data in notification preference datastore."
)

val dataStored = notificationDatastore.getDeckSchedData(deckNotification.did)
assertNotNull(
dataStored,
"Unable to read the stored deck data from notification preference datastore."
)
}
}

@Test
fun unStoredDeckSchedDataTest() {
runBlocking {
val deckIdUnStored = Random.nextLong()
val dataUnStored = notificationDatastore.getDeckSchedData(deckIdUnStored)
assertNull(dataUnStored, "Expected null, But found data for deckId $deckIdUnStored")
}
}

@Test(expected = Exception::class)
fun deckSchedDataDeSerializationTest() {
val deckId = 2L

@Language("JSON")
val invalidDeckSchedJson = """
{
"deckPreference":{
"number":10,
"valueType":"CARDS"
},
"did":1,
"enabled":false,
"includeSubdecks":true,
"notificationTime": 0
}
""".trimIndent()

runBlocking {
notificationDatastore.putStringAsync(deckId.toString(), invalidDeckSchedJson)
notificationDatastore.getDeckSchedData(deckId)
}
}

@After
fun cleanUp() {
runBlocking {
notificationDatastore.clearDatastore()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,6 @@ class NotificationDatastore private constructor(val context: Context) {
* */
/*
* We actually are not blocking the thread. This method throws an exception. It will not create problem for us.
* TODO: unit test that :
* * if there is no preference at all, we return null
* * if there is a preference without entry for this key we return null
* * if there is a preference whose entry for this key can't be cast to DeckNotification, throw
* * if there is a preference with entry for this key that can be cast, we get expected notification
*/
@Suppress("BlockingMethodInNonBlockingContext")
suspend fun getDeckSchedData(did: DeckId): DeckNotification? {
Expand Down

0 comments on commit 391acf2

Please sign in to comment.