specs for the public transport agent

This commit is contained in:
Umar M. Sheikh 2014-01-14 20:32:06 +05:00
parent f7f10656d0
commit f104ce335c
2 changed files with 58 additions and 1 deletions

View file

@ -63,7 +63,7 @@ module Agents
predictions = page.css("//prediction")
predictions.each do |pr|
parent = pr.parent.parent
vals = {routeTitle: parent["routeTitle"], stopTag: parent["stopTag"]}
vals = {"routeTitle" => parent["routeTitle"], "stopTag" => parent["stopTag"]}
if pr["minutes"] && pr["minutes"].to_i < options["alert_window_in_minutes"].to_i
vals = vals.merge Hash.from_xml(pr.to_xml)
if not_already_in_memory?(vals)

View file

@ -0,0 +1,57 @@
require 'spec_helper'
require 'pry'
describe Agents::PublicTransportAgent do
before do
valid_params = {
"name" => "sf muni agent",
"options" => {
"alert_window_in_minutes" => "20",
"stops" => ['N|5221', 'N|5215'],
"agency" => "sf-muni"
}
}
@agent = Agents::PublicTransportAgent.new(valid_params)
@agent.user = users(:bob)
@agent.save!
end
describe "#check" do
before do
stub_request(:get, "http://webservices.nextbus.com/service/publicXMLFeed?a=sf-muni&command=predictionsForMultiStops&stops=N%7C5215").
with(:headers => {'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
to_return(:status => 200, :body => File.read(Rails.root.join("spec/data_fixtures/public_transport_agent.xml")), :headers => {})
stub(Time).now {"2014-01-14 20:21:30 +0500".to_time}
end
it "should create 4 events" do
lambda { @agent.check }.should change {@agent.events.count}.by(4)
end
it "should add 4 items to memory" do
@agent.memory.should == {}
@agent.check
@agent.memory.should == {"existing_routes" => [{"stopTag"=>"5221", "tripTag"=>"5840324", "epochTime"=>"1389706393991", "currentTime"=>"2014-01-14 20:21:30 +0500"}, {"stopTag"=>"5221", "tripTag"=>"5840083", "epochTime"=>"1389706512784", "currentTime"=>"2014-01-14 20:21:30 +0500"}, {"stopTag"=>"5215", "tripTag"=>"5840324", "epochTime"=>"1389706282012", "currentTime"=>"2014-01-14 20:21:30 +0500"}, {"stopTag"=>"5215", "tripTag"=>"5840083", "epochTime"=>"1389706400805", "currentTime"=>"2014-01-14 20:21:30 +0500"}]
}
end
it "should not create events twice" do
lambda { @agent.check }.should change {@agent.events.count}.by(4)
lambda { @agent.check }.should_not change {@agent.events.count}
end
end
describe "validation" do
it "should validate presence of stops" do
@agent.options['stops'] = nil
@agent.should_not be_valid
end
it "should validate presence of agency" do
@agent.options['agency'] = ""
@agent.should_not be_valid
end
it "should validate presence of alert_window_in_minutes" do
@agent.options['alert_window_in_minutes'] = ""
@agent.should_not be_valid
end
end
end