api_version_test.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class ApiVersionTest < Test::Unit::TestCase
  4. test "find_version returns any version object given" do
  5. version = ShopifyAPI::ApiVersion.new(handle: :unstable)
  6. assert_same(version, ShopifyAPI::ApiVersion.find_version(version))
  7. end
  8. test "find_version converts a known version into a version object" do
  9. versions = [
  10. ShopifyAPI::ApiVersion.new(handle: :unstable),
  11. ShopifyAPI::ApiVersion.new(handle: '2019-01'),
  12. ]
  13. assert_equal(versions, [
  14. ShopifyAPI::ApiVersion.find_version('unstable'),
  15. ShopifyAPI::ApiVersion.find_version('2019-01'),
  16. ])
  17. end
  18. test "find_version removes unverified versions from version set if mode is set to :raise_on_unknown" do
  19. ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
  20. assert ShopifyAPI::ApiVersion.versions.values.all?(&:verified?)
  21. assert_equal 6, ShopifyAPI::ApiVersion.versions.size
  22. ShopifyAPI::ApiVersion.find_version('2019-30')
  23. refute ShopifyAPI::ApiVersion.versions.values.all?(&:verified?)
  24. assert_equal 7, ShopifyAPI::ApiVersion.versions.size
  25. ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
  26. assert ShopifyAPI::ApiVersion.versions.values.all?(&:verified?)
  27. assert_equal 6, ShopifyAPI::ApiVersion.versions.size
  28. end
  29. test "find_version does not raise when coercing a string if no versions are defined when version_lookup_mode is :define_on_unknown" do
  30. ShopifyAPI::ApiVersion.clear_known_versions
  31. ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
  32. assert_equal :define_on_unknown, ShopifyAPI::ApiVersion.version_lookup_mode
  33. assert_nothing_raised do
  34. ShopifyAPI::ApiVersion.find_version('made up version')
  35. end
  36. end
  37. test "find_version does raise when coercing a string if no versions are defined when version_lookup_mode is :raise_on_unknown" do
  38. refute ShopifyAPI::ApiVersion.versions['made up version']
  39. ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
  40. assert_raises(ShopifyAPI::ApiVersion::UnknownVersion) do
  41. ShopifyAPI::ApiVersion.find_version('made up version')
  42. end
  43. end
  44. test "find_version raises ArgumentError when given an ShopifyAPI::ApiVersion::NullVersion object" do
  45. ShopifyAPI::ApiVersion.clear_known_versions
  46. ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
  47. assert_equal :define_on_unknown, ShopifyAPI::ApiVersion.version_lookup_mode
  48. assert_raises(ArgumentError) do
  49. ShopifyAPI::ApiVersion.find_version(ShopifyAPI::ApiVersion::NullVersion)
  50. end
  51. end
  52. test 'two versions with the same version number are equal' do
  53. version_1 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
  54. version_2 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
  55. assert_equal version_2, version_1
  56. end
  57. test 'two versions with the different version numbers are not equal' do
  58. version_1 = ShopifyAPI::ApiVersion.new(handle: '2019-07')
  59. version_2 = ShopifyAPI::ApiVersion.new(handle: '2019-11')
  60. refute_equal version_2, version_1
  61. end
  62. test 'versions are ordered by version number with unstable always being the newest' do
  63. version_1 = ShopifyAPI::ApiVersion.new(handle: '2017-11')
  64. version_2 = ShopifyAPI::ApiVersion.new(handle: '2019-11')
  65. version_3 = ShopifyAPI::ApiVersion.new(handle: '2039-01')
  66. version_4 = ShopifyAPI::ApiVersion.new(handle: '2039-02')
  67. unstable = ShopifyAPI::ApiVersion.new(handle: :unstable)
  68. assert_equal([
  69. version_1,
  70. version_2,
  71. version_3,
  72. version_4,
  73. unstable,
  74. ], [
  75. version_3,
  76. version_1,
  77. version_4,
  78. unstable,
  79. version_2,
  80. ].sort)
  81. end
  82. test 'latest_stable_version will return the version that is newest and stable' do
  83. assert_equal(
  84. ShopifyAPI::ApiVersion.versions,
  85. {
  86. "2019-01" => ShopifyAPI::ApiVersion.new(handle: '2019-01', supported: true, latest_supported: false),
  87. "2019-04" => ShopifyAPI::ApiVersion.new(handle: '2019-04', supported: true, latest_supported: false),
  88. "2019-07" => ShopifyAPI::ApiVersion.new(handle: '2019-07', supported: true, latest_supported: false),
  89. "2019-10" => ShopifyAPI::ApiVersion.new(handle: '2019-10', supported: false, latest_supported: false),
  90. "2020-01" => ShopifyAPI::ApiVersion.new(handle: '2020-01', supported: false, latest_supported: true),
  91. "unstable" => ShopifyAPI::ApiVersion.new(handle: 'unstable', supported: false, latest_supported: false),
  92. }
  93. )
  94. silence_warnings do
  95. refute_equal(
  96. ShopifyAPI::ApiVersion.new(handle: '2019-01'),
  97. ShopifyAPI::ApiVersion.latest_stable_version
  98. )
  99. assert_equal(
  100. ShopifyAPI::ApiVersion.new(handle: '2020-01'),
  101. ShopifyAPI::ApiVersion.latest_stable_version
  102. )
  103. end
  104. end
  105. test "NullVersion raises ApiVersionNotSetError" do
  106. assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
  107. ShopifyAPI::ApiVersion::NullVersion.construct_api_path(:string)
  108. end
  109. assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
  110. ShopifyAPI::ApiVersion::NullVersion.construct_graphql_path
  111. end
  112. assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
  113. ShopifyAPI::ApiVersion::NullVersion.stable?
  114. end
  115. end
  116. test "NullVersion cannot be instantiated and raises NoMethodError if attempted" do
  117. assert_raises(NoMethodError) do
  118. ShopifyAPI::ApiVersion::NullVersion.new
  119. end
  120. end
  121. test "handle_to_date converts a version handle to a date" do
  122. version_1 = ShopifyAPI::ApiVersion.new(handle: '2019-01')
  123. version_2 = ShopifyAPI::ApiVersion.new(handle: 'unstable')
  124. assert_equal(version_1.handle_as_date, Time.utc(2019, 01, 01))
  125. assert_equal(version_2.handle_as_date, ShopifyAPI::ApiVersion::UNSTABLE_AS_DATE)
  126. end
  127. class TestApiVersion < ShopifyAPI::ApiVersion
  128. def initialize(name)
  129. super(name)
  130. @version_name = name
  131. end
  132. end
  133. end