Browse Source

Implement Session#==

Mike Ragalie 5 years ago
parent
commit
0ab26e4c36
2 changed files with 90 additions and 0 deletions
  1. 8 0
      lib/shopify_api/session.rb
  2. 82 0
      test/session_test.rb

+ 8 - 0
lib/shopify_api/session.rb

@@ -149,6 +149,14 @@ module ShopifyAPI
       expires_in <= 0
     end
 
+    def ==(other)
+      self.class == other.class &&
+        domain == other.domain &&
+        token == other.token &&
+        api_version == other.api_version &&
+        extra == other.extra
+    end
+
     private
 
     def parameterize(params)

+ 82 - 0
test/session_test.rb

@@ -346,6 +346,88 @@ class SessionTest < Test::Unit::TestCase
     assert_equal('testshop.myshopify.com', session.url)
   end
 
+  test "equality verifies domain" 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, other_session)
+    refute_equal(session, different_session)
+  end
+
+  test "equality verifies token" do
+    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://testshop.myshopify.com",
+      token: "very-different-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+    refute_equal(session, different_session)
+  end
+
+  test "equality verifies api_version" do
+    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://testshop.myshopify.com",
+      token: "any-token",
+      api_version: :unstable,
+      extra: { foo: "bar" }
+    )
+    refute_equal(session, different_session)
+  end
+
+  test "equality verifies extra" do
+    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://testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { bar: "other-bar" }
+    )
+    refute_equal(session, different_session)
+  end
+
+  test "equality verifies other is a Session" do
+    session = ShopifyAPI::Session.new(
+      domain: "http://testshop.myshopify.com",
+      token: "any-token",
+      api_version: '2019-01',
+      extra: { foo: "bar" }
+    )
+    different_session = nil
+    refute_equal(session, different_session)
+  end
+
   private
 
   def make_sorted_params(params)