Skip to content

Instantly share code, notes, and snippets.

@hannesfostie
Last active December 23, 2015 14:49
Show Gist options
  • Save hannesfostie/a62041cb6f9d21dee5a2 to your computer and use it in GitHub Desktop.
Save hannesfostie/a62041cb6f9d21dee5a2 to your computer and use it in GitHub Desktop.
class CreateOpeningHours < ActiveRecord::Migration
def change
create_table :opening_hours do |t|
t.timestamps
t.integer :week_day, null: false
t.datetime :start_time, null: false
t.datetime :end_time, null: false
end
add_index :opening_hours, :week_day
end
end
require 'test_helper'
describe OpeningHour do
parallelize_me!
it 'requires all attributes to be set' do
invalid_opening_hour = OpeningHour.new
assert !invalid_opening_hour.valid?
end
describe '#active?' do
it 'returns true when current time is between opening and closing time' do
active_opening_hour = OpeningHour.new week_day: Time.now.wday, start_time: Time.now - 5.hours, end_time: Time.now + 2.hours
assert active_opening_hour.active?
end
it 'returns true when current time is between opening and closing time even if after midnight' do
active_opening_hour = OpeningHour.new week_day: Time.now.wday, start_time: Time.now - 2.hours, end_time: Time.now.end_of_day + 2.hours
Time.stub :now, Time.now.end_of_day + 1.hour do
assert active_opening_hour.active?
end
end
it 'returns false when current time is before opening time' do
inactive_opening_hour = OpeningHour.new week_day: Time.now.wday, start_time: Time.now + 2.hours, end_time: Time.now.end_of_day + 5.hours
assert !inactive_opening_hour.active?
# not sure if this works..
# assert_false inactive_opening_hour.active?
end
it 'returns false when current time is after closing time' do
inactive_opening_hour = OpeningHour.new week_day: Time.now.wday, start_time: Time.now - 5.hours, end_time: Time.now.end_of_day - 2.hours
assert !inactive_opening_hour.active?
# not sure if this works..
# assert_false inactive_opening_hour.active?
end
end
end
class OpeningHour < ActiveRecord::Base
validates_presence_of :week_day, :start_time, :end_time
scope :today, -> {where(week_day: Time.now.wday)}
def active?
week_day == Time.now.wday && Time.now === start_time..end_time
end
end
# Running tests:
EEEE.
Finished tests in 0.061518s, 81.2770 tests/s, 16.2554 assertions/s.
1) Error:
OpeningHour::#active?#test_0004_returns false when current time is after closing time:
ArgumentError: bad value for range
app/models/opening_hour.rb:7:in `active?'
test/models/opening_hour.rb:33:in `block (3 levels) in <top (required)>'
2) Error:
OpeningHour::#active?#test_0002_returns true when current time is between opening and closing time even if after midnight:
NoMethodError: undefined method `stub' for Time:Class
test/models/opening_hour.rb:19:in `block (3 levels) in <top (required)>'
3) Error:
OpeningHour::#active?#test_0001_returns true when current time is between opening and closing time:
ArgumentError: bad value for range
app/models/opening_hour.rb:7:in `active?'
test/models/opening_hour.rb:14:in `block (3 levels) in <top (required)>'
4) Error:
OpeningHour::#active?#test_0003_returns false when current time is before opening time:
ArgumentError: bad value for range
app/models/opening_hour.rb:7:in `active?'
test/models/opening_hour.rb:26:in `block (3 levels) in <top (required)>'
5 tests, 1 assertions, 0 failures, 4 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment