grouping_spec.rb - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(HTM) git clone git://jay.scot/reportable
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
grouping_spec.rb (7606B)
---
1 require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))),'spec_helper')
2
3 describe Saulabs::Reportable::Grouping do
4
5 describe '#new' do
6
7 it 'should raise an error if an unsupported grouping is specified' do
8 lambda { Saulabs::Reportable::Grouping.new(:unsupported) }.should raise_error(ArgumentError)
9 end
10
11 end
12
13 describe '#to_sql' do
14
15 describe 'for MySQL' do
16
17 before do
18 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('MySQL')
19 end
20
21 it 'should use DATE_FORMAT with format string "%Y/%m/%d/%H" for grouping :hour' do
22 Saulabs::Reportable::Grouping.new(:hour).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%m/%d/%H')"
23 end
24
25 it 'should use DATE_FORMAT with format string "%Y/%m/%d" for grouping :day' do
26 Saulabs::Reportable::Grouping.new(:day).send(:to_sql, 'created_at').should == "DATE(created_at)"
27 end
28
29 it 'should use YEARWEEK with mode 3 for grouping :week' do
30 Saulabs::Reportable::Grouping.new(:week).send(:to_sql, 'created_at').should == "YEARWEEK(created_at, 3)"
31 end
32
33 it 'should use DATE_FORMAT with format string "%Y/%m" for grouping :month' do
34 Saulabs::Reportable::Grouping.new(:month).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%m')"
35 end
36
37 end
38
39 describe 'for PostgreSQL' do
40
41 before do
42 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('PostgreSQL')
43 end
44
45 for grouping in [:hour, :day, :week, :month] do
46
47 it "should use date_trunc with truncation identifier \"#{grouping.to_s}\" for grouping :#{grouping.to_s}" do
48 Saulabs::Reportable::Grouping.new(grouping).send(:to_sql, 'created_at').should == "date_trunc('#{grouping.to_s}', created_at)"
49 end
50
51 end
52
53 end
54
55 describe 'for SQLite3' do
56
57 before do
58 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('SQLite')
59 end
60
61 it 'should use strftime with format string "%Y/%m/%d/%H" for grouping :hour' do
62 Saulabs::Reportable::Grouping.new(:hour).send(:to_sql, 'created_at').should == "strftime('%Y/%m/%d/%H', created_at)"
63 end
64
65 it 'should use strftime with format string "%Y/%m/%d" for grouping :day' do
66 Saulabs::Reportable::Grouping.new(:day).send(:to_sql, 'created_at').should == "strftime('%Y/%m/%d', created_at)"
67 end
68
69 it 'should use date with mode "weekday 0" for grouping :week' do
70 Saulabs::Reportable::Grouping.new(:week).send(:to_sql, 'created_at').should == "date(created_at, 'weekday 0')"
71 end
72
73 it 'should use strftime with format string "%Y/%m" for grouping :month' do
74 Saulabs::Reportable::Grouping.new(:month).send(:to_sql, 'created_at').should == "strftime('%Y/%m', created_at)"
75 end
76
77 end
78
79 describe 'for MS SQL Server' do
80
81 before do
82 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('sqlserver')
83 end
84
85 it 'should output a format of "YYYY-MM-DD HH:00:00.0" for grouping :hour' do # string "%Y-%m-%d %h:00:00.0"
86 Saulabs::Reportable::Grouping.new(:hour).send(:to_sql, 'created_at').should == "DATEADD(hh,DATEDIFF(hh,DATEADD(dd,DATEDIFF(dd,'1 Jan 1900',created_at), '1 Jan 1900'),created_at), DATEADD(dd,DATEDIFF(dd,'1 Jan 1900',created_at), '1 Jan 1900'))"
87 end
88
89 it 'should output a format of "YYYY-MM-DD" for grouping :day' do
90 Saulabs::Reportable::Grouping.new(:day).send(:to_sql, 'created_at').should == "DATEADD(dd,DATEDIFF(dd,'1 Jan 1900',created_at), '1 Jan 1900')"
91 end
92
93 it 'should output a format of "YYYY-WW" for grouping :week' do
94 Saulabs::Reportable::Grouping.new(:week).send(:to_sql, 'created_at').should == "LEFT(CONVERT(varchar,created_at,120), 4) + '-' + CAST(DATEPART(isowk,created_at) AS VARCHAR)"
95 end
96
97 it 'should output a format of "YYYY-MM-01" for grouping :month' do
98 Saulabs::Reportable::Grouping.new(:month).send(:to_sql, 'created_at').should == "DATEADD(mm,DATEDIFF(mm,'1 Jan 1900',created_at), '1 Jan 1900')"
99 end
100
101 end
102
103 end
104
105 describe '#date_parts_from_db_string' do
106
107 describe 'for SQLite3' do
108
109 before do
110 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('SQLite')
111 end
112
113 for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month, '2008/12']] do
114
115 it "should split the string with '/' for grouping :#{grouping[0].to_s}" do
116 Saulabs::Reportable::Grouping.new(grouping[0]).date_parts_from_db_string(grouping[1]).should == grouping[1].split('/').map(&:to_i)
117 end
118
119 end
120
121 it 'should split the string with "-" and return the calendar year and week for grouping :week' do
122 db_string = '2008-2-1'
123 expected = [2008, 5]
124
125 Saulabs::Reportable::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
126 end
127
128 end
129
130 describe 'for PostgreSQL' do
131
132 before do
133 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('PostgreSQL')
134 end
135
136 it 'should split the date part of the string with "-" and read out the hour for grouping :hour' do
137 Saulabs::Reportable::Grouping.new(:hour).date_parts_from_db_string('2008-12-03 06:00:00').should == [2008, 12, 03, 6]
138 end
139
140 it 'should split the date part of the string with "-" for grouping :day' do
141 Saulabs::Reportable::Grouping.new(:day).date_parts_from_db_string('2008-12-03 00:00:00').should == [2008, 12, 03]
142 end
143
144 it 'should split the date part of the string with "-" and calculate the calendar week for grouping :week' do
145 Saulabs::Reportable::Grouping.new(:week).date_parts_from_db_string('2008-12-01 00:00:00').should == [2008, 49]
146 end
147
148 it 'should split the date part of the string with "-" and return year and month for grouping :month' do
149 Saulabs::Reportable::Grouping.new(:month).date_parts_from_db_string('2008-12-01 00:00:00').should == [2008, 12]
150 end
151
152 end
153
154 describe 'for MySQL' do
155
156 before do
157 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('MySQL')
158 end
159
160 for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month, '2008/12']] do
161
162 it "should split the string with '/' for grouping :#{grouping[0].to_s}" do
163 Saulabs::Reportable::Grouping.new(grouping[0]).date_parts_from_db_string(grouping[1]).should == grouping[1].split('/').map(&:to_i)
164 end
165
166 end
167
168 it 'should use the first 4 numbers for the year and the last 2 numbers for the week for grouping :week' do
169 db_string = '200852'
170 expected = [2008, 52]
171
172 Saulabs::Reportable::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
173 end
174
175 end
176
177 describe 'for MS SQL Server' do
178
179 before do
180 ActiveRecord::Base.connection.stub!(:adapter_name).and_return('sqlserver')
181 end
182
183 for grouping in [[:hour, '2008-12-31 12'], [:day, '2008-12-31'], [:month, '2008-12']] do
184
185 it "should split the string with '-' and ' ' for grouping :#{grouping[0].to_s}" do
186 Saulabs::Reportable::Grouping.new(grouping[0]).date_parts_from_db_string(grouping[1]).should == grouping[1].split(/[- ]/).map(&:to_i)
187 end
188
189 end
190
191 it 'should use the first 4 numbers for the year and the last 2 numbers for the week for grouping :week' do
192 db_string = '2008-52'
193 expected = [2008, 52]
194
195 Saulabs::Reportable::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
196 end
197
198 end
199
200 end
201
202 end