fix tests

This commit is contained in:
Milos Kozak 2022-05-03 20:33:34 +02:00
parent bb8c799c6f
commit 787bd4a074
4 changed files with 6 additions and 122 deletions

View file

@ -37,16 +37,6 @@ class Persistence @Inject constructor(
} }
// For mocking only
fun base64decode(str: String, flags: Int): ByteArray {
return Base64.decode(str, flags)
}
// For mocking only
fun base64encodeToString(input: ByteArray, flags: Int): String {
return Base64.encodeToString(input, flags)
}
fun getString(key: String, defaultValue: String): String { fun getString(key: String, defaultValue: String): String {
return sp.getString(key, defaultValue) return sp.getString(key, defaultValue)
} }
@ -177,7 +167,7 @@ class Persistence @Inject constructor(
fun explodeSet(joined: String, separator: String): MutableSet<String> { fun explodeSet(joined: String, separator: String): MutableSet<String> {
// special RegEx literal \\Q starts sequence we escape, \\E ends is // special RegEx literal \\Q starts sequence we escape, \\E ends is
// we use it to escape separator for use in RegEx // we use it to escape separator for use in RegEx
val items = joined.split("\\Q$separator\\E").toTypedArray() val items = joined.split(Regex("\\Q$separator\\E")).toTypedArray()
val set: MutableSet<String> = HashSet() val set: MutableSet<String> = HashSet()
for (item in items) { for (item in items) {
val itemToAdd = item.trim { it <= ' ' } val itemToAdd = item.trim { it <= ' ' }

View file

@ -6,7 +6,6 @@ import android.os.PowerManager;
import com.google.android.gms.wearable.DataMap; import com.google.android.gms.wearable.DataMap;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -32,7 +31,6 @@ public class WearUtil {
private final boolean debug_wakelocks = false; private final boolean debug_wakelocks = false;
private final Map<String, Long> rateLimits = new HashMap<>(); private final Map<String, Long> rateLimits = new HashMap<>();
private final String TAG = WearUtil.class.getName();
//============================================================================================== //==============================================================================================
// Time related util methods // Time related util methods

View file

@ -6,9 +6,9 @@ import info.nightscout.androidaps.interaction.utils.Persistence
import info.nightscout.androidaps.interaction.utils.WearUtil import info.nightscout.androidaps.interaction.utils.WearUtil
import info.nightscout.androidaps.testing.mockers.WearUtilMocker import info.nightscout.androidaps.testing.mockers.WearUtilMocker
import info.nightscout.androidaps.testing.mocks.SharedPreferencesMock import info.nightscout.androidaps.testing.mocks.SharedPreferencesMock
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
import info.nightscout.shared.logging.AAPSLoggerTest import info.nightscout.shared.logging.AAPSLoggerTest
import info.nightscout.shared.sharedPreferences.SP
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.mockito.ArgumentMatchers import org.mockito.ArgumentMatchers
@ -21,9 +21,10 @@ import java.util.*
open class TestBase { open class TestBase {
@Mock lateinit var context: Context @Mock lateinit var context: Context
@Mock lateinit var sp: SP
@Mock lateinit var dateUtil: DateUtil
val aapsLogger = AAPSLoggerTest() val aapsLogger = AAPSLoggerTest()
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
val wearUtil: WearUtil = Mockito.spy(WearUtil()) val wearUtil: WearUtil = Mockito.spy(WearUtil())
val wearUtilMocker = WearUtilMocker(wearUtil) val wearUtilMocker = WearUtilMocker(wearUtil)
@ -47,21 +48,8 @@ open class TestBase {
wearUtilMocker.prepareMockNoReal() wearUtilMocker.prepareMockNoReal()
wearUtil.aapsLogger = aapsLogger wearUtil.aapsLogger = aapsLogger
wearUtil.context = context wearUtil.context = context
persistence = Mockito.spy(Persistence(context, aapsLogger, wearUtil)) persistence = Mockito.spy(Persistence(aapsLogger, dateUtil, sp))
Mockito.doAnswer { invocation ->
val payload = invocation.getArgument<String>(0)
try {
return@doAnswer Base64.getDecoder().decode(payload)
} catch (ex: IllegalArgumentException) {
return@doAnswer null
}
}.`when`(persistence).base64decode(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())
Mockito.doAnswer { invocation ->
val payload = invocation.getArgument<ByteArray>(0)
Base64.getEncoder().encodeToString(payload)
}.`when`(persistence).base64encodeToString(ArgumentMatchers.any(), ArgumentMatchers.anyInt())
} }
// Add a JUnit rule that will setup the @Mock annotated vars and log. // Add a JUnit rule that will setup the @Mock annotated vars and log.

View file

@ -1,92 +0,0 @@
package info.nightscout.androidaps.interaction.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Set;
import info.nightscout.androidaps.TestBase;
@SuppressWarnings("SpellCheckingInspection")
public class PersistenceTest extends TestBase {
@Test
public void putStringTest() {
// WHEN
final String emptyGot = persistence.getString("test-key", "default-value");
persistence.putString("test-key", "newValue");
final String updatedGot = persistence.getString("test-key", "another-default-value");
// THEN
assertEquals(emptyGot, "default-value");
assertEquals(updatedGot, "newValue");
}
@Test
public void putBooleanTest() {
// WHEN
final boolean emptyGot = persistence.getBoolean("test-key", false);
persistence.putBoolean("test-key", true);
final boolean updatedGot = persistence.getBoolean("test-key", false);
// THEN
assertFalse(emptyGot);
assertTrue(updatedGot);
}
@Test
public void setsTest() {
// WHEN
Set<String> emptySet = persistence.getSetOf("some fake id");
persistence.addToSet("test-set", "element1");
persistence.addToSet("test-set", "second-elem");
persistence.addToSet("test-set", "3rd");
persistence.addToSet("test-set", "czwarty");
persistence.addToSet("test-set", "V");
persistence.addToSet("test-set", "6");
Set<String> initialSet = persistence.getSetOf("test-set");
Set<String> sameInitialSet = persistence.getSetOf("test-set");
persistence.addToSet("test-set", "second-elem");
persistence.addToSet("test-set", "new-one");
Set<String> extendedSet = persistence.getSetOf("test-set");
persistence.removeFromSet("test-set", "czwarty");
persistence.removeFromSet("test-set", "6");
persistence.removeFromSet("test-set", "3rd");
Set<String> reducedSet = persistence.getSetOf("test-set");
// THEN
assertEquals(emptySet.size(), 0);
assertEquals(initialSet.size(), 6);
assertTrue(initialSet.contains("element1"));
assertTrue(initialSet.contains("second-elem"));
assertTrue(initialSet.contains("3rd"));
assertTrue(initialSet.contains("czwarty"));
assertTrue(initialSet.contains("V"));
assertTrue(initialSet.contains("6"));
assertEquals(initialSet, sameInitialSet);
assertEquals(extendedSet.size(), 7);
assertTrue(extendedSet.contains("new-one"));
assertEquals(reducedSet.size(), 4);
assertTrue(reducedSet.contains("element1"));
assertTrue(reducedSet.contains("second-elem"));
assertFalse(reducedSet.contains("3rd"));
assertFalse(reducedSet.contains("czwarty"));
assertTrue(reducedSet.contains("V"));
assertFalse(reducedSet.contains("6"));
assertTrue(reducedSet.contains("new-one"));
}
}