acts_as_flux_capacitor 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "The Temporal mixin" do
4
+
5
+ before(:all) do
6
+ @present_time = Time.parse "Sun May 18 16:41:58 2008"
7
+ @twenty_mins_later = Time.parse "Sun May 18 17:01:58 2008"
8
+ end
9
+
10
+ it "should make Time, Date and DateTime into Temporal objects" do
11
+ [Time, Date, DateTime].each do |klass|
12
+ klass.included_modules.should include(Temporal)
13
+ end
14
+ end
15
+
16
+ it "should determine the length of time between two Temporal objects" do
17
+ Time.period_between(@present_time,@twenty_mins_later).should == 20.minutes
18
+ end
19
+
20
+ it "should determine the length of time until a Temporal object" do
21
+ Time.length_of_time_until(@twenty_mins_later,@present_time).should == 20.minutes
22
+ end
23
+
24
+ it "should determine if a temporal object is in the future" do
25
+ @twenty_mins_later.in_the_future?(@present_time).should be_true
26
+ @present_time.in_the_future?(@twenty_mins_later).should be_false
27
+ end
28
+
29
+ it "should determine the length of time since a Temporal object" do
30
+ Time.length_of_time_since(@present_time,@twenty_mins_later).should == 20.minutes
31
+ end
32
+
33
+ it "should determine if a temporal object is in the past" do
34
+ @present_time.in_the_past?(@twenty_mins_later).should be_true
35
+ @twenty_mins_later.in_the_past?(@present_time).should be_false
36
+ end
37
+
38
+ it "should determine if one Temporal object is before another" do
39
+ @present_time.before?(@twenty_mins_later).should be_true
40
+ @twenty_mins_later.before?(@present_time).should be_false
41
+ end
42
+
43
+ it "should determine if one Temporal object is after another" do
44
+ @twenty_mins_later.before?(@present_time).should be_false
45
+ @present_time.before?(@twenty_mins_later).should be_true
46
+ end
47
+
48
+ end
49
+
50
+ describe "The class TimePeriod < ActiveRecord::Base" do
51
+
52
+ it "should be a flux capacitor" do
53
+ TimePeriod.flux_capacitor?.should be_true
54
+ end
55
+
56
+ it "should be a Temporal object" do
57
+ TimePeriod.should include(Temporal)
58
+ end
59
+
60
+ it "should possess all four bitemporal database attributes" do
61
+ TimePeriod.column_names.should include(
62
+ 'begins_at','ends_at','created_at','deleted_at' )
63
+ end
64
+
65
+ it "should not be valid if either of the two time spans are 0 or less" do
66
+ time_period = TimePeriod.new
67
+ time_period.should_not be_valid
68
+
69
+ time_period.created_at = @present_time
70
+ time_period.begins_at = @present_time
71
+
72
+ time_period.deleted_at = @twenty_mins_later
73
+ time_period.ends_at = @twenty_mins_later
74
+ end
75
+ end
76
+
77
+ describe "The TimePeriod.find extensions" do
78
+
79
+ include TimePeriodFixtures
80
+
81
+ it "should find time_periods in the present" do
82
+ TimePeriod.find(:first,:present=>true ,:current_time => present_time).
83
+ happening_now?(present_time).should be_true
84
+ end
85
+
86
+ it "should find time_periods in the past" do
87
+ TimePeriod.find(:first,:past=>true ,:current_time => present_time).
88
+ in_the_past?(present_time).should be_true
89
+ end
90
+
91
+ it "should find time_periods in the future" do
92
+ TimePeriod.find(:first,:future=>true ,:current_time => present_time).
93
+ in_the_future?(present_time).should be_true
94
+ end
95
+
96
+ it "should find time_periods occuring at a specific time" do
97
+ TimePeriod.find(:first,:at=>future_time).at?(future_time).should be_true
98
+ end
99
+
100
+ it "should find time_periods occuring within a specific range of time" do
101
+ TimePeriod.find(:first,:from => more_past_time , :to => more_future_time).
102
+ at?(present_time).should be_true
103
+ end
104
+
105
+ end
106
+
107
+ describe "The TimePeriod comparison helpers" do
108
+
109
+ include TimePeriodFixtures
110
+
111
+ it "should calculate the time between time_periods" do
112
+ TimePeriod.period_between(past_period,future_period).should == 140.minutes
113
+ end
114
+
115
+ it "should determine whether two time_periods overlap" do
116
+ past_period.overlaps_with?(future_period).should be_false
117
+ end
118
+
119
+ it "should determine the length of time by which two time_periods overlap" do
120
+ future_period.overlap_with(long_period).should == 3.hours
121
+ end
122
+
123
+ it "should determine if two time_periods are back-to-back" do
124
+ long_period.back_to_back_with?(future_period).should be_false
125
+ past_period.back_to_back_with?(present_period).should be_true
126
+ end
127
+
128
+ it "should determine if a time_period is between two other time_periods" do
129
+ present_period.between?(past_period,future_period).should be_true
130
+ future_period.between?(past_period,present_period).should be_false
131
+ end
132
+ end
133
+
134
+ describe "The TimePeriod status helpers" do
135
+
136
+ it "should determine if a time_period has started" do
137
+ past_period.started?(present_time).should be_true
138
+ present_period.started?(present_time).should be_true
139
+ future_period.started?(present_time).should be_false
140
+ end
141
+
142
+ it "should determine how much time has elapsed since an event started" do
143
+ past_period.elapsed(present_time).should be_nil
144
+ present_period.elapsed(present_time).should == 2.hours
145
+ future_period.elapsed(present_time).should be_nil
146
+ end
147
+
148
+ it "should determine what percentage of time has elapsed since an event started" do
149
+ past_period.percent_elapsed(present_time).should be_nil
150
+ present_period.percent_elapsed(present_time).should eql(7200.0 / (7200+1200) * 100)
151
+ future_period.percent_elapsed(present_time).should be_nil
152
+ end
153
+
154
+ it "should determine how much time is remaining until an event ends" do
155
+ past_period.remaining(present_time).should be_nil
156
+ present_period.remaining(present_time).should == 20.minutes
157
+ future_period.remaining(present_time).should be_nil
158
+ end
159
+
160
+ it "should determine what percentage of time is remaining until an event ends" do
161
+ past_period.percent_remaining(present_time).should be_nil
162
+ present_period.percent_remaining(present_time).should eql(1200.0 / (7200+1200) * 100)
163
+ future_period.percent_remaining(present_time).should be_nil
164
+ end
165
+
166
+ it "should determine if a time_period has ended" do
167
+ past_period.ended?(present_time).should be_true
168
+ present_period.ended?(present_time).should be_false
169
+ future_period.ended?(present_time).should be_false
170
+ end
171
+
172
+ it "should determine the duration of a time_period" do
173
+ future_period.duration.should == 3.hours
174
+ end
175
+
176
+ end
177
+
178
+ describe "The TimePeriod modification helpers" do
179
+
180
+ it "should shift a time_period into the past or future" do
181
+ less_future_period = future_period.shift(-7200)
182
+
183
+ less_future_period.duration.should == future_period.duration
184
+ TimePeriod.length_of_time_until(future_period.begins_at,less_future_period.begins_at).
185
+ should == 2.hours
186
+ TimePeriod.length_of_time_until(future_period.ends_at,less_future_period.ends_at).
187
+ should == 2.hours
188
+ end
189
+
190
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,8 @@
1
+ mysql:
2
+ adapter: mysql
3
+ encoding: utf8
4
+ database: acts_as_flux_capacitor_test
5
+ username: root
6
+ password:
7
+ host: localhost
8
+ socket: /var/run/mysqld/mysqld.sock
data/spec/fixtures.rb ADDED
@@ -0,0 +1,57 @@
1
+ module TimePeriodFixtures
2
+
3
+ # require 'time'
4
+
5
+ def more_past_time
6
+ Time.parse "Sun May 18 11:41:58 2008"
7
+ end
8
+
9
+ def past_time
10
+ Time.parse "Sun May 18 14:41:58 2008"
11
+ end
12
+
13
+ def present_time
14
+ Time.parse "Sun May 18 16:41:58 2008"
15
+ end
16
+
17
+ def future_time
18
+ Time.parse "Sun May 18 17:01:58 2008"
19
+ end
20
+
21
+ def more_future_time
22
+ Time.parse "Sun May 18 20:01:58 2008"
23
+ end
24
+
25
+ ###################################
26
+
27
+ def present_period
28
+ time_period = TimePeriod.find_or_initialize_by_begins_at_and_ends_at past_time,future_time
29
+ time_period.save!
30
+ time_period
31
+ end
32
+
33
+ def past_period
34
+ time_period = TimePeriod.find_or_initialize_by_begins_at_and_ends_at more_past_time , past_time
35
+ time_period.save!
36
+ time_period
37
+ end
38
+
39
+ def future_period
40
+ time_period = TimePeriod.find_or_initialize_by_begins_at_and_ends_at future_time,more_future_time
41
+ time_period.save!
42
+ time_period
43
+ end
44
+
45
+ def long_period
46
+ time_period = TimePeriod.find_or_initialize_by_begins_at_and_ends_at past_time , more_future_time
47
+ time_period.save!
48
+ time_period
49
+ end
50
+
51
+ def put_fixtures_in_db!
52
+ present_period
53
+ past_period
54
+ future_period
55
+ long_period
56
+ end
57
+ end
@@ -0,0 +1,274 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
+ <head>
7
+ <title>RSpec results</title>
8
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
+ <meta http-equiv="Expires" content="-1" />
10
+ <meta http-equiv="Pragma" content="no-cache" />
11
+ <style type="text/css">
12
+ body {
13
+ margin: 0;
14
+ padding: 0;
15
+ background: #fff;
16
+ font-size: 80%;
17
+ }
18
+ </style>
19
+ </head>
20
+ <body>
21
+ <div class="rspec-report">
22
+ <script type="text/javascript">
23
+ // <![CDATA[
24
+ function moveProgressBar(percentDone) {
25
+ document.getElementById("rspec-header").style.width = percentDone +"%";
26
+ }
27
+ function makeRed(element_id) {
28
+ document.getElementById(element_id).style.background = '#C40D0D';
29
+ document.getElementById(element_id).style.color = '#FFFFFF';
30
+ }
31
+
32
+ function makeYellow(element_id) {
33
+ if (element_id == "rspec-header" && document.getElementById(element_id).style.background != '#C40D0D')
34
+ {
35
+ document.getElementById(element_id).style.background = '#FAF834';
36
+ document.getElementById(element_id).style.color = '#000000';
37
+ }
38
+ else
39
+ {
40
+ document.getElementById(element_id).style.background = '#FAF834';
41
+ document.getElementById(element_id).style.color = '#000000';
42
+ }
43
+ }
44
+
45
+ // ]]>
46
+ </script>
47
+ <style type="text/css">
48
+ #rspec-header {
49
+ background: #65C400; color: #fff;
50
+ }
51
+
52
+ .rspec-report h1 {
53
+ margin: 0px 10px 0px 10px;
54
+ padding: 10px;
55
+ font-family: "Lucida Grande", Helvetica, sans-serif;
56
+ font-size: 1.8em;
57
+ }
58
+
59
+ #summary {
60
+ margin: 0; padding: 5px 10px;
61
+ font-family: "Lucida Grande", Helvetica, sans-serif;
62
+ text-align: right;
63
+ position: absolute;
64
+ top: 0px;
65
+ right: 0px;
66
+ }
67
+
68
+ #summary p {
69
+ margin: 0 0 0 2px;
70
+ }
71
+
72
+ #summary #totals {
73
+ font-size: 1.2em;
74
+ }
75
+
76
+ .example_group {
77
+ margin: 0 10px 5px;
78
+ background: #fff;
79
+ }
80
+
81
+ dl {
82
+ margin: 0; padding: 0 0 5px;
83
+ font: normal 11px "Lucida Grande", Helvetica, sans-serif;
84
+ }
85
+
86
+ dt {
87
+ padding: 3px;
88
+ background: #65C400;
89
+ color: #fff;
90
+ font-weight: bold;
91
+ }
92
+
93
+ dd {
94
+ margin: 5px 0 5px 5px;
95
+ padding: 3px 3px 3px 18px;
96
+ }
97
+
98
+ dd.spec.passed {
99
+ border-left: 5px solid #65C400;
100
+ border-bottom: 1px solid #65C400;
101
+ background: #DBFFB4; color: #3D7700;
102
+ }
103
+
104
+ dd.spec.failed {
105
+ border-left: 5px solid #C20000;
106
+ border-bottom: 1px solid #C20000;
107
+ color: #C20000; background: #FFFBD3;
108
+ }
109
+
110
+ dd.spec.not_implemented {
111
+ border-left: 5px solid #FAF834;
112
+ border-bottom: 1px solid #FAF834;
113
+ background: #FCFB98; color: #131313;
114
+ }
115
+
116
+ dd.spec.pending_fixed {
117
+ border-left: 5px solid #0000C2;
118
+ border-bottom: 1px solid #0000C2;
119
+ color: #0000C2; background: #D3FBFF;
120
+ }
121
+
122
+ .backtrace {
123
+ color: #000;
124
+ font-size: 12px;
125
+ }
126
+
127
+ a {
128
+ color: #BE5C00;
129
+ }
130
+
131
+ /* Ruby code, style similar to vibrant ink */
132
+ .ruby {
133
+ font-size: 12px;
134
+ font-family: monospace;
135
+ color: white;
136
+ background-color: black;
137
+ padding: 0.1em 0 0.2em 0;
138
+ }
139
+
140
+ .ruby .keyword { color: #FF6600; }
141
+ .ruby .constant { color: #339999; }
142
+ .ruby .attribute { color: white; }
143
+ .ruby .global { color: white; }
144
+ .ruby .module { color: white; }
145
+ .ruby .class { color: white; }
146
+ .ruby .string { color: #66FF00; }
147
+ .ruby .ident { color: white; }
148
+ .ruby .method { color: #FFCC00; }
149
+ .ruby .number { color: white; }
150
+ .ruby .char { color: white; }
151
+ .ruby .comment { color: #9933CC; }
152
+ .ruby .symbol { color: white; }
153
+ .ruby .regex { color: #44B4CC; }
154
+ .ruby .punct { color: white; }
155
+ .ruby .escape { color: white; }
156
+ .ruby .interp { color: white; }
157
+ .ruby .expr { color: white; }
158
+
159
+ .ruby .offending { background-color: gray; }
160
+ .ruby .linenum {
161
+ width: 75px;
162
+ padding: 0.1em 1em 0.2em 0;
163
+ color: #000000;
164
+ background-color: #FFFBD3;
165
+ }
166
+
167
+ </style>
168
+
169
+ <div id="rspec-header">
170
+ <h1>RSpec Results</h1>
171
+
172
+ <div id="summary">
173
+ <p id="totals">&nbsp;</p>
174
+ <p id="duration">&nbsp;</p>
175
+ </div>
176
+ </div>
177
+
178
+ <div class="results">
179
+ <div class="example_group">
180
+ <dl>
181
+ <dt id="example_group_1">The Temporal mixin</dt>
182
+ <script type="text/javascript">moveProgressBar('3.3');</script>
183
+ <dd class="spec passed"><span class="passed_spec_name">should make Time, Date and DateTime into Temporal objects</span></dd>
184
+ <script type="text/javascript">moveProgressBar('6.6');</script>
185
+ <dd class="spec passed"><span class="passed_spec_name">should determine the length of time between two Temporal objects</span></dd>
186
+ <script type="text/javascript">moveProgressBar('10.0');</script>
187
+ <dd class="spec passed"><span class="passed_spec_name">should determine the length of time until a Temporal object</span></dd>
188
+ <script type="text/javascript">moveProgressBar('13.3');</script>
189
+ <dd class="spec passed"><span class="passed_spec_name">should determine if a temporal object is in the future</span></dd>
190
+ <script type="text/javascript">moveProgressBar('16.6');</script>
191
+ <dd class="spec passed"><span class="passed_spec_name">should determine the length of time since a Temporal object</span></dd>
192
+ <script type="text/javascript">moveProgressBar('20.0');</script>
193
+ <dd class="spec passed"><span class="passed_spec_name">should determine if a temporal object is in the past</span></dd>
194
+ <script type="text/javascript">moveProgressBar('23.3');</script>
195
+ <dd class="spec passed"><span class="passed_spec_name">should determine if one Temporal object is before another</span></dd>
196
+ <script type="text/javascript">moveProgressBar('26.6');</script>
197
+ <dd class="spec passed"><span class="passed_spec_name">should determine if one Temporal object is after another</span></dd>
198
+ </dl>
199
+ </div>
200
+ <div class="example_group">
201
+ <dl>
202
+ <dt id="example_group_2">The class TimePeriod &lt; ActiveRecord::Base</dt>
203
+ <script type="text/javascript">moveProgressBar('30.0');</script>
204
+ <dd class="spec passed"><span class="passed_spec_name">should be a flux capacitor</span></dd>
205
+ <script type="text/javascript">moveProgressBar('33.3');</script>
206
+ <dd class="spec passed"><span class="passed_spec_name">should be a Temporal object</span></dd>
207
+ <script type="text/javascript">moveProgressBar('36.6');</script>
208
+ <dd class="spec passed"><span class="passed_spec_name">should possess all four bitemporal database attributes</span></dd>
209
+ <script type="text/javascript">moveProgressBar('40.0');</script>
210
+ <dd class="spec passed"><span class="passed_spec_name">should not be valid if either of the two time spans are 0 or less</span></dd>
211
+ </dl>
212
+ </div>
213
+ <div class="example_group">
214
+ <dl>
215
+ <dt id="example_group_3">The TimePeriod.find extensions</dt>
216
+ <script type="text/javascript">moveProgressBar('43.3');</script>
217
+ <dd class="spec passed"><span class="passed_spec_name">should find time_periods in the present</span></dd>
218
+ <script type="text/javascript">moveProgressBar('46.6');</script>
219
+ <dd class="spec passed"><span class="passed_spec_name">should find time_periods in the past</span></dd>
220
+ <script type="text/javascript">moveProgressBar('50.0');</script>
221
+ <dd class="spec passed"><span class="passed_spec_name">should find time_periods in the future</span></dd>
222
+ <script type="text/javascript">moveProgressBar('53.3');</script>
223
+ <dd class="spec passed"><span class="passed_spec_name">should find time_periods occuring at a specific time</span></dd>
224
+ <script type="text/javascript">moveProgressBar('56.6');</script>
225
+ <dd class="spec passed"><span class="passed_spec_name">should find time_periods occuring within a specific range of time</span></dd>
226
+ </dl>
227
+ </div>
228
+ <div class="example_group">
229
+ <dl>
230
+ <dt id="example_group_4">The TimePeriod comparison helpers</dt>
231
+ <script type="text/javascript">moveProgressBar('60.0');</script>
232
+ <dd class="spec passed"><span class="passed_spec_name">should calculate the time between time_periods</span></dd>
233
+ <script type="text/javascript">moveProgressBar('63.3');</script>
234
+ <dd class="spec passed"><span class="passed_spec_name">should determine whether two time_periods overlap</span></dd>
235
+ <script type="text/javascript">moveProgressBar('66.6');</script>
236
+ <dd class="spec passed"><span class="passed_spec_name">should determine the length of time by which two time_periods overlap</span></dd>
237
+ <script type="text/javascript">moveProgressBar('70.0');</script>
238
+ <dd class="spec passed"><span class="passed_spec_name">should determine if two time_periods are back-to-back</span></dd>
239
+ <script type="text/javascript">moveProgressBar('73.3');</script>
240
+ <dd class="spec passed"><span class="passed_spec_name">should determine if a time_period is between two other time_periods</span></dd>
241
+ </dl>
242
+ </div>
243
+ <div class="example_group">
244
+ <dl>
245
+ <dt id="example_group_5">The TimePeriod status helpers</dt>
246
+ <script type="text/javascript">moveProgressBar('76.6');</script>
247
+ <dd class="spec passed"><span class="passed_spec_name">should determine if a time_period has started</span></dd>
248
+ <script type="text/javascript">moveProgressBar('80.0');</script>
249
+ <dd class="spec passed"><span class="passed_spec_name">should determine how much time has elapsed since an event started</span></dd>
250
+ <script type="text/javascript">moveProgressBar('83.3');</script>
251
+ <dd class="spec passed"><span class="passed_spec_name">should determine what percentage of time has elapsed since an event started</span></dd>
252
+ <script type="text/javascript">moveProgressBar('86.6');</script>
253
+ <dd class="spec passed"><span class="passed_spec_name">should determine how much time is remaining until an event ends</span></dd>
254
+ <script type="text/javascript">moveProgressBar('90.0');</script>
255
+ <dd class="spec passed"><span class="passed_spec_name">should determine what percentage of time is remaining until an event ends</span></dd>
256
+ <script type="text/javascript">moveProgressBar('93.3');</script>
257
+ <dd class="spec passed"><span class="passed_spec_name">should determine if a time_period has ended</span></dd>
258
+ <script type="text/javascript">moveProgressBar('96.6');</script>
259
+ <dd class="spec passed"><span class="passed_spec_name">should determine the duration of a time_period</span></dd>
260
+ </dl>
261
+ </div>
262
+ <div class="example_group">
263
+ <dl>
264
+ <dt id="example_group_6">The TimePeriod modification helpers</dt>
265
+ <script type="text/javascript">moveProgressBar('100.0');</script>
266
+ <dd class="spec passed"><span class="passed_spec_name">should shift a time_period into the past or future</span></dd>
267
+ </dl>
268
+ </div>
269
+ <script type="text/javascript">document.getElementById('duration').innerHTML = "Finished in <strong>0.292237 seconds</strong>";</script>
270
+ <script type="text/javascript">document.getElementById('totals').innerHTML = "30 examples, 0 failures";</script>
271
+ </div>
272
+ </div>
273
+ </body>
274
+ </html>