BackupManager tests

This commit is contained in:
Sad Ellie 2024-01-03 18:31:48 +03:00
parent d0fcf5b138
commit e638de6857
6 changed files with 180 additions and 160 deletions

View File

@ -25,8 +25,10 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStoreFile import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import com.sadellie.unitto.data.database.CalculatorHistoryEntity
import com.sadellie.unitto.data.database.TimeZoneEntity
import com.sadellie.unitto.data.database.UnitsEntity
import com.sadellie.unitto.data.userprefs.PrefsKeys import com.sadellie.unitto.data.userprefs.PrefsKeys
import com.sadellie.unitto.data.userprefs.getThemingMode
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.Assert.* import org.junit.Assert.*
@ -47,6 +49,7 @@ class BackupManagerTest {
@Before @Before
fun setup() { fun setup() {
// Inserting dummy data as app data (db and prefs)
val appContext = InstrumentationRegistry.getInstrumentation().targetContext val appContext = InstrumentationRegistry.getInstrumentation().targetContext
dataStore = PreferenceDataStoreFactory.create( dataStore = PreferenceDataStoreFactory.create(
@ -58,147 +61,111 @@ class BackupManagerTest {
mContext = appContext, mContext = appContext,
dataStore = dataStore, dataStore = dataStore,
unitsDao = FakeUnitsDao, unitsDao = FakeUnitsDao,
timeZoneDao = FakeTimeZoneDao timeZoneDao = FakeTimeZoneDao,
calculatorHistoryDao = FakeCalculatorHistoryDao
) )
runBlocking { runBlocking {
dataStore.edit { dataStore.edit {
it[PrefsKeys.THEMING_MODE] = FakeUsrPreferenceValues.themingMode it[PrefsKeys.THEMING_MODE] = fakeUserData.themingMode
it[PrefsKeys.ENABLE_DYNAMIC_THEME] = FakeUsrPreferenceValues.enableDynamicTheme it[PrefsKeys.ENABLE_DYNAMIC_THEME] = fakeUserData.enableDynamicTheme
it[PrefsKeys.ENABLE_AMOLED_THEME] = FakeUsrPreferenceValues.enableAmoledTheme it[PrefsKeys.ENABLE_AMOLED_THEME] = fakeUserData.enableAmoledTheme
it[PrefsKeys.CUSTOM_COLOR] = FakeUsrPreferenceValues.customColor it[PrefsKeys.CUSTOM_COLOR] = fakeUserData.customColor
it[PrefsKeys.MONET_MODE] = FakeUsrPreferenceValues.monetMode it[PrefsKeys.MONET_MODE] = fakeUserData.monetMode
it[PrefsKeys.STARTING_SCREEN] = FakeUsrPreferenceValues.startingScreen it[PrefsKeys.STARTING_SCREEN] = fakeUserData.startingScreen
it[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] = FakeUsrPreferenceValues.enableToolsExperiment it[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] = fakeUserData.enableToolsExperiment
it[PrefsKeys.ENABLE_VIBRATIONS] = FakeUsrPreferenceValues.enableVibrations it[PrefsKeys.SYSTEM_FONT] = fakeUserData.systemFont
it[PrefsKeys.MIDDLE_ZERO] = FakeUsrPreferenceValues.middleZero it[PrefsKeys.ENABLE_VIBRATIONS] = fakeUserData.enableVibrations
it[PrefsKeys.AC_BUTTON] = FakeUsrPreferenceValues.acButton it[PrefsKeys.MIDDLE_ZERO] = fakeUserData.middleZero
it[PrefsKeys.RPN_MODE] = FakeUsrPreferenceValues.rpnMode it[PrefsKeys.AC_BUTTON] = fakeUserData.acButton
it[PrefsKeys.RPN_MODE] = fakeUserData.rpnMode
// FORMATTER it[PrefsKeys.DIGITS_PRECISION] = fakeUserData.precision
it[PrefsKeys.DIGITS_PRECISION] = FakeUsrPreferenceValues.precision it[PrefsKeys.SEPARATOR] = fakeUserData.separator
it[PrefsKeys.SEPARATOR] = FakeUsrPreferenceValues.separator it[PrefsKeys.OUTPUT_FORMAT] = fakeUserData.outputFormat
it[PrefsKeys.OUTPUT_FORMAT] = FakeUsrPreferenceValues.outputFormat it[PrefsKeys.RADIAN_MODE] = fakeUserData.radianMode
it[PrefsKeys.PARTIAL_HISTORY_VIEW] = fakeUserData.partialHistoryView
// CALCULATOR it[PrefsKeys.LATEST_LEFT_SIDE] = fakeUserData.latestLeftSide
it[PrefsKeys.RADIAN_MODE] = FakeUsrPreferenceValues.radianMode it[PrefsKeys.LATEST_RIGHT_SIDE] = fakeUserData.latestRightSide
it[PrefsKeys.PARTIAL_HISTORY_VIEW] = FakeUsrPreferenceValues.partialHistoryView it[PrefsKeys.SHOWN_UNIT_GROUPS] = fakeUserData.shownUnitGroups
it[PrefsKeys.UNIT_CONVERTER_FAVORITES_ONLY] = fakeUserData.unitConverterFavoritesOnly
// UNIT CONVERTER it[PrefsKeys.UNIT_CONVERTER_FORMAT_TIME] = fakeUserData.unitConverterFormatTime
it[PrefsKeys.LATEST_LEFT_SIDE] = FakeUsrPreferenceValues.latestLeftSide it[PrefsKeys.UNIT_CONVERTER_SORTING] = fakeUserData.unitConverterSorting
it[PrefsKeys.LATEST_RIGHT_SIDE] = FakeUsrPreferenceValues.latestRightSide
it[PrefsKeys.SHOWN_UNIT_GROUPS] = FakeUsrPreferenceValues.shownUnitGroups.joinToString(",")
it[PrefsKeys.UNIT_CONVERTER_FAVORITES_ONLY] = FakeUsrPreferenceValues.unitConverterFavoritesOnly
it[PrefsKeys.UNIT_CONVERTER_FORMAT_TIME] = FakeUsrPreferenceValues.unitConverterFormatTime
it[PrefsKeys.UNIT_CONVERTER_SORTING] = FakeUsrPreferenceValues.unitConverterSorting.name
} }
} }
} }
@Test @Test
fun getUserDataTest() = runBlocking{ fun testBackup() = runBlocking{
// Backup manager also saves the file to disk, but it is not tested here.
// There is probably no need to test if the data in app is valid since moshi will throw an
// exceptions if the data in app is invalid. For example, if unitConverterSorting is set to
// "Qwerty" (this sorting doesn't exist) there will be an exception.
val actualUserData = backupManager.userDataFromApp() val actualUserData = backupManager.userDataFromApp()
val expectedUserData = UserData( val expectedUserData = fakeUserData
themingMode = FakeUsrPreferenceValues.themingMode,
enableDynamicTheme = FakeUsrPreferenceValues.enableDynamicTheme,
enableAmoledTheme = FakeUsrPreferenceValues.enableAmoledTheme,
customColor = FakeUsrPreferenceValues.customColor,
monetMode = FakeUsrPreferenceValues.monetMode,
startingScreen = FakeUsrPreferenceValues.startingScreen,
enableToolsExperiment = FakeUsrPreferenceValues.enableToolsExperiment,
enableVibrations = FakeUsrPreferenceValues.enableVibrations,
middleZero = FakeUsrPreferenceValues.middleZero,
acButton = FakeUsrPreferenceValues.acButton,
rpnMode = FakeUsrPreferenceValues.rpnMode,
precision = FakeUsrPreferenceValues.precision,
separator = FakeUsrPreferenceValues.separator,
outputFormat = FakeUsrPreferenceValues.outputFormat,
radianMode = FakeUsrPreferenceValues.radianMode,
partialHistoryView = FakeUsrPreferenceValues.partialHistoryView,
latestLeftSide = FakeUsrPreferenceValues.latestLeftSide,
latestRightSide = FakeUsrPreferenceValues.latestRightSide,
shownUnitGroups = FakeUsrPreferenceValues.shownUnitGroups,
unitConverterFavoritesOnly = FakeUsrPreferenceValues.unitConverterFavoritesOnly,
unitConverterFormatTime = FakeUsrPreferenceValues.unitConverterFormatTime,
unitConverterSorting = FakeUsrPreferenceValues.unitConverterSorting,
unitsTable = units,
timeZoneTable = timeZones
)
assertEquals(expectedUserData, actualUserData) assertEquals(expectedUserData, actualUserData)
} }
@Test @Test
fun updateDatastoreTest() = runBlocking{ fun testRestoreDatastore() = runBlocking{
backupManager.updateDatastore( // Backup manager also saves the file to disk, but it is not tested here.
UserData( // There is probably no need to test if the data in app is valid since moshi will throw an
themingMode = FakeUsrPreferenceValues.themingMode, // exceptions if the data in app is invalid. For example, if unitConverterSorting is set to
enableDynamicTheme = FakeUsrPreferenceValues.enableDynamicTheme, // "Qwerty" (this sorting doesn't exist) there will be an exception.
enableAmoledTheme = FakeUsrPreferenceValues.enableAmoledTheme, backupManager.updateDatastore(fakeUserData)
customColor = FakeUsrPreferenceValues.customColor,
monetMode = FakeUsrPreferenceValues.monetMode,
startingScreen = FakeUsrPreferenceValues.startingScreen,
enableToolsExperiment = FakeUsrPreferenceValues.enableToolsExperiment,
enableVibrations = FakeUsrPreferenceValues.enableVibrations,
middleZero = FakeUsrPreferenceValues.middleZero,
acButton = FakeUsrPreferenceValues.acButton,
rpnMode = FakeUsrPreferenceValues.rpnMode,
precision = FakeUsrPreferenceValues.precision,
separator = FakeUsrPreferenceValues.separator,
outputFormat = FakeUsrPreferenceValues.outputFormat,
radianMode = FakeUsrPreferenceValues.radianMode,
partialHistoryView = FakeUsrPreferenceValues.partialHistoryView,
clearInputAfterEquals = FakeUsrPreferenceValues.clearInputAfterEquals,
latestLeftSide = FakeUsrPreferenceValues.latestLeftSide,
latestRightSide = FakeUsrPreferenceValues.latestRightSide,
shownUnitGroups = FakeUsrPreferenceValues.shownUnitGroups,
unitConverterFavoritesOnly = FakeUsrPreferenceValues.unitConverterFavoritesOnly,
unitConverterFormatTime = FakeUsrPreferenceValues.unitConverterFormatTime,
unitConverterSorting = FakeUsrPreferenceValues.unitConverterSorting,
unitsTable = units,
timeZoneTable = timeZones
)
)
val data = dataStore.data.first() assertEquals(backupManager.userDataFromApp(), fakeUserData)
// TODO Wrong implementation, should test all
assertEquals(FakeUsrPreferenceValues.themingMode, data.getThemingMode())
} }
@Test @Test
fun updateUnitsTableTest() = runBlocking { fun testRestoreCalculatorHistoryTable() = runBlocking {
backupManager.updateUnitsTable( // Data for import
UserData( val fakeUserData2 = fakeUserData.copy(
themingMode = FakeUsrPreferenceValues.themingMode, calculatorHistoryTable = listOf(
enableDynamicTheme = FakeUsrPreferenceValues.enableDynamicTheme, CalculatorHistoryEntity(
enableAmoledTheme = FakeUsrPreferenceValues.enableAmoledTheme, timestamp = System.currentTimeMillis(),
customColor = FakeUsrPreferenceValues.customColor, expression = "69+420",
monetMode = FakeUsrPreferenceValues.monetMode, result = "444"
startingScreen = FakeUsrPreferenceValues.startingScreen, )
enableToolsExperiment = FakeUsrPreferenceValues.enableToolsExperiment,
enableVibrations = FakeUsrPreferenceValues.enableVibrations,
middleZero = FakeUsrPreferenceValues.middleZero,
acButton = FakeUsrPreferenceValues.acButton,
rpnMode = FakeUsrPreferenceValues.rpnMode,
precision = FakeUsrPreferenceValues.precision,
separator = FakeUsrPreferenceValues.separator,
outputFormat = FakeUsrPreferenceValues.outputFormat,
radianMode = FakeUsrPreferenceValues.radianMode,
partialHistoryView = FakeUsrPreferenceValues.partialHistoryView,
clearInputAfterEquals = FakeUsrPreferenceValues.clearInputAfterEquals,
latestLeftSide = FakeUsrPreferenceValues.latestLeftSide,
latestRightSide = FakeUsrPreferenceValues.latestRightSide,
shownUnitGroups = FakeUsrPreferenceValues.shownUnitGroups,
unitConverterFavoritesOnly = FakeUsrPreferenceValues.unitConverterFavoritesOnly,
unitConverterFormatTime = FakeUsrPreferenceValues.unitConverterFormatTime,
unitConverterSorting = FakeUsrPreferenceValues.unitConverterSorting,
unitsTable = emptyList(),
timeZoneTable = timeZones
) )
) )
backupManager.updateCalculatorHistoryTable(fakeUserData2)
val data = FakeUnitsDao.getAllFlow().first() assertEquals(FakeCalculatorHistoryDao.getAllDescending().first(), fakeUserData2.calculatorHistoryTable)
// TODO Wrong implementation }
assertEquals("$units | $data", units, data)
@Test
fun testRestoreUnitsTable() = runBlocking {
// Data for import
val fakeUserData2 = fakeUserData.copy(
unitsTable = listOf(
UnitsEntity(
unitId = "UnitId3",
isFavorite = false,
pairedUnitId = "Pair4",
frequency = 123
)
)
)
backupManager.updateUnitsTable(fakeUserData2)
assertEquals(FakeUnitsDao.getAllFlow().first(), fakeUserData2.unitsTable)
}
@Test
fun testRestoreTimeZonesTable() = runBlocking {
// Data for import
val fakeUserData2 = fakeUserData.copy(
timeZoneTable = listOf(
TimeZoneEntity(
id = "id3",
position = 123,
label = "label456"
)
)
)
backupManager.updateTimeZonesTable(fakeUserData2)
assertEquals(FakeTimeZoneDao.getFavorites().first(), fakeUserData2.timeZoneTable)
} }
} }

View File

@ -0,0 +1,46 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.data.backup
import com.sadellie.unitto.data.database.CalculatorHistoryDao
import com.sadellie.unitto.data.database.CalculatorHistoryEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
var calculatorHistory: List<CalculatorHistoryEntity> = listOf(
CalculatorHistoryEntity(
timestamp = System.currentTimeMillis(),
expression = "2+2",
result = "4"
)
)
object FakeCalculatorHistoryDao: CalculatorHistoryDao {
override fun getAllDescending(): Flow<List<CalculatorHistoryEntity>> = flow {
emit(calculatorHistory)
}
override suspend fun insert(vararg historyEntity: CalculatorHistoryEntity) {
calculatorHistory += historyEntity
}
override suspend fun clear() {
calculatorHistory = emptyList()
}
}

View File

@ -23,7 +23,7 @@ import com.sadellie.unitto.data.database.TimeZoneEntity
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
val timeZones = listOf( var timeZones = listOf(
TimeZoneEntity( TimeZoneEntity(
id = "id1", id = "id1",
position = 9, position = 9,
@ -37,18 +37,20 @@ val timeZones = listOf(
) )
object FakeTimeZoneDao: TimeZoneDao { object FakeTimeZoneDao: TimeZoneDao {
override fun getFavorites(): Flow<List<TimeZoneEntity>> { override fun getFavorites(): Flow<List<TimeZoneEntity>> = flow {
return flow { emit(timeZones)
emit(timeZones)
}
} }
override fun getMaxPosition(): Int = 0 override fun getMaxPosition(): Int = 0
override suspend fun insert(vararg timeZoneEntity: TimeZoneEntity) {} override suspend fun insert(vararg timeZoneEntity: TimeZoneEntity) {
timeZones += timeZoneEntity
}
override suspend fun removeFromFavorites(id: String) {} override suspend fun removeFromFavorites(id: String) {}
override suspend fun updateLabel(id: String, label: String) {} override suspend fun updateLabel(id: String, label: String) {}
override suspend fun updateDragged(id: String, oldPosition: Int, newPosition: Int) {} override suspend fun updateDragged(id: String, oldPosition: Int, newPosition: Int) {}
override suspend fun moveDown(currentPosition: Int, targetPosition: Int) {} override suspend fun moveDown(currentPosition: Int, targetPosition: Int) {}
override suspend fun moveUp(currentPosition: Int, targetPosition: Int) {} override suspend fun moveUp(currentPosition: Int, targetPosition: Int) {}
override suspend fun clear() {} override suspend fun clear() {
timeZones = emptyList()
}
} }

View File

@ -23,7 +23,7 @@ import com.sadellie.unitto.data.database.UnitsEntity
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
val units = listOf( var units = listOf(
UnitsEntity( UnitsEntity(
unitId = "UnitId1", unitId = "UnitId1",
isFavorite = false, isFavorite = false,
@ -45,7 +45,11 @@ object FakeUnitsDao : UnitsDao {
} }
} }
override suspend fun insertUnit(unit: UnitsEntity) {} override suspend fun insertUnit(unit: UnitsEntity) {
units += unit
}
override suspend fun getById(unitId: String): UnitsEntity? = null override suspend fun getById(unitId: String): UnitsEntity? = null
override suspend fun clear() {} override suspend fun clear() {
units = emptyList()
}
} }

View File

@ -18,31 +18,31 @@
package com.sadellie.unitto.data.backup package com.sadellie.unitto.data.backup
import com.sadellie.unitto.data.model.ALL_UNIT_GROUPS internal val fakeUserData = UserData(
import com.sadellie.unitto.data.model.UnitsListSorting themingMode = "AUTO",
enableDynamicTheme = false,
object FakeUsrPreferenceValues { enableAmoledTheme = false,
const val themingMode = "ThemingMode" customColor = 777L,
const val enableDynamicTheme = false monetMode = "TonalSpot",
const val enableAmoledTheme = false startingScreen = "calculator_route",
const val customColor = 777L enableToolsExperiment = false,
const val monetMode = "MonetMode" systemFont = false,
const val startingScreen = "StartingScreen" enableVibrations = false,
const val enableToolsExperiment = false middleZero = false,
const val enableVibrations = false acButton = false,
const val middleZero = false rpnMode = false,
const val acButton = false precision = 11,
const val rpnMode = false separator = 1,
const val precision = 69 outputFormat = 1,
const val separator = 1 radianMode = false,
const val outputFormat = 1 partialHistoryView = false,
const val radianMode = false latestLeftSide = "kilometer",
const val partialHistoryView = false latestRightSide = "mile",
const val clearInputAfterEquals = false shownUnitGroups = "LENGTH",
const val latestLeftSide = "LeftSideUnit" unitConverterFavoritesOnly = false,
const val latestRightSide = "RightSideUnit" unitConverterFormatTime = false,
val shownUnitGroups = ALL_UNIT_GROUPS unitConverterSorting = "USAGE",
const val unitConverterFavoritesOnly = false calculatorHistoryTable = calculatorHistory,
const val unitConverterFormatTime = false unitsTable = units,
val unitConverterSorting = UnitsListSorting.USAGE timeZoneTable = timeZones
} )

View File

@ -99,7 +99,8 @@ class BackupManager @Inject constructor(
} }
// Return error // Return error
val userData: UserData = jsonAdapter.fromJson(jsonContent.toString()) ?: return@withContext IllegalArgumentException("Can't parse: $jsonContent") val userData: UserData = jsonAdapter.fromJson(jsonContent.toString())
?: return@withContext IllegalArgumentException("Can't parse: $jsonContent")
// Apply tables // Apply tables
updateCalculatorHistoryTable(userData) updateCalculatorHistoryTable(userData)