Skip to content

Commit

Permalink
NSClientV3PluginTest
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Jan 4, 2023
1 parent d190b53 commit eb1fff5
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ interface NsClient : Sync {

fun resetToFullSync()

fun dbAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String)
fun dbUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String)
fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String)
fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String)
}
1 change: 1 addition & 0 deletions plugins/sync/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation project(':core:utils')
implementation project(':core:validators')

testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation "androidx.work:work-testing:$work_version"
testImplementation project(':implementation')
testImplementation project(':plugins:aps')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
bolus.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
activePlugin.activeNsClient?.nsAdd(
"treatments",
DataSyncSelector.PairBolus(bolus.first, bolus.second.id),
" $startId/$lastDbId"
)
// with nsId = update if it's modified record
bolus.first.interfaceIDs.nightscoutId != null && bolus.first.id != bolus.second.id ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairBolus(bolus.first, bolus.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -188,10 +188,10 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
carb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairCarbs(carb.first, carb.second.id), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairCarbs(carb.first, carb.second.id), "$startId/$lastDbId")
// with nsId = update if it's modified record
carb.first.interfaceIDs.nightscoutId != null && carb.first.id != carb.second.id ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairCarbs(carb.first, carb.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -237,14 +237,14 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
bolusCalculatorResult.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
activePlugin.activeNsClient?.nsAdd(
"treatments",
DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id),
"$startId/$lastDbId"
)
// with nsId = update if it's modified record
bolusCalculatorResult.first.interfaceIDs.nightscoutId != null && bolusCalculatorResult.first.id != bolusCalculatorResult.second.id ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id), "$startId/$lastDbId"
)
Expand Down Expand Up @@ -289,14 +289,14 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
tt.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
activePlugin.activeNsClient?.nsAdd(
"treatments",
DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id),
"$startId/$lastDbId"
)
// existing with nsId = update
tt.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -342,10 +342,10 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
food.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("food", DataSyncSelector.PairFood(food.first, food.second.id), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("food", DataSyncSelector.PairFood(food.first, food.second.id), "$startId/$lastDbId")
// with nsId = update
food.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"food",
DataSyncSelector.PairFood(food.first, food.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -392,10 +392,10 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
gv.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("entries", DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("entries", DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id), "$startId/$lastDbId")
// with nsId = update
else -> // gv.first.interfaceIDs.nightscoutId != null
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"entries",
DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -445,10 +445,10 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
te.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairTherapyEvent(te.first, te.second.id), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairTherapyEvent(te.first, te.second.id), "$startId/$lastDbId")
// nsId = update
te.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairTherapyEvent(te.first, te.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -480,7 +480,7 @@ class DataSyncSelectorImplementation @Inject constructor(
when {
// without nsId = create new
deviceStatus.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("devicestatus", DataSyncSelector.PairDeviceStatus(deviceStatus, 0), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("devicestatus", DataSyncSelector.PairDeviceStatus(deviceStatus, 0), "$startId/$lastDbId")
// with nsId = ignore
deviceStatus.interfaceIDs.nightscoutId != null -> Any()
}
Expand Down Expand Up @@ -524,14 +524,14 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
tb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
activePlugin.activeNsClient?.nsAdd(
"treatments",
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
// with nsId = update
tb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -579,14 +579,14 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
eb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
activePlugin.activeNsClient?.nsAdd(
"treatments",
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId"
)
// with nsId = update
eb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -638,10 +638,10 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
ps.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id), "$startId/$lastDbId")
// with nsId = update
ps.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -687,14 +687,14 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
ps.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
activePlugin.activeNsClient?.nsAdd(
"treatments",
DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId"
)
// with nsId = update
ps.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId"
Expand Down Expand Up @@ -740,10 +740,10 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
oe.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id), "$startId/$lastDbId")
activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id), "$startId/$lastDbId")
// existing with nsId = update
oe.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
activePlugin.activeNsClient?.nsUpdate(
"treatments",
DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id),
"$startId/$lastDbId"
Expand All @@ -765,7 +765,7 @@ class DataSyncSelectorImplementation @Inject constructor(
if (lastChange > lastSync) {
if (activePlugin.activeProfileSource.profile?.allProfilesValid != true) return
val profileJson = activePlugin.activeProfileSource.profile?.data ?: return
activePlugin.activeNsClient?.dbAdd("profile", DataSyncSelector.PairProfileStore(profileJson, dateUtil.now()), "")
activePlugin.activeNsClient?.nsAdd("profile", DataSyncSelector.PairProfileStore(profileJson, dateUtil.now()), "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class NSClientPlugin @Inject constructor(
dataSyncSelector.resetToNextFullSync()
}

override fun dbAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
override fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
when (dataPair) {
is DataSyncSelector.PairBolus -> dataPair.value.toJson(true, dateUtil)
is DataSyncSelector.PairCarbs -> dataPair.value.toJson(true, dateUtil)
Expand All @@ -266,7 +266,7 @@ class NSClientPlugin @Inject constructor(
}
}

override fun dbUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
override fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
val id = when (dataPair) {
is DataSyncSelector.PairBolus -> dataPair.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairCarbs -> dataPair.value.interfaceIDs.nightscoutId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package info.nightscout.plugins.sync.nsclient

import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.interfaces.receivers.ReceiverStatusStore
import info.nightscout.plugins.sync.R
import info.nightscout.rx.bus.RxBus
Expand All @@ -11,6 +12,7 @@ import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject
import javax.inject.Singleton

@OpenForTesting
@Singleton
class NsClientReceiverDelegate @Inject constructor(
private val rxBus: RxBus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class NSClientV3Plugin @Inject constructor(
}

private val disposable = CompositeDisposable()
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
var scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private lateinit var runLoop: Runnable
private val handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
private val listLog: MutableList<EventNSClientNewLog> = ArrayList()
Expand Down Expand Up @@ -320,11 +320,11 @@ class NSClientV3Plugin @Inject constructor(
storeLastLoadedSrvModified()
}

override fun dbAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
override fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
dbOperation(collection, dataPair, progress, Operation.CREATE)
}

override fun dbUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
override fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String) {
dbOperation(collection, dataPair, progress, Operation.UPDATE)
}

Expand Down
Loading

0 comments on commit eb1fff5

Please sign in to comment.