Skip to content

Commit

Permalink
Fix issue with geocoder by removing metadata from test
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 6, 2024
1 parent 30f5147 commit b896f5e
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 31 deletions.
10 changes: 8 additions & 2 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Address < ApplicationRecord
has_many :ride_origins, class_name: "Ride", foreign_key: "from_address_id", dependent: nil, inverse_of: :from_address
has_many :ride_destinations, class_name: "Ride", foreign_key: "to_address_id", dependent: nil, inverse_of: :to_address

# validates :line_1, :city, :state, :zip_code, :place_id, :latitude, :longitude, presence: true
validates :line_1, :city, :state, :zip_code, :place_id, :latitude, :longitude, presence: true
validates :place_id, uniqueness: true
validates :zip_code, uniqueness: { scope: %i[line_1 line_2] }

Expand All @@ -17,7 +17,13 @@ class Address < ApplicationRecord
end
end

before_validation :geocode
before_validation :geocode,
if: ->(obj) {
obj.full_address.present? && %i[line_1 line_2 city state zip_code place_id latitude
longitude].any? do
obj.send("#{_1}_changed?")
end
}

def full_address
[line_1, line_2, city, state, zip_code].compact.join(", ")
Expand Down
4 changes: 2 additions & 2 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Ride < ApplicationRecord
# with corresponding from_address_id
addresses = Address.where.not(id: current_driver_address.id)
.near([current_driver_address.latitude, current_driver_address.longitude], driver.max_radius)
binding.pry
selectable.where(id: addresses.map(&:id))

selectable.where(from_address_id: addresses.map(&:id))
}

def origin_place_id
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
api_key: ENV["GOOGLE_API_KEY"],

# geocoding service request timeout, in seconds (default 3):
timeout: 5,
timeout: 15,

# set default units to kilometers:
units: :mi,
Expand Down
4 changes: 4 additions & 0 deletions lib/client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def post(url, body, headers = nil)
connection.post(url, body.to_json, headers)
end

def get(url, params = nil, headers = nil)
connection.get(url, params, headers)
end

attr_reader :connection
private :connection

Expand Down
335 changes: 335 additions & 0 deletions spec/cassettes/nearby_drivers.yml

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions spec/factories/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
city { Faker::Address.city }
state { Faker::Address.state }
zip_code { Faker::Address.zip_code }
place_id { nil }
latitude { nil }
longitude { nil }
place_id { Faker::Internet.unique.device_token }
latitude { Faker::Address.latitude }
longitude { Faker::Address.longitude }

trait :with_out_place_id do
trait :with_out_location_data do
place_id { nil }
latitude { nil }
longitude { nil }
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/rides/commands/get_commute_duration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
it "gets data for an address" do
VCR.use_cassette("first_commute") do
from_address = create(
:address, :with_out_place_id, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
:address, :with_out_location_data, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
zip_code: "80547"
)
to_address = create(
:address, :with_out_place_id, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
:address, :with_out_location_data, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
zip_code: "80524"
)
create_list(:ride, 2, from_address:, to_address:)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

it "#full_address" do
VCR.use_cassette("initial_geocode") do
address = create(:address, :with_out_place_id, line_1: "711 Oval Drive", city: "Fort Collins", state: "CO",
address = create(:address, :with_out_location_data, line_1: "711 Oval Drive", city: "Fort Collins", state: "CO",
zip_code: "80521")
expect(address.full_address).to eq("711 Oval Drive, Fort Collins, CO, 80521")
expect(address.latitude).to eq(40.577655)
Expand Down
77 changes: 58 additions & 19 deletions spec/models/ride_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,75 @@

require "rails_helper"

RSpec.describe Ride, :skip_geocode, type: :model do
RSpec.describe Ride, type: :model do
subject { create(:ride) }
describe "associations" do
describe "associations", :skip_geocode do
it { is_expected.to belong_to(:driver).optional(true) }
it { is_expected.to belong_to(:from_address).class_name("Address") }
it { is_expected.to belong_to(:to_address).class_name("Address") }
end

describe "attributes" do
describe "attributes", :skip_geocode do
it { is_expected.to monetize(:amount_cents).as(:amount) }
it { is_expected.to validate_numericality_of(:amount_cents) }
end

describe "scopes" do
it ".nearby_drivers" do
VCR.use_cassette("nearby_drivers") do
from_address = create(
:address, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
place_id: "ChIJ0zbP73SzbocR7mXVIY-QdBM",
zip_code: "80547"
)
to_address = create(
:address, :with_out_place_id, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
place_id: "ChIJlRJnwIpKaYcRYzG0fYZOwpY",
zip_code: "80524"
)
create_list(:ride, 2, from_address:, to_address:)
driver = create(:driver, current_address: to_address)
rides = Ride.selectable.nearby_driver(driver)
binding.pry
describe ".nearby_drivers" do
it "gets rides within the driver's default max_radius (defaults to 10mi.)" do
VCR.use_cassette("nearby_drivers") do
from_address_1 = create(
:address, :with_out_location_data, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
zip_code: "80547"
)
to_address_1 = create(
:address, :with_out_location_data, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
zip_code: "80524"
)
create(:ride, from_address: from_address_1, to_address: to_address_1)
from_address_2 = create(
:address, :with_out_location_data, line_1: " 216 N College Ave #110", city: "Fort Collins", state: "CO",
zip_code: "80524"
)
to_address_2 = create(
:address, :with_out_location_data, line_1: "501 W 20th St", city: "Greeley", state: "CO",
zip_code: "80639"
)
create(:ride, from_address: from_address_2, to_address: to_address_2)
driver = create(:driver, current_address: to_address_1)
rides = Ride.selectable.nearby_driver(driver)

expect(rides.length).to eq(2)
end
end

it "gets rides near drivers with different max_radius" do
VCR.use_cassette("nearby_drivers") do
from_address_1 = create(
:address, :with_out_location_data, line_1: "4705 Weitzel Street", city: "Timnath", state: "CO",
zip_code: "80547"
)
to_address_1 = create(
:address, :with_out_location_data, line_1: "151 N College Ave", city: "Fort Collins", state: "CO",
zip_code: "80524"
)
create(:ride, from_address: from_address_1, to_address: to_address_1)
from_address_2 = create(
:address, :with_out_location_data, line_1: " 216 N College Ave #110", city: "Fort Collins", state: "CO",
zip_code: "80524"
)
to_address_2 = create(
:address, :with_out_location_data, line_1: "501 W 20th St", city: "Greeley", state: "CO",
zip_code: "80639"
)
create(:ride, from_address: from_address_2, to_address: to_address_2)
driver = create(:driver, current_address: from_address_2, max_radius: 5)
rides = Ride.selectable.nearby_driver(driver)

# Since the driver's current home address is in Greeley,
# The Timnath ride(from_address) is too far away as well as the Fort Collins ride (from_address_2)
expect(rides.length).to eq(0)
end
end
end
end
Expand Down

0 comments on commit b896f5e

Please sign in to comment.