diff options
| author | Teddy Wing | 2018-03-13 15:37:55 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2018-03-14 16:42:02 +0100 | 
| commit | dbcd099b3d38f7c584450edffc8bee0a2502b170 (patch) | |
| tree | 0ddcd221974db03e957e6621a5f4b050833ffc87 /spec/services | |
| parent | 012363fca2bbe2d75145e8ca3b85f8fcb48ad164 (diff) | |
| download | chouette-core-dbcd099b3d38f7c584450edffc8bee0a2502b170.tar.bz2 | |
Route: Add `#calculate_costs!` to populate `#costs` with `WayCost`s
This new method will launch a worker that takes the route's `StopArea`s,
converts them to `WayCost`s, sends them to the TomTom API to calculate
distance and time costs, and saves those costs to the route's `costs`
field.
Refs #6095
Diffstat (limited to 'spec/services')
| -rw-r--r-- | spec/services/route_way_cost_calculator_spec.rb | 31 | 
1 files changed, 31 insertions, 0 deletions
| diff --git a/spec/services/route_way_cost_calculator_spec.rb b/spec/services/route_way_cost_calculator_spec.rb new file mode 100644 index 000000000..d5358fcf6 --- /dev/null +++ b/spec/services/route_way_cost_calculator_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe RouteWayCostCalculator do +  describe "#calculate!" do +    it "calculates and stores WayCosts in the given route's #cost field" do +      route = create(:route) + +      # Fake the request to the TomTom API, but don't actually send the right +      # things in the request or response. This is just to fake the request so +      # we don't actually call their API in tests. The test doesn't test +      # anything given in the response. +      stub_request(:post, "https://api.tomtom.com/routing/1/batch/json?key") +        .with( +          headers: { +            'Accept'=>'*/*', +            'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', +            'Content-Type'=>'application/json', +            'User-Agent'=>'Faraday v0.9.2' +          } +        ) +        .to_return( +          status: 200, +          body: "{\"formatVersion\":\"0.0.1\",\"batchItems\":[{\"statusCode\":200,\"response\":{\"routes\":[{\"summary\":{\"lengthInMeters\":117947,\"travelTimeInSeconds\":7969,\"trafficDelayInSeconds\":0,\"departureTime\":\"2018-03-12T12:32:26+01:00\",\"arrivalTime\":\"2018-03-12T14:45:14+01:00\"}}]}}]}", +          headers: {} +        ) + +      RouteWayCostCalculator.new(route).calculate! + +      expect(route.costs).not_to be_nil +      expect { JSON.parse(JSON.dump(route.costs)) }.not_to raise_error +    end +  end +end | 
