api_version_test.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. class TestApiVersion < ShopifyAPI::ApiVersion
  66. def initialize(name)
  67. @version_name = name
  68. end
  69. end
  70. end