|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'yaml' |
| 4 | +require 'open-uri' |
| 5 | +require 'json' |
| 6 | +require 'minitest/autorun' |
| 7 | + |
| 8 | +API_KEY="org.onebusaway.iphone" |
| 9 | +BASE_URL="http://localhost:8080/api/where" |
| 10 | + |
| 11 | +SERVICES = { |
| 12 | + dash: { |
| 13 | + agency_id: '1', |
| 14 | + route_id: '1_35', |
| 15 | + stop_id: '1_512', |
| 16 | + lat: 38.83334, |
| 17 | + lon: -77.09186, |
| 18 | + route_min_count: 10 |
| 19 | + }, |
| 20 | + raba: { |
| 21 | + agency_id: '25', |
| 22 | + route_id: '25_24', |
| 23 | + stop_id: '25_2000', |
| 24 | + lat: 40.3061885, |
| 25 | + lon: -122.121985, |
| 26 | + search_radius: 20_000, |
| 27 | + route_min_count: 10 |
| 28 | + }, |
| 29 | + sdmts: { |
| 30 | + agency_id: 'MTS', |
| 31 | + route_id: 'MTS_992', |
| 32 | + stop_id: 'MTS_60499', |
| 33 | + lat: 32.899853, |
| 34 | + lon: -116.731188, |
| 35 | + route_min_count: 100, |
| 36 | + search_radius: 20_000, |
| 37 | + agencies_count: 4 |
| 38 | + }, |
| 39 | + sta: { |
| 40 | + agency_id: 'STA', |
| 41 | + route_id: 'STA_63', |
| 42 | + stop_id: 'STA_CONC', |
| 43 | + lat: 47.622782, |
| 44 | + lon: -117.390875, |
| 45 | + route_min_count: 50 |
| 46 | + }, |
| 47 | + tampa: { |
| 48 | + agency_id: '1', |
| 49 | + route_id: '1_10', |
| 50 | + stop_id: '1_4340', |
| 51 | + lat: 27.950712, |
| 52 | + lon: -82.397875, |
| 53 | + route_min_count: 30, |
| 54 | + agencies_count: 2 |
| 55 | + }, |
| 56 | + unitrans: { |
| 57 | + agency_id: 'unitrans', |
| 58 | + route_id: 'unitrans_O', |
| 59 | + stop_id: 'unitrans_22102', |
| 60 | + lat: 38.555308, |
| 61 | + lon: -121.73599, |
| 62 | + route_min_count: 20 |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +def call_api(endpoint, params = {}) |
| 67 | + url = "#{BASE_URL}/#{endpoint}?key=#{API_KEY}" |
| 68 | + url += "&#{URI.encode_www_form(params)}" unless params.empty? |
| 69 | + response = URI.open(url).read |
| 70 | + JSON.parse(response) |
| 71 | +end |
| 72 | + |
| 73 | +def service_name |
| 74 | + compose = YAML.load_file(File.join(__dir__, '..', 'docker-compose.yaml')) |
| 75 | + compose.dig('services', 'oba_app', 'build', 'dockerfile').split('.').last.to_sym |
| 76 | +end |
| 77 | + |
| 78 | +def load_data |
| 79 | + service = SERVICES[service_name] |
| 80 | + raise "Service not found: #{service_name}" unless service |
| 81 | + service |
| 82 | +end |
| 83 | + |
| 84 | +puts "Running API tests for service: #{service_name}" |
| 85 | + |
| 86 | +class ApiTests < Minitest::Test |
| 87 | + def setup |
| 88 | + @service = load_data |
| 89 | + end |
| 90 | + |
| 91 | + def test_current_time |
| 92 | + response = call_api("current-time.json") |
| 93 | + assert_equal(200, response["code"]) |
| 94 | + assert_match(/\d+/, response["currentTime"].to_s) |
| 95 | + end |
| 96 | + |
| 97 | + def test_agencies_with_coverage |
| 98 | + response = call_api("agencies-with-coverage.json") |
| 99 | + agencies = response.dig('data', 'list') |
| 100 | + assert_equal(@service[:agencies_count] || 1, agencies.length) |
| 101 | + agency = agencies.filter {|a| a['agencyId'] == @service[:agency_id] }.first |
| 102 | + assert_in_delta(@service[:lat], agency['lat']) |
| 103 | + assert_in_delta(@service[:lon], agency['lon']) |
| 104 | + end |
| 105 | + |
| 106 | + def test_routes_for_agency |
| 107 | + response = call_api("routes-for-agency/#{@service[:agency_id]}.json") |
| 108 | + routes = response.dig('data', 'list') |
| 109 | + assert_operator(@service[:route_min_count], :<, routes.length) |
| 110 | + route = routes.first |
| 111 | + assert_equal(@service[:agency_id], route['agencyId']) |
| 112 | + refute_empty(route['nullSafeShortName']) |
| 113 | + end |
| 114 | + |
| 115 | + def test_stops_for_route |
| 116 | + response = call_api("stops-for-route/#{@service[:route_id]}.json") |
| 117 | + entry = response.dig('data', 'entry') |
| 118 | + assert_equal(["polylines", "routeId", "stopGroupings", "stopIds"], entry.keys) |
| 119 | + assert_equal(@service[:route_id], entry['routeId']) |
| 120 | + end |
| 121 | + |
| 122 | + def test_stop |
| 123 | + response = call_api("stop/#{@service[:stop_id]}.json") |
| 124 | + data = response.dig('data', 'entry') |
| 125 | + assert_equal(@service[:stop_id], data['id']) |
| 126 | + end |
| 127 | + |
| 128 | + def test_stops_for_location |
| 129 | + params = {lat: @service[:lat], lon: @service[:lon]} |
| 130 | + if @service[:search_radius] |
| 131 | + params[:radius] = @service[:search_radius] |
| 132 | + end |
| 133 | + response = call_api("stops-for-location.json", params) |
| 134 | + assert_operator(0, :<, response.dig('data', 'list').count) |
| 135 | + end |
| 136 | + |
| 137 | + def test_arrivals_and_departures_for_stop |
| 138 | + response = call_api("arrivals-and-departures-for-stop/#{@service[:stop_id]}.json") |
| 139 | + data = response.dig('data', 'entry', 'arrivalsAndDepartures') |
| 140 | + |
| 141 | + departures = data.collect {|d| d['predictedDepartureTime'] }.compact.select {|d| d > 0 } |
| 142 | + assert_operator(0, :<, departures.count, 'has real time data') |
| 143 | + end |
| 144 | +end |
0 commit comments