mirror of https://github.com/kcal-app/kcal.git
Add tests for recipe volume-based nutrients calculation
This commit is contained in:
parent
e8f2ed8108
commit
431de7f696
|
@ -3,6 +3,8 @@
|
||||||
namespace Tests\Feature\Support;
|
namespace Tests\Feature\Support;
|
||||||
|
|
||||||
use App\Models\Food;
|
use App\Models\Food;
|
||||||
|
use App\Models\IngredientAmount;
|
||||||
|
use App\Models\Recipe;
|
||||||
use App\Support\Nutrients;
|
use App\Support\Nutrients;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
@ -35,8 +37,45 @@ class NutrientsTest extends TestCase
|
||||||
float $expectedMultiplier
|
float $expectedMultiplier
|
||||||
): void {
|
): void {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
Nutrients::calculateFoodNutrientMultiplier($food, $amount, $fromUnit),
|
$expectedMultiplier,
|
||||||
$expectedMultiplier
|
Nutrients::calculateFoodNutrientMultiplier($food, $amount, $fromUnit)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test valid Recipe nutrient amount calculation.
|
||||||
|
*
|
||||||
|
* @dataProvider recipesValidRecipeNutrientAmountsProvider
|
||||||
|
*/
|
||||||
|
public function testCalculateValidRecipeNutrientAmount(
|
||||||
|
string $nutrient,
|
||||||
|
float $amount,
|
||||||
|
string $fromUnit,
|
||||||
|
float $expectedAmount
|
||||||
|
): void {
|
||||||
|
/** @var \App\Models\Recipe $recipe */
|
||||||
|
$recipe = Recipe::factory()
|
||||||
|
->create(['volume' => 2, 'weight' => 400]);
|
||||||
|
/** @var \App\Models\Food $food */
|
||||||
|
$food = Food::factory()->create([
|
||||||
|
'calories' => 20,
|
||||||
|
'carbohydrates' => 20,
|
||||||
|
'cholesterol' => 200,
|
||||||
|
'fat' => 20,
|
||||||
|
'protein' => 20,
|
||||||
|
'sodium' => 200,
|
||||||
|
]);
|
||||||
|
$ingredient = new IngredientAmount();
|
||||||
|
$ingredient->fill([
|
||||||
|
'amount' => 1,
|
||||||
|
'unit' => 'serving',
|
||||||
|
'weight' => 0,
|
||||||
|
])->ingredient()->associate($food);
|
||||||
|
$recipe->ingredientAmounts()->save($ingredient);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
$expectedAmount,
|
||||||
|
Nutrients::calculateRecipeNutrientAmount($recipe, $nutrient, $amount, $fromUnit)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +83,26 @@ class NutrientsTest extends TestCase
|
||||||
* Data providers.
|
* Data providers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide example recipe and expected nutrient amounts.
|
||||||
|
*/
|
||||||
|
public function recipesValidRecipeNutrientAmountsProvider(): array {
|
||||||
|
return [
|
||||||
|
['calories', 1, 'cup', 10],
|
||||||
|
['calories', 2, 'cup', 20],
|
||||||
|
['carbohydrates', 8, 'tbsp', 5],
|
||||||
|
['carbohydrates', 16, 'tbsp', 10],
|
||||||
|
['cholesterol', 48, 'tsp', 100],
|
||||||
|
['cholesterol', 96, 'tsp', 200],
|
||||||
|
['fat', 100, 'gram', 5],
|
||||||
|
['fat', 200, 'gram', 10],
|
||||||
|
['protein', 100, 'gram', 5],
|
||||||
|
['protein', 200, 'gram', 10],
|
||||||
|
['sodium', 2, 'oz', Nutrients::$gramsPerOunce],
|
||||||
|
['sodium', 4, 'oz', Nutrients::$gramsPerOunce * 2],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide example foods and expected nutrient multipliers.
|
* Provide example foods and expected nutrient multipliers.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue