Skip to content

Commit

Permalink
ApiVersion.find_version raises if passed NullVersion as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgrenz committed Sep 3, 2019
1 parent 8905f90 commit 7270b6d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/shopify_api/api_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def version_lookup_mode=(mode)
end

def find_version(version_or_handle)
raise ArgumentError, "NullVersion is not a valid version or version handle." if version_or_handle == NullVersion
return version_or_handle if version_or_handle.is_a?(ApiVersion)
handle = version_or_handle.to_s
@versions ||= {}
Expand Down Expand Up @@ -174,6 +175,10 @@ def handle_as_date

class NullVersion
class << self
def new(*_args)
raise NoMethodError, "NullVersion is an abstract class and cannot be instantiated."
end

def raise_not_set_error(*_args)
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
end
Expand Down
15 changes: 15 additions & 0 deletions test/api_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class ApiVersionTest < Test::Unit::TestCase
end
end

test "find_version raises ArgumentError when given an ShopifyAPI::ApiVersion::NullVersion object" do
ShopifyAPI::ApiVersion.clear_known_versions
ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
assert_equal :define_on_unknown, ShopifyAPI::ApiVersion.version_lookup_mode
assert_raises ArgumentError do
ShopifyAPI::ApiVersion.find_version(ShopifyAPI::ApiVersion::NullVersion)
end
end

test 'two versions with the same version number are equal' do
version_1 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
version_2 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
Expand Down Expand Up @@ -125,6 +134,12 @@ class ApiVersionTest < Test::Unit::TestCase
end
end

test "NullVersion cannot be instantiated and raises NoMethodError if attempted" do
assert_raises(NoMethodError) do
ShopifyAPI::ApiVersion::NullVersion.new
end
end

test "handle_to_date converts a version handle to a date" do
version_1 = ShopifyAPI::ApiVersion.new(handle: '2019-01')
version_2 = ShopifyAPI::ApiVersion.new(handle: 'unstable')
Expand Down

0 comments on commit 7270b6d

Please sign in to comment.