by_star 3.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/mysql.yml +92 -0
  3. data/.github/workflows/postgresql.yml +99 -0
  4. data/.gitignore +1 -0
  5. data/.travis.yml +67 -36
  6. data/CHANGELOG.md +15 -0
  7. data/README.md +172 -133
  8. data/UPGRADING +1 -3
  9. data/by_star.gemspec +2 -2
  10. data/lib/by_star/base.rb +16 -15
  11. data/lib/by_star/between.rb +81 -52
  12. data/lib/by_star/directional.rb +2 -4
  13. data/lib/by_star/kernel/date.rb +0 -9
  14. data/lib/by_star/kernel/in_time_zone.rb +20 -0
  15. data/lib/by_star/normalization.rb +52 -23
  16. data/lib/by_star/orm/active_record/by_star.rb +20 -6
  17. data/lib/by_star/orm/mongoid/by_star.rb +20 -6
  18. data/lib/by_star/version.rb +1 -1
  19. data/lib/by_star.rb +1 -0
  20. data/spec/database.yml +1 -1
  21. data/spec/fixtures/active_record/models.rb +2 -2
  22. data/spec/fixtures/mongoid/models.rb +1 -1
  23. data/spec/fixtures/shared/seeds.rb +10 -0
  24. data/spec/gemfiles/Gemfile.rails +5 -0
  25. data/spec/gemfiles/Gemfile.rails32 +7 -0
  26. data/spec/gemfiles/Gemfile.rails40 +1 -1
  27. data/spec/gemfiles/Gemfile.rails41 +1 -1
  28. data/spec/gemfiles/Gemfile.rails42 +2 -2
  29. data/spec/gemfiles/Gemfile.rails50 +1 -4
  30. data/spec/gemfiles/Gemfile.rails51 +1 -4
  31. data/spec/gemfiles/Gemfile.rails52 +7 -0
  32. data/spec/gemfiles/Gemfile.rails60 +7 -0
  33. data/spec/gemfiles/Gemfile.rails61 +7 -0
  34. data/spec/integration/active_record/active_record_spec.rb +6 -3
  35. data/spec/integration/mongoid/mongoid_spec.rb +3 -1
  36. data/spec/integration/shared/at_time.rb +53 -0
  37. data/spec/integration/shared/between_dates.rb +99 -0
  38. data/spec/integration/shared/between_times.rb +27 -10
  39. data/spec/integration/shared/by_day.rb +24 -0
  40. data/spec/integration/shared/by_direction.rb +11 -57
  41. data/spec/integration/shared/index_scope_parameter.rb +111 -0
  42. data/spec/spec_helper.rb +4 -0
  43. data/spec/unit/kernel_date_spec.rb +6 -6
  44. data/spec/unit/normalization_spec.rb +128 -49
  45. metadata +33 -21
  46. data/spec/gemfiles/Gemfile.master +0 -6
  47. data/spec/integration/shared/scope_parameter.rb +0 -73
@@ -4,7 +4,4 @@ gemspec path: '../../'
4
4
 
5
5
  gem 'activerecord', '~> 5.0.0'
6
6
  gem 'pg', '~> 0.18'
7
- gem 'mongoid', '~> 6.0'
8
- # Due to weird connection issues in 2.5.0
9
- # TODO: double check this for non 2.5.0 versions
10
- gem 'mongo', '~> 2.4.3'
7
+ gem 'mongoid'
@@ -4,7 +4,4 @@ gemspec path: '../../'
4
4
 
5
5
  gem 'activerecord', '~> 5.1.0'
6
6
  gem 'pg', '~> 0.18'
7
- gem 'mongoid', '~> 6.0'
8
- # Due to weird connection issues in 2.5.0
9
- # TODO: double check this for non 2.5.0 versions
10
- gem 'mongo', '~> 2.4.3'
7
+ gem 'mongoid'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 5.2.0'
6
+ gem 'pg', '~> 0.18'
7
+ gem 'mongoid'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 6.0'
6
+ gem 'pg'
7
+ gem 'mongoid'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 6.1'
6
+ gem 'pg'
7
+ gem 'mongoid'
@@ -5,8 +5,9 @@ describe ActiveRecord do
5
5
  before(:all) do
6
6
  ActiveRecord::Base.default_timezone = :utc
7
7
  # ActiveRecord::Base.logger = Logger.new(STDOUT)
8
-
9
- db_config = YAML::load_file(File.dirname(__FILE__) + '/../../database.yml')
8
+ database_file = File.dirname(__FILE__) + '/../../database.yml'
9
+ parsed_config = ERB.new(File.read(database_file)).result
10
+ db_config = YAML.safe_load(parsed_config)
10
11
  if db_config.has_key?('sqlite') && db_config['sqlite'].has_key?('database')
11
12
  db_config['sqlite']['database'] = File.dirname(__FILE__) + '/../../tmp/' + db_config['sqlite']['database']
12
13
  end
@@ -21,9 +22,11 @@ describe ActiveRecord do
21
22
  end
22
23
 
23
24
  it_behaves_like 'between_times'
25
+ it_behaves_like 'between_dates'
26
+ it_behaves_like 'at_time'
24
27
  it_behaves_like 'offset parameter'
25
28
  it_behaves_like 'order parameter'
26
- it_behaves_like 'scope parameter'
29
+ it_behaves_like 'index_scope parameter'
27
30
  it_behaves_like 'by day'
28
31
  it_behaves_like 'by direction'
29
32
  it_behaves_like 'by fortnight'
@@ -20,9 +20,11 @@ describe 'Mongoid' do
20
20
  end
21
21
 
22
22
  it_behaves_like 'between_times'
23
+ it_behaves_like 'between_dates'
24
+ it_behaves_like 'at_time'
23
25
  it_behaves_like 'offset parameter'
24
26
  it_behaves_like 'order parameter'
25
- it_behaves_like 'scope parameter'
27
+ it_behaves_like 'index_scope parameter'
26
28
  it_behaves_like 'by day'
27
29
  it_behaves_like 'by direction'
28
30
  it_behaves_like 'by fortnight'
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'at_time' do
4
+
5
+ describe '#at_time' do
6
+
7
+ context 'point object' do
8
+
9
+ context 'exactly equal' do
10
+ subject { Post.at_time(Time.zone.parse('2013-12-28 17:00:00')) }
11
+ it { expect(subject.count).to eql(1) }
12
+ end
13
+
14
+ context 'not exactly equal' do
15
+ subject { Post.at_time(Time.zone.parse('2013-12-28 17:00:01')) }
16
+ it { expect(subject.count).to eql(0) }
17
+ end
18
+ end
19
+
20
+ context 'timespan object' do
21
+
22
+ context 'before start time' do
23
+ subject { Event.at_time(Time.zone.parse('2013-12-23 16:59:59')) }
24
+ it { expect(subject.count).to eql(2) }
25
+ end
26
+
27
+ context 'at start time' do
28
+ subject { Event.at_time(Time.zone.parse('2013-12-23 17:00:00')) }
29
+ it { expect(subject.count).to eql(3) }
30
+ end
31
+
32
+ context 'after start time' do
33
+ subject { Event.at_time(Time.zone.parse('2013-12-23 17:00:01')) }
34
+ it { expect(subject.count).to eql(3) }
35
+ end
36
+
37
+ context 'before end time' do
38
+ subject { Event.at_time(Time.zone.parse('2013-11-06 16:59:59')) }
39
+ it { expect(subject.count).to eql(1) }
40
+ end
41
+
42
+ context 'at end time' do
43
+ subject { Event.at_time(Time.zone.parse('2013-11-06 17:00:00')) }
44
+ it { expect(subject.count).to eql(0) }
45
+ end
46
+
47
+ context 'after end time' do
48
+ subject { Event.at_time(Time.zone.parse('2013-11-06 17:00:01')) }
49
+ it { expect(subject.count).to eql(0) }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'between_dates' do
4
+
5
+ describe '#between_dates' do
6
+ subject { Post.between_dates(Time.zone.parse('2014-01-01'), Time.zone.parse('2014-01-06')) }
7
+
8
+ if testing_active_record?
9
+ it { is_expected.to be_a(ActiveRecord::Relation) }
10
+ else testing_mongoid?
11
+ it { is_expected.to be_a(Mongoid::Criteria) }
12
+ end
13
+
14
+ it { expect(subject.count).to eql(3) }
15
+
16
+ context 'one-sided query' do
17
+
18
+ context 'point query' do
19
+
20
+ context 'only start time' do
21
+ subject { Post.between_dates(Time.zone.parse('2014-01-01'), nil) }
22
+ it { expect(subject.count).to eql(12) }
23
+ end
24
+
25
+ context 'only end time' do
26
+ subject { Post.between_dates(nil, Time.zone.parse('2014-01-01')) }
27
+ it { expect(subject.count).to eql(12) }
28
+
29
+ context 'neither start nor end time' do
30
+ subject { Post.between_dates(nil, nil) }
31
+ it { expect(subject.count).to eql(22) }
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'timespan loose query' do
37
+
38
+ context 'only start time' do
39
+ subject { Event.between_dates(Time.zone.parse('2014-01-01'), nil, strict: false) }
40
+ it { expect(subject.count).to eql(17) }
41
+ end
42
+
43
+ context 'only end time' do
44
+ subject { Event.between_dates(nil, Time.zone.parse('2014-01-01'), strict: false) }
45
+ it { expect(subject.count).to eql(13) }
46
+
47
+ context 'neither start nor end time' do
48
+ subject { Event.between_dates(nil, nil) }
49
+ it { expect(subject.count).to eql(30) }
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'timespan strict query' do
55
+
56
+ context 'only start time' do
57
+ subject { Event.between_dates(Time.zone.parse('2014-01-01'), nil) }
58
+ it { expect(subject.count).to eql(17) }
59
+ end
60
+
61
+ context 'only end time' do
62
+ subject { Event.between_dates(nil, Time.zone.parse('2014-01-01')) }
63
+ it { expect(subject.count).to eql(13) }
64
+
65
+ context 'neither start nor end time' do
66
+ subject { Event.between_dates(nil, nil) }
67
+ it { expect(subject.count).to eql(30) }
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'two-sided query' do
74
+ context 'DST starts (Sydney)', sydney: true do
75
+ context 'day before' do
76
+ subject { Event.between_dates(Date.parse('2020-04-04'), Date.parse('2020-04-04'), offset: 5.hours) }
77
+ it { expect(subject.count).to eql(3) }
78
+ end
79
+
80
+ context 'same day' do
81
+ subject { Event.between_dates(Date.parse('2020-04-05'), Date.parse('2020-04-05'), offset: 5.hours) }
82
+ it { expect(subject.count).to eql(1) }
83
+ end
84
+ end
85
+
86
+ context 'when DST ends (Sydney)', sydney: true do
87
+ context 'day before' do
88
+ subject { Event.between_dates(Date.parse('2020-10-03'), Date.parse('2020-10-03'), offset: 5.hours) }
89
+ it { expect(subject.count).to eql(1) }
90
+ end
91
+
92
+ context 'same day' do
93
+ subject { Event.between_dates(Date.parse('2020-10-04'), Date.parse('2020-10-04'), offset: 5.hours) }
94
+ it { expect(subject.count).to eql(3) }
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -37,7 +37,7 @@ shared_examples_for 'between_times' do
37
37
 
38
38
  context 'only start time' do
39
39
  subject { Event.between_times(Time.zone.parse('2014-01-01'), nil, strict: false) }
40
- it { expect(subject.count).to eql(9) }
40
+ it { expect(subject.count).to eql(17) }
41
41
  end
42
42
 
43
43
  context 'only end time' do
@@ -46,7 +46,7 @@ shared_examples_for 'between_times' do
46
46
 
47
47
  context 'neither start nor end time' do
48
48
  subject { Event.between_times(nil, nil) }
49
- it { expect(subject.count).to eql(22) }
49
+ it { expect(subject.count).to eql(30) }
50
50
  end
51
51
  end
52
52
  end
@@ -55,7 +55,7 @@ shared_examples_for 'between_times' do
55
55
 
56
56
  context 'only start time' do
57
57
  subject { Event.between_times(Time.zone.parse('2014-01-01'), nil) }
58
- it { expect(subject.count).to eql(9) }
58
+ it { expect(subject.count).to eql(17) }
59
59
  end
60
60
 
61
61
  context 'only end time' do
@@ -64,18 +64,35 @@ shared_examples_for 'between_times' do
64
64
 
65
65
  context 'neither start nor end time' do
66
66
  subject { Event.between_times(nil, nil) }
67
- it { expect(subject.count).to eql(22) }
67
+ it { expect(subject.count).to eql(30) }
68
68
  end
69
69
  end
70
70
  end
71
71
  end
72
- end
73
72
 
74
- if testing_mongoid?
75
- describe '#between' do
76
- it 'should not override Mongoid between method' do
77
- posts = Post.between(created_at: Time.zone.parse('2014-01-01')..Time.zone.parse('2014-01-06'))
78
- expect(posts.count).to eql(3)
73
+ context 'two-sided query' do
74
+ context 'DST starts (Sydney)', sydney: true do
75
+ context 'day before' do
76
+ subject { Event.between_times(Date.parse('2020-04-04'), Date.parse('2020-04-04'), offset: 5.hours) }
77
+ it { expect(subject.count).to eql(3) }
78
+ end
79
+
80
+ context 'same day' do
81
+ subject { Event.between_times(Date.parse('2020-04-05'), Date.parse('2020-04-05'), offset: 5.hours) }
82
+ it { expect(subject.count).to eql(1) }
83
+ end
84
+ end
85
+
86
+ context 'when DST ends (Sydney)', sydney: true do
87
+ context 'day before' do
88
+ subject { Event.between_times(Date.parse('2020-10-03'), Date.parse('2020-10-03'), offset: 5.hours) }
89
+ it { expect(subject.count).to eql(1) }
90
+ end
91
+
92
+ context 'same day' do
93
+ subject { Event.between_times(Date.parse('2020-10-04'), Date.parse('2020-10-04'), offset: 5.hours) }
94
+ it { expect(subject.count).to eql(3) }
95
+ end
79
96
  end
80
97
  end
81
98
  end
@@ -23,6 +23,30 @@ shared_examples_for 'by day' do
23
23
  it 'should support :offset option' do
24
24
  expect(Post.by_day('2014-01-01', offset: -16.hours).count).to eq(1)
25
25
  end
26
+
27
+ context 'when DST starts (Sydney)', sydney: true do
28
+ context 'day before' do
29
+ subject { Event.by_day('2020-04-04', offset: 5.hours) }
30
+ it { expect(subject.count).to eq(3) }
31
+ end
32
+
33
+ context 'same day' do
34
+ subject { Event.by_day('2020-04-05', offset: 5.hours) }
35
+ it { expect(subject.count).to eq(1) }
36
+ end
37
+ end
38
+
39
+ context 'when DST ends (Sydney)', sydney: true do
40
+ context 'day before' do
41
+ subject { Event.by_day('2020-10-03', offset: 5.hours) }
42
+ it { expect(subject.count).to eq(1) }
43
+ end
44
+
45
+ context 'same day' do
46
+ subject { Event.by_day('2020-10-04', offset: 5.hours) }
47
+ it { expect(subject.count).to eq(3) }
48
+ end
49
+ end
26
50
  end
27
51
 
28
52
  describe '#today' do # 2014-01-01
@@ -26,22 +26,12 @@ shared_examples_for 'by direction' do
26
26
 
27
27
  context 'alternative field' do
28
28
  subject { Event.before('2014-01-05', field: 'created_at') }
29
- it { expect(subject.count).to eql(12) }
29
+ it { expect(subject.count).to eql(20) }
30
30
  end
31
31
 
32
32
  context 'with default scope' do
33
33
  subject { Appointment.before('2014-01-05', field: 'created_at') }
34
- it { expect(subject.count).to eql(4) }
35
- end
36
-
37
- context 'with scope as a query criteria' do
38
- subject { Post.before('2014-01-05', field: 'created_at', scope: Post.where(day_of_month: 5)) }
39
- it { expect(subject.count).to eql(1) }
40
- end
41
-
42
- context 'with scope as a proc' do
43
- subject { Post.before('2014-01-05', field: 'created_at', scope: ->{ where(day_of_month: 5) }) }
44
- it { expect(subject.count).to eql(1) }
34
+ it { expect(subject.count).to eql(12) }
45
35
  end
46
36
  end
47
37
 
@@ -54,17 +44,17 @@ shared_examples_for 'by direction' do
54
44
 
55
45
  context 'timespan default' do
56
46
  subject { Event.after(Date.parse '2014-01-05') }
57
- it { expect(subject.count).to eql(9) }
47
+ it { expect(subject.count).to eql(17) }
58
48
  end
59
49
 
60
50
  context 'timespan strict' do
61
51
  subject { Event.after('2014-01-05', strict: true) }
62
- it { expect(subject.count).to eql(9) }
52
+ it { expect(subject.count).to eql(17) }
63
53
  end
64
54
 
65
55
  context 'timespan not strict' do
66
56
  subject { Event.after('2014-01-05', strict: false) }
67
- it { expect(subject.count).to eql(9) }
57
+ it { expect(subject.count).to eql(17) }
68
58
  end
69
59
 
70
60
  context 'alternative field' do
@@ -74,56 +64,35 @@ shared_examples_for 'by direction' do
74
64
 
75
65
  context 'with default scope' do
76
66
  subject { Appointment.after('2014-01-05', field: 'created_at') }
77
- it { expect(subject.count).to eql(3) }
78
- end
79
-
80
- context 'with scope as a query criteria' do
81
- subject { Post.after('2014-01-05', field: 'created_at', scope: Post.where(day_of_month: 5)) }
82
- it { expect(subject.count).to eql(1) }
83
- end
84
-
85
- context 'with scope as a proc' do
86
- subject { Post.after('2014-01-05', field: 'created_at', scope: ->{ where(day_of_month: 5) }) }
87
- it { expect(subject.count).to eql(1) }
67
+ it { expect(subject.count).to eql(10) }
88
68
  end
89
69
  end
90
70
 
91
71
  describe '#oldest and #newest' do
92
-
93
72
  context 'point-in-time' do
94
73
  it { expect(Post.newest.created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
95
74
  it { expect(Post.oldest.created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
96
75
  end
97
76
 
98
77
  context 'timespan' do
99
- it { expect(Event.newest.created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
78
+ it { expect(Event.newest.created_at).to eq Time.zone.parse('2011-01-01 00:00:00') }
100
79
  it { expect(Event.oldest.created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
101
80
  end
102
81
 
103
82
  context 'timespan strict' do
104
- it { expect(Event.newest(strict: true).created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
83
+ it { expect(Event.newest(strict: true).created_at).to eq Time.zone.parse('2011-01-01 00:00:00') }
105
84
  it { expect(Event.oldest(strict: true).created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
106
85
  end
107
86
 
108
87
  context 'alternative field' do
109
88
  it { expect(Event.newest(field: 'created_at').created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
110
- it { expect(Event.oldest(field: 'created_at').created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
89
+ it { expect(Event.oldest(field: 'created_at').created_at).to eq Time.zone.parse('2011-01-01 00:00:00') }
111
90
  end
112
91
 
113
92
  context 'with default scope' do
114
- it { expect(Appointment.newest(field: 'created_at').created_at).to eq Time.zone.parse('2014-04-01 17:00:00') }
93
+ it { expect(Appointment.newest(field: 'created_at').created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
115
94
  it { expect(Appointment.oldest(field: 'created_at').created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
116
95
  end
117
-
118
- context 'with scope as a query criteria' do
119
- it { expect(Post.newest(field: 'created_at', scope: Post.where(day_of_month: 5)).created_at).to eq Time.zone.parse('2014-01-05 17:00:00') }
120
- it { expect(Post.oldest(field: 'created_at', scope: Post.where(day_of_month: 5)).created_at).to eq Time.zone.parse('2013-12-05 17:00:00') }
121
- end
122
-
123
- context 'with scope as a proc' do
124
- it { expect(Post.newest(field: 'created_at', scope: ->{ where(day_of_month: 5) }).created_at).to eq Time.zone.parse('2014-01-05 17:00:00') }
125
- it { expect(Post.oldest(field: 'created_at', scope: ->{ where(day_of_month: 5) }).created_at).to eq Time.zone.parse('2013-12-05 17:00:00') }
126
- end
127
96
  end
128
97
 
129
98
  describe '#previous and #next' do
@@ -143,21 +112,7 @@ shared_examples_for 'by direction' do
143
112
  context 'with default scope' do
144
113
  subject { Appointment.where(created_at: Time.zone.parse('2014-01-05 17:00:00')).first }
145
114
  it{ expect(subject.previous.created_at).to eq Time.zone.parse('2014-01-01 17:00:00') }
146
- it{ expect(subject.next.created_at).to eq Time.zone.parse('2014-02-01 17:00:00') }
147
- end
148
-
149
- context 'with scope as a query criteria' do
150
- subject { Post.where(created_at: Time.zone.parse('2014-01-05 17:00:00')).first }
151
- it{ expect(subject.previous({ scope: Post.where(day_of_month: 5) }).created_at).to eq Time.zone.parse('2013-12-05 17:00:00') }
152
- it{ expect(subject.next({ scope: Post.where(day_of_month: 1) }).created_at).to eq Time.zone.parse('2014-02-01 17:00:00') }
153
- end
154
-
155
- context 'with scope as a proc' do
156
- subject { Post.where(created_at: Time.zone.parse('2014-01-05 17:00:00')).first }
157
- it{ expect(subject.previous({ scope: Proc.new{ where(day_of_month: 5) } }).created_at).to eq Time.zone.parse('2013-12-05 17:00:00') }
158
- it{ expect(subject.previous({ scope: Proc.new{|record| where(day_of_month: record.day_of_month) } }).created_at).to eq Time.zone.parse('2013-12-05 17:00:00') }
159
- it{ expect(subject.next({ scope: ->{ where(day_of_month: 1) } }).created_at).to eq Time.zone.parse('2014-02-01 17:00:00') }
160
- it{ expect(subject.next({ scope: ->(record){ where(day_of_month: record.day_of_month - 4) } }).created_at).to eq Time.zone.parse('2014-02-01 17:00:00') }
115
+ it{ expect(subject.next.created_at).to eq Time.zone.parse('2014-01-10 17:00:00') }
161
116
  end
162
117
 
163
118
  context 'specify a field' do
@@ -167,6 +122,5 @@ shared_examples_for 'by direction' do
167
122
  it{ expect(subject.previous(field: 'updated_at').created_at).to eq Time.zone.parse('2013-12-31 17:00:00') }
168
123
  it{ expect(subject.next(field: 'updated_at').created_at).to eq Time.zone.parse('2014-01-01 17:00:00') }
169
124
  end
170
-
171
125
  end
172
126
  end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'index_scope parameter' do
4
+
5
+ describe ':scope' do
6
+
7
+ it 'should memoize the scope variable' do
8
+ expect(Event.instance_variable_get(:@by_star_index_scope)).to be_nil
9
+ expect(Post.instance_variable_get(:@by_star_index_scope)).to be_nil
10
+ expect(Appointment.instance_variable_get(:@by_star_index_scope)).to be_a Proc
11
+ end
12
+
13
+ context 'between_times with index_scope' do
14
+
15
+ context 'nil' do
16
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: nil) }
17
+ it { expect(subject.count).to eql(16) }
18
+ end
19
+
20
+ context 'false' do
21
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: false) }
22
+ it { expect(subject.count).to eql(16) }
23
+ end
24
+
25
+ context 'Time' do
26
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Time.zone.parse('2013-11-30 17:00:00')) }
27
+ it { expect(subject.count).to eql(14) }
28
+ end
29
+
30
+ context 'DateTime' do
31
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Time.zone.parse('2013-11-30 17:00:00').to_datetime) }
32
+ it { expect(subject.count).to eql(14) }
33
+ end
34
+
35
+ context 'Date' do
36
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Date.parse('2013-11-30')) }
37
+ it { expect(subject.count).to eql(14) }
38
+ end
39
+
40
+ context 'ActiveSupport::Duration' do
41
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: 3.hours) }
42
+ it { expect(subject.count).to eql(13) }
43
+ end
44
+
45
+ context 'Numeric' do
46
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: 3600) }
47
+ it { expect(subject.count).to eql(13) }
48
+ end
49
+
50
+ context ':beginning_of_day' do
51
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: :beginning_of_day) }
52
+ it { expect(subject.count).to eql(13) }
53
+ end
54
+
55
+ context 'unsupported type' do
56
+ subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Integer) }
57
+ it { expect{subject.count}.to raise_error(RuntimeError) }
58
+ end
59
+ end
60
+
61
+ context 'at_time with index_scope' do
62
+
63
+ context 'nil' do
64
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: nil) }
65
+ it { expect(subject.count).to eql(3) }
66
+ end
67
+
68
+ context 'false' do
69
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: false) }
70
+ it { expect(subject.count).to eql(3) }
71
+ end
72
+
73
+ context 'Time' do
74
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Time.zone.parse('2013-10-30 17:00:00')) }
75
+ it { expect(subject.count).to eql(3) }
76
+ end
77
+
78
+ context 'DateTime' do
79
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Time.zone.parse('2013-11-30 17:00:00').to_datetime) }
80
+ it { expect(subject.count).to eql(1) }
81
+ end
82
+
83
+ context 'Date' do
84
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Date.parse('2013-11-30')) }
85
+ it { expect(subject.count).to eql(1) }
86
+ end
87
+
88
+ context 'ActiveSupport::Duration' do
89
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: 100.hours) }
90
+ it { expect(subject.count).to eql(1) }
91
+ end
92
+
93
+ context 'Numeric' do
94
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: 60 * 60 * 1000) }
95
+ it { expect(subject.count).to eql(3) }
96
+ end
97
+
98
+ context ':beginning_of_day' do
99
+ let!(:custom_event){ t = Time.zone.parse('2013-12-30 17:00'); Event.create!(start_time: t - 1.hour, end_time: t + 1.hour) }
100
+ subject { Event.at_time(Time.zone.parse('2013-12-30 16:00'), index_scope: :beginning_of_day) }
101
+ it { expect(subject.count).to eql(1) }
102
+ after { custom_event.delete }
103
+ end
104
+
105
+ context 'unsupported type' do
106
+ subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Integer) }
107
+ it { expect{subject.count}.to raise_error(RuntimeError) }
108
+ end
109
+ end
110
+ end
111
+ end
data/spec/spec_helper.rb CHANGED
@@ -20,6 +20,10 @@ puts "Running specs in #{Time.zone} timezone..."
20
20
  # Set Rails time to 2014-01-01 00:00:00
21
21
  Timecop.travel(Time.zone.local(2014))
22
22
 
23
+ RSpec.configure do |c|
24
+ c.filter_run_excluding sydney: true unless Time.zone.name == 'Australia/Sydney'
25
+ end
26
+
23
27
  def testing_mongoid?
24
28
  ENV['DB'] == 'mongodb'
25
29
  end
@@ -16,9 +16,9 @@ describe Date do
16
16
  expect_any_instance_of(Date).to receive(:in_time_zone)
17
17
  end
18
18
 
19
- context 'when ByStar::Kernel::Date included' do
19
+ context 'when ByStar::Kernel::InTimeZone included' do
20
20
  before do
21
- ::Date.__send__(:include, ByStar::Kernel::Date)
21
+ ::Date.__send__(:include, ByStar::Kernel::InTimeZone)
22
22
  end
23
23
 
24
24
  it 'should not be aliased to #to_time_in_current_zone' do
@@ -27,7 +27,7 @@ describe Date do
27
27
  end
28
28
  end
29
29
 
30
- context 'when ByStar::Kernel::Date not included' do
30
+ context 'when ByStar::Kernel::InTimeZone not included' do
31
31
 
32
32
  it 'should not be aliased to #to_time_in_current_zone' do
33
33
  expect(subject).not_to receive(:to_time_in_current_zone)
@@ -38,9 +38,9 @@ describe Date do
38
38
 
39
39
  context 'when #in_time_zone is not defined' do
40
40
 
41
- context 'when ByStar::Kernel::Date included' do
41
+ context 'when ByStar::Kernel::InTimeZone included' do
42
42
  before do
43
- ::Date.__send__(:include, ByStar::Kernel::Date)
43
+ ::Date.__send__(:include, ByStar::Kernel::InTimeZone)
44
44
  end
45
45
 
46
46
  it 'should be aliased to #to_time_in_current_zone' do
@@ -49,7 +49,7 @@ describe Date do
49
49
  end
50
50
  end
51
51
 
52
- context 'when ByStar::Kernel::Date not included' do
52
+ context 'when ByStar::Kernel::InTimeZone not included' do
53
53
 
54
54
  it 'should raise NoMethodError' do
55
55
  expect{ subject.in_time_zone }.to raise_error(NoMethodError)