Skip to content
This repository was archived by the owner on Jun 13, 2018. It is now read-only.

Commit

Permalink
Add equality operator to ShipmentEvent and TrackingResponse (#372)
Browse files Browse the repository at this point in the history
added equality operator to ShipmentEvent and TrackingResponse
  • Loading branch information
richardwu committed May 18, 2016
1 parent fe978d6 commit 26a0d91
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/active_shipping/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,9 @@ def zip_plus_4
def address2_and_3
[address2, address3].reject(&:blank?).join(", ")
end

def ==(other)
to_hash == other.to_hash
end
end
end
5 changes: 5 additions & 0 deletions lib/active_shipping/shipment_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ def delivered?
def status
@status ||= name.downcase.gsub("\s", "_").to_sym
end

def ==(other)
attributes = %i(name time location message type_code)
attributes.all? { |attr| self.public_send(attr) == other.public_send(attr) }
end
end
end
17 changes: 16 additions & 1 deletion lib/active_shipping/tracking_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module ActiveShipping
#
# @!attribute attempted_delivery_date
# @return [Date, Time]
#
#
# @!attribute delivery_signature
# @return [String]
#
Expand Down Expand Up @@ -101,5 +101,20 @@ def has_exception?
alias_method :scheduled_delivery_time, :scheduled_delivery_date
alias_method :actual_delivery_time, :actual_delivery_date
alias_method :attempted_delivery_time, :attempted_delivery_date

def ==(other)
attributes = %i(carrier carrier_name status status_code status_description ship_time scheduled_delivery_date
actual_delivery_date attempted_delivery_date delivery_signature tracking_number shipper_address
origin destination
)

attributes.all? { |attr| self.public_send(attr) == other.public_send(attr) } && compare_shipment_events(other)
end

private
# Ensure order doesn't matter when comparing shipment_events
def compare_shipment_events(other)
shipment_events.sort_by(&:time) == other.shipment_events.sort_by(&:time)
end
end
end
7 changes: 7 additions & 0 deletions test/unit/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,11 @@ def test_address2_and_3
location = Location.from(:address3 => address3)
assert_equal 'Victory Lane', location.address2_and_3
end

def test_equality
location_1 = location_fixtures[:ottawa]
location_2 = Location.from(location_1.to_hash)

assert_equal location_1, location_2
end
end
20 changes: 20 additions & 0 deletions test/unit/shipment_event_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'test_helper'

class ShipmentEventTest < Minitest::Test
def test_equality
options1 = [
'ARRIVED AT UNIT',
DateTime.new(2016, 5, 12, 5, 45),
Location.new(city: 'SAN JOSE', state: 'CA', postal_code: '90001', country: 'US'),
'ARRIVED AT UNIT',
'07'
]
# Copies options to create new DateTime and Location objects to check for similar distinct objects
options2 = options1.dup

shipment_event_1 = ShipmentEvent.new(*options1)
shipment_event_2 = ShipmentEvent.new(*options2)

assert_equal shipment_event_1, shipment_event_2
end
end
41 changes: 41 additions & 0 deletions test/unit/tracking_response_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'test_helper'

class TrackingResponseTest < Minitest::Test
def test_equality
options1 = {
carrier: 'usps',
status: 'DELIVERED',
status_code: 'I0',
status_description: 'DELIVERED',
actual_delivery_date: DateTime.new(2016, 5, 14, 13, 20),
tracking_number: 'TRACKINGNUMBER1234ABC',
shipment_events: [
ShipmentEvent.new(
'DELIVERED',
DateTime.new(2016, 5, 14, 13, 20),
Location.new(city: 'LOS ANGELES', state: 'CA', postal_code: '90210', country: 'US'),
'DELIVERED',
'I0'
),
ShipmentEvent.new(
'ARRIVED AT UNIT',
DateTime.new(2016, 5, 12, 5, 45),
Location.new(city: 'SAN JOSE', state: 'CA', postal_code: '90001', country: 'US'),
'ARRIVED AT UNIT',
'07'
)
],
destination: Location.new(postal_code: '90210'),
origin: Location.new(postal_code: '00001')
}
# Deep copies options1 to create new ShipmentEvent, Location, etc. objects to check for similar distinct objects
options2 = Marshal.load(Marshal.dump(options1))
options2[:shipment_events][0], options2[:shipment_events][1] =
options2[:shipment_events][1], options2[:shipment_events][0]

tracking_response_1 = TrackingResponse.new(true, nil, {}, options1)
tracking_response_2 = TrackingResponse.new(true, nil, {}, options2)

assert_equal tracking_response_1, tracking_response_2
end
end

0 comments on commit 26a0d91

Please sign in to comment.