api_version_test.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class ApiVersionTest < Test::Unit::TestCase
  4. def teardown
  5. super
  6. ShopifyAPI::ApiVersion.clear_defined_versions
  7. ShopifyAPI::ApiVersion.define_known_versions
  8. end
  9. test "no version creates url that start with /admin/" do
  10. assert_equal(
  11. "/admin/resource_path/id.json",
  12. ShopifyAPI::ApiVersion::NoVersion.new.construct_api_path("resource_path/id.json")
  13. )
  14. end
  15. test "no version creates graphql url that start with /admin/api" do
  16. assert_equal(
  17. "/admin/api/graphql.json",
  18. ShopifyAPI::ApiVersion::NoVersion.new.construct_graphql_path
  19. )
  20. end
  21. test "unstable version creates url that start with /admin/api/unstable/" do
  22. assert_equal(
  23. "/admin/api/unstable/resource_path/id.json",
  24. ShopifyAPI::ApiVersion::Unstable.new.construct_api_path("resource_path/id.json")
  25. )
  26. end
  27. test "unstable version creates graphql url that start with /admin/api/unstable/" do
  28. assert_equal(
  29. "/admin/api/unstable/graphql.json",
  30. ShopifyAPI::ApiVersion::Unstable.new.construct_graphql_path
  31. )
  32. end
  33. test "coerce_to_version returns any version object given" do
  34. version = ShopifyAPI::ApiVersion::Unstable.new
  35. assert_same(version, ShopifyAPI::ApiVersion.coerce_to_version(version))
  36. end
  37. test "coerce_to_version converts a known version into a version object" do
  38. versions = [
  39. ShopifyAPI::ApiVersion::Unstable.new,
  40. ShopifyAPI::ApiVersion::NoVersion.new,
  41. ]
  42. assert_equal(versions, [
  43. ShopifyAPI::ApiVersion.coerce_to_version('unstable'),
  44. ShopifyAPI::ApiVersion.coerce_to_version(:no_version),
  45. ])
  46. end
  47. test "coerce_to_version raises when coercing a string that doesn't match a known version" do
  48. assert_raises ShopifyAPI::ApiVersion::UnknownVersion do
  49. ShopifyAPI::ApiVersion.coerce_to_version('made up version')
  50. end
  51. end
  52. test "additional defined versions will also be coerced" do
  53. versions = [
  54. TestApiVersion.new('my_name'),
  55. TestApiVersion.new('other_name'),
  56. ]
  57. versions.each do |version|
  58. ShopifyAPI::ApiVersion.define_version(version)
  59. end
  60. assert_equal(versions, [
  61. ShopifyAPI::ApiVersion.coerce_to_version('my_name'),
  62. ShopifyAPI::ApiVersion.coerce_to_version('other_name'),
  63. ])
  64. end
  65. test 'allows a release version with the correct format format to be created' do
  66. assert ShopifyAPI::ApiVersion::Release.new('2019-03')
  67. end
  68. test 'release versions must follow the format' do
  69. assert_raises ShopifyAPI::ApiVersion::InvalidVersion do
  70. assert ShopifyAPI::ApiVersion::Release.new('crazy-name')
  71. end
  72. end
  73. test 'release versions create a url that is /admin/api/<version_name>/' do
  74. assert_equal(
  75. '/admin/api/2022-03/shop.json',
  76. ShopifyAPI::ApiVersion::Release.new('2022-03').construct_api_path('shop.json')
  77. )
  78. end
  79. test 'two versions with the same version number are equal' do
  80. version_1 = ShopifyAPI::ApiVersion::Release.new('2018-09')
  81. version_2 = ShopifyAPI::ApiVersion::Release.new('2018-09')
  82. assert_equal version_2, version_1
  83. end
  84. test 'two versions with the different version numbers are not equal' do
  85. version_1 = ShopifyAPI::ApiVersion::Release.new('2019-07')
  86. version_2 = ShopifyAPI::ApiVersion::Release.new('2019-11')
  87. refute_equal version_2, version_1
  88. end
  89. class TestApiVersion < ShopifyAPI::ApiVersion
  90. def initialize(name)
  91. @version_name = name
  92. end
  93. end
  94. end