From f104ce335c0b570987ab966306e3c3abedb2a475 Mon Sep 17 00:00:00 2001 From: "Umar M. Sheikh" Date: Tue, 14 Jan 2014 20:32:06 +0500 Subject: [PATCH] specs for the public transport agent --- app/models/agents/public_transport_agent.rb | 2 +- .../agents/public_transport_agent_spec.rb | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 spec/models/agents/public_transport_agent_spec.rb diff --git a/app/models/agents/public_transport_agent.rb b/app/models/agents/public_transport_agent.rb index 5ca85f99..5fcd14c5 100644 --- a/app/models/agents/public_transport_agent.rb +++ b/app/models/agents/public_transport_agent.rb @@ -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) diff --git a/spec/models/agents/public_transport_agent_spec.rb b/spec/models/agents/public_transport_agent_spec.rb new file mode 100644 index 00000000..fb7b87c4 --- /dev/null +++ b/spec/models/agents/public_transport_agent_spec.rb @@ -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