require 'rails_helper'
require 'usda_importer'

RSpec.describe UsdaImporter do

  subject(:import) { UsdaImporter.new(Rails.root.join('spec', 'test_data')).import }

  it 'imports the correct number of foods with weights' do
    import

    expect(UsdaFood.count).to eq 5

    butter = UsdaFood.find_by(ndbn: '01001')
    expect(butter).not_to be_nil
    expect(butter.usda_food_weights.count).to eq 4

    clif_bar = UsdaFood.find_by(ndbn: '45042066')
    expect(clif_bar).not_to be_nil
    expect(clif_bar.usda_food_weights.count).to eq 1
  end

  it 'imports SR28 nutrition fields correctly' do
    import

    butter = UsdaFood.find_by(ndbn: '01001')
    expect(butter.kcal).to eq 717
    expect(butter.protein).to eq 0.85
    expect(butter.source).to eq 'sr'
  end

  it 'imports branded nutrition fields via map_function' do
    import

    clif_bar = UsdaFood.find_by(ndbn: '45042066')
    expect(clif_bar.kcal).to eq 368
    expect(clif_bar.protein).to eq 13.24
    expect(clif_bar.source).to eq 'bf'
  end

  it 'updates linked Food records with usda nutrition data' do
    food = create(:food, ndbn: '01001')
    import
    food.reload
    expect(food.kcal).to eq 717
  end

end
