mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-08-05 23:48:41 +00:00
Instrumentation tests for ServerModelStore
This commit is contained in:
parent
03c687def5
commit
b8dd2c0d24
5 changed files with 134 additions and 1 deletions
|
@ -23,6 +23,7 @@ ext.versions = [
|
||||||
rxkPrefs : '1.2.0',
|
rxkPrefs : '1.2.0',
|
||||||
|
|
||||||
timber : '4.7.1',
|
timber : '4.7.1',
|
||||||
|
testRunner : '1.0.2',
|
||||||
junit : '4.12',
|
junit : '4.12',
|
||||||
mockito : '2.23.0',
|
mockito : '2.23.0',
|
||||||
mockitoKotlin : '2.0.0-RC1',
|
mockitoKotlin : '2.0.0-RC1',
|
||||||
|
|
|
@ -10,6 +10,8 @@ android {
|
||||||
targetSdkVersion versions.compileSdk
|
targetSdkVersion versions.compileSdk
|
||||||
versionCode versions.publishVersionCode
|
versionCode versions.publishVersionCode
|
||||||
versionName versions.publishVersion
|
versionName versions.publishVersion
|
||||||
|
|
||||||
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +34,9 @@ dependencies {
|
||||||
testImplementation 'org.mockito:mockito-core:' + versions.mockito
|
testImplementation 'org.mockito:mockito-core:' + versions.mockito
|
||||||
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:' + versions.mockitoKotlin
|
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:' + versions.mockitoKotlin
|
||||||
testImplementation 'com.google.truth:truth:' + versions.truth
|
testImplementation 'com.google.truth:truth:' + versions.truth
|
||||||
|
|
||||||
|
androidTestImplementation 'com.android.support.test:runner:' + versions.testRunner
|
||||||
|
androidTestImplementation 'com.google.truth:truth:' + versions.truth
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: '../spotless.gradle'
|
apply from: '../spotless.gradle'
|
|
@ -0,0 +1,119 @@
|
||||||
|
/*
|
||||||
|
* Licensed under Apache-2.0
|
||||||
|
*
|
||||||
|
* Designed and developed by Aidan Follestad (@afollestad)
|
||||||
|
*/
|
||||||
|
package com.afollestad.nocknock.engine
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import android.support.test.runner.AndroidJUnit4
|
||||||
|
import com.afollestad.nocknock.data.ServerModel
|
||||||
|
import com.afollestad.nocknock.data.ServerStatus.CHECKING
|
||||||
|
import com.afollestad.nocknock.data.ServerStatus.ERROR
|
||||||
|
import com.afollestad.nocknock.data.ValidationMode.JAVASCRIPT
|
||||||
|
import com.afollestad.nocknock.data.ValidationMode.STATUS_CODE
|
||||||
|
import com.afollestad.nocknock.data.ValidationMode.TERM_SEARCH
|
||||||
|
import com.afollestad.nocknock.engine.db.RealServerModelStore
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import android.support.test.InstrumentationRegistry.getTargetContext as context
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class ServerModelStoreTest {
|
||||||
|
|
||||||
|
private lateinit var store: RealServerModelStore
|
||||||
|
|
||||||
|
@Before fun setup() {
|
||||||
|
store = RealServerModelStore(context().applicationContext as Application)
|
||||||
|
store.db()
|
||||||
|
.wipe()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun get() = runBlocking {
|
||||||
|
// Put some fake data to retrieve
|
||||||
|
store.put(fakeModel(1))
|
||||||
|
val model2 = store.put(fakeModel(2))
|
||||||
|
|
||||||
|
val model = store.get(2)
|
||||||
|
.single()
|
||||||
|
assertThat(model).isEqualTo(model2.copy(id = 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun getAll() = runBlocking {
|
||||||
|
// Put some fake data to retrieve
|
||||||
|
val model1 = store.put(fakeModel(1))
|
||||||
|
val model2 = store.put(fakeModel(2))
|
||||||
|
|
||||||
|
val models = store.get()
|
||||||
|
assertThat(models.size).isEqualTo(2)
|
||||||
|
assertThat(models[0]).isEqualTo(model1.copy(id = 1))
|
||||||
|
assertThat(models[1]).isEqualTo(model2.copy(id = 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun update() = runBlocking {
|
||||||
|
store.put(
|
||||||
|
ServerModel(
|
||||||
|
name = "Wakanda Forever",
|
||||||
|
url = "https://www.wakanda.gov",
|
||||||
|
status = ERROR,
|
||||||
|
checkInterval = 5,
|
||||||
|
lastCheck = 10,
|
||||||
|
reason = "Body doesn't contain your term.",
|
||||||
|
validationMode = TERM_SEARCH,
|
||||||
|
validationContent = "Vibranium",
|
||||||
|
disabled = false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
store.put(fakeModel(2))
|
||||||
|
|
||||||
|
val originalModel1 = store.get(id = 1)
|
||||||
|
.single()
|
||||||
|
|
||||||
|
val defaultJs = "var responseObj = JSON.parse(response);\\nreturn responseObj.success === true;"
|
||||||
|
val newModel1 = originalModel1.copy(
|
||||||
|
name = "HYDRA",
|
||||||
|
url = "https://www.hyrda.dict",
|
||||||
|
status = CHECKING,
|
||||||
|
checkInterval = 10,
|
||||||
|
lastCheck = 20,
|
||||||
|
reason = "Evaluation failed.",
|
||||||
|
validationMode = JAVASCRIPT,
|
||||||
|
validationContent = defaultJs,
|
||||||
|
disabled = true
|
||||||
|
)
|
||||||
|
assertThat(store.update(newModel1)).isEqualTo(1)
|
||||||
|
|
||||||
|
val newModels = store.get()
|
||||||
|
assertThat(newModels.size).isEqualTo(2)
|
||||||
|
assertThat(newModels.first()).isEqualTo(newModel1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun delete() = runBlocking {
|
||||||
|
// Put some fake data to delete
|
||||||
|
val model1 = store.put(fakeModel(1))
|
||||||
|
val model2 = store.put(fakeModel(2))
|
||||||
|
|
||||||
|
assertThat(store.delete(model1)).isEqualTo(1)
|
||||||
|
|
||||||
|
val newModels = store.get()
|
||||||
|
assertThat(newModels.single()).isEqualTo(model2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun deleteAll() = runBlocking {
|
||||||
|
// Put some fake data to delete
|
||||||
|
store.put(fakeModel(1))
|
||||||
|
store.put(fakeModel(2))
|
||||||
|
|
||||||
|
store.deleteAll()
|
||||||
|
assertThat(store.get()).isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fakeModel(index: Int) = ServerModel(
|
||||||
|
name = "Model $index",
|
||||||
|
url = "https://hello.com/$index",
|
||||||
|
validationMode = STATUS_CODE
|
||||||
|
)
|
||||||
|
}
|
|
@ -52,4 +52,9 @@ class ServerModelDbHelper(context: Context) : SQLiteOpenHelper(
|
||||||
oldVersion: Int,
|
oldVersion: Int,
|
||||||
newVersion: Int
|
newVersion: Int
|
||||||
) = onUpgrade(db, oldVersion, newVersion)
|
) = onUpgrade(db, oldVersion, newVersion)
|
||||||
|
|
||||||
|
fun wipe() {
|
||||||
|
this.writableDatabase.execSQL(SQL_DELETE_ENTRIES)
|
||||||
|
onCreate(this.writableDatabase)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,10 @@ import com.afollestad.nocknock.data.ServerModel.Companion.COLUMN_ID
|
||||||
import com.afollestad.nocknock.data.ServerModel.Companion.DEFAULT_SORT_ORDER
|
import com.afollestad.nocknock.data.ServerModel.Companion.DEFAULT_SORT_ORDER
|
||||||
import com.afollestad.nocknock.data.ServerModel.Companion.TABLE_NAME
|
import com.afollestad.nocknock.data.ServerModel.Companion.TABLE_NAME
|
||||||
import com.afollestad.nocknock.utilities.ext.diffFrom
|
import com.afollestad.nocknock.utilities.ext.diffFrom
|
||||||
|
import org.jetbrains.annotations.TestOnly
|
||||||
|
import javax.inject.Inject
|
||||||
import timber.log.Timber.d as log
|
import timber.log.Timber.d as log
|
||||||
import timber.log.Timber.w as warn
|
import timber.log.Timber.w as warn
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
/** @author Aidan Follestad (@afollestad) */
|
/** @author Aidan Follestad (@afollestad) */
|
||||||
interface ServerModelStore {
|
interface ServerModelStore {
|
||||||
|
@ -128,6 +129,8 @@ class RealServerModelStore @Inject constructor(app: Application) : ServerModelSt
|
||||||
return dbHelper.writableDatabase.delete(TABLE_NAME, null, null)
|
return dbHelper.writableDatabase.delete(TABLE_NAME, null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestOnly fun db() = dbHelper
|
||||||
|
|
||||||
private fun readModels(cursor: Cursor): List<ServerModel> {
|
private fun readModels(cursor: Cursor): List<ServerModel> {
|
||||||
val results = mutableListOf<ServerModel>()
|
val results = mutableListOf<ServerModel>()
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue