technoweenie / can_search

Build common named scopes automatically, and provide a simple way to merge them with a single #search call.

This URL has Read+Write access

can_search / spec / date_range_scope_spec.rb
100644 146 lines (116 sloc) 5.468 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require File.dirname(__FILE__) + '/spec_helper'
 
module CanSearch
  DateRangeScope.periods[:spec] = lambda do |now|
    (now..now + 300)
  end
 
  describe "all DateRange Scopes", :shared => true do
    include CanSearchSpecHelper
 
    it "instantiates date range scope" do
      Record.search_scopes[@scope.name].should == @scope
    end
 
    it "creates named_scope" do
      Record.scopes[@scope.named_scope].should_not be_nil
    end
 
    it "filters records by time range" do
      compare_records Record.search(@scope.name => (@now-5.days..@now-7.minutes)), [:day, :week_1, :week_2]
    end
 
    it "filters today's records" do
      compare_records Record.search(@scope.name => {:period => :daily}), [:default, :day]
    end
    
    it "filters daily records" do
      compare_records Record.search(@scope.name => {:period => :daily, :start => @now - 3.days}), [:week_1]
    end
    
    it "filters this week's records" do
      compare_records Record.search(@scope.name => {:period => :weekly}), [:default, :day, :week_1, :week_2]
    end
    
    it "filters this fortnight's records" do
      compare_records Record.search(@scope.name => {:period => :'bi-weekly'}), [:default, :day, :week_1, :week_2, :biweek_1, :biweek_2]
    end
    
    it "filters earlier fortnight's records" do
      compare_records Record.search(@scope.name => {:period => :'bi-weekly', :start => '2007-6-14 6:00:00'}), [:month_1, :month_2]
    end
    
    it "filters this month's records" do
      compare_records Record.search(@scope.name => {:period => :monthly}), [:default, :day, :week_1, :week_2, :biweek_1, :biweek_2, :month_1, :month_2]
    end
    
    it "filters older month's records" do
      compare_records Record.search(@scope.name => {:period => :monthly, :start => '2007-5-5'}), [:archive]
    end
  end
 
  describe DateRangeScope do
    describe "(DateRange Scope with no options)" do
      before do
        Record.can_search do
          scoped_by :created, :scope => :date_range
        end
        @scope = DateRangeScope.new(Record, :created, :attribute => :created_at, :scope => :date_range, :named_scope => :created)
      end
 
      it_should_behave_like "all DateRange Scopes"
    end
 
    describe "(DateRange Scope with custom attribute)" do
      before do
        Record.can_search do
          scoped_by :latest, :scope => :date_range, :attribute => :created_at
        end
        @scope = DateRangeScope.new(Record, :latest, :attribute => :created_at, :scope => :date_range, :named_scope => :latest )
      end
 
      it_should_behave_like "all DateRange Scopes"
    end
 
    describe "(DateRange Scope with custom attribute and finder)" do
      before do
        Record.can_search do
          scoped_by :latest, :scope => :date_range, :attribute => :created_at, :named_scope => :woot
        end
        @scope = DateRangeScope.new(Record, :latest, :attribute => :created_at, :scope => :date_range, :named_scope => :woot)
      end
 
      it_should_behave_like "all DateRange Scopes"
    end
 
    describe "ActiveRecord::Base.date_range_for(period, time = nil) with ActiveSupport defaults" do
      it "creates daily range" do
        Record.date_range_for(:daily, Time.utc(2008, 1, 1, 12)).should == (Time.utc(2008, 1, 1)..Time.utc(2008, 1, 2)-1.second)
      end
      
      it "creates weekly range" do
        Record.date_range_for(:weekly, Time.utc(2008, 1, 1)).should == (Time.utc(2007, 12, 31)..Time.utc(2008, 1, 7)-1.second)
      end
 
      it "creates bi-weekly range for first half of the month" do
        Record.date_range_for(:'bi-weekly', Time.utc(2008, 1, 5)).should == (Time.utc(2008, 1, 1)..Time.utc(2008, 1, 16)-1.second)
      end
 
      it "creates bi-weekly range for second half of the month" do
        Record.date_range_for(:'bi-weekly', Time.utc(2008, 1, 17)).should == (Time.utc(2008, 1, 16)..Time.utc(2008, 2, 1)-1.second)
      end
 
      it "creates monthly range" do
        Record.date_range_for(:monthly, Time.utc(2008, 1, 5)).should == (Time.utc(2008, 1, 1)..Time.utc(2008, 2, 1)-1)
      end
 
      it "parses time and calls #with_date_range with valid filter" do
        Record.date_range_for(:spec, '2008-1-1').should == (Time.utc(2008, 1, 1)..Time.utc(2008, 1, 1, 0, 5))
      end
      
      it "allows custom instance-level filter" do
        Record.date_periods[:custom_spec] = lambda { |now| (now..now + 420) }
        Record.date_range_for(:custom_spec, '2008-1-1').should == (Time.utc(2008, 1, 1)..Time.utc(2008, 1, 1, 0, 7))
        Record.date_periods.delete(:custom_spec)
      end
      
      it "raises exception on bad filter" do
        lambda { Record.date_range_for(:snozzberries, nil) }.should raise_error(RuntimeError)
      end
    end
 
    describe "ActiveRecord::Base.parse_filtered_time(time_or_string)" do
      before :all do
        def Record.public_parse_filtered_time(*args)
          parse_filtered_time(*args)
        end
      end
  
      it "converts strings to times" do
        Record.public_parse_filtered_time("2008-1-1").should == Time.utc(2008, 1, 1)
      end
    
      it "converts times to utc" do
        time = Time.now
        time.should_not be_utc
        parsed = Record.public_parse_filtered_time(time)
        parsed.should == time
        parsed.should be_utc
      end
    
      it "raises error on bad filtered date value" do
        lambda { Record.public_parse_filtered_time(:boom) }.should raise_error(RuntimeError)
      end
    end
  end
end