trackoid_mongoid4 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Time do
4
+ describe "when working with UTC dates" do
5
+ it "should return the correct timestamp" do
6
+ time1 = Time.utc(2011, 1, 1, 0, 0, 0)
7
+ time1.to_i_timestamp.should == 14975
8
+
9
+ time2 = Time.utc(2011, 1, 1, 23, 59, 59)
10
+ time2.to_i_timestamp.should == 14975
11
+ end
12
+
13
+ it "should return the correct hours" do
14
+ time1 = Time.utc(2011, 1, 1, 0, 0, 0)
15
+ time1.to_i_hour.should == 0
16
+
17
+ time2 = Time.utc(2011, 1, 1, 23, 59, 59)
18
+ time2.to_i_hour.should == 23
19
+ end
20
+
21
+ it "should convert back with timestamps and hours" do
22
+ t = Time.from_key(14975, 23).utc
23
+ t.to_s.should == "2011-01-01 23:00:00 UTC"
24
+ end
25
+
26
+ it "should work also on ranges (dates)" do
27
+ time1 = Time.utc(2011, 1, 1, 0, 0, 0)
28
+ range = time1...(time1 + 10*Range::DAYS)
29
+ range.map(&:to_i_timestamp).should == [14975, 14976, 14977, 14978, 14979, 14980, 14981, 14982, 14983, 14984]
30
+ end
31
+
32
+ it "should work also on ranges (hours)" do
33
+ range = Time.utc(2011, 1, 1)...Time.utc(2011, 1, 2)
34
+
35
+ # With Range::HOURS
36
+ range.map(Range::HOURS){|d| d.to_i_hour}.should == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
37
+
38
+ # With ActiveSupport Numeric extensions
39
+ range.map(1.hour){|d| d.to_i_hour}.should == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
40
+ end
41
+
42
+ it "should iterate for a day and return the correct UTC keys" do
43
+ today = Time.utc(2011, 1, 1).whole_day
44
+ today.diff(Range::HOURS).should == 24
45
+ today.map(Range::HOURS) {|d| d.to_key}.should == [
46
+ "14975.0", "14975.1", "14975.2", "14975.3", "14975.4", "14975.5",
47
+ "14975.6", "14975.7", "14975.8", "14975.9", "14975.10", "14975.11",
48
+ "14975.12", "14975.13", "14975.14", "14975.15", "14975.16", "14975.17",
49
+ "14975.18", "14975.19", "14975.20", "14975.21", "14975.22", "14975.23"
50
+ ]
51
+ end
52
+
53
+ it "should iterate for a day and return the correct UTC keys (Helper methods)" do
54
+ today = Time.utc(2011, 1, 1).whole_day
55
+ today.hour_diff.should == 24
56
+ today.hour_map {|d| d.to_key}.should == [
57
+ "14975.0", "14975.1", "14975.2", "14975.3", "14975.4", "14975.5",
58
+ "14975.6", "14975.7", "14975.8", "14975.9", "14975.10", "14975.11",
59
+ "14975.12", "14975.13", "14975.14", "14975.15", "14975.16", "14975.17",
60
+ "14975.18", "14975.19", "14975.20", "14975.21", "14975.22", "14975.23"
61
+ ]
62
+ end
63
+ end
64
+
65
+ describe "when working with TZ dates (Europe)" do
66
+ before do
67
+ ENV["TZ"] = "Europe/Madrid"
68
+ end
69
+
70
+ it "should return the correct timestamp" do
71
+ # NOTE: January 1, 2011 00:00 GMT+1 Timezone corresponds to: December, 31 2010 23:00 UTC
72
+ time1 = Time.local(2011, 1, 1, 0, 0, 0)
73
+ time1.to_i_timestamp.should == 14974
74
+
75
+ time2 = Time.local(2011, 1, 1, 23, 59, 59)
76
+ time2.to_i_timestamp.should == 14975
77
+ end
78
+
79
+ it "should return the correct hours" do
80
+ time1 = Time.local(2011, 1, 1, 0, 0, 0)
81
+ time1.to_i_hour.should == 23 # This is for the previous day
82
+
83
+ time2 = Time.local(2011, 1, 1, 23, 59, 59)
84
+ time2.to_i_hour.should == 22
85
+ end
86
+
87
+ it "should convert back with timestamps and hours" do
88
+ t = Time.from_key(14975, 23)
89
+ t.to_s.should == "2011-01-02 00:00:00 +0100"
90
+ end
91
+ end
92
+
93
+ describe "when working with TZ dates (America)" do
94
+ before do
95
+ ENV["TZ"] = "America/Los_Angeles"
96
+ end
97
+
98
+ it "should return the correct timestamp" do
99
+ time1 = Time.local(2011, 1, 1, 0, 0, 0)
100
+ time1.to_i_timestamp.should == 14975
101
+
102
+ # Note: January 1, 2011 23:00 PST Timezone corresponds to: January, 2 2010 07:00 UTC
103
+ time2 = Time.local(2011, 1, 1, 23, 59, 59)
104
+ time2.to_i_timestamp.should == 14976
105
+ end
106
+
107
+ it "should return the correct hours" do
108
+ time1 = Time.local(2011, 1, 1, 0, 0, 0)
109
+ time1.to_i_hour.should == 8
110
+
111
+ time2 = Time.local(2011, 1, 1, 23, 59, 59)
112
+ time2.to_i_hour.should == 7 # This is for the next day
113
+ end
114
+
115
+ it "should convert back with timestamps and hours" do
116
+ t = Time.from_key(14976, 7)
117
+ t.to_s.should == "2011-01-01 23:00:00 -0800"
118
+ end
119
+
120
+ it "should iterate for a day and return the correct UTC keys" do
121
+ today = Time.local(2011, 1, 1).whole_day
122
+ today.map(Range::HOURS) {|d| d.to_key}.should == [
123
+ "14975.8", "14975.9", "14975.10", "14975.11", "14975.12", "14975.13",
124
+ "14975.14", "14975.15", "14975.16", "14975.17", "14975.18",
125
+ "14975.19", "14975.20", "14975.21", "14975.22", "14975.23",
126
+ "14976.0", "14976.1", "14976.2", "14976.3", "14976.4", "14976.5",
127
+ "14976.6", "14976.7"
128
+ ]
129
+ end
130
+
131
+ it "should iterate for a day and return the correct UTC keys (Helper methods)" do
132
+ today = Time.local(2011, 1, 1).whole_day
133
+ today.hour_map {|d| d.to_key}.should == [
134
+ "14975.8", "14975.9", "14975.10", "14975.11", "14975.12", "14975.13",
135
+ "14975.14", "14975.15", "14975.16", "14975.17", "14975.18",
136
+ "14975.19", "14975.20", "14975.21", "14975.22", "14975.23",
137
+ "14976.0", "14976.1", "14976.2", "14976.3", "14976.4", "14976.5",
138
+ "14976.6", "14976.7"
139
+ ]
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Mongoid::Tracking::ReaderExtender do
4
+ it "should behave like a number" do
5
+ num = Mongoid::Tracking::ReaderExtender.new(5, [])
6
+ num.should == 5
7
+ num.should < 10
8
+ (num * 10).should == 50
9
+ end
10
+
11
+ it "should behave like a float" do
12
+ num = Mongoid::Tracking::ReaderExtender.new(5, [])
13
+ num.should == 5.0
14
+ num.should < 10.0
15
+ (num * 10.0).should == 50.0
16
+ end
17
+
18
+ it "to_f should return a Float (for ActionView::Template compat.)" do
19
+ num = Mongoid::Tracking::ReaderExtender.new(5, [])
20
+ num.to_f.should == 5.0
21
+ num.to_f.should be_kind_of(Float)
22
+ end
23
+
24
+ it "as_json should not return a 'total' and a 'hours' member" do
25
+ json = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3]).as_json
26
+ json.should == 5
27
+ end
28
+
29
+ it "hourly as_json should return an array" do
30
+ json = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3]).hourly.as_json
31
+ json.should == [1, 2, 3]
32
+ end
33
+
34
+ it "should be able to add additional data to it" do
35
+ num = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
36
+ num.hourly.should == [1, 2, 3, 4]
37
+ end
38
+
39
+ it "should be able to sum two ReadersExtenders" do
40
+ a = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
41
+ b = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
42
+ c = a + b
43
+ c.should == 10
44
+ c.hourly.should == [2, 4, 6, 8]
45
+ end
46
+
47
+ it "should be able to sum more than two ReadersExtenders" do
48
+ a = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
49
+ b = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
50
+ c = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
51
+ d = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
52
+ e = Mongoid::Tracking::ReaderExtender.new(5, [1, 2, 3, 4])
53
+ f = a + b + c + d + e
54
+ f.should == 25
55
+ f.hourly.should == [5, 10, 15, 20]
56
+ end
57
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Test
4
+ include Mongoid::Document
5
+ include Mongoid::Tracking
6
+
7
+ field :name # Dummy field
8
+ track :visits
9
+ end
10
+
11
+ describe "Testing Readers with a model" do
12
+ before do
13
+ Test.delete_all
14
+ Test.create(:name => "test")
15
+ @object_id = Test.first.id
16
+ @mock = Test.find(@object_id)
17
+ end
18
+
19
+ it "should return the correct values for a range using 'on'" do
20
+ @mock.visits.set(1, "2010-07-11")
21
+ @mock.visits.set(2, "2010-07-12")
22
+ @mock.visits.set(3, "2010-07-13")
23
+
24
+ range = Date.parse("2010-07-11")..Date.parse("2010-07-13")
25
+ @mock.visits.on(range).should == [1, 2, 3]
26
+ end
27
+
28
+ it "should return the correct values for .all_values" do
29
+ @mock.visits.set(1, "2010-07-11")
30
+ @mock.visits.set(2, "2010-07-12")
31
+ @mock.visits.set(3, "2010-07-13")
32
+
33
+ @mock.visits.all_values.should == [1, 2, 3]
34
+ end
35
+
36
+ it "should return the correct values for .all_values (Take II)" do
37
+ @mock.visits.set(5, "2010-07-01")
38
+ @mock.visits.set(10, "2010-07-30")
39
+
40
+ @mock.visits.all_values.should == [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]
41
+ @mock.visits.last_value.should == 10
42
+ @mock.visits.first_value.should == 5
43
+ end
44
+
45
+ it "should return the total if a value is set" do
46
+ @mock.visits.set(1, "2010-07-11")
47
+ @mock.visits.set(2, "2010-07-12")
48
+ @mock.visits.set(3, "2010-07-13")
49
+ @mock.visits.all_values.sum.should == 6
50
+ @mock.visits.all_values_total.should == 6
51
+ end
52
+
53
+
54
+ it "should return the total if a value is set" do
55
+ #@mock.visits.set(nil, "2010-07-11")
56
+ #@mock.visits.set(nil, "2010-07-12")
57
+ #@mock.visits.set(3, "2010-07-13")
58
+ @mock.visits.all_values.should == nil
59
+ @mock.visits.all_values_total.should == 0
60
+ end
61
+
62
+
63
+ end
64
+
65
+
66
+ describe "Testing Readers with an empty model" do
67
+ before do
68
+ Test.delete_all
69
+ Test.create(:name => "test")
70
+ @object_id = Test.first.id
71
+ @mock = Test.first
72
+ end
73
+
74
+ it "should return nil for .first_date" do
75
+ @mock.visits.first_date.should be_nil
76
+ end
77
+
78
+ it "should return nil for .last_date" do
79
+ @mock.visits.last_date.should be_nil
80
+ end
81
+
82
+ it "should return nil for .first_value" do
83
+ @mock.visits.first_value.should be_nil
84
+ end
85
+
86
+ it "should return nil for .last_value" do
87
+ @mock.visits.last_value.should be_nil
88
+ end
89
+
90
+ it "should return nil for .all_values" do
91
+ @mock.visits.all_values.should be_nil
92
+ end
93
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,21 @@
1
+ # FIXME: We should modify the load path here.
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+
5
+ require 'mongoid'
6
+ require 'trackoid_mongoid4'
7
+ require 'rspec'
8
+ require 'pry'
9
+
10
+ RSpec.configure do |config|
11
+ config.before(:suite) do
12
+ Mongoid.load!(File.expand_path(File.dirname(__FILE__) + "/../config/mongoid.yml"), :test)
13
+ end
14
+ config.expect_with :rspec do |c|
15
+ c.syntax = [:should, :expect]
16
+ end
17
+
18
+ config.after(:each) do
19
+ Mongoid::Config.purge!
20
+ end
21
+ end
@@ -0,0 +1,303 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Mongoid::Tracking do
4
+ describe "Testing the system TZ for Europe/Madrid" do
5
+ before do
6
+ ENV['TZ'] = 'Europe/Madrid'
7
+ end
8
+
9
+ it "should convert dates to UTC" do
10
+ t = Time.now
11
+ t.should_not be_utc
12
+ t.utc
13
+ t.should be_utc
14
+ end
15
+
16
+ it "should create dates in UTC" do
17
+ t = Time.utc(2011, 1, 1, 20, 30)
18
+ t.to_s.should == "2011-01-01 20:30:00 UTC"
19
+ end
20
+
21
+ it "should create dates in local time" do
22
+ t = Time.local(2011, 1, 1, 20, 30)
23
+ t.to_s.should == "2011-01-01 20:30:00 +0100"
24
+ t.utc_offset.should == 3600
25
+ end
26
+
27
+ it "should detect daylight saving for local times" do
28
+ t = Time.local(2011, 6, 1, 20, 30)
29
+ t.should be_dst
30
+ t.to_s.should == "2011-06-01 20:30:00 +0200"
31
+ t.utc_offset.should == 7200
32
+ end
33
+
34
+ it "timestamps should be offset by the utc_offset" do
35
+ local = Time.local(2011, 1, 1, 0, 0)
36
+ utc = Time.utc(2011, 1, 1, 0, 0)
37
+ (utc - local).should == local.utc_offset
38
+ end
39
+
40
+ it "timestamps should be offset by the utc_offset even when daylight saving is on" do
41
+ local = Time.local(2011, 6, 1, 0, 0)
42
+ utc = Time.utc(2011, 6, 1, 0, 0)
43
+ local.should be_dst
44
+ utc.should_not be_dst
45
+ (local - utc).abs.should == local.utc_offset
46
+ end
47
+ end
48
+
49
+ describe "Testing the system TZ for America/San Francisco" do
50
+ before do
51
+ ENV['TZ'] = 'America/Los_Angeles'
52
+ end
53
+
54
+ it "should convert dates to UTC" do
55
+ t = Time.now
56
+ t.should_not be_utc
57
+ t.utc
58
+ t.should be_utc
59
+ end
60
+
61
+ it "should create dates in UTC" do
62
+ t = Time.utc(2011, 1, 1, 20, 30)
63
+ t.to_s.should == "2011-01-01 20:30:00 UTC"
64
+ end
65
+
66
+ it "should create dates in local time" do
67
+ t = Time.local(2011, 1, 1, 20, 30)
68
+ t.to_s.should == "2011-01-01 20:30:00 -0800"
69
+ t.utc_offset.should == -28800
70
+ end
71
+
72
+ it "should detect daylight saving for local times" do
73
+ t = Time.local(2011, 6, 1, 20, 30)
74
+ t.should be_dst
75
+ t.to_s.should == "2011-06-01 20:30:00 -0700"
76
+ t.utc_offset.should == -25200
77
+ end
78
+
79
+ it "timestamps should be offset by the utc_offset" do
80
+ local = Time.local(2011, 1, 1, 0, 0)
81
+ utc = Time.utc(2011, 1, 1, 0, 0)
82
+ (utc - local).should == local.utc_offset
83
+ end
84
+
85
+ it "timestamps should be offset by the utc_offset even when daylight saving is on" do
86
+ local = Time.local(2011, 6, 1, 0, 0)
87
+ utc = Time.utc(2011, 6, 1, 0, 0)
88
+ local.should be_dst
89
+ utc.should_not be_dst
90
+ (utc - local).should == local.utc_offset
91
+ end
92
+ end
93
+
94
+ describe "Testing the RAILS TZ capabilities" do
95
+ before do
96
+ ENV['TZ'] = nil
97
+ end
98
+
99
+ it "should support time zone changing" do
100
+ Time.zone = ActiveSupport::TimeZone["America/Los_Angeles"]
101
+
102
+ local = Time.local(2011, 6, 1, 0, 0).in_time_zone
103
+ local.should be_dst
104
+ local.utc_offset.should == -25200
105
+
106
+ Time.zone = ActiveSupport::TimeZone["Europe/Madrid"]
107
+ local = Time.local(2011, 6, 1, 0, 0).in_time_zone
108
+ local.should be_dst
109
+ local.utc_offset.should == 7200
110
+ end
111
+
112
+ it "should correctly handle UTC offseted dates" do
113
+ utc = Time.utc(2011, 6, 1, 0, 0)
114
+ utc_localized = Time.utc(2011, 6, 1, 0, 0) + 7200
115
+
116
+ (utc_localized - utc).should == 7200
117
+ end
118
+ end
119
+
120
+ describe "Testing TZ data with real models" do
121
+ before(:all) do
122
+ class Test
123
+ include Mongoid::Document
124
+ include Mongoid::Tracking
125
+
126
+ field :name # Dummy field
127
+ track :visits
128
+ end
129
+ end
130
+
131
+ before do
132
+ Test.delete_all
133
+ Test.create(:name => "test")
134
+ @object_id = Test.first.id
135
+ @mock = Test.find(@object_id)
136
+ end
137
+
138
+ it "should correctly handle hours for my TimeZone" do
139
+ # WARNING: Volatile test, time dependant... Do not run at 00:00:00 :-)
140
+ t1 = Time.now.change(:hour => 0)
141
+ t2 = Time.now.change(:hour => 23)
142
+ @mock.visits.inc(t1)
143
+ @mock.visits.inc(t2)
144
+ @mock.reload
145
+ expect(@mock.visits.today.to_i).to eq 2
146
+ expect(@mock.visits.today.hourly).to eq [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
147
+ end
148
+
149
+ it "should correctly handle hours for UTC" do
150
+ # WARNING: Volatile test, time dependant... Do not run at 00:00:00 :-)
151
+ t1 = Time.now.utc.change(:hour => 0)
152
+ t2 = Time.now.utc.change(:hour => 23)
153
+
154
+ @mock.visits.inc(t1)
155
+ @mock.visits.inc(t2)
156
+ @mock.reload
157
+ expect(@mock.visits.on(Time.now.utc).to_i).to eq 2
158
+ expect(@mock.visits.on(Time.now.utc).hourly).to eq [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
159
+ end
160
+
161
+ it "Hours in Europe/Madrid (Winter time) should be shifted by 1 hour from UTC" do
162
+ ENV["TZ"] = "Europe/Madrid"
163
+
164
+ time = Time.parse("2011-01-01")
165
+ t1 = time.change(:hour => 1)
166
+ t2 = time.change(:hour => 22)
167
+
168
+ @mock.visits.inc(t1)
169
+ @mock.visits.inc(t2)
170
+
171
+ # This test is interesting. We added with local TZ time but want to
172
+ # query shifted data on UTC. We need to read the expected span dates
173
+ # separately
174
+ @mock.reload
175
+ visits = @mock.visits.on(time.utc..(time.utc + 1.day))
176
+
177
+ # Data from 2010-12-31 00:00:00 UTC up to 2011-12-31 23:59:59 UTC
178
+ expect(visits.first.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
179
+
180
+ # Data from 2011-01-01 00:00:00 UTC up to 2011-01-01 23:59:59 UTC
181
+ expect(visits.last.hourly).to eq [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
182
+ end
183
+
184
+ it "Hours in Europe/Madrid (Summer time) should be shifted by 2 hour from UTC" do
185
+ ENV["TZ"] = "Europe/Madrid"
186
+
187
+ time = Time.parse("2011-06-01")
188
+ t1 = time.change(:hour => 1)
189
+ t2 = time.change(:hour => 22)
190
+
191
+ @mock.visits.inc(t1)
192
+ @mock.visits.inc(t2)
193
+
194
+ # This test is interesting. We added with local TZ time but want to
195
+ # query shifted data on UTC. We need to read the expected span dates
196
+ # separately
197
+ @mock.reload
198
+ visits = @mock.visits.on(time.utc..(time.utc + 1.day))
199
+
200
+ # Data from 2011-05-31 00:00:00 UTC up to 2011-05-31 23:59:59 UTC
201
+ expect(visits.first.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
202
+
203
+ # Data from 2011-06-01 00:00:00 UTC up to 2011-06-01 23:59:59 UTC
204
+ expect(visits.last.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
205
+ end
206
+
207
+ it "Hours in America/Los_Angeles (Winter time) should be shifted by -8 hours from UTC" do
208
+ ENV["TZ"] = "America/Los_Angeles"
209
+
210
+ time = Time.parse("2011-01-01")
211
+ t1 = time.change(:hour => 1)
212
+ t2 = time.change(:hour => 22)
213
+
214
+ @mock.visits.inc(t1)
215
+ @mock.visits.inc(t2)
216
+
217
+ # This test is interesting. We added with local TZ time but want to
218
+ # query shifted data on UTC. We need to read the expected span dates
219
+ # separately
220
+ @mock.reload
221
+ visits = @mock.visits.on(time.utc..(time.utc + 1.day))
222
+
223
+ # Data from 2011-01-01 00:00:00 UTC up to 2011-01-01 23:59:59 UTC
224
+ expect(visits.first.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
225
+
226
+ # Data from 2011-01-02 00:00:00 UTC up to 2011-01-02 23:59:59 UTC
227
+ expect(visits.last.hourly).to eq [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
228
+ end
229
+
230
+ it "Hours in America/Los_Angeles (Summer time) should be shifted by -7 hours from UTC" do
231
+ ENV["TZ"] = "America/Los_Angeles"
232
+
233
+ time = Time.parse("2011-06-01")
234
+ t1 = time.change(:hour => 1)
235
+ t2 = time.change(:hour => 22)
236
+
237
+ @mock.visits.inc(t1)
238
+ @mock.visits.inc(t2)
239
+
240
+ # This test is interesting. We added with local TZ time but want to
241
+ # query shifted data on UTC. We need to read the expected span dates
242
+ # separately
243
+ @mock.reload
244
+
245
+ visits = @mock.visits.on(time.utc..(time.utc + 1.day))
246
+
247
+ # Data from 2011-01-01 00:00:00 UTC up to 2011-01-01 23:59:59 UTC
248
+ expect(visits.first.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
249
+
250
+ # Data from 2011-01-02 00:00:00 UTC up to 2011-01-02 23:59:59 UTC
251
+ expect(visits.last.hourly).to eq [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
252
+ end
253
+
254
+ it "A value set at 10am on Madrid should appear as 01am on San Francisco" do
255
+ ENV["TZ"] = "Europe/Madrid"
256
+
257
+ time = Time.parse("2011-04-19 10:00:00")
258
+ @mock.visits.inc(time)
259
+ @mock.reload
260
+ visits = @mock.visits.on(time)
261
+ expect(visits.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
262
+
263
+ ENV["TZ"] = "America/Los_Angeles"
264
+ time = Time.parse("2011-04-19 01:00:00")
265
+ visits = @mock.visits.on(time)
266
+ expect(visits.hourly).to eq [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
267
+ end
268
+
269
+ it "A value set at 10am on Madrid should appear as 01am on San Francisco (Using offsets)" do
270
+ pending "Time.getlocal isn't implemented in JRuby" if RUBY_ENGINE == 'jruby'
271
+
272
+ ENV["TZ"] = "Europe/Madrid"
273
+
274
+ time = Time.parse("2011-04-19 10:00:00").getlocal('+02:00')
275
+ @mock.visits.inc(time)
276
+ @mock.reload
277
+ visits = @mock.visits.on(time)
278
+ expect(visits.hourly).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
279
+
280
+ time = Time.parse("2011-04-19 10:00:00").getlocal('-07:00')
281
+ visits = @mock.visits.on(time)
282
+ expect(visits.hourly).to eq [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
283
+ end
284
+
285
+ it "A value set now on Madrid should appear shifted 9 hours on San Francisco (Using offsets)" do
286
+ pending "Time.getlocal isn't implemented in JRuby" if RUBY_ENGINE == 'jruby'
287
+
288
+ ENV["TZ"] = "Europe/Madrid"
289
+
290
+ now = Time.now
291
+ time = now.getlocal('+02:00')
292
+ @mock.visits.inc(time)
293
+ @mock.reload
294
+ visits = @mock.visits.on(time)
295
+ expect(visits.hourly).to eq Array.new(24, 0).tap {|a| a[time.hour] = 1}
296
+
297
+ time = now.getlocal('-07:00')
298
+ visits = @mock.visits.on(time)
299
+ expect(visits.hourly).to eq Array.new(24, 0).tap {|a| a[time.hour] = 1}
300
+ end
301
+
302
+ end
303
+ end