public_transport_agent_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. describe Agents::PublicTransportAgent do
  3. before do
  4. valid_params = {
  5. "name" => "sf muni agent",
  6. "options" => {
  7. "alert_window_in_minutes" => "20",
  8. "stops" => ['N|5221', 'N|5215'],
  9. "agency" => "sf-muni"
  10. }
  11. }
  12. @agent = Agents::PublicTransportAgent.new(valid_params)
  13. @agent.user = users(:bob)
  14. @agent.save!
  15. end
  16. describe "#check" do
  17. before do
  18. stub_request(:get, "http://webservices.nextbus.com/service/publicXMLFeed?a=sf-muni&command=predictionsForMultiStops&stops=N%7C5215").
  19. with(:headers => {'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
  20. to_return(:status => 200, :body => File.read(Rails.root.join("spec/data_fixtures/public_transport_agent.xml")), :headers => {})
  21. end
  22. it "should create 4 events" do
  23. expect { @agent.check }.to change {@agent.events.count}.by(4)
  24. end
  25. it "should add 4 items to memory" do
  26. travel_to Time.parse("2014-01-14 20:21:30 +0500") do
  27. expect(@agent.memory).to eq({})
  28. @agent.check
  29. @agent.save
  30. expect(@agent.reload.memory).to eq({"existing_routes" => [
  31. {"stopTag"=>"5221", "tripTag"=>"5840324", "epochTime"=>"1389706393991", "currentTime"=>Time.now.to_s},
  32. {"stopTag"=>"5221", "tripTag"=>"5840083", "epochTime"=>"1389706512784", "currentTime"=>Time.now.to_s},
  33. {"stopTag"=>"5215", "tripTag"=>"5840324", "epochTime"=>"1389706282012", "currentTime"=>Time.now.to_s},
  34. {"stopTag"=>"5215", "tripTag"=>"5840083", "epochTime"=>"1389706400805", "currentTime"=>Time.now.to_s}
  35. ]
  36. })
  37. end
  38. end
  39. it "should not create events twice" do
  40. expect { @agent.check }.to change {@agent.events.count}.by(4)
  41. expect { @agent.check }.not_to change {@agent.events.count}
  42. end
  43. it "should reset memory after 2 hours" do
  44. travel_to Time.parse("2014-01-14 20:21:30 +0500") do
  45. expect { @agent.check }.to change {@agent.events.count}.by(4)
  46. end
  47. travel_to "2014-01-14 23:21:30 +0500".to_time do
  48. @agent.cleanup_old_memory
  49. expect { @agent.check }.to change {@agent.events.count}.by(4)
  50. end
  51. end
  52. end
  53. describe "validation" do
  54. it "should validate presence of stops" do
  55. @agent.options['stops'] = nil
  56. expect(@agent).not_to be_valid
  57. end
  58. it "should validate presence of agency" do
  59. @agent.options['agency'] = ""
  60. expect(@agent).not_to be_valid
  61. end
  62. it "should validate presence of alert_window_in_minutes" do
  63. @agent.options['alert_window_in_minutes'] = ""
  64. expect(@agent).not_to be_valid
  65. end
  66. end
  67. end