require 'rails_helper'

RSpec.describe NutritionData, type: :model do

  let(:rec1_ingredients) do
    [
      RecipeIngredient.new({
        quantity: '100',
        units: 'g',
        sort_order: 1,
        food: create(:food, kcal: 10, protein: 2)
      }),
      RecipeIngredient.new({
        quantity: '100',
        units: 'g',
        sort_order: 2,
        food: create(:food, kcal: 10, lipids: 2)
      })
    ]
  end

  let(:recipe1) do
    create(:recipe, yields: '500 g').tap do |r|
      r.recipe_ingredients = rec1_ingredients
    end
  end

  let(:rec2_ingredients) do
    [
      RecipeIngredient.new({
        quantity: '250',
        units: 'g',
        sort_order: 1,
        recipe_as_ingredient: recipe1
      }),
      RecipeIngredient.new({
        quantity: '100',
        units: 'g',
        sort_order: 2,
        food: create(:food, kcal: 10, lipids: 2)
      })
    ]
  end

  let(:recipe2) do
    create(:recipe, yields: '500 g').tap do |r|
      r.recipe_ingredients = rec2_ingredients
    end
  end

  it 'runs and calculates properly' do
    n = recipe2.nutrition_data

    expect(n.kcal).to eq 20
    expect(n.protein).to eq 1
    expect(n.lipids).to eq 3
  end

end
