Bugfix and specs for first and last harvest

This commit is contained in:
Brenda Wallace
2017-11-05 17:33:58 +13:00
parent 996706ee78
commit 9b1b22afa8
3 changed files with 62 additions and 7 deletions

View File

@@ -39,13 +39,14 @@ class Harvest < ActiveRecord::Base
##
## Scopes
default_scope { joins(:owner).order(created_at: :desc) }
default_scope { joins(:owner) }
##
## Validations
validates :crop, approved: true
validates :crop, presence: { message: "must be present and exist in our database" }
validates :plant_part, presence: { message: "must be present and exist in our database" }
validates :harvested_at, presence: true
validate :crop_must_match_planting
validate :harvest_must_be_after_planting
validates :quantity, numericality: {

View File

@@ -96,7 +96,7 @@ class Planting < ActiveRecord::Base
def expected_lifespan
if planted_at.present? && finished_at.present?
return finished_at - planted_at
return (finished_at - planted_at).to_i
end
crop.median_lifespan
end
@@ -114,13 +114,14 @@ class Planting < ActiveRecord::Base
end
def update_harvest_days
first_harvest = nil
last_harvest = nil
if harvests.size.positive?
# how long ago was this planted
first_harvest = (harvests.first.harvested_at - planted_at).to_i
last_harvest = finished? ? (harvests.last.harvested_at - planted_at).to_i : nil
else
first_harvest = nil
last_harvest = nil
first_harvest = (harvests.order(harvested_at: :asc).limit(1).first.harvested_at - planted_at).to_i
if finished?
last_harvest = (harvests.order(harvested_at: :desc).limit(1).first.harvested_at - planted_at).to_i
end
end
update(first_harvest: first_harvest, last_harvest: last_harvest)
end

View File

@@ -79,6 +79,59 @@ describe Planting do
end
end
describe 'planting first harvest preductions' do
context 'no data' do
let(:planting) { FactoryBot.create :planting }
it { expect(planting.crop.median_first_harvest).to eq(nil) }
it { expect(planting.first_harvest).to eq(nil) }
it { expect(planting.expected_lifespan).to eq(nil) }
end
context 'lots of data' do
def one_hundred_day_old_planting
FactoryBot.create(:planting, crop: planting.crop, planted_at: 100.days.ago)
end
before do
# 50 days to harvest
FactoryBot.create(:harvest, harvested_at: 50.days.ago, crop: planting.crop,
planting: one_hundred_day_old_planting)
# 20 days to harvest
FactoryBot.create(:harvest, harvested_at: 80.days.ago, crop: planting.crop,
planting: one_hundred_day_old_planting)
# 10 days to harvest
FactoryBot.create(:harvest, harvested_at: 90.days.ago, crop: planting.crop,
planting: one_hundred_day_old_planting)
planting.crop.plantings.each(&:update_harvest_days)
planting.crop.update_lifespan_medians
planting.crop.update_harvest_medians
end
it { expect(planting.crop.median_first_harvest).to eq(20) }
end
describe 'planting has no harvests' do
before { planting.update_harvest_days }
let(:planting) { FactoryBot.create :planting }
it { expect(planting.first_harvest).to eq(nil) }
end
describe 'planting has first harvest' do
let(:planting) { FactoryBot.create :planting, planted_at: 100.days.ago }
before do
FactoryBot.create :harvest, planting: planting, crop: planting.crop, harvested_at: 10.days.ago
planting.update_harvest_days
end
it { expect(planting.first_harvest).to eq(90) }
it { expect(planting.last_harvest).to eq(nil) }
end
describe 'planting has last harvest' do
let(:planting) { FactoryBot.create :planting, planted_at: 100.days.ago, finished_at: 1.day.ago, finished: true }
before do
FactoryBot.create :harvest, planting: planting, crop: planting.crop, harvested_at: 90.days.ago
FactoryBot.create :harvest, planting: planting, crop: planting.crop, harvested_at: 10.days.ago
planting.update_harvest_days
end
it { expect(planting.first_harvest).to eq(10) }
it { expect(planting.last_harvest).to eq(90) }
end
end
it 'has an owner' do
planting.owner.should be_an_instance_of Member
end