Browse Source

Merge pull request #718 from Shopify/implement-the-rest-of-equality

Implement Session#hash and Session#eql?
Mike Ragalie 5 years ago
parent
commit
b6bfb7ecbf
2 changed files with 52 additions and 5 deletions
  1. 13 5
      lib/shopify_api/session.rb
  2. 39 0
      test/session_test.rb

+ 13 - 5
lib/shopify_api/session.rb

@@ -149,12 +149,20 @@ module ShopifyAPI
       expires_in <= 0
     end
 
+    def hash
+      state.hash
+    end
+
     def ==(other)
-      self.class == other.class &&
-        domain == other.domain &&
-        token == other.token &&
-        api_version == other.api_version &&
-        extra == other.extra
+      self.class == other.class && state == other.state
+    end
+
+    alias_method :eql?, :==
+
+    protected
+
+    def state
+      [domain, token, api_version, extra]
     end
 
     private

+ 39 - 0
test/session_test.rb

@@ -346,6 +346,22 @@ class SessionTest < Test::Unit::TestCase
     assert_equal('testshop.myshopify.com', session.url)
   end
 
+  test "#hash returns the same value for equal Sessions" do
+    session = ShopifyAPI::Session.new(
+      domain: "http://testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+    other_session = ShopifyAPI::Session.new(
+      domain: "http://testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+    assert_equal(session.hash, other_session.hash)
+  end
+
   test "equality verifies domain" do
     session = ShopifyAPI::Session.new(
       domain: "http://testshop.myshopify.com",
@@ -428,6 +444,29 @@ class SessionTest < Test::Unit::TestCase
     refute_equal(session, different_session)
   end
 
+  test "#eql? and #hash are implemented" do
+    session = ShopifyAPI::Session.new(
+      domain: "http://testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+    other_session = ShopifyAPI::Session.new(
+      domain: "http://testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+    different_session = ShopifyAPI::Session.new(
+      domain: "http://another_testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+
+    assert_equal([session, different_session], [session, other_session, different_session].uniq)
+  end
+
   private
 
   def make_sorted_params(params)