Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array#uniq to correctly identify == GlobalIDs #108

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/global_id/global_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def model_class
def ==(other)
other.is_a?(GlobalID) && @uri == other.uri
end
alias_method :eql?, :==

def hash
self.class.hash | @uri.hash
end

def to_param
# remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
Expand Down
24 changes: 24 additions & 0 deletions test/cases/global_id_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ class GlobalIDCreationTest < ActiveSupport::TestCase
person_gid = GlobalID.create(Person.new(5), app: nil)
end
end

test 'equality' do
p1 = Person.new(5)
p2 = Person.new(5)
p3 = Person.new(10)
assert_equal p1, p2
assert_not_equal p2, p3

gid1 = GlobalID.create(p1)
gid2 = GlobalID.create(p2)
gid3 = GlobalID.create(p3)
assert_equal gid1, gid2
assert_not_equal gid2, gid3

# hash and eql? to match for two GlobalID's pointing to the same object
assert_equal [gid1], [gid1, gid2].uniq
assert_equal [gid1, gid3], [gid1, gid2, gid3].uniq

# verify that the GlobalID's hash is different to the underlaying URI
assert_not_equal gid1.hash, gid1.uri.hash

# verify that URI and GlobalID do not pass the uniq test
assert_equal [gid1, gid1.uri], [gid1, gid1.uri].uniq
end
end

class GlobalIDCustomParamsTest < ActiveSupport::TestCase
Expand Down