Skip to content

Instantly share code, notes, and snippets.

@triskweline
Created July 5, 2012 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triskweline/3056556 to your computer and use it in GitHub Desktop.
Save triskweline/3056556 to your computer and use it in GitHub Desktop.
Production example where factories are useful vs. a global set of fixtures.
describe Contract do
describe '.overlapping' do
it 'should match a contract partially overlapping the given contract' do
match = Contract.make(:start_date => Date.today + 5, :end_date => Date.today + 10)
given = Contract.new(:start_date => Date.today + 7, :end_date => Date.today + 12)
Contract.overlapping(given).all.should == [match]
end
it 'should match a contract entirely enclosed by the given contract' do
match = Contract.make(:start_date => Date.today + 5, :end_date => Date.today + 10)
given = Contract.new(:start_date => Date.today + 2, :end_date => Date.today + 50)
Contract.overlapping(given).all.should == [match]
end
it 'should match a contract entirely enclosing the given contract' do
match = Contract.make(:start_date => Date.today + 5, :end_date => Date.today + 10)
given = Contract.new(:start_date => Date.today + 7, :end_date => Date.today + 8)
Contract.overlapping(given).all.should == [match]
end
it 'should match a contract ending on the same date as the given contract starts (since the dates are inclusive)' do
match = Contract.make(:start_date => Date.today + 5, :end_date => Date.today + 10)
given = Contract.new(:start_date => Date.today + 10, :end_date => Date.today + 20)
Contract.overlapping(given).all.should == [match]
end
it 'should match a contract starting on the same date as the given contract ends (since the dates are inclusive)' do
match = Contract.make(:start_date => Date.today + 10, :end_date => Date.today + 20)
given = Contract.new(:start_date => Date.today + 5, :end_date => Date.today + 10)
Contract.overlapping(given).all.should == [match]
end
it 'should not match a contract occuring entirely before the given contract' do
no_match = Contract.make(:start_date => Date.today + 2, :end_date => Date.today + 3)
given = Contract.new(:start_date => Date.today + 5, :end_date => Date.today + 10)
Contract.overlapping(given).all.should == []
end
it 'should not match a contract occuring entirely after the given contract' do
no_match = Contract.make(:start_date => Date.today + 100, :end_date => Date.today + 105)
given = Contract.new(:start_date => Date.today + 5, :end_date => Date.today + 10)
Contract.overlapping(given).all.should == []
end
it 'should recognize overlap if the given contract is open-ended' do
match = Contract.make(:start_date => Date.today + 5, :end_date => Date.today + 10)
given = Contract.new(:start_date => Date.today + 7, :end_date => nil)
Contract.overlapping(given).all.should == [match]
end
it 'should recognize overlap with other open-ended contracts' do
match = Contract.make(:start_date => Date.today + 5, :end_date => nil)
given = Contract.new(:start_date => Date.today + 7, :end_date => Date.today + 10)
Contract.overlapping(given).all.should == [match]
end
it 'should recognize overlap when both the given and another contract is open-ended' do
match = Contract.make(:start_date => Date.today + 5, :end_date => nil)
given = Contract.new(:start_date => Date.today + 7, :end_date => nil)
Contract.overlapping(given).all.should == [match]
end
it 'should not match the given contract itself' do
given = Contract.make
Contract.overlapping(given).all.should == []
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment