Skip to content

Commit

Permalink
add cucumber test for the trip plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
chaupow committed Sep 1, 2015
1 parent 74e00cf commit a711596
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
3 changes: 3 additions & 0 deletions features/step_definitions/matching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
end
end

puts table
puts sub_matchings

ok = true
encoded_result = ""
extended_target = ""
Expand Down
121 changes: 121 additions & 0 deletions features/step_definitions/trip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
When /^I plan a trip I should get$/ do |table|
reprocess
actual = []
OSRMLoader.load(self,"#{prepared_file}.osrm") do
table.hashes.each_with_index do |row,ri|
if row['request']
got = {'request' => row['request'] }
response = request_url row['request']
else
params = @query_params
waypoints = []
if row['from'] and row['to']
node = find_node_by_name(row['from'])
raise "*** unknown from-node '#{row['from']}" unless node
waypoints << node

node = find_node_by_name(row['to'])
raise "*** unknown to-node '#{row['to']}" unless node
waypoints << node

got = {'from' => row['from'], 'to' => row['to'] }
response = request_trip waypoints, params
elsif row['waypoints']
row['waypoints'].split(',').each do |n|
node = find_node_by_name(n.strip)
raise "*** unknown waypoint node '#{n.strip}" unless node
waypoints << node
end
got = {'waypoints' => row['waypoints'] }
response = request_trip waypoints, params
else
raise "*** no waypoints"
end
end

row.each_pair do |k,v|
if k =~ /param:(.*)/
if v=='(nil)'
params[$1]=nil
elsif v!=nil
params[$1]=v
end
got[k]=v
end
end

if response.body.empty? == false
json = JSON.parse response.body
end

if table.headers.include? 'status'
got['status'] = json['status'].to_s
end
if table.headers.include? 'message'
got['message'] = json['status_message']
end
if table.headers.include? '#' # comment column
got['#'] = row['#'] # copy value so it always match
end

if response.code == "200"
if table.headers.include? 'trips'
sub_trips = json['trips'].compact.map { |sub| sub['via_points']}
end
end

######################
ok = true
encoded_result = ""
extended_target = ""
row['trips'].split(',').each_with_index do |sub, sub_idx|
if sub_idx >= sub_trips.length
ok = false
break
end

ok = false;
#TODO: Check all rotations of the round trip
sub.length.times do |node_idx|
node = find_node_by_name(sub[node_idx])
out_node = sub_trips[sub_idx][node_idx]
if FuzzyMatch.match_location out_node, node
encoded_result += sub[node_idx]
extended_target += sub[node_idx]
ok = true
else
encoded_result += "? [#{out_node[0]},#{out_node[1]}]"
extended_target += "#{sub[node_idx]} [#{node.lat},#{node.lon}]"
end
end
end

if ok
got['trips'] = row['trips']
got['via_points'] = row['via_points']
else
got['trips'] = encoded_result
row['trips'] = extended_target
log_fail row,got, { 'trip' => {:query => @query, :response => response} }
end


ok = true
row.keys.each do |key|
if FuzzyMatch.match got[key], row[key]
got[key] = row[key]
else
ok = false
end
end

unless ok
log_fail row,got, { 'trip' => {:query => @query, :response => response} }
end

actual << got
end
end
table.diff! actual
end

14 changes: 14 additions & 0 deletions features/support/trip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'net/http'
HOST = "http://127.0.0.1:#{OSRM_PORT}"

def request_trip waypoints=[], params={}
defaults = { 'output' => 'json' }
locs = waypoints.compact.map { |w| "loc=#{w.lat},#{w.lon}" }

params = (locs + defaults.merge(params).to_param).join('&')
params = nil if params==""

uri = generate_request_url ("trip" + '?' + params)
response = send_request uri, waypoints, params
end

86 changes: 86 additions & 0 deletions features/testbot/trip.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
@trip @testbot
Feature: Basic trip planning

Background:
Given the profile "testbot"
Given a grid size of 10 meters

Scenario: Testbot - Trip planning with less than 10 nodes
Given the node map
| a | b |
| d | c |

And the ways
| nodes |
| ab |
| bc |
| cb |
| da |

When I plan a trip I should get
| waypoints | trips |
| a,b,c,d | dcba |

Scenario: Testbot - Trip planning with more than 10 nodes
Given the node map
| a | b | c | d |
| l | | | e |
| k | | | f |
| j | i | h | g |

And the ways
| nodes |
| ab |
| bc |
| cb |
| de |
| ef |
| fg |
| gh |
| hi |
| ij |
| jk |
| kl |
| la |


When I plan a trip I should get
| waypoints | trips |
| a,b,c,d,e,f,g,h,i,j,k,l | cbalkjihgfedc |

Scenario: Testbot - Trip planning with multiple scc
Given the node map
| a | b | c | d |
| l | | | e |
| k | | | f |
| j | i | h | g |
| | | | |
| m | n | | |
| p | o | | |

And the ways
| nodes |
| ab |
| bc |
| cb |
| de |
| ef |
| fg |
| gh |
| hi |
| ij |
| jk |
| kl |
| la |
| mn |
| no |
| op |
| pm |


When I plan a trip I should get
| waypoints | trips |
| a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p | cbalkjihgfedc,ponm |



0 comments on commit a711596

Please sign in to comment.