Add unit tests

This commit is contained in:
Kirill Kamakin
2022-10-31 19:41:49 +01:00
parent 98e082b95e
commit 7c02df4d30
27 changed files with 903 additions and 180 deletions

View File

@@ -13,6 +13,7 @@ import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
import gq.kirmanak.mealient.extensions.*
import gq.kirmanak.mealient.logging.Logger
import javax.inject.Inject
import javax.inject.Singleton
@@ -22,6 +23,7 @@ class MealieDataSourceWrapper @Inject constructor(
private val authRepo: AuthRepo,
private val v0Source: MealieDataSourceV0,
private val v1Source: MealieDataSourceV1,
private val logger: Logger,
) : AddRecipeDataSource, RecipeDataSource {
override suspend fun addRecipe(
@@ -68,9 +70,11 @@ class MealieDataSourceWrapper @Inject constructor(
val version = serverInfoRepo.getVersion()
return runCatchingExceptCancel { block(authHeader, url, version) }.getOrElse {
if (it is NetworkError.Unauthorized) {
logger.e { "Unauthorized, trying to invalidate token" }
authRepo.invalidateAuthHeader()
// Trying again with new authentication header
val newHeader = authRepo.getAuthHeader()
logger.e { "New token ${if (newHeader == authHeader) "matches" else "doesn't match"} old token" }
if (newHeader == authHeader) throw it else block(newHeader, url, version)
} else {
throw it

View File

@@ -8,10 +8,10 @@ import gq.kirmanak.mealient.database.AppDb
import gq.kirmanak.mealient.database.recipe.RecipeDao
import gq.kirmanak.mealient.database.recipe.entity.FullRecipeEntity
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
import gq.kirmanak.mealient.extensions.recipeEntity
import gq.kirmanak.mealient.extensions.toRecipeEntity
import gq.kirmanak.mealient.extensions.toRecipeIngredientEntity
import gq.kirmanak.mealient.extensions.toRecipeInstructionEntity
import gq.kirmanak.mealient.extensions.toRecipeSummaryEntity
import gq.kirmanak.mealient.logging.Logger
import javax.inject.Inject
import javax.inject.Singleton
@@ -29,7 +29,7 @@ class RecipeStorageImpl @Inject constructor(
logger.v { "saveRecipes() called with $recipes" }
for (recipe in recipes) {
val recipeSummaryEntity = recipe.recipeEntity()
val recipeSummaryEntity = recipe.toRecipeSummaryEntity()
recipeDao.insertRecipe(recipeSummaryEntity)
}
}

View File

@@ -53,7 +53,7 @@ fun GetRecipeSummaryResponseV1.toRecipeSummaryInfo() = RecipeSummaryInfo(
imageId = remoteId,
)
fun RecipeSummaryInfo.recipeEntity() = RecipeSummaryEntity(
fun RecipeSummaryInfo.toRecipeSummaryEntity() = RecipeSummaryEntity(
remoteId = remoteId,
name = name,
slug = slug,