timely 0.4.2 → 0.9.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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/release.yml +59 -0
  4. data/.github/workflows/ruby.yml +19 -0
  5. data/.rubocop.yml +22 -2
  6. data/.ruby-version +1 -1
  7. data/CHANGELOG.md +23 -0
  8. data/Gemfile +2 -0
  9. data/README.md +13 -2
  10. data/Rakefile +6 -4
  11. data/gemfiles/rails60.gemfile +8 -0
  12. data/gemfiles/rails61.gemfile +8 -0
  13. data/lib/timely.rb +2 -0
  14. data/lib/timely/date.rb +6 -2
  15. data/lib/timely/date_chooser.rb +29 -30
  16. data/lib/timely/date_range.rb +17 -19
  17. data/lib/timely/date_time.rb +3 -1
  18. data/lib/timely/rails.rb +2 -0
  19. data/lib/timely/rails/calendar_tag.rb +3 -3
  20. data/lib/timely/rails/date.rb +3 -1
  21. data/lib/timely/rails/date_group.rb +42 -18
  22. data/lib/timely/rails/date_range_validity_module.rb +8 -6
  23. data/lib/timely/rails/date_time.rb +5 -3
  24. data/lib/timely/rails/extensions.rb +12 -37
  25. data/lib/timely/rails/period.rb +14 -12
  26. data/lib/timely/rails/season.rb +14 -33
  27. data/lib/timely/rails/time.rb +7 -3
  28. data/lib/timely/railtie.rb +2 -0
  29. data/lib/timely/range.rb +4 -2
  30. data/lib/timely/string.rb +4 -2
  31. data/lib/timely/time.rb +7 -3
  32. data/lib/timely/time_since.rb +5 -3
  33. data/lib/timely/trackable_date_set.rb +21 -21
  34. data/lib/timely/version.rb +3 -1
  35. data/lib/timely/week_days.rb +22 -14
  36. data/rails/init.rb +2 -0
  37. data/spec/calendar_tag_spec.rb +11 -10
  38. data/spec/date_chooser_spec.rb +67 -62
  39. data/spec/date_group_spec.rb +103 -5
  40. data/spec/date_range_spec.rb +29 -19
  41. data/spec/date_spec.rb +3 -1
  42. data/spec/extensions_spec.rb +5 -9
  43. data/spec/rails/date_spec.rb +5 -3
  44. data/spec/rails/date_time_spec.rb +9 -7
  45. data/spec/rails/period_spec.rb +2 -0
  46. data/spec/rails/time_spec.rb +6 -4
  47. data/spec/schema.rb +4 -3
  48. data/spec/season_spec.rb +23 -26
  49. data/spec/spec_helper.rb +16 -1
  50. data/spec/support/coverage_loader.rb +3 -1
  51. data/spec/temporal_patterns_spec.rb +5 -5
  52. data/spec/time_since_spec.rb +6 -4
  53. data/spec/time_spec.rb +6 -4
  54. data/spec/trackable_date_set_spec.rb +20 -18
  55. data/spec/week_days_spec.rb +41 -16
  56. data/timely.gemspec +23 -19
  57. metadata +54 -26
  58. data/.travis.yml +0 -22
  59. data/gemfiles/rails5.gemfile +0 -6
  60. data/gemfiles/rails6.gemfile +0 -6
  61. data/spec/string_spec.rb +0 -14
@@ -1,21 +1,29 @@
1
- require "spec_helper"
2
- describe Timely::DateGroup do
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Timely::DateGroup do
3
6
  before do
4
7
  @date_group = Timely::DateGroup.new(
5
- :start_date => '2000-01-01', :end_date => '2000-01-03', :weekdays => %w(1 1 1 1 1 1 1)
8
+ start_date: '2000-01-01', end_date: '2000-01-03', weekdays: %w[1 1 1 1 1 1 1]
6
9
  )
7
10
  end
8
11
 
12
+ it 'is not valid to set nil weekdays' do
13
+ @date_group.weekdays_bit_array = nil
14
+ expect(@date_group).not_to be_valid
15
+ end
16
+
9
17
  it 'should detect overlaps' do
10
18
  expect(@date_group.applicable_for_duration?(Timely::DateRange.new('2000-01-01'.to_date, '2000-01-01'.to_date))).to be true
11
19
  expect(@date_group.applicable_for_duration?(Timely::DateRange.new('2000-01-01'.to_date, '2000-01-06'.to_date))).to be true
12
20
  expect(@date_group.applicable_for_duration?(Timely::DateRange.new('2001-01-01'.to_date, '2001-01-01'.to_date))).to be false
13
- expect(@date_group.applicable_for_duration?(Timely::DateRange.new('1999-12-29'.to_date, '2000-01-05'.to_date))).to be true
21
+ expect(@date_group.applicable_for_duration?(Timely::DateRange.new('1999-12-29'.to_date, '2000-01-05'.to_date))).to be true
14
22
  end
15
23
 
16
24
  it "should detect overlaps when certain weekdays aren't selected" do
17
25
  @date_group = Timely::DateGroup.create!(
18
- :start_date => '2012-01-01', :end_date => '2012-01-07', :weekdays => %w(1 0 1 0 1 0 1)
26
+ start_date: '2012-01-01', end_date: '2012-01-07', weekdays: %w[1 0 1 0 1 0 1]
19
27
  )
20
28
  # Note: 2012-01-1 is a Sunday
21
29
  expect(@date_group.applicable_for_duration?(Timely::DateRange.new('2012-01-01'.to_date, '2012-01-01'.to_date))).to be true
@@ -24,3 +32,93 @@ describe Timely::DateGroup do
24
32
  expect(@date_group.applicable_for_duration?(Timely::DateRange.new('2012-01-01'.to_date, '2012-01-03'.to_date))).to be true
25
33
  end
26
34
  end
35
+
36
+ RSpec.describe Timely::DateGroup, 'Timely::DateGroup.applying_for_duration' do
37
+ let!(:date_group_a) { Timely::DateGroup.create!(
38
+ start_date: '2020-01-01', end_date: '2020-04-04', weekdays: %w[1 1 1 1 1 1 1]
39
+ ) }
40
+
41
+ let!(:date_group_b) { Timely::DateGroup.create!(
42
+ start_date: '2020-04-02', end_date: '2020-04-09', weekdays: %w[1 1 1 1 1 1 1]
43
+ ) }
44
+
45
+ subject {
46
+ Timely::DateGroup.applying_for_duration(Timely::DateRange.new(start_date, end_date)).to_a
47
+ }
48
+
49
+ context 'intersecting dates (inside first)' do
50
+ let(:start_date) { '2020-03-29'.to_date }
51
+ let(:end_date) { '2020-04-13'.to_date }
52
+ it { is_expected.to eq([date_group_a, date_group_b]) }
53
+ end
54
+
55
+ context 'intersecting dates full range' do
56
+ let(:start_date) { '2020-01-01'.to_date }
57
+ let(:end_date) { '2020-04-09'.to_date }
58
+ it { is_expected.to eq([date_group_a, date_group_b]) }
59
+ end
60
+
61
+ context 'touching end of range' do
62
+ let(:start_date) { '2020-04-09'.to_date }
63
+ let(:end_date) { '2020-04-09'.to_date }
64
+ it { is_expected.to eq([date_group_b]) }
65
+ end
66
+
67
+ context 'touching start of range' do
68
+ let(:start_date) { '2020-04-01'.to_date }
69
+ let(:end_date) { '2020-04-01'.to_date }
70
+ it { is_expected.to eq([date_group_a]) }
71
+ end
72
+ end
73
+
74
+ RSpec.describe Timely::DateGroup, 'without weekdays' do
75
+ subject { Timely::DateGroup.new(start_date: Date.current, end_date: Date.current) }
76
+ it { is_expected.to be_valid }
77
+ end
78
+
79
+ RSpec.describe 'Timely::DateGroup.for_any_weekdays' do
80
+ let(:date_range) { ('2019-10-17'.to_date)..('2019-10-18'.to_date) }
81
+ let(:weekdays_int) { Timely::WeekDays.from_range(date_range).weekdays_int }
82
+
83
+ let!(:date_groups) { [
84
+ Timely::DateGroup.create(start_date: date_range.first, end_date: date_range.last, weekdays: { thu: true }),
85
+ Timely::DateGroup.create(start_date: date_range.first, end_date: date_range.last, weekdays: { mon: true }),
86
+ ] }
87
+
88
+ RSpec.shared_examples 'finds expected date groups' do
89
+ it 'finds expected date groups' do
90
+ includes_date_groups.each do |date_group|
91
+ expect(date_group.includes_date?(date_range.first)).to eq(true)
92
+ end
93
+ expected_groups.each do |date_group|
94
+ expect(scoped_result).to include(date_group)
95
+ end
96
+ absent_groups.each do |date_group|
97
+ expect(date_group.includes_date?(date_range.first)).to eq(false)
98
+ expect(scoped_result).not_to include(date_group)
99
+ end
100
+ end
101
+ end
102
+
103
+ context '#within_range' do
104
+ let(:scoped_result) { Timely::DateGroup.within_range(date_range).to_a }
105
+ let(:expected_groups) { [date_groups[0], date_groups[1]] }
106
+ let(:absent_groups) { [] }
107
+ let(:includes_date_groups) { [] }
108
+ it_behaves_like 'finds expected date groups'
109
+ end
110
+
111
+ let(:includes_date_groups) { expected_groups }
112
+ let(:expected_groups) { [date_groups[0]] }
113
+ let(:absent_groups) { [date_groups[1]] }
114
+
115
+ context '#for_any_weekdays' do
116
+ let(:scoped_result) { Timely::DateGroup.for_any_weekdays(weekdays_int).to_a }
117
+ it_behaves_like 'finds expected date groups'
118
+ end
119
+
120
+ context '#applying_for_duration' do
121
+ let(:scoped_result) { Timely::DateGroup.applying_for_duration(date_range).to_a }
122
+ it_behaves_like 'finds expected date groups'
123
+ end
124
+ end
@@ -1,22 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Timely::DateRange do
4
-
5
- it "should allow initialization with two dates" do
6
+ it 'should allow initialization with two dates' do
6
7
  expect { @date_range = Timely::DateRange.new(Date.today, Date.today + 3) }.to_not raise_error
7
8
  expect(@date_range.start_date).to eq Date.today
8
9
  expect(@date_range.end_date).to eq Date.today + 3
9
10
  expect(@date_range.number_of_nights).to eq 4
10
11
  end
11
12
 
12
- it "should allow initialization with one date" do
13
+ it 'should allow initialization with one date' do
13
14
  expect { @date_range = Timely::DateRange.new(Date.today) }.to_not raise_error
14
15
  expect(@date_range.start_date).to eq Date.today
15
16
  expect(@date_range.end_date).to eq Date.today
16
17
  expect(@date_range.number_of_nights).to eq 1
17
18
  end
18
19
 
19
- it "should allow initialization with a range" do
20
+ it 'should allow initialization with a range' do
20
21
  expect { @date_range = Timely::DateRange.new(Date.today..Date.today + 3) }.to_not raise_error
21
22
  expect(@date_range.start_date).to eq Date.today
22
23
  expect(@date_range.end_date).to eq Date.today + 3
@@ -59,21 +60,30 @@ describe Timely::DateRange do
59
60
  expect(today.last).to eq '2000-01-05'.to_date
60
61
  end
61
62
 
62
- it "should correctly find the interesection between two date ranges" do
63
- @date_range = Timely::DateRange.new("2000-01-03".to_date, "2000-01-06".to_date)
64
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-07".to_date))).to eq ("2000-01-04".to_date.."2000-01-06".to_date)
65
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-02".to_date))).to eq []
66
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-03".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
67
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-06".to_date, "2000-01-07".to_date))).to eq ("2000-01-06".to_date.."2000-01-06".to_date)
68
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-06".to_date, "2000-01-07".to_date))).to eq ("2000-01-06".to_date.."2000-01-06".to_date)
69
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-05".to_date))).to eq ("2000-01-04".to_date.."2000-01-05".to_date)
70
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-05".to_date, "2000-01-05".to_date))).to eq ("2000-01-05".to_date.."2000-01-05".to_date)
63
+ it 'should correctly find the interesection between two date ranges' do
64
+ @date_range = Timely::DateRange.new('2000-01-03'.to_date, '2000-01-06'.to_date)
65
+ {
66
+ ['2000-01-04'.to_date, '2000-01-07'.to_date] => '2000-01-04'.to_date..'2000-01-06'.to_date,
67
+ ['2000-01-01'.to_date, '2000-01-02'.to_date] => [],
68
+ ['2000-01-01'.to_date, '2000-01-03'.to_date] => '2000-01-03'.to_date..'2000-01-03'.to_date,
69
+ ['2000-01-06'.to_date, '2000-01-07'.to_date] => '2000-01-06'.to_date..'2000-01-06'.to_date,
70
+ ['2000-01-06'.to_date, '2000-01-07'.to_date] => '2000-01-06'.to_date..'2000-01-06'.to_date,
71
+ ['2000-01-04'.to_date, '2000-01-05'.to_date] => '2000-01-04'.to_date..'2000-01-05'.to_date,
72
+ ['2000-01-05'.to_date, '2000-01-05'.to_date] => '2000-01-05'.to_date..'2000-01-05'.to_date
73
+ }.each do |args, result|
74
+ tdr = Timely::DateRange.new(*args)
75
+ expect(@date_range.intersecting_dates(tdr)).to eq(result)
76
+ end
71
77
 
72
- @date_range = Timely::DateRange.new("2000-01-03".to_date, "2000-01-03".to_date)
73
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-04".to_date, "2000-01-07".to_date))).to eq []
74
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-01".to_date, "2000-01-03".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
75
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-03".to_date, "2000-01-05".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
76
- expect(@date_range.intersecting_dates(Timely::DateRange.new("2000-01-02".to_date, "2000-01-04".to_date))).to eq ("2000-01-03".to_date.."2000-01-03".to_date)
78
+ @date_range = Timely::DateRange.new('2000-01-03'.to_date, '2000-01-03'.to_date)
79
+ {
80
+ ['2000-01-04'.to_date, '2000-01-07'.to_date] => [],
81
+ ['2000-01-01'.to_date, '2000-01-03'.to_date] => '2000-01-03'.to_date..'2000-01-03'.to_date,
82
+ ['2000-01-03'.to_date, '2000-01-05'.to_date] => '2000-01-03'.to_date..'2000-01-03'.to_date,
83
+ ['2000-01-02'.to_date, '2000-01-04'.to_date] => '2000-01-03'.to_date..'2000-01-03'.to_date
84
+ }.each do |args, result|
85
+ tdr = Timely::DateRange.new(*args)
86
+ expect(@date_range.intersecting_dates(tdr)).to eq(result)
87
+ end
77
88
  end
78
-
79
89
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Date do
@@ -63,7 +65,7 @@ describe Date do
63
65
  end
64
66
 
65
67
  it 'should return the passed-in time on the date' do
66
- @time = Time.now - 12345
68
+ @time = Time.now - 12_345
67
69
  expected = Time.local(@date.year, @date.month, @date.day, @time.hour, @time.min, @time.sec)
68
70
  expect(@date.at_time(@time)).to eq expected
69
71
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'timely/rails/extensions'
3
5
 
@@ -13,20 +15,14 @@ describe Timely::Extensions do
13
15
 
14
16
  it 'should cache boundary start' do
15
17
  season = Timely::Season.new
16
- season.date_groups.build(:start_date => Date.current, :end_date => Date.current + 2)
17
- season.date_groups.build(:start_date => Date.current - 1, :end_date => Date.current + 1)
18
+ season.date_groups.build(start_date: Date.current, end_date: Date.current + 2)
19
+ season.date_groups.build(start_date: Date.current - 1, end_date: Date.current + 1)
18
20
  season.save!
19
21
 
20
22
  o = TimelyExtensionsTestSeasonal.new
21
23
  o.season = season
22
24
  o.save!
23
25
  expect(o.boundary_start).to eq Date.current - 1
24
- expect(o.boundary_end ).to eq Date.current + 2
25
-
26
- expect(TimelyExtensionsTestSeasonal.season_on(Date.current + 3)).to be_empty
27
- expect(TimelyExtensionsTestSeasonal.season_on(Date.current + 2)).to eq [o]
28
- expect(TimelyExtensionsTestSeasonal.season_on(Date.current - 1)).to eq [o]
29
- expect(TimelyExtensionsTestSeasonal.season_on(Date.current - 2)).to be_empty
30
-
26
+ expect(o.boundary_end).to eq Date.current + 2
31
27
  end
32
28
  end
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Date do
4
- let(:date) { Date.parse("2010-01-01") }
6
+ let(:date) { Date.parse('2010-01-01') }
5
7
 
6
- before { Time.zone = ActiveSupport::TimeZone["Adelaide"] }
8
+ before { Time.zone = ActiveSupport::TimeZone['Adelaide'] }
7
9
 
8
10
  subject(:converted) { date.to_time_in_time_zone }
9
11
 
10
- it "should convert to time in time zone" do
12
+ it 'should convert to time in time zone' do
11
13
  expect(converted.year).to eq date.year
12
14
  expect(converted.month).to eq date.month
13
15
  expect(converted.day).to eq date.day
@@ -1,21 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe DateTime do
4
- let(:date_time) { parse_time("2010-01-01 00:00:00") }
6
+ let(:date_time) { parse_time('2010-01-01 00:00:00') }
5
7
 
6
- it "should allow advancing by calendar days" do
8
+ it 'should allow advancing by calendar days' do
7
9
  expect(date_time.advance_considering_calendar(:calendar_days, 10))
8
- .to eq parse_time("2010-01-10 23:59:59")
10
+ .to eq parse_time('2010-01-10 23:59:59')
9
11
  end
10
12
 
11
- it "should allow advancing by calendar months" do
13
+ it 'should allow advancing by calendar months' do
12
14
  expect(date_time.advance_considering_calendar(:calendar_months, 10))
13
- .to eq parse_time("2010-10-31 23:59:59")
15
+ .to eq parse_time('2010-10-31 23:59:59')
14
16
  end
15
17
 
16
- it "should allow advancing by calendar years" do
18
+ it 'should allow advancing by calendar years' do
17
19
  expect(date_time.advance_considering_calendar(:calendar_years, 10))
18
- .to eq parse_time("2019-12-31 23:59:59")
20
+ .to eq parse_time('2019-12-31 23:59:59')
19
21
  end
20
22
 
21
23
  def parse_time(time)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  require 'timely/rails/period'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  TestRailsTime = Class.new(Time) do
@@ -9,7 +11,7 @@ describe TestRailsTime do
9
11
 
10
12
  it 'should be able to set date on a time' do
11
13
  xmas = Date.new(2012, 12, 25)
12
- lunch_time = TestRailsTime.parse("12:00")
14
+ lunch_time = TestRailsTime.parse('12:00')
13
15
  xmas_lunch = lunch_time.on_date(xmas)
14
16
  expect(xmas_lunch.year).to eq 2012
15
17
  expect(xmas_lunch.month).to eq 12
@@ -19,9 +21,9 @@ describe TestRailsTime do
19
21
  expect(xmas_lunch.sec).to eq 0
20
22
  end
21
23
 
22
- it "should allow setting the date part given a date" do
23
- time = TestRailsTime.parse("2010-01-01 09:30:00")
24
- expect(time.on_date(Date.parse("2012-12-31"))).to eq TestRailsTime.zone.parse("2012-12-31 09:30:00")
24
+ it 'should allow setting the date part given a date' do
25
+ time = TestRailsTime.parse('2010-01-01 09:30:00')
26
+ expect(time.on_date(Date.parse('2012-12-31'))).to eq TestRailsTime.zone.parse('2012-12-31 09:30:00')
25
27
  end
26
28
  end
27
29
 
@@ -1,4 +1,6 @@
1
- ActiveRecord::Schema.define(:version => 1) do
1
+ # frozen_string_literal: true
2
+
3
+ ActiveRecord::Schema.define(version: 1) do
2
4
  create_table :seasonals do |t|
3
5
  t.date :boundary_start, :boundary_end
4
6
  t.integer :season_id
@@ -9,8 +11,7 @@ ActiveRecord::Schema.define(:version => 1) do
9
11
  end
10
12
 
11
13
  create_table :date_groups do |t|
12
- t.integer :season_id, :weekdays_bit_array
14
+ t.integer :season_id, :weekdays_bit_array, null: false, default: Timely::WeekDays::ALL_WEEKDAYS.weekdays_int
13
15
  t.date :start_date, :end_date
14
16
  end
15
17
  end
16
-
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe Timely::Season, "in general" do
5
+ describe Timely::Season, 'in general' do
4
6
  before do
5
7
  # 1st and 3rd Quarter
6
8
  @simple_low_season = Timely::Season.new
7
- @simple_low_season.date_groups.build(:start_date => '2009-01-01'.to_date, :end_date => '2009-03-31'.to_date)
8
- @simple_low_season.date_groups.build(:start_date => '2009-07-01'.to_date, :end_date => '2009-09-30'.to_date)
9
+ @simple_low_season.date_groups.build(start_date: '2009-01-01'.to_date, end_date: '2009-03-31'.to_date)
10
+ @simple_low_season.date_groups.build(start_date: '2009-07-01'.to_date, end_date: '2009-09-30'.to_date)
9
11
 
10
12
  # 2nd and 4th Quarter
11
13
  @simple_high_season = Timely::Season.new
12
- @simple_high_season.date_groups.build(:start_date => '2009-04-01'.to_date, :end_date => '2009-06-30'.to_date)
13
- @simple_high_season.date_groups.build(:start_date => '2009-10-01'.to_date, :end_date => '2009-12-31'.to_date)
14
+ @simple_high_season.date_groups.build(start_date: '2009-04-01'.to_date, end_date: '2009-06-30'.to_date)
15
+ @simple_high_season.date_groups.build(start_date: '2009-10-01'.to_date, end_date: '2009-12-31'.to_date)
14
16
  end
15
17
 
16
- it "be able to tell if a date is in or out of its season" do
18
+ it 'be able to tell if a date is in or out of its season' do
17
19
  low_season_date = '2009-02-22'.to_date
18
20
  high_season_date = '2009-04-22'.to_date
19
21
 
@@ -24,35 +26,31 @@ describe Timely::Season, "in general" do
24
26
  expect(@simple_high_season.includes_date?(high_season_date)).to be true
25
27
  end
26
28
 
27
- it "should be able to tell the boundary range (the range including the absolute first and last date) of itself" do
29
+ it 'should be able to tell the boundary range (the range including the absolute first and last date) of itself' do
28
30
  expect(@simple_low_season.boundary_range).to eq('2009-01-01'.to_date..'2009-09-30'.to_date)
29
31
  expect(@simple_high_season.boundary_range).to eq('2009-04-01'.to_date..'2009-12-31'.to_date)
30
32
  end
31
- end
32
-
33
-
34
- describe Timely::Season, 'when asked to build season for given dates' do
35
- subject(:season) { Timely::Season.build_season_for(dates) }
36
33
 
37
- context 'when three dates' do
38
- let(:dates) { [Date.current + 1, Date.current + 4, Date.current + 5] }
39
- its(:class) { should eq Timely::Season }
40
- its('date_groups.size') { should eq 3 }
34
+ context 'creating with nested attributes' do
35
+ subject { Timely::Season.new(date_groups_attributes: attrs) }
41
36
 
42
- it "should generate proper date groups" do
43
- expect(season.date_groups.first.start_date).to eq (Date.current + 1)
44
- expect(season.date_groups.last.start_date).to eq (Date.current + 5)
45
- expect(season.date_groups.last.end_date).to eq (Date.current + 5)
37
+ context 'with valid attributes' do
38
+ let(:attrs) { [ { 'start_date' => '2019-11-26' } ] }
39
+ it 'accepts nested date groups' do
40
+ expect(subject.date_groups).not_to be_empty
41
+ expect(subject.date_groups.first.start_date).to eq('2019-11-26'.to_date)
42
+ end
46
43
  end
47
- end
48
44
 
49
- context 'when dates are empty' do
50
- let(:dates) { [] }
51
- its(:date_groups) { should be_empty }
45
+ context 'invalid nested date groups' do
46
+ let(:attrs) { [ { 'start_date' => '' } ] }
47
+ it 'rejects nested date groups with nil start date' do
48
+ expect(subject.date_groups).to be_empty
49
+ end
50
+ end
52
51
  end
53
52
  end
54
53
 
55
-
56
54
  # == Schema Information
57
55
  #
58
56
  # Table name: seasons
@@ -62,4 +60,3 @@ end
62
60
  # created_at :datetime
63
61
  # updated_at :datetime
64
62
  #
65
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file was generated by the `rspec --init` command. Conventionally, all
2
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
5
  # Require this file using `require "spec_helper.rb"` to ensure that it is only
@@ -9,6 +11,8 @@ require 'rubygems'
9
11
  require 'bundler/setup'
10
12
  require 'rspec/its'
11
13
  require 'active_record'
14
+ require 'pry'
15
+ require 'database_cleaner'
12
16
 
13
17
  require 'support/coverage_loader'
14
18
 
@@ -21,11 +25,22 @@ DB_FILE = 'tmp/test_db'
21
25
  FileUtils.mkdir_p File.dirname(DB_FILE)
22
26
  FileUtils.rm_f DB_FILE
23
27
 
24
- ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
28
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: DB_FILE
25
29
 
26
30
  load('spec/schema.rb')
27
31
 
28
32
  RSpec.configure do |config|
29
33
  config.run_all_when_everything_filtered = true
30
34
  config.filter_run :focus
35
+
36
+ config.before(:suite) do
37
+ DatabaseCleaner.strategy = :transaction
38
+ DatabaseCleaner.clean_with(:truncation, except: %w(ar_internal_metadata))
39
+ end
40
+
41
+ config.around(:each) do |example|
42
+ DatabaseCleaner.cleaning do
43
+ example.run
44
+ end
45
+ end
31
46
  end