2016-03-09 18:53:47 -06:00
|
|
|
require 'rails_helper'
|
2016-03-09 20:12:38 -06:00
|
|
|
require 'usda_importer'
|
2016-03-09 18:53:47 -06:00
|
|
|
|
2024-09-28 11:50:07 -05:00
|
|
|
RSpec.describe UsdaImporter do
|
2016-03-09 18:53:47 -06:00
|
|
|
|
2026-04-20 14:42:22 -05:00
|
|
|
subject(:import) { UsdaImporter.new(Rails.root.join('spec', 'test_data')).import }
|
|
|
|
|
|
|
|
|
|
it 'imports the correct number of foods with weights' do
|
|
|
|
|
import
|
2016-03-09 20:12:38 -06:00
|
|
|
|
2018-09-14 19:32:49 -05:00
|
|
|
expect(UsdaFood.count).to eq 5
|
2026-04-20 14:42:22 -05:00
|
|
|
|
|
|
|
|
butter = UsdaFood.find_by(ndbn: '01001')
|
2016-03-09 20:12:38 -06:00
|
|
|
expect(butter).not_to be_nil
|
|
|
|
|
expect(butter.usda_food_weights.count).to eq 4
|
2018-09-14 19:32:49 -05:00
|
|
|
|
2026-04-20 14:42:22 -05:00
|
|
|
clif_bar = UsdaFood.find_by(ndbn: '45042066')
|
2018-09-14 19:32:49 -05:00
|
|
|
expect(clif_bar).not_to be_nil
|
|
|
|
|
expect(clif_bar.usda_food_weights.count).to eq 1
|
2026-04-20 14:42:22 -05:00
|
|
|
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')
|
2018-09-14 19:32:49 -05:00
|
|
|
expect(clif_bar.kcal).to eq 368
|
2026-04-20 14:42:22 -05:00
|
|
|
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
|
2016-03-09 20:12:38 -06:00
|
|
|
end
|
|
|
|
|
|
2022-12-01 18:02:26 -06:00
|
|
|
end
|