spec/timespan/mongoid/advanced_search_spec.rb in timespan-0.4.9 vs spec/timespan/mongoid/advanced_search_spec.rb in timespan-0.5.0

- old
+ new

@@ -52,46 +52,30 @@ @acc8 = Account.create name: '10', period: Timespan.new(start_date: 1.week.from_now, duration: 100.days), time_period: TimePeriod.new(flex: (3..6).minutes) end - def yesterday - 1.day.ago.to_i - end - - def where_hash - {:'period.from'.gte => yesterday, :'period.from'.lte => max_asap} - end - - def exactly - [{:'time_period.flex.from' => period}, {:'time_period.flex.to' => period}] - end - - def in_between - [{:'time_period.flex.from' => min_period}, {:'time_period.flex.to' => max_period}] - end - - def at_least type = :min - {:'time_period.flex.to'.gte => send("#{type}_period") } - end - - def at_most type = :max - {:'time_period.flex.to'.lte => send("#{type}_period") } - end - describe 'ASAP 2 minutes' do let(:period) { 2.minutes.to_i } let(:criteria) do - Account.where(where_hash).or(exactly) + Account.where(TimePeriod.asap).or(TimePeriod.exactly period) end # it 'should have a nice criteria' do # criteria.selector['$or'].should == [{"time_period.flex.from"=>120}, {"time_period.flex.to"=>120}] # criteria.selector["period.from"].should be_a Hash # end + it 'should have custom max_asap' do + TimePeriod.max_asap.should == 14.days.from_now.to_i + end + + it 'should have custom min_asap' do + TimePeriod.min_asap.should == 2.days.ago.to_i + end + it 'should find #1, #2, #3, #4, #5' do criteria.to_a.map(&:name).should include '1', '2', '5', '7' end let(:time_period) { criteria.first.time_period } @@ -105,11 +89,11 @@ describe 'ASAP 3 minutes' do let(:period) { 3.minutes.to_i } let(:criteria) do - Account.where(where_hash).or(exactly) + Account.where(TimePeriod.asap).or(TimePeriod.exactly period) end it 'should find #4, #10' do # puts criteria.to_a.map(&:name) criteria.to_a.map(&:name).should include '4', '10' @@ -118,24 +102,23 @@ describe 'ASAP 4 minutes' do let(:period) { 4.minutes.to_i } let(:criteria) do - Account.where(where_hash).or(exactly) + Account.where(TimePeriod.asap).or(TimePeriod.exactly period) end it 'should find #2, #3' do criteria.to_a.should include(@acc2, @acc3) end end describe 'ASAP 1-4 minutes' do - let(:min_period) { 1.minute } - let(:max_period) { 4.minutes } + let(:period) { 1.minute..4.minutes } let(:criteria) do - Account.where(where_hash).or(in_between) + Account.where(TimePeriod.asap).or(TimePeriod.in_between period) end it 'should find #2, #3' do # puts "ASAP 1-4 minutes" # puts criteria.selector @@ -144,52 +127,50 @@ criteria.to_a.map(&:name).should include('2', '3') end end describe 'ASAP 3-5 minutes' do - let(:min_period) { 3.minutes } - let(:max_period) { 5.minutes } + let(:period) { 3.minutes..5.minutes } it 'should have a nice criteria' do # puts "ASAP 3-5 minutes" # puts criteria.selector criteria.selector["time_period.flex.from"] = 180 criteria.selector["time_period.flex.to"] = 300 criteria.selector["period.from"].should be_a Hash end let(:criteria) do - Account.where(where_hash).or(in_between) + Account.where(TimePeriod.asap).or(TimePeriod.in_between period) end it 'should find #2, #3' do # puts criteria.to_a.map(&:name) criteria.to_a.map(&:name).should_not include('8', '9') criteria.to_a.map(&:name).should include('10') end end describe 'ASAP 5-7 minutes' do - let(:min_period) { 5.minutes } - let(:max_period) { 7.minutes } + let(:period) { 5.minutes..7.minutes } let(:criteria) do - Account.where(where_hash).or(in_between) + Account.where(TimePeriod.asap).or(TimePeriod.in_between period) end it 'should find #2, #3' do # puts criteria.to_a.map(&:name) criteria.to_a.map(&:name).should_not include('6', '9', '10') criteria.to_a.map(&:name).should include('8') end end describe 'ASAP at least 4 minutes' do - let(:min_period) { 4.minutes } + let(:period) { 4.minutes } let(:criteria) do - Account.where where_hash.merge(at_least) + Account.where TimePeriod.asap.merge(TimePeriod.at_least period) end it 'should find #2, #3' do # puts criteria.selector # puts criteria.to_a.map(&:name) @@ -197,13 +178,13 @@ criteria.to_a.map(&:name).should include '1', '2', '3', '8', '10' end end describe 'ASAP at most 3 minutes' do - let(:max_period) { 3.minutes } + let(:period) { 3.minutes } let(:criteria) do - Account.where where_hash.merge(at_most) + Account.where TimePeriod.asap.merge(TimePeriod.at_most period) end it 'should find #2, #3' do # puts criteria.selector # puts criteria.to_a.map(&:name) \ No newline at end of file