2016-01-12 18:43:00 -06:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
RSpec.describe Food, type: :model do
|
2016-01-14 15:22:15 -06:00
|
|
|
|
|
|
|
describe 'validation' do
|
|
|
|
|
|
|
|
it 'validates density' do
|
2018-09-11 10:38:07 -05:00
|
|
|
i = build(:food)
|
2016-01-14 15:22:15 -06:00
|
|
|
expect(i).to be_valid
|
|
|
|
|
|
|
|
i.density = '5'
|
|
|
|
expect(i).not_to be_valid
|
|
|
|
|
|
|
|
i.density = '5 cup'
|
|
|
|
expect(i).not_to be_valid
|
|
|
|
|
|
|
|
i.density = '5 gram/cup'
|
|
|
|
expect(i).to be_valid
|
|
|
|
|
|
|
|
i.density = '5 mile/hour'
|
|
|
|
expect(i).not_to be_valid
|
2024-10-01 09:32:09 -05:00
|
|
|
|
|
|
|
i.density = '1 g/ml'
|
|
|
|
expect(i).to be_valid
|
2016-01-14 15:22:15 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-02-02 15:48:20 -06:00
|
|
|
describe 'set_usda_food' do
|
|
|
|
it 'sets the density' do
|
2018-09-11 10:38:07 -05:00
|
|
|
i = build(:food)
|
2016-02-02 15:48:20 -06:00
|
|
|
f = create(:salted_butter)
|
2016-07-05 16:31:36 -05:00
|
|
|
create(:usda_food_weight, usda_food: f)
|
2016-02-02 15:48:20 -06:00
|
|
|
|
|
|
|
i.set_usda_food(f)
|
|
|
|
|
|
|
|
expect(i.density).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
describe '#custom_units' do
|
|
|
|
it 'returns a hash based on food_units' do
|
|
|
|
i = build(:food)
|
|
|
|
i.food_units << FoodUnit.new(name: 'clove', gram_weight: 20.0)
|
2016-07-05 16:31:36 -05:00
|
|
|
|
2018-09-11 10:38:07 -05:00
|
|
|
units = i.custom_units
|
|
|
|
expect(units['clove']).to be_a UnitConversion::ValueUnit
|
|
|
|
expect(units['clove'].value.value).to eq 20.0
|
|
|
|
expect(units['clove'].unit.unit).to eq 'g'
|
2016-07-05 16:31:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-09 17:38:08 -05:00
|
|
|
describe '#cache_key' do
|
|
|
|
it 'has a key for new foods' do
|
|
|
|
f = Food.new
|
|
|
|
expect(f.cache_key).to eq "foods/new"
|
|
|
|
expect(f.cache_version).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a different key for new foods with an ndbn' do
|
|
|
|
f = Food.new(ndbn: '123456')
|
|
|
|
expect(f.cache_key).to eq "foods/new/123456"
|
|
|
|
expect(f.cache_version).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a key for persisted foods' do
|
|
|
|
f = create(:food)
|
|
|
|
expect(f.cache_key).to eq "foods/#{f.id}"
|
|
|
|
expect(f.cache_version).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the same key for persisted foods with an ndbn' do
|
|
|
|
f = create(:food, ndbn: '123456')
|
|
|
|
expect(f.cache_key).to eq "foods/#{f.id}"
|
|
|
|
expect(f.cache_version).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|