Add step definition and support code for matching

This commit is contained in:
Patrick Niklaus 2015-03-05 00:12:26 +01:00
parent 98dba11c5e
commit 7829e3c132
2 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,88 @@
When /^I match 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
trace = []
timestamps = []
if row['trace']
row['trace'].split(',').each do |n|
node = find_node_by_name(n.strip)
raise "*** unknown waypoint node '#{n.strip}" unless node
trace << node
end
got = {'trace' => row['trace'] }
response = request_matching trace, timestamps, 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
sub_matchings = []
if response.code == "200"
if table.headers.include? 'matchings'
sub_matchings = json['matchings'].compact.map { |sub| sub['matched_points']}
end
end
ok = true
encoded_result = ""
extended_target = ""
row['matchings'].split(',').each_with_index do |sub, sub_idx|
sub.length.times do |node_idx|
node = find_node_by_name(sub[node_idx])
out_node = sub_matchings[sub_idx][node_idx]
if FuzzyMatch.match_location out_node, node
encoded_result += sub[node_idx]
extended_target += sub[node_idx]
else
encoded_result += "[#{out_node[0]},#{out_node[1]}]"
extended_target += "#{sub[node_idx]} [#{node.lat},#{node.lon}]"
ok = false
end
end
end
if ok
got['matchings'] = row['matchings']
else
got['matchings'] = encoded_result
row['matchings'] = extended_target
log_fail row,got, { 'matching' => {:query => @query, :response => response} }
end
actual << got
end
end
table.routing_diff! actual
end

30
features/support/match.rb Normal file
View File

@ -0,0 +1,30 @@
require 'net/http'
HOST = "http://127.0.0.1:#{OSRM_PORT}"
DESTINATION_REACHED = 15 #OSRM instruction code
class Hash
def to_param(namespace = nil)
collect do |key, value|
"#{key}=#{value}"
end.sort
end
end
def request_matching trace=[], timestamps=[], options={}
defaults = { 'output' => 'json' }
locs = waypoints.compact.map { |w| "loc=#{w.lat},#{w.lon}" }
ts = timestamps.compact.map { |t| "t=#{t}" }
params = (locs + ts + defaults.merge(options).to_param).join('&')
params = nil if params==""
uri = URI.parse ["#{HOST}/match", params].compact.join('?')
@query = uri.to_s
Timeout.timeout(OSRM_TIMEOUT) do
Net::HTTP.get_response uri
end
rescue Errno::ECONNREFUSED => e
raise "*** osrm-routed is not running."
rescue Timeout::Error
raise "*** osrm-routed did not respond."
end