Browse Source

Ensure we handle empty addresses correctly

tags/0.3.0^2
Bèr Kessels 1 year ago
parent
commit
4f0f8e8721

+ 3
- 1
app/models/place.rb View File

@@ -43,11 +43,13 @@ module Hours
end

def address
return unless raw_address

OpenStruct.new(Sequel::Postgres::HStore.parse(raw_address).to_hash)
end

def region
address.city
address&.city
end

def raw_address

+ 1
- 1
app/projections/query.rb View File

@@ -86,7 +86,7 @@ module Hours
first_place = dataset.select(:address).first(region_slug: @region_slug)
return unless first_place

@region_name = Hours::Models::Place.new(first_place).address[:city]
@region_name = Hours::Models::Place.new(first_place).region
end
end
end

+ 9
- 9
test/fixtures/input/addressless.json View File

@@ -1,11 +1,11 @@
{
"id": 42,
"name": "Sint-Nicolaaskapel",
"addr_housenumber": null,
"addr_postcode": null,
"addr_street": null,
"addr_city": null,
"addr_country_code": null,
"opening_hours": "24/7; PH closed",
"geometry": { "type": "Point", "coordinates": [51.8473397, 5.8653234] }
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.8653234, 51.8473397]
},
"properties": {
"name": "Sint-Nicolaaskapel",
"opening_hours": "24/7; PH closed"
}
}

+ 11
- 3
test/models/place_test.rb View File

@@ -28,18 +28,26 @@ describe Hours::Models::Place do
it 'has a city' do
assert_equal 'Nijmegen', subject.address.city
end

it 'handles nil addresses' do
subject.address = nil
assert_nil subject.address&.city
end
end

describe 'region' do
describe 'with city' do
before do
subject.address = hstore_address
end
before { subject.address = hstore_address }

it 'uses the city' do
assert_equal 'Nijmegen', subject.region
end
end

it 'handles nil addresses' do
subject.address = nil
assert_nil subject.region
end
end

describe 'location' do

+ 4
- 3
test/projections/query_test.rb View File

@@ -102,13 +102,14 @@ describe Hours::Projections::RegionQuery do
id: SecureRandom.uuid,
place_id: SecureRandom.hex,
region_slug: 'nijmegen',
location: location
location: location,
address: Sequel.hstore(city: 'Nijmegen')
)
end
result = subject.handle('nijmegen')
assert_equal result.centerpoint, center
places = result.to_a
assert_equal center, result.centerpoint

places = result.to_a
distance0 = center.euclidian_distance(places[0].location)
distance1 = center.euclidian_distance(places[1].location)
assert distance1 > distance0

Loading…
Cancel
Save