diff options
Diffstat (limited to 'spec/lib')
| -rw-r--r-- | spec/lib/compliance_control_set_cloner_spec.rb | 2 | ||||
| -rw-r--r-- | spec/lib/compliance_control_set_copier_spec.rb | 2 | ||||
| -rw-r--r-- | spec/lib/route_way_cost_unit_converter_spec.rb | 28 | ||||
| -rw-r--r-- | spec/lib/tom_tom/matrix/request_json_serializer_spec.rb | 39 | ||||
| -rw-r--r-- | spec/lib/tom_tom/matrix_spec.rb | 228 | 
5 files changed, 297 insertions, 2 deletions
| diff --git a/spec/lib/compliance_control_set_cloner_spec.rb b/spec/lib/compliance_control_set_cloner_spec.rb index 7efe27659..0d3561e0e 100644 --- a/spec/lib/compliance_control_set_cloner_spec.rb +++ b/spec/lib/compliance_control_set_cloner_spec.rb @@ -49,7 +49,7 @@ RSpec.describe ComplianceControlSetCloner do        context 'Directed Acyclic Graph is copied correctly' do          let(:source_blox){ -          3.times.map{ |_| create :compliance_control_block, compliance_control_set: source_set } +          3.times.map{ |n| create :compliance_control_block, compliance_control_set: source_set, transport_mode: StifTransportModeEnumerations.transport_modes[n], transport_submode: StifTransportSubmodeEnumerations.transport_submodes[n]   }          }          let(:direct_ccs){            3.times.map{ |n| create :generic_attribute_control_min_max, compliance_control_set: source_set, name: "direct #{n.succ}", code: "direct-#{n.succ}" } diff --git a/spec/lib/compliance_control_set_copier_spec.rb b/spec/lib/compliance_control_set_copier_spec.rb index 0f15d86d0..d1a56cd7f 100644 --- a/spec/lib/compliance_control_set_copier_spec.rb +++ b/spec/lib/compliance_control_set_copier_spec.rb @@ -23,7 +23,7 @@ RSpec.describe ComplianceControlSetCopier do        context 'Directed Acyclic Graph is copied correctly' do          let(:cc_blox){ -          3.times.map{ |_| create :compliance_control_block, compliance_control_set: cc_set } +          3.times.map{ |n| create :compliance_control_block, compliance_control_set: cc_set, transport_mode: StifTransportModeEnumerations.transport_modes[n], transport_submode: StifTransportSubmodeEnumerations.transport_submodes[n] }          }          let!(:direct_ccs){            3.times.map{ |n| create :compliance_control, compliance_control_set: cc_set, name: "direct #{n.succ}", code: "direct-#{n.succ}" } diff --git a/spec/lib/route_way_cost_unit_converter_spec.rb b/spec/lib/route_way_cost_unit_converter_spec.rb index 3c5e51710..aa25d57d2 100644 --- a/spec/lib/route_way_cost_unit_converter_spec.rb +++ b/spec/lib/route_way_cost_unit_converter_spec.rb @@ -35,4 +35,32 @@ RSpec.describe RouteWayCostUnitConverter do        })      end    end + +  describe ".meters_to_kilometers" do +    it "converts meters to integer kilometres" do +      expect( +        RouteWayCostUnitConverter.meters_to_kilometers(6350) +      ).to eq(6) +    end + +    it "snaps values between 0 and 1 to 1" do +      expect( +        RouteWayCostUnitConverter.meters_to_kilometers(50) +      ).to eq(1) +    end +  end + +  describe ".seconds_to_minutes" do +    it "converts seconds to minutes" do +      expect( +        RouteWayCostUnitConverter.seconds_to_minutes(300) +      ).to eq(5) +    end + +    it "snaps values between 0 and 1 to 1" do +      expect( +        RouteWayCostUnitConverter.seconds_to_minutes(3) +      ).to eq(1) +    end +  end  end diff --git a/spec/lib/tom_tom/matrix/request_json_serializer_spec.rb b/spec/lib/tom_tom/matrix/request_json_serializer_spec.rb new file mode 100644 index 000000000..1fafad302 --- /dev/null +++ b/spec/lib/tom_tom/matrix/request_json_serializer_spec.rb @@ -0,0 +1,39 @@ +RSpec.describe TomTom::Matrix::RequestJSONSerializer do +  describe ".dump" do +    it "serializes BigDecimal values to floats" do +      points = [{ +        point: { +          latitude: 52.50867.to_d, +          longitude: 13.42879.to_d +        }, +      }] +      data = { +        origins: points, +        destinations: points +      } + +      expect( +        TomTom::Matrix::RequestJSONSerializer.dump(data) +      ).to eq(<<-JSON.delete(" \n")) +        { +          "origins": [ +            { +              "point": { +                "latitude": 52.50867, +                "longitude": 13.42879 +              } +            } +          ], +          "destinations": [ +            { +              "point": { +                "latitude": 52.50867, +                "longitude": 13.42879 +              } +            } +          ] +        } +      JSON +    end +  end +end diff --git a/spec/lib/tom_tom/matrix_spec.rb b/spec/lib/tom_tom/matrix_spec.rb new file mode 100644 index 000000000..605f1d254 --- /dev/null +++ b/spec/lib/tom_tom/matrix_spec.rb @@ -0,0 +1,228 @@ +RSpec.describe TomTom::Matrix do +  let(:matrix) { TomTom::Matrix.new(nil) } + +  describe "#points_from_way_costs" do +    it "extracts a set of lat/lng coordinates from a list of WayCosts" do +      way_costs = [ +        WayCost.new( +          departure: Geokit::LatLng.new(48.85086, 2.36143), +          arrival: Geokit::LatLng.new(47.91231, 1.87606), +          id: '44-77' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(47.91231, 1.87606), +          arrival: Geokit::LatLng.new(52.50867, 13.42879), +          id: '77-88' +        ) +      ] + +      expect( +        matrix.points_from_way_costs(way_costs) +      ).to eq([ +        TomTom::Matrix::Point.new( +          Geokit::LatLng.new(48.85086, 2.36143), +          '44' +        ), +        TomTom::Matrix::Point.new( +          Geokit::LatLng.new(47.91231, 1.87606), +          '77' +        ), +        TomTom::Matrix::Point.new( +          Geokit::LatLng.new(52.50867, 13.42879), +          '88' +        ) +      ]) +    end +  end + +  describe "#points_as_params" do +    it "transforms a set of LatLng points into a hash for use by TomTom Matrix" do +      points = [ +        TomTom::Matrix::Point.new( +          Geokit::LatLng.new(48.85086, 2.36143), +          '44' +        ), +        TomTom::Matrix::Point.new( +          Geokit::LatLng.new(47.91231, 1.87606), +          '77' +        ), +        TomTom::Matrix::Point.new( +          Geokit::LatLng.new(52.50867, 13.42879), +          '88' +        ) +      ] + +      expect( +        matrix.points_as_params(points) +      ).to eq([ +        { +          point: { +            latitude: 48.85086, +            longitude: 2.36143 +          }, +        }, +        { +          point: { +            latitude: 47.91231, +            longitude: 1.87606 +          }, +        }, +        { +          point: { +            latitude: 52.50867, +            longitude: 13.42879 +          }, +        } +      ]) +    end +  end + +  describe "#build_request_body" do +    it "serializes BigDecimal coordinates to floats" do +      points = [ +        { +          point: { +            latitude: 48.85086.to_d, +            longitude: 2.36143.to_d +          }, +        }, +        { +          point: { +            latitude: 47.91231.to_d, +            longitude: 1.87606.to_d +          }, +        }, +        { +          point: { +            latitude: 52.50867.to_d, +            longitude: 13.42879.to_d +          }, +        } +      ] + +      expect( +        matrix.build_request_body(points) +      ).to eq(<<-JSON.delete(" \n")) +        { +          "origins": [ +            { +              "point": { +                "latitude": 48.85086, +                "longitude": 2.36143 +              } +            }, +            { +              "point": { +                "latitude": 47.91231, +                "longitude": 1.87606 +              } +            }, +            { +              "point": { +                "latitude": 52.50867, +                "longitude": 13.42879 +              } +            } +          ], +          "destinations": [ +            { +              "point": { +                "latitude": 48.85086, +                "longitude": 2.36143 +              } +            }, +            { +              "point": { +                "latitude": 47.91231, +                "longitude": 1.87606 +              } +            }, +            { +              "point": { +                "latitude": 52.50867, +                "longitude": 13.42879 +              } +            } +          ] +        } +      JSON +    end +  end + +  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), +          id: '55-99' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(47.91231, 1.87606), +          arrival: Geokit::LatLng.new(52.50867, 13.42879), +          id: '99-22' +        ) +      ] + +      expected_way_costs = [ +        WayCost.new( +          departure: Geokit::LatLng.new(48.85086, 2.36143), +          arrival: Geokit::LatLng.new(47.91231, 1.87606), +          distance: 117947, +          time: 8356, +          id: '55-99' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(48.85086, 2.36143), +          arrival: Geokit::LatLng.new(52.50867, 13.42879), +          distance: 999088, +          time: 62653, +          id: '55-22' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(47.91231, 1.87606), +          arrival: Geokit::LatLng.new(48.85086, 2.36143), +          distance: 117231, +          time: 9729, +          id: '99-55' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(47.91231, 1.87606), +          arrival: Geokit::LatLng.new(52.50867, 13.42879), +          distance: 1114635, +          time: 72079, +          id: '99-22' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(52.50867, 13.42879), +          arrival: Geokit::LatLng.new(48.85086, 2.36143), +          distance: 997232, +          time: 63245, +          id: '22-55' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(52.50867, 13.42879), +          arrival: Geokit::LatLng.new(47.91231, 1.87606), +          distance: 1113108, +          time: 68485, +          id: '22-99' +        ), +        WayCost.new( +          departure: Geokit::LatLng.new(52.50867, 13.42879), +          arrival: Geokit::LatLng.new(52.50867, 13.42879), +          distance: 344, +          time: 109, +          id: '22-22' +        ) +      ] + +      matrix_response = JSON.parse(read_fixture('tom_tom_matrix.json')) + +      points = matrix.points_from_way_costs(way_costs) + +      expect( +        matrix.extract_costs_to_way_costs!(way_costs, points, matrix_response) +      ).to match_array(expected_way_costs) +    end +  end +end | 
