Add saveRecipeInfo tests

This commit is contained in:
Kirill Kamakin
2021-11-20 00:07:01 +03:00
parent d64a49b31f
commit 2f825361cd
3 changed files with 253 additions and 10 deletions

View File

@@ -1,6 +1,13 @@
package gq.kirmanak.mealie.data.recipes
import gq.kirmanak.mealie.data.recipes.db.entity.RecipeEntity
import gq.kirmanak.mealie.data.recipes.db.entity.RecipeIngredientEntity
import gq.kirmanak.mealie.data.recipes.db.entity.RecipeInstructionEntity
import gq.kirmanak.mealie.data.recipes.db.entity.RecipeSummaryEntity
import gq.kirmanak.mealie.data.recipes.impl.FullRecipeInfo
import gq.kirmanak.mealie.data.recipes.network.response.GetRecipeIngredientResponse
import gq.kirmanak.mealie.data.recipes.network.response.GetRecipeInstructionResponse
import gq.kirmanak.mealie.data.recipes.network.response.GetRecipeResponse
import gq.kirmanak.mealie.data.recipes.network.response.GetRecipeSummaryResponse
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
@@ -67,7 +74,7 @@ object RecipeImplTestData {
{"detail":"Unauthorized"}
"""
val CAKE_RECIPE_ENTITY = RecipeSummaryEntity(
val CAKE_RECIPE_SUMMARY_ENTITY = RecipeSummaryEntity(
remoteId = 1,
name = "Cake",
slug = "cake",
@@ -78,7 +85,7 @@ object RecipeImplTestData {
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13")
)
val PORRIDGE_RECIPE_ENTITY = RecipeSummaryEntity(
val PORRIDGE_RECIPE_SUMMARY_ENTITY = RecipeSummaryEntity(
remoteId = 2,
name = "Porridge",
slug = "porridge",
@@ -89,7 +96,7 @@ object RecipeImplTestData {
dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"),
)
val TEST_RECIPE_ENTITIES = listOf(CAKE_RECIPE_ENTITY, PORRIDGE_RECIPE_ENTITY)
val TEST_RECIPE_ENTITIES = listOf(CAKE_RECIPE_SUMMARY_ENTITY, PORRIDGE_RECIPE_SUMMARY_ENTITY)
fun MockWebServer.enqueueSuccessfulRecipeSummaryResponse() {
val response = MockResponse()
@@ -106,4 +113,186 @@ object RecipeImplTestData {
.setResponseCode(401)
enqueue(response)
}
val SUGAR_INGREDIENT = GetRecipeIngredientResponse(
title = "Sugar",
note = "2 oz of white sugar",
unit = "",
food = "",
disableAmount = true,
quantity = 1
)
val BREAD_INGREDIENT = GetRecipeIngredientResponse(
title = "Bread",
note = "2 oz of white bread",
unit = "",
food = "",
disableAmount = false,
quantity = 2
)
val MILK_INGREDIENT = GetRecipeIngredientResponse(
title = "Milk",
note = "2 oz of white milk",
unit = "",
food = "",
disableAmount = true,
quantity = 3
)
val MIX_INSTRUCTION = GetRecipeInstructionResponse(
title = "Mix",
text = "Mix the ingredients"
)
val BAKE_INSTRUCTION = GetRecipeInstructionResponse(
title = "Bake",
text = "Bake the ingredients"
)
val BOIL_INSTRUCTION = GetRecipeInstructionResponse(
title = "Boil",
text = "Boil the ingredients"
)
val GET_CAKE_RESPONSE = GetRecipeResponse(
remoteId = 1,
name = "Cake",
slug = "cake",
image = "86",
description = "A tasty cake",
recipeCategories = listOf("dessert", "tasty"),
tags = listOf("gluten", "allergic"),
rating = 4,
dateAdded = LocalDate.parse("2021-11-13"),
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13"),
recipeYield = "4 servings",
recipeIngredients = listOf(SUGAR_INGREDIENT, BREAD_INGREDIENT),
recipeInstructions = listOf(MIX_INSTRUCTION, BAKE_INSTRUCTION)
)
val GET_PORRIDGE_RESPONSE = GetRecipeResponse(
remoteId = 2,
name = "Porridge",
slug = "porridge",
image = "89",
description = "A tasty porridge",
recipeCategories = listOf("porridge", "tasty"),
tags = listOf("gluten", "milk"),
rating = 5,
dateAdded = LocalDate.parse("2021-11-12"),
dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"),
recipeYield = "3 servings",
recipeIngredients = listOf(SUGAR_INGREDIENT, MILK_INGREDIENT),
recipeInstructions = listOf(MIX_INSTRUCTION, BOIL_INSTRUCTION)
)
val MIX_CAKE_RECIPE_INSTRUCTION_ENTITY = RecipeInstructionEntity(
localId = 1,
recipeId = 1,
title = "Mix",
text = "Mix the ingredients",
)
val BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY = RecipeInstructionEntity(
localId = 2,
recipeId = 1,
title = "Bake",
text = "Bake the ingredients",
)
val CAKE_RECIPE_ENTITY = RecipeEntity(
remoteId = 1,
recipeYield = "4 servings"
)
val CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY = RecipeIngredientEntity(
localId = 1,
recipeId = 1,
title = "Sugar",
note = "2 oz of white sugar",
unit = "",
food = "",
disableAmount = true,
quantity = 1
)
val CAKE_BREAD_RECIPE_INGREDIENT_ENTITY = RecipeIngredientEntity(
localId = 2,
recipeId = 1,
title = "Bread",
note = "2 oz of white bread",
unit = "",
food = "",
disableAmount = false,
quantity = 2
)
val FULL_CAKE_INFO_ENTITY = FullRecipeInfo(
recipeEntity = CAKE_RECIPE_ENTITY,
recipeSummaryEntity = CAKE_RECIPE_SUMMARY_ENTITY,
recipeIngredients = listOf(
CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY,
CAKE_BREAD_RECIPE_INGREDIENT_ENTITY,
),
recipeInstructions = listOf(
MIX_CAKE_RECIPE_INSTRUCTION_ENTITY,
BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY,
),
)
val PORRIDGE_RECIPE_ENTITY_FULL = RecipeEntity(
remoteId = 2,
recipeYield = "3 servings"
)
val PORRIDGE_MILK_RECIPE_INGREDIENT_ENTITY = RecipeIngredientEntity(
localId = 4,
recipeId = 2,
title = "Milk",
note = "2 oz of white milk",
unit = "",
food = "",
disableAmount = true,
quantity = 3
)
val PORRIDGE_SUGAR_RECIPE_INGREDIENT_ENTITY = RecipeIngredientEntity(
localId = 3,
recipeId = 2,
title = "Sugar",
note = "2 oz of white sugar",
unit = "",
food = "",
disableAmount = true,
quantity = 1
)
val PORRIDGE_MIX_RECIPE_INSTRUCTION_ENTITY = RecipeInstructionEntity(
localId = 3,
recipeId = 2,
title = "Mix",
text = "Mix the ingredients"
)
val PORRIDGE_BOIL_RECIPE_INSTRUCTION_ENTITY = RecipeInstructionEntity(
localId = 4,
recipeId = 2,
title = "Boil",
text = "Boil the ingredients"
)
val FULL_PORRIDGE_INFO_ENTITY = FullRecipeInfo(
recipeEntity = PORRIDGE_RECIPE_ENTITY_FULL,
recipeSummaryEntity = PORRIDGE_RECIPE_SUMMARY_ENTITY,
recipeIngredients = listOf(
PORRIDGE_SUGAR_RECIPE_INGREDIENT_ENTITY,
PORRIDGE_MILK_RECIPE_INGREDIENT_ENTITY,
),
recipeInstructions = listOf(
PORRIDGE_MIX_RECIPE_INSTRUCTION_ENTITY,
PORRIDGE_BOIL_RECIPE_INSTRUCTION_ENTITY,
)
)
}

View File

@@ -4,9 +4,18 @@ import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealie.data.MealieDb
import gq.kirmanak.mealie.data.auth.impl.HiltRobolectricTest
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.CAKE_RECIPE_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.PORRIDGE_RECIPE_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.BREAD_INGREDIENT
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.CAKE_BREAD_RECIPE_INGREDIENT_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.CAKE_RECIPE_SUMMARY_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.FULL_CAKE_INFO_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.FULL_PORRIDGE_INFO_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.GET_CAKE_RESPONSE
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.GET_PORRIDGE_RESPONSE
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.MIX_CAKE_RECIPE_INSTRUCTION_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.MIX_INSTRUCTION
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.PORRIDGE_RECIPE_SUMMARY_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.RECIPE_SUMMARY_CAKE
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.RECIPE_SUMMARY_PORRIDGE
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.TEST_RECIPE_SUMMARIES
import gq.kirmanak.mealie.data.recipes.db.entity.CategoryEntity
import gq.kirmanak.mealie.data.recipes.db.entity.CategoryRecipeEntity
@@ -51,7 +60,10 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
fun `when saveRecipes then saves recipes`(): Unit = runBlocking {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
val actualTags = mealieDb.recipeDao().queryAllRecipes()
assertThat(actualTags).containsExactly(CAKE_RECIPE_ENTITY, PORRIDGE_RECIPE_ENTITY)
assertThat(actualTags).containsExactly(
CAKE_RECIPE_SUMMARY_ENTITY,
PORRIDGE_RECIPE_SUMMARY_ENTITY
)
}
@Test
@@ -83,7 +95,7 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
subject.refreshAll(listOf(RECIPE_SUMMARY_CAKE))
val actual = mealieDb.recipeDao().queryAllRecipes()
assertThat(actual).containsExactly(CAKE_RECIPE_ENTITY)
assertThat(actual).containsExactly(CAKE_RECIPE_SUMMARY_ENTITY)
}
@Test
@@ -131,4 +143,43 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
val actual = mealieDb.recipeDao().queryAllTags()
assertThat(actual).isEmpty()
}
@Test
fun `when saveRecipeInfo then saves recipe info`(): Unit = runBlocking {
subject.saveRecipes(listOf(RECIPE_SUMMARY_CAKE))
subject.saveRecipeInfo(GET_CAKE_RESPONSE)
val actual = mealieDb.recipeDao().queryFullRecipeInfo(1)
assertThat(actual).isEqualTo(FULL_CAKE_INFO_ENTITY)
}
@Test
fun `when saveRecipeInfo with two then saves second`(): Unit = runBlocking {
subject.saveRecipes(listOf(RECIPE_SUMMARY_CAKE, RECIPE_SUMMARY_PORRIDGE))
subject.saveRecipeInfo(GET_CAKE_RESPONSE)
subject.saveRecipeInfo(GET_PORRIDGE_RESPONSE)
val actual = mealieDb.recipeDao().queryFullRecipeInfo(2)
assertThat(actual).isEqualTo(FULL_PORRIDGE_INFO_ENTITY)
}
@Test
fun `when saveRecipeInfo secondly then overwrites ingredients`(): Unit = runBlocking {
subject.saveRecipes(listOf(RECIPE_SUMMARY_CAKE))
subject.saveRecipeInfo(GET_CAKE_RESPONSE)
val newRecipe = GET_CAKE_RESPONSE.copy(recipeIngredients = listOf(BREAD_INGREDIENT))
subject.saveRecipeInfo(newRecipe)
val actual = mealieDb.recipeDao().queryFullRecipeInfo(1)?.recipeIngredients
val expected = listOf(CAKE_BREAD_RECIPE_INGREDIENT_ENTITY.copy(localId = 3))
assertThat(actual).isEqualTo(expected)
}
@Test
fun `when saveRecipeInfo secondly then overwrites instructions`(): Unit = runBlocking {
subject.saveRecipes(listOf(RECIPE_SUMMARY_CAKE))
subject.saveRecipeInfo(GET_CAKE_RESPONSE)
val newRecipe = GET_CAKE_RESPONSE.copy(recipeInstructions = listOf(MIX_INSTRUCTION))
subject.saveRecipeInfo(newRecipe)
val actual = mealieDb.recipeDao().queryFullRecipeInfo(1)?.recipeInstructions
val expected = listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY.copy(localId = 3))
assertThat(actual).isEqualTo(expected)
}
}

View File

@@ -10,8 +10,8 @@ import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.TEST_PASSWORD
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.TEST_USERNAME
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.enqueueSuccessfulAuthResponse
import gq.kirmanak.mealie.data.auth.impl.MockServerTest
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.CAKE_RECIPE_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.PORRIDGE_RECIPE_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.CAKE_RECIPE_SUMMARY_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.PORRIDGE_RECIPE_SUMMARY_ENTITY
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.TEST_RECIPE_ENTITIES
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.enqueueSuccessfulRecipeSummaryResponse
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.enqueueUnsuccessfulRecipeSummaryResponse
@@ -58,7 +58,10 @@ class RecipesRemoteMediatorTest : MockServerTest() {
mockServer.enqueueSuccessfulRecipeSummaryResponse()
subject.load(REFRESH, pagingState())
val actual = mealieDb.recipeDao().queryAllRecipes()
assertThat(actual).containsExactly(CAKE_RECIPE_ENTITY, PORRIDGE_RECIPE_ENTITY)
assertThat(actual).containsExactly(
CAKE_RECIPE_SUMMARY_ENTITY,
PORRIDGE_RECIPE_SUMMARY_ENTITY
)
}
@Test