spec/data/geo_spec.rb in barometer-0.8.0 vs spec/data/geo_spec.rb in barometer-0.9.0
- old
+ new
@@ -1,91 +1,92 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require_relative '../spec_helper'
-describe "Data::Geo" do
-
- describe "when initialized" do
-
- before(:each) do
- @geo = Data::Geo.new
+module Barometer::Data
+ describe Geo do
+ describe '#coordinates' do
+ it 'joins latitude and longitude' do
+ geo = Geo.new(
+ longitude: '99.99',
+ latitude: '88.88'
+ )
+ expect( geo.coordinates ).to eq '88.88,99.99'
+ end
end
- it "responds to query" do
- @geo.query.should be_nil
- end
+ describe '#to_s' do
+ it 'defaults to an empty string' do
+ geo = Geo.new
+ expect( geo.to_s ).to be_empty
+ end
- it "responds to latitude" do
- @geo.latitude.should be_nil
- end
+ it 'returns only the address' do
+ geo = Geo.new(address: 'address')
+ expect( geo.to_s ).to eq 'address'
+ end
- it "responds to longitude" do
- @geo.longitude.should be_nil
- end
+ it 'returns address + locality' do
+ geo = Geo.new(
+ address: 'address',
+ locality: 'locality'
+ )
+ expect( geo.to_s ).to eq 'address, locality'
+ end
- it "responds to country_code" do
- @geo.country_code.should be_nil
+ it 'returns address + locality + code' do
+ geo = Geo.new(
+ address: 'address',
+ locality: 'locality',
+ country_code: 'code'
+ )
+ expect( geo.to_s ).to eq 'address, locality, code'
+ end
end
- it "responds to locality" do
- @geo.locality.should be_nil
- end
+ describe '#merge' do
+ it 'returns a new Geo object' do
+ target_geo = Geo.new
+ source_geo = Geo.new
- it "responds to region" do
- @geo.region.should be_nil
- end
+ geo = target_geo.merge(source_geo)
+ expect( geo.object_id ).not_to eq target_geo.object_id
+ expect( geo.object_id ).not_to eq source_geo.object_id
+ end
- it "responds to country" do
- @geo.country.should be_nil
- end
+ it 'uses original target values' do
+ target_geo = Geo.new(
+ locality: 'foo',
+ postal_code: '90210'
+ )
+ source_geo = Geo.new(postal_code: '10001')
- it "responds to address" do
- @geo.address.should be_nil
- end
+ geo = target_geo.merge(source_geo)
+ expect( geo.locality ).to eq 'foo'
+ expect( geo.postal_code ).to eq '90210'
+ end
- it "responds to coordinates" do
- @geo.longitude = "99.99"
- @geo.latitude = "88.88"
- @geo.coordinates.should == [@geo.latitude, @geo.longitude].join(',')
- end
+ it 'leaves blank target values' do
+ target_geo = Geo.new(postal_code: '')
+ source_geo = Geo.new(postal_code: '10001')
- it "should print a string" do
- @geo = Data::Geo.new
- @geo.to_s.should == ""
- @geo.address = "address"
- @geo.to_s.should == "address"
- @geo.locality = "locality"
- @geo.to_s.should == "address, locality"
- @geo.country_code = "code"
- @geo.to_s.should == "address, locality, code"
- end
+ geo = target_geo.merge(source_geo)
+ expect( geo.postal_code ).to eq ''
+ end
- it "requires Hash object" do
- lambda { Data::Geo.new(1) }.should raise_error(ArgumentError)
- lambda { Data::Geo.new(Hash.new) }.should_not raise_error(ArgumentError)
- end
+ it 'updates nil target values' do
+ target_geo = Geo.new(country: nil)
+ source_geo = Geo.new(country: 'Foo Bar')
- it "returns a Barometer::Geo object" do
- geo = Data::Geo.new(Hash.new)
- geo.is_a?(Data::Geo).should be_true
- end
+ geo = target_geo.merge(source_geo)
+ expect( geo.country ).to eq 'Foo Bar'
+ end
- end
+ it 'updates unset target values' do
+ target_geo = Geo.new
+ source_geo = Geo.new(latitude: 12.34, longitude: -56.78)
- describe "when converting" do
-
- before(:each) do
- @geo = Data::Geo.new
- end
-
- describe "from HTTParty" do
-
- it "accepts HTTParty::Response object" do
- location = Hash.new
- lambda { @geo.build_from_hash(1) }.should raise_error(ArgumentError)
- lambda { @geo.build_from_hash }.should_not raise_error(ArgumentError)
- lambda { @geo.build_from_hash(location) }.should_not raise_error(ArgumentError)
+ geo = target_geo.merge(source_geo)
+ expect( geo.latitude ).to eq 12.34
+ expect( geo.longitude ).to eq -56.78
end
-
end
-
end
-
end