test/unit/ierail.rb in ierail-0.3.5 vs test/unit/ierail.rb in ierail-0.4.0

- old
+ new

@@ -1,64 +1,151 @@ $:.unshift(File.join(File.dirname(__FILE__), '..','..', 'lib')) require_relative 'helper' -require 'minitest/autorun' -require 'ierail' -require 'tzinfo' - class IERailTest < MiniTest::Unit::TestCase def setup @ir = IERail.new - @now = Time.now - - if TZInfo::Timezone.get('Europe/Dublin').current_period.dst? - unless @now.zone == 'IST' - @now -= @now.utc_offset - 3600 - end - else - @now -= @now.utc_offset + VCR.configure do |c| + c.cassette_library_dir = 'fixtures/vcr_cassettes' + c.hook_into :webmock end end def test_that_the_train_directions_are_correct - northbound_train = @ir.northbound_from('Howth Junction').sample - southbound_train = @ir.southbound_from('Clongriffin').sample - assert_equal northbound_train.direction, 'Northbound' - assert_equal southbound_train.direction, 'Southbound' + VCR.use_cassette('northbound') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + northbound_train = @ir.northbound_from('Dublin Connolly').sample + assert_equal northbound_train.direction, 'Northbound' + end + end + VCR.use_cassette('northbound') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + southbound_train = @ir.southbound_from('Dublin Connolly').sample + assert_equal southbound_train.direction, 'Southbound' + end + end end def test_that_an_empty_array_is_returned_when_no_data - nonexistant = @ir.westbound_from('Clongriffin') - assert_empty nonexistant + VCR.use_cassette('westbound') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + nonexistant = @ir.westbound_from('Clongriffin') + assert_empty nonexistant + end + end end def test_that_the_before_time_constraint_works - #Thirty minutes from now - thirty_mins = @now + (60 * 30) - time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM" - before_train = @ir.southbound_from('Dublin Connolly').before(time).sample - assert before_train.expdepart <= thirty_mins + VCR.use_cassette('northbound') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + #Thirty minutes from now + thirty_mins = Time.now + (60 * 30) + time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM" + before_train = @ir.southbound_from('Dublin Connolly').before(time).sample + assert before_train.expected_departure <= thirty_mins + end + end end def test_that_the_after_time_constraint_works - #Thirty minutes from now - thirty_mins = @now + (60 * 30) - time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM" - after_train = @ir.southbound_from('Dublin Connolly').after(time).sample - assert after_train.expdepart >= thirty_mins + VCR.use_cassette('southbound') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + #Thirty minutes from now + thirty_mins = Time.now + (60 * 30) + time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM" + after_train = @ir.southbound_from('Dublin Connolly').after(time).sample + assert after_train.expected_departure >= thirty_mins + end + end end def test_that_the_in_constraint_works - mins = 30 - thirty_mins = @now + (60 * mins) - time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM" - before_train = @ir.southbound_from('Malahide').before(time) + VCR.use_cassette('southbound_from') do |cassette| + time = get_original_time(cassette.originally_recorded_at) - in_half_an_hour = @ir.southbound_from('Malahide').in(mins) - assert_equal before_train.count, in_half_an_hour.count - before_train.each_with_index { |b,i| - assert_equal b.traincode, in_half_an_hour[i].traincode - } + Timecop.freeze(time || Time.now) do + mins = 30 + thirty_mins = Time.now + (60 * mins) + time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM" + southbounds = @ir.southbound_from('Dublin Connolly') + + before_train = southbounds.before(time) + + in_half_an_hour = southbounds.in(mins) + assert_equal before_train.count, in_half_an_hour.count + before_train.each_with_index { |b,i| + assert_equal b.train_code, in_half_an_hour[i].train_code + } + end + end + end + + def test_station_times + VCR.use_cassette('station_times') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + station_data = @ir.station_times('Dublin Connolly',30).sample + assert_instance_of StationData, station_data + end + end + end + + def test_find_station + VCR.use_cassette('find_station') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + station = @ir.find_station('Dublin Connolly').sample + assert_instance_of Struct::Station, station + end + end + end + + def test_that_station_times_returns_station_data + VCR.use_cassette('station_times') do |cassette| + time = get_original_time(cassette.originally_recorded_at) + + Timecop.freeze(time || Time.now) do + train = @ir.station_times('Dublin Connolly', 30).sample #random train in next 30 mins + assert_equal train.class, StationData #StationData has already been tested + end + end + end + + def test_that_station_times_equivalent_to_in + VCR.use_cassette('station_times') do |cassette| + trains = @ir.station_times('Dublin Connolly', 30) + + VCR.use_cassette('station') do |cassette| + in_half_an_hour = @ir.station('Dublin Connolly').in(30) + + assert_equal trains.count, in_half_an_hour.count + trains_codes = trains.map {|t| t.train_code} + half_hour_train_codes = in_half_an_hour.map {|t| t.train_code} + assert_equal trains_codes, half_hour_train_codes + end + end + end + + def test_that_found_station_is_a_struct_with_name_description_code + VCR.use_cassette('find_station') do |cassette| + station = @ir.find_station('Dublin Connolly').sample + assert_equal station.class, Struct::Station + refute_nil station.name + refute_nil station.description + refute_nil station.code + end end end