aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/iev_api/client_spec.rb261
-rw-r--r--spec/lib/model_attribute_spec.rb1
-rw-r--r--spec/lib/route_way_cost_unit_converter_spec.rb38
-rw-r--r--spec/lib/stop_areas_to_way_costs_converter_spec.rb70
-rw-r--r--spec/lib/tom_tom/batch_spec.rb52
-rw-r--r--spec/lib/tom_tom_spec.rb15
-rw-r--r--spec/lib/way_cost_collection_json_serializer_spec.rb37
7 files changed, 212 insertions, 262 deletions
diff --git a/spec/lib/iev_api/client_spec.rb b/spec/lib/iev_api/client_spec.rb
deleted file mode 100644
index f82bf0cae..000000000
--- a/spec/lib/iev_api/client_spec.rb
+++ /dev/null
@@ -1,261 +0,0 @@
-# require 'spec_helper'
-
-# describe IevApi::Client do
-# describe 'initialization' do
-# before do
-# @keys = IevApi::Configuration::VALID_OPTIONS_KEYS
-# end
-
-# context "with module configuration" do
-# before do
-# IevApi.configure do |config|
-# @keys.each do |key|
-# config.send("#{key}=", key)
-# end
-# end
-# end
-
-# after do
-# IevApi.reset
-# end
-
-# it "should inherit module configuration" do
-# api = IevApi::Client.new
-# @keys.each do |key|
-# expect(api.send(key)).to eq(key)
-# end
-# end
-
-# context "with class configuration" do
-
-# before do
-# @configuration = {
-# :account => 'test',
-# :auth_token => 'token',
-# :secure => true,
-# :connection_options => {},
-# :adapter => :em_http,
-# :user_agent => 'Iev API Tests',
-# :middleware => IevApi::Configuration::DEFAULT_MIDDLEWARE
-# }
-# end
-
-# context "during initialization" do
-# it "should override module configuration" do
-# api = IevApi::Client.new(@configuration)
-# @keys.each do |key|
-# expect(api.send(key)).to eq(@configuration[key])
-# end
-# end
-# end
-
-# context "after initilization" do
-# it "should override module configuration after initialization" do
-# api = IevApi::Client.new
-# @configuration.each do |key, value|
-# api.send("#{key}=", value)
-# end
-# @keys.each do |key|
-# expect(api.send(key)).to eq(@configuration[key])
-# end
-# end
-# end
-# end
-# end
-
-# # context 'with customized middleware' do
-# # let(:logdev) { StringIO.new }
-# # # Client#connection is a private method.
-# # # Adding logger middleware component with an argument it should receive
-# # # when a connection is initialized
-# # let(:logger_middleware) { [Faraday::Response::Logger, Logger.new(logdev)] }
-# # let(:options) do
-# # {
-# # :account => 'myapp', :auth_token => 'abcdefg123456', :secure => false,
-# # :middleware => IevApi::Configuration::DEFAULT_MIDDLEWARE + [logger_middleware]
-# # }
-# # end
-# # let(:api) { IevApi::Client.new(options) }
-
-# # # request something to initialize @connection with middleware
-# # #before { api.jobs("test") }
-
-# # it 'splats array to initialize middleware with arguments' do
-# # # check that the logger added above did receive the argument
-# # expect(logdev.string).to include(api.projects_path)
-# # end
-# # end
-# end
-
-# describe 'api requests'do
-# before(:all) do
-# options = { :account => 'myapp', :secure => false }
-# IevApi.configure(options)
-
-# @client = IevApi::Client.new
-# end
-
-# # it "should fail with errors" do
-# # expect {
-# # @client.notices(1696172)
-# # }.to raise_error(IevApi::AirbrakeError, /You are not authorized to see that page/)
-# # end
-
-# # describe '#deploys' do
-# # it 'returns an array of deploys' do
-# # expect(@client.deploys('12345')).to be_kind_of(Array)
-# # end
-
-# # it 'returns deploy data' do
-# # deploys = @client.deploys('12345')
-# # first_deploy = deploys.first
-
-# # expect(first_deploy.rails_env).to eq('production')
-# # end
-
-# # it 'returns empty when no data' do
-# # expect(@client.deploys('67890')).to be_kind_of(Array)
-# # end
-# # end
-
-# # describe '#projects' do
-# # it 'returns an array of projects' do
-# # expect(@client.projects).to be_kind_of(Array)
-# # end
-
-# # it 'returns project data' do
-# # projects = @client.projects
-# # expect(projects.size).to eq(4)
-# # expect(projects.first.id).to eq('1')
-# # expect(projects.first.name).to eq('Venkman')
-# # end
-# # end
-
-# # describe '#update' do
-# # it 'should update the status of an error' do
-# # error = @client.update(1696170, :group => { :resolved => true})
-# # expect(error.resolved).to be_truthy
-# # end
-# # end
-
-# # describe '#errors' do
-# # it "should find a page of the 30 most recent errors" do
-# # errors = @client.errors
-# # ordered = errors.sort_by(&:most_recent_notice_at).reverse
-# # expect(ordered).to eq(errors)
-# # expect(errors.size).to eq(30)
-# # end
-
-# # it "should paginate errors" do
-# # errors = @client.errors(:page => 2)
-# # ordered = errors.sort_by(&:most_recent_notice_at).reverse
-# # expect(ordered).to eq(errors)
-# # expect(errors.size).to eq(2)
-# # end
-
-# # it "should use project_id for error path" do
-# # expect(@client).to receive(:request).with(:get, "/projects/123/groups.xml", {}).and_return(double(:group => 111))
-# # @client.errors(:project_id => 123)
-# # end
-# # end
-
-# # describe '#error' do
-# # it "should find an individual error" do
-# # error = @client.error(1696170)
-# # expect(error.action).to eq('index')
-# # expect(error.id).to eq(1696170)
-# # end
-# # end
-
-# # describe '#notice' do
-# # it "finds individual notices" do
-# # expect(@client.notice(1234, 1696170)).not_to be_nil
-# # end
-
-# # it "finds broken notices" do
-# # expect(@client.notice(666, 1696170)).not_to be_nil
-# # end
-# # end
-
-# # describe '#notices' do
-# # it "finds all error notices" do
-# # notices = @client.notices(1696170)
-# # expect(notices.size).to eq(42)
-# # end
-
-# # it "finds error notices for a specific page" do
-# # notices = @client.notices(1696170, :page => 1)
-# # expect(notices.size).to eq(30)
-# # expect(notices.first.backtrace).not_to eq(nil)
-# # expect(notices.first.id).to eq(1234)
-# # end
-
-# # it "finds all error notices with a page limit" do
-# # notices = @client.notices(1696171, :pages => 2)
-# # expect(notices.size).to eq(60)
-# # end
-
-# # it "yields batches" do
-# # batches = []
-# # notices = @client.notices(1696171, :pages => 2) do |batch|
-# # batches << batch
-# # end
-# # expect(notices.size).to eq(60)
-# # expect(batches.map(&:size)).to eq([30,30])
-# # end
-
-# # it "can return raw results" do
-# # notices = @client.notices(1696170, :raw => true)
-# # expect(notices.first.backtrace).to eq(nil)
-# # expect(notices.first.id).to eq(1234)
-# # end
-# # end
-
-# # describe '#connection' do
-# # it 'returns a Faraday connection' do
-# # expect(@client.send(:connection)).to be_kind_of(Faraday::Connection)
-# # end
-# # end
-# end
-
-# # describe '#url_for' do
-# # before(:all) do
-# # options = { :account => 'myapp', :auth_token => 'abcdefg123456', :secure => false }
-# # IevApi.configure(options)
-
-# # @client = IevApi::Client.new
-# # end
-
-# # it 'generates web urls for projects' do
-# # expect(@client.url_for(:projects)).to eq('http://myapp.airbrake.io/projects')
-# # end
-
-# # it 'generates web urls for deploys' do
-# # expect(@client.url_for(:deploys, '2000')).to eq('http://myapp.airbrake.io/projects/2000/deploys')
-# # end
-
-# # it 'generates web urls for errors' do
-# # expect(@client.url_for(:errors)).to eq('http://myapp.airbrake.io/groups')
-# # end
-
-# # it 'generates web urls for errors with project_id' do
-# # expect(@client.url_for(:errors, :project_id => 123)).to eq('http://myapp.airbrake.io/projects/123/groups')
-# # end
-
-# # it 'generates web urls for individual errors' do
-# # expect(@client.url_for(:error, 1696171)).to eq('http://myapp.airbrake.io/errors/1696171')
-# # end
-
-# # it 'generates web urls for notices' do
-# # expect(@client.url_for(:notices, 1696171)).to eq('http://myapp.airbrake.io/groups/1696171/notices')
-# # end
-
-# # it 'generates web urls for individual notices' do
-# # expect(@client.url_for(:notice, 123, 1696171)).to eq('http://myapp.airbrake.io/groups/1696171/notices/123')
-# # end
-
-# # it 'raises an exception when passed an unknown endpoint' do
-# # expect { @client.url_for(:foo) }.to raise_error(ArgumentError)
-# # end
-# # end
-# end
diff --git a/spec/lib/model_attribute_spec.rb b/spec/lib/model_attribute_spec.rb
index 6da740dcb..b35a71155 100644
--- a/spec/lib/model_attribute_spec.rb
+++ b/spec/lib/model_attribute_spec.rb
@@ -82,7 +82,6 @@ RSpec.describe ModelAttribute do
ModelAttribute.define(:route, :name, :string)
ModelAttribute.define(:route, :checked_at, :date)
ModelAttribute.define(:journey_pattern, :name, :string)
- ModelAttribute.define(:journey_pattern, :section_status, :integer)
expect(ModelAttribute.methods_by_class_and_type(:route, :string)).to match_array([
ModelAttribute.new(:route, :name, :string)
diff --git a/spec/lib/route_way_cost_unit_converter_spec.rb b/spec/lib/route_way_cost_unit_converter_spec.rb
new file mode 100644
index 000000000..3c5e51710
--- /dev/null
+++ b/spec/lib/route_way_cost_unit_converter_spec.rb
@@ -0,0 +1,38 @@
+RSpec.describe RouteWayCostUnitConverter do
+ describe ".convert" do
+ it "converts distance from meters to km and time from seconds to minutes " \
+ "and rounds to two decimal places" do
+ costs = {
+ '1-2' => {
+ 'distance' => 35223,
+ 'time' => 5604
+ },
+ '94435-97513' => {
+ 'distance' => 35919,
+ 'time' => 6174
+ },
+ '2-3' => {
+ 'distance' => nil,
+ 'time' => nil
+ }
+ }
+
+ expect(
+ RouteWayCostUnitConverter.convert(costs)
+ ).to eq({
+ '1-2' => {
+ 'distance' => 35,
+ 'time' => 93
+ },
+ '94435-97513' => {
+ 'distance' => 35,
+ 'time' => 102
+ },
+ '2-3' => {
+ 'distance' => 0,
+ 'time' => 0
+ }
+ })
+ end
+ end
+end
diff --git a/spec/lib/stop_areas_to_way_costs_converter_spec.rb b/spec/lib/stop_areas_to_way_costs_converter_spec.rb
new file mode 100644
index 000000000..7e09ede2e
--- /dev/null
+++ b/spec/lib/stop_areas_to_way_costs_converter_spec.rb
@@ -0,0 +1,70 @@
+RSpec.describe StopAreasToWayCostsConverter do
+ describe "#convert" do
+ it "converts a StopArea collection to WayCosts" do
+ route = create(:route_common)
+ coordinates = [
+ [1.5, 1.8],
+ [2.2, 2.1],
+ [3.0, 3.6],
+ [4.9, 4.3]
+ ]
+
+ stop_areas = coordinates.map do |latlng|
+ stop_area = create(
+ :stop_area,
+ area_type: 'zdep',
+ latitude: latlng[0],
+ longitude: latlng[1]
+ )
+
+ create(
+ :stop_point,
+ route: route,
+ stop_area: stop_area
+ )
+
+ stop_area
+ end
+
+ way_costs = [
+ WayCost.new(
+ departure: Geokit::LatLng.new(
+ stop_areas[0].latitude,
+ stop_areas[0].longitude
+ ),
+ arrival: Geokit::LatLng.new(
+ stop_areas[1].latitude,
+ stop_areas[1].longitude
+ ),
+ id: "#{stop_areas[0].id}-#{stop_areas[1].id}"
+ ),
+ WayCost.new(
+ departure: Geokit::LatLng.new(
+ stop_areas[1].latitude,
+ stop_areas[1].longitude
+ ),
+ arrival: Geokit::LatLng.new(
+ stop_areas[2].latitude,
+ stop_areas[2].longitude
+ ),
+ id: "#{stop_areas[1].id}-#{stop_areas[2].id}"
+ ),
+ WayCost.new(
+ departure: Geokit::LatLng.new(
+ stop_areas[2].latitude,
+ stop_areas[2].longitude
+ ),
+ arrival: Geokit::LatLng.new(
+ stop_areas[3].latitude,
+ stop_areas[3].longitude
+ ),
+ id: "#{stop_areas[2].id}-#{stop_areas[3].id}"
+ )
+ ]
+
+ expect(
+ StopAreasToWayCostsConverter.new(stop_areas).convert
+ ).to eq(way_costs)
+ end
+ end
+end
diff --git a/spec/lib/tom_tom/batch_spec.rb b/spec/lib/tom_tom/batch_spec.rb
new file mode 100644
index 000000000..c3ab46a4b
--- /dev/null
+++ b/spec/lib/tom_tom/batch_spec.rb
@@ -0,0 +1,52 @@
+RSpec.describe TomTom::Batch do
+ let(:batch) { TomTom::Batch.new(nil) }
+
+ describe "#extract_costs_to_way_costs" do
+ it "puts distance & time costs in way_costs" do
+ way_costs = [
+ WayCost.new(
+ departure: Geokit::LatLng.new(48.85086, 2.36143),
+ arrival: Geokit::LatLng.new(47.91231, 1.87606)
+ ),
+ WayCost.new(
+ departure: Geokit::LatLng.new(47.91231, 1.87606),
+ arrival: Geokit::LatLng.new(52.50867, 13.42879)
+ )
+ ]
+
+ expected_way_costs = way_costs.deep_dup
+ expected_way_costs[0].distance = 117947
+ expected_way_costs[0].time = 7969
+ expected_way_costs[1].distance = 1114379
+ expected_way_costs[1].time = 71010
+
+ batch_response = JSON.parse(read_fixture('tom_tom_batch.json'))
+
+ expect(
+ batch.extract_costs_to_way_costs!(way_costs, batch_response)
+ ).to match_array(expected_way_costs)
+ end
+ end
+
+ describe "#convert_way_costs" do
+ it "turns WayCost points into a collection of colon-separated strings" do
+ way_costs = [
+ WayCost.new(
+ departure: Geokit::LatLng.new(2, 48),
+ arrival: Geokit::LatLng.new(3, 46)
+ ),
+ WayCost.new(
+ departure: Geokit::LatLng.new(-71, 42),
+ arrival: Geokit::LatLng.new(-71.5, 42.9)
+ )
+ ]
+
+ expect(
+ batch.convert_way_costs(way_costs)
+ ).to eq([
+ '2,48:3,46',
+ '-71,42:-71.5,42.9'
+ ])
+ end
+ end
+end
diff --git a/spec/lib/tom_tom_spec.rb b/spec/lib/tom_tom_spec.rb
new file mode 100644
index 000000000..71584e242
--- /dev/null
+++ b/spec/lib/tom_tom_spec.rb
@@ -0,0 +1,15 @@
+RSpec.describe TomTom do
+ describe ".enabled?" do
+ it "returns true when API key is set" do
+ TomTom.instance_variable_set(:@api_key, 'fake key')
+
+ expect(TomTom.enabled?).to be true
+ end
+
+ it "returns false without an API key" do
+ TomTom.instance_variable_set(:@api_key, '')
+
+ expect(TomTom.enabled?).to be false
+ end
+ end
+end
diff --git a/spec/lib/way_cost_collection_json_serializer_spec.rb b/spec/lib/way_cost_collection_json_serializer_spec.rb
new file mode 100644
index 000000000..22faf88d3
--- /dev/null
+++ b/spec/lib/way_cost_collection_json_serializer_spec.rb
@@ -0,0 +1,37 @@
+RSpec.describe WayCostCollectionJSONSerializer do
+ describe ".dump" do
+ it "creates a JSON hash of hashed WayCost attributes" do
+ way_costs = [
+ WayCost.new(
+ departure: Geokit::LatLng.new(48.85086, 2.36143),
+ arrival: Geokit::LatLng.new(47.91231, 1.87606),
+ distance: 1234,
+ time: 99,
+ id: '1-2'
+ ),
+ WayCost.new(
+ departure: Geokit::LatLng.new(47.91231, 1.87606),
+ arrival: Geokit::LatLng.new(52.50867, 13.42879),
+ distance: 5678,
+ time: 999,
+ id: '2-3'
+ )
+ ]
+
+ expect(
+ WayCostCollectionJSONSerializer.dump(way_costs)
+ ).to eq(<<-JSON.delete(" \n"))
+ {
+ "1-2": {
+ "distance": 1234,
+ "time": 99
+ },
+ "2-3": {
+ "distance": 5678,
+ "time": 999
+ }
+ }
+ JSON
+ end
+ end
+end