2016-01-12 18:43:00 -06:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe RecipeIngredient, type: :model do
|
2016-02-02 15:48:20 -06:00
|
|
|
|
2016-07-05 16:31:36 -05:00
|
|
|
describe 'to_mass' do
|
|
|
|
|
|
|
|
it 'converts volume ingredients with density' do
|
2018-09-11 10:38:07 -05:00
|
|
|
ri = RecipeIngredient.new(quantity: 2, units: 'tbsp', food: create(:food_with_density))
|
2016-07-05 16:31:36 -05:00
|
|
|
ri.to_mass
|
2018-09-11 22:56:26 -05:00
|
|
|
expect(ri.units).to eq 'ounce'
|
2016-07-05 16:31:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts ingredients with custom units' do
|
2018-09-11 10:38:07 -05:00
|
|
|
i = create(:food_with_density)
|
|
|
|
i.food_units << FoodUnit.new(name: 'pat', gram_weight: 25)
|
|
|
|
ri = RecipeIngredient.new(quantity: 2, units: 'pat', food: i)
|
2016-07-05 16:31:36 -05:00
|
|
|
ri.to_mass
|
2018-09-11 22:56:26 -05:00
|
|
|
expect(ri.quantity).to eq '50'
|
|
|
|
expect(ri.units).to eq 'gram'
|
2016-07-05 16:31:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
describe '#calculate_nutrition_ratio' do
|
|
|
|
it 'returns nil if no ratio can be calculated' do
|
|
|
|
ri = create(:recipe_ingredient)
|
|
|
|
expect(ri.calculate_nutrition_ratio).to be_nil
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
ri.quantity = 50
|
|
|
|
expect(ri.calculate_nutrition_ratio).to be_nil
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
ri.units = 'cats'
|
|
|
|
expect(ri.calculate_nutrition_ratio).to be_nil
|
|
|
|
end
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
describe 'with recipe_as_ingredient' do
|
|
|
|
let(:recipe) { create(:recipe, yields: '250 g, 0.5 l, 2 rolls') }
|
|
|
|
let(:recipe_ingredient) { create(:recipe_ingredient, food: nil, recipe_as_ingredient: recipe) }
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
it 'returns nil with no quantity' do
|
|
|
|
ri = recipe_ingredient
|
|
|
|
expect(ri.calculate_nutrition_ratio).to be_nil
|
2018-08-27 16:44:45 -05:00
|
|
|
end
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
it 'returns returns a proper scale with a unitless quantity' do
|
|
|
|
ri = recipe_ingredient
|
|
|
|
ri.quantity = 2
|
|
|
|
expect(ri.calculate_nutrition_ratio).to eq 2
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
ri.quantity = 4
|
|
|
|
expect(ri.calculate_nutrition_ratio).to eq 4
|
2018-08-27 16:44:45 -05:00
|
|
|
end
|
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
it 'returns a proper scale with a counted unit' do
|
|
|
|
ri = recipe_ingredient
|
|
|
|
ri.quantity = 3
|
|
|
|
ri.units = "rolls"
|
|
|
|
expect(ri.calculate_nutrition_ratio).to eq 1.5
|
2018-08-27 16:44:45 -05:00
|
|
|
end
|
2016-02-14 19:29:34 -06:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
it 'returns a proper scale with a mass unit' do
|
|
|
|
ri = recipe_ingredient
|
|
|
|
ri.quantity = 500
|
|
|
|
ri.units = 'g'
|
|
|
|
expect(ri.calculate_nutrition_ratio).to eq 2
|
2018-08-27 16:44:45 -05:00
|
|
|
end
|
2016-02-14 19:29:34 -06:00
|
|
|
end
|
2018-08-27 16:44:45 -05:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
describe 'with food' do
|
|
|
|
let(:food) { create(:food) }
|
|
|
|
let(:recipe_ingredient) { create(:recipe_ingredient, food: food) }
|
2018-08-27 16:44:45 -05:00
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
it 'returns a proper scale with a mass unit' do
|
|
|
|
ri = recipe_ingredient
|
|
|
|
ri.quantity = 500
|
|
|
|
ri.units = 'g'
|
|
|
|
expect(ri.calculate_nutrition_ratio).to eq 5
|
|
|
|
end
|
|
|
|
end
|
2016-02-14 19:29:34 -06:00
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|