ta_by_star 4.0.0
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +59 -0
- data/Gemfile +18 -0
- data/MIT-LICENSE +20 -0
- data/README.md +616 -0
- data/Rakefile +18 -0
- data/UPGRADING +4 -0
- data/by_star.gemspec +34 -0
- data/cleaner.rb +25 -0
- data/lib/by_star/base.rb +76 -0
- data/lib/by_star/between.rb +190 -0
- data/lib/by_star/directional.rb +35 -0
- data/lib/by_star/kernel/date.rb +41 -0
- data/lib/by_star/kernel/in_time_zone.rb +20 -0
- data/lib/by_star/kernel/time.rb +41 -0
- data/lib/by_star/normalization.rb +156 -0
- data/lib/by_star/orm/active_record/by_star.rb +75 -0
- data/lib/by_star/orm/mongoid/by_star.rb +90 -0
- data/lib/by_star/orm/mongoid/reorder.rb +23 -0
- data/lib/by_star/version.rb +3 -0
- data/lib/by_star.rb +18 -0
- data/spec/database.yml +15 -0
- data/spec/fixtures/active_record/models.rb +12 -0
- data/spec/fixtures/active_record/schema.rb +19 -0
- data/spec/fixtures/mongoid/models.rb +31 -0
- data/spec/fixtures/shared/seeds.rb +36 -0
- data/spec/gemfiles/Gemfile.rails +5 -0
- data/spec/gemfiles/Gemfile.rails32 +7 -0
- data/spec/gemfiles/Gemfile.rails40 +7 -0
- data/spec/gemfiles/Gemfile.rails41 +7 -0
- data/spec/gemfiles/Gemfile.rails42 +7 -0
- data/spec/gemfiles/Gemfile.rails50 +7 -0
- data/spec/gemfiles/Gemfile.rails51 +7 -0
- data/spec/gemfiles/Gemfile.rails52 +7 -0
- data/spec/gemfiles/Gemfile.rails60 +7 -0
- data/spec/gemfiles/Gemfile.rails61 +7 -0
- data/spec/integration/active_record/active_record_spec.rb +41 -0
- data/spec/integration/mongoid/mongoid_spec.rb +39 -0
- data/spec/integration/shared/at_time.rb +53 -0
- data/spec/integration/shared/between_dates.rb +99 -0
- data/spec/integration/shared/between_times.rb +99 -0
- data/spec/integration/shared/by_calendar_month.rb +55 -0
- data/spec/integration/shared/by_cweek.rb +54 -0
- data/spec/integration/shared/by_day.rb +120 -0
- data/spec/integration/shared/by_direction.rb +126 -0
- data/spec/integration/shared/by_fortnight.rb +48 -0
- data/spec/integration/shared/by_month.rb +50 -0
- data/spec/integration/shared/by_quarter.rb +49 -0
- data/spec/integration/shared/by_week.rb +54 -0
- data/spec/integration/shared/by_weekend.rb +49 -0
- data/spec/integration/shared/by_year.rb +48 -0
- data/spec/integration/shared/index_scope_parameter.rb +111 -0
- data/spec/integration/shared/offset_parameter.rb +32 -0
- data/spec/integration/shared/order_parameter.rb +36 -0
- data/spec/integration/shared/relative.rb +174 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/unit/kernel_date_spec.rb +113 -0
- data/spec/unit/kernel_time_spec.rb +57 -0
- data/spec/unit/normalization_spec.rb +384 -0
- data/tmp/.gitignore +1 -0
- metadata +298 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by calendar month' do
|
4
|
+
|
5
|
+
describe '#by_calendar_month' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_calendar_month('Feb') }
|
9
|
+
it { expect(subject.count).to eql(3) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_calendar_month(1) }
|
14
|
+
it { expect(subject.count).to eql(10) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_calendar_month(Date.parse('2014-02-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(2) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_calendar_month(12, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(12) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_calendar_month('December', year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(13) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_calendar_month('Dec', year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(9) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect{ Post.by_calendar_month(0) }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
|
42
|
+
expect{ Post.by_calendar_month(13) }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
|
43
|
+
expect{ Post.by_calendar_month('foobar') }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should be able to use an alternative field' do
|
47
|
+
expect(Event.by_calendar_month(field: 'end_time').count).to eql(9)
|
48
|
+
end
|
49
|
+
|
50
|
+
context ':start_day option' do
|
51
|
+
subject { Post.by_calendar_month(1, start_day: :wednesday) }
|
52
|
+
it{ expect(subject.count).to eql(7) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by cweek' do
|
4
|
+
|
5
|
+
describe '#by_cweek' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_cweek('2014-01-02') }
|
9
|
+
it { expect(subject.count).to eql(4) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_cweek(1) }
|
14
|
+
it { expect(subject.count).to eql(7) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_cweek(Date.parse('2014-01-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(0) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_cweek(53, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(4) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_cweek(53, year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(7) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_cweek(53, year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(0) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect { Post.by_cweek(0) }.to raise_error(ByStar::ParseError, 'cweek number must be between 1 and 53')
|
42
|
+
expect { Post.by_cweek(54) }.to raise_error(ByStar::ParseError, 'cweek number must be between 1 and 53')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should be able to use an alternative field' do
|
46
|
+
expect(Event.by_cweek(field: 'end_time').count).to eq 3
|
47
|
+
end
|
48
|
+
|
49
|
+
context ':start_day option' do
|
50
|
+
subject { Post.by_cweek('2014-01-02', start_day: :thursday) }
|
51
|
+
it { expect(subject.count).to eql(1) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by day' do
|
4
|
+
|
5
|
+
describe '#by_day' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
it { expect(Post.by_day('2014-01-01').count).to eql(2) }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'timespan' do
|
12
|
+
it { expect(Event.by_day(Time.zone.parse '2014-01-01').count).to eql(5) }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'timespan strict' do
|
16
|
+
it { expect(Event.by_day(Date.parse('2014-01-01'), strict: true).count).to eql(0) }
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should be able to use an alternative field' do
|
20
|
+
expect(Event.by_day(field: 'end_time').count).to eql(0)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should support :offset option' do
|
24
|
+
expect(Post.by_day('2014-01-01', offset: -16.hours).count).to eq(1)
|
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
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#today' do # 2014-01-01
|
53
|
+
|
54
|
+
context 'point-in-time' do
|
55
|
+
it { expect(Post.today.count).to eql(2) }
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'timespan' do
|
59
|
+
it { expect(Event.today.count).to eql(5) }
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'timespan strict' do
|
63
|
+
it { expect(Event.today(strict: true).count).to eql(0) }
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should be able to use an alternative field' do
|
67
|
+
expect(Event.today(field: 'created_at').count).to eql(2)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should support :offset option' do
|
71
|
+
expect(Post.today(offset: -24.hours).count).to eql(1)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#yesterday' do # 2013-12-31
|
76
|
+
|
77
|
+
context 'point-in-time' do
|
78
|
+
it { expect(Post.yesterday.count).to eql(1) }
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'timespan' do
|
82
|
+
it { expect(Event.yesterday.count).to eql(5) }
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'timespan strict' do
|
86
|
+
it { expect(Event.yesterday(strict: true).count).to eql(0) }
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should be able to use an alternative field' do
|
90
|
+
expect(Event.yesterday(field: 'created_at').count).to eql(1)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should support :offset option' do
|
94
|
+
expect(Post.yesterday(offset: 24.hours).count).to eql(2)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '#tomorrow' do # 2014-01-02
|
99
|
+
|
100
|
+
context 'point-in-time' do
|
101
|
+
it { expect(Post.tomorrow.count).to eql(0) }
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'timespan' do
|
105
|
+
it { expect(Event.tomorrow.count).to eql(5) }
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'timespan strict' do
|
109
|
+
it { expect(Event.tomorrow(strict: true).count).to eql(0) }
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should be able to use an alternative field' do
|
113
|
+
expect(Event.tomorrow(field: 'created_at').count).to eql(0)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should support :offset option' do
|
117
|
+
expect(Post.tomorrow(offset: -24.hours).count).to eql(2)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by direction' do
|
4
|
+
|
5
|
+
describe '#before' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.before(Date.parse '2014-01-05') }
|
9
|
+
it { expect(subject.count).to eql(12) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan default' do
|
13
|
+
subject { Event.before(Time.zone.parse '2014-01-05') }
|
14
|
+
it { expect(subject.count).to eql(13) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.before('2014-01-05', strict: true) }
|
19
|
+
it { expect(subject.count).to eql(13) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'timespan not strict' do
|
23
|
+
subject { Event.before('2014-01-05', strict: false) }
|
24
|
+
it { expect(subject.count).to eql(13) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'alternative field' do
|
28
|
+
subject { Event.before('2014-01-05', field: 'created_at') }
|
29
|
+
it { expect(subject.count).to eql(20) }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with default scope' do
|
33
|
+
subject { Appointment.before('2014-01-05', field: 'created_at') }
|
34
|
+
it { expect(subject.count).to eql(12) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#after' do
|
39
|
+
|
40
|
+
context 'point-in-time' do
|
41
|
+
subject { Post.after('2014-01-05') }
|
42
|
+
it { expect(subject.count).to eql(10) }
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'timespan default' do
|
46
|
+
subject { Event.after(Date.parse '2014-01-05') }
|
47
|
+
it { expect(subject.count).to eql(17) }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'timespan strict' do
|
51
|
+
subject { Event.after('2014-01-05', strict: true) }
|
52
|
+
it { expect(subject.count).to eql(17) }
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'timespan not strict' do
|
56
|
+
subject { Event.after('2014-01-05', strict: false) }
|
57
|
+
it { expect(subject.count).to eql(17) }
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'alternative field' do
|
61
|
+
subject { Event.after('2014-01-05', field: 'created_at') }
|
62
|
+
it { expect(subject.count).to eql(10) }
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with default scope' do
|
66
|
+
subject { Appointment.after('2014-01-05', field: 'created_at') }
|
67
|
+
it { expect(subject.count).to eql(10) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#oldest and #newest' do
|
72
|
+
context 'point-in-time' do
|
73
|
+
it { expect(Post.newest.created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
|
74
|
+
it { expect(Post.oldest.created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'timespan' do
|
78
|
+
it { expect(Event.newest.created_at).to eq Time.zone.parse('2011-01-01 00:00:00') }
|
79
|
+
it { expect(Event.oldest.created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'timespan strict' do
|
83
|
+
it { expect(Event.newest(strict: true).created_at).to eq Time.zone.parse('2011-01-01 00:00:00') }
|
84
|
+
it { expect(Event.oldest(strict: true).created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'alternative field' do
|
88
|
+
it { expect(Event.newest(field: 'created_at').created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
|
89
|
+
it { expect(Event.oldest(field: 'created_at').created_at).to eq Time.zone.parse('2011-01-01 00:00:00') }
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'with default scope' do
|
93
|
+
it { expect(Appointment.newest(field: 'created_at').created_at).to eq Time.zone.parse('2014-04-15 17:00:00') }
|
94
|
+
it { expect(Appointment.oldest(field: 'created_at').created_at).to eq Time.zone.parse('2013-11-01 17:00:00') }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '#previous and #next' do
|
99
|
+
|
100
|
+
context 'point-in-time' do
|
101
|
+
subject { Post.where(created_at: Time.zone.parse('2014-01-10 17:00:00')).first }
|
102
|
+
it{ expect(subject.previous.created_at).to eq Time.zone.parse('2014-01-05 17:00:00') }
|
103
|
+
it{ expect(subject.next.created_at).to eq Time.zone.parse('2014-01-12 17:00:00') }
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'timespan' do
|
107
|
+
subject { Event.where(start_time: Time.zone.parse('2014-01-05 17:00:00')).first }
|
108
|
+
it{ expect(subject.previous.start_time).to eq Time.zone.parse('2013-12-31 17:00:00') }
|
109
|
+
it{ expect(subject.next.start_time).to eq Time.zone.parse('2014-01-07 17:00:00') }
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'with default scope' do
|
113
|
+
subject { Appointment.where(created_at: Time.zone.parse('2014-01-05 17:00:00')).first }
|
114
|
+
it{ expect(subject.previous.created_at).to eq Time.zone.parse('2014-01-01 17:00:00') }
|
115
|
+
it{ expect(subject.next.created_at).to eq Time.zone.parse('2014-01-10 17:00:00') }
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'specify a field' do
|
119
|
+
subject { Post.where(created_at: Time.zone.parse('2014-01-01 17:00:00')).first }
|
120
|
+
it{ expect(subject.previous.created_at).to eq Time.zone.parse('2013-12-31 17:00:00') }
|
121
|
+
it{ expect(subject.next.created_at).to eq Time.zone.parse('2014-01-05 17:00:00') }
|
122
|
+
it{ expect(subject.previous(field: 'updated_at').created_at).to eq Time.zone.parse('2013-12-31 17:00:00') }
|
123
|
+
it{ expect(subject.next(field: 'updated_at').created_at).to eq Time.zone.parse('2014-01-01 17:00:00') }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by fortnight' do
|
4
|
+
|
5
|
+
describe '#by_fortnight' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_fortnight('2014-01-01') }
|
9
|
+
it { expect(subject.count).to eql(5) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_fortnight(0) }
|
14
|
+
it { expect(subject.count).to eql(7) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_fortnight(Date.parse('2014-01-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(0) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_fortnight(26, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(6) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_fortnight(26, year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(7) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_fortnight(26, year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(1) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect { Post.by_fortnight(27) }.to raise_error(ByStar::ParseError, 'Fortnight number must be between 0 and 26')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should be able to use an alternative field' do
|
45
|
+
expect(Event.by_fortnight(field: 'end_time').count).to eq 5
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by month' do
|
4
|
+
|
5
|
+
describe '#by_month' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_month('Feb') }
|
9
|
+
it { expect(subject.count).to eql(2) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_month(1) }
|
14
|
+
it { expect(subject.count).to eql(9) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_month(Date.parse('2014-02-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(1) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_month(12, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(8) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_month('December', year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(12) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_month('Dec', year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(4) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect{ Post.by_month(0) }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
|
42
|
+
expect{ Post.by_month(13) }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
|
43
|
+
expect{ Post.by_month('foobar') }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should be able to use an alternative field' do
|
47
|
+
expect(Event.by_month(field: 'end_time').count).to eq 8
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by quarter' do
|
4
|
+
|
5
|
+
describe '#by_quarter' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_quarter(2) }
|
9
|
+
it { expect(subject.count).to eql(2) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_quarter(1) }
|
14
|
+
it { expect(subject.count).to eql(13) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_quarter(Date.parse('2014-02-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(7) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_quarter(4, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(10) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_quarter(4, year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(13) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_quarter(4, year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(8) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect{ Post.by_quarter(0) }.to raise_error(ByStar::ParseError, 'Quarter number must be between 1 and 4')
|
42
|
+
expect{ Post.by_quarter(5) }.to raise_error(ByStar::ParseError, 'Quarter number must be between 1 and 4')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should be able to use an alternative field' do
|
46
|
+
expect(Event.by_quarter(1, field: 'end_time').count).to eq 12
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by week' do
|
4
|
+
|
5
|
+
describe '#by_week' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_week('2014-01-02') }
|
9
|
+
it { expect(subject.count).to eql(4) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_week(0) }
|
14
|
+
it { expect(subject.count).to eql(7) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_week(Date.parse('2014-01-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(0) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_week(52, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(4) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_week(52, year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(7) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_week(52, year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(0) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect { Post.by_week(-1) }.to raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
|
42
|
+
expect { Post.by_week(53) }.to raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should be able to use an alternative field' do
|
46
|
+
expect(Event.by_week(field: 'end_time').count).to eq 3
|
47
|
+
end
|
48
|
+
|
49
|
+
context ':start_day option' do
|
50
|
+
subject { Post.by_week('2014-01-02', start_day: :thursday) }
|
51
|
+
it { expect(subject.count).to eql(1) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by weekend' do
|
4
|
+
|
5
|
+
describe '#by_weekend' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_weekend('2014-01-01') }
|
9
|
+
it { expect(subject.count).to eql(1) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_weekend(0) }
|
14
|
+
it { expect(subject.count).to eql(5) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_weekend(Date.parse('2014-01-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(0) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with :year option' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_weekend(52, year: 2013) }
|
26
|
+
it { expect(subject.count).to eql(1) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_weekend(52, year: 2013) }
|
31
|
+
it { expect(subject.count).to eql(5) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_weekend(52, year: 2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(0) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should raise an error when given an invalid argument' do
|
41
|
+
expect { Post.by_weekend(-1) }.to raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
|
42
|
+
expect { Post.by_weekend(53) }.to raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should be able to use an alternative field' do
|
46
|
+
expect(Event.by_weekend(field: 'end_time').count).to eq 1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'by year' do
|
4
|
+
|
5
|
+
describe '#by_year' do
|
6
|
+
|
7
|
+
context 'point-in-time' do
|
8
|
+
subject { Post.by_year('2014') }
|
9
|
+
it { expect(subject.count).to eql(12) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'timespan' do
|
13
|
+
subject { Event.by_year(13) }
|
14
|
+
it { expect(subject.count).to eql(13) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'timespan strict' do
|
18
|
+
subject { Event.by_year(Date.parse('2014-02-01'), strict: true) }
|
19
|
+
it { expect(subject.count).to eql(9) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'integer' do
|
23
|
+
|
24
|
+
context 'point-in-time' do
|
25
|
+
subject { Post.by_year(2013) }
|
26
|
+
it { expect(subject.count).to eql(10) }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'timespan' do
|
30
|
+
subject { Event.by_year(2014) }
|
31
|
+
it { expect(subject.count).to eql(14) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'timespan strict' do
|
35
|
+
subject { Event.by_year(2013, strict: true) }
|
36
|
+
it { expect(subject.count).to eql(8) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should be able to use an alternative field' do
|
41
|
+
expect(Event.by_year(field: 'end_time').count).to eq 14
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can use a 2-digit year' do
|
45
|
+
expect(Post.by_year(13).count).to eq 10
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|