|
| 1 | +require File.expand_path('../../../test_helper', __FILE__) |
| 2 | + |
| 3 | +module VX |
| 4 | +end |
| 5 | + |
| 6 | +class MultipleActiveRelationResourceTest < ActiveSupport::TestCase |
| 7 | + def setup |
| 8 | + end |
| 9 | + |
| 10 | + def teardown |
| 11 | + teardown_test_constant(::VX, :BaseResource) |
| 12 | + teardown_test_constant(::VX, :DuplicateSubBaseResource) |
| 13 | + teardown_test_constant(::VX, :InvalidSubBaseResource) |
| 14 | + teardown_test_constant(::VX, :ValidCustomBaseResource) |
| 15 | + end |
| 16 | + |
| 17 | + def teardown_test_constant(namespace, constant_name) |
| 18 | + return unless namespace.const_defined?(constant_name) |
| 19 | + namespace.send(:remove_const, constant_name) |
| 20 | + rescue NameError |
| 21 | + end |
| 22 | + |
| 23 | + def test_correct_resource_retrieval_strategy |
| 24 | + expected = 'JSONAPI::ActiveRelationRetrieval' |
| 25 | + default = JSONAPI.configuration.default_resource_retrieval_strategy |
| 26 | + assert_equal expected, default |
| 27 | + assert_nil JSONAPI::Resource._resource_retrieval_strategy_loaded |
| 28 | + |
| 29 | + expected = 'JSONAPI::ActiveRelationRetrieval' |
| 30 | + assert_silent do |
| 31 | + ::VX.module_eval <<~MODULE |
| 32 | + class BaseResource < JSONAPI::Resource |
| 33 | + abstract |
| 34 | + end |
| 35 | + MODULE |
| 36 | + end |
| 37 | + assert_equal expected, VX::BaseResource._resource_retrieval_strategy_loaded |
| 38 | + |
| 39 | + strategy = 'JSONAPI::ActiveRelationRetrieval' |
| 40 | + expected = 'JSONAPI::ActiveRelationRetrieval' |
| 41 | + assert_output nil, "Resource retrieval strategy #{expected} already loaded for VX::DuplicateSubBaseResource\n" do |
| 42 | + ::VX.module_eval <<~MODULE |
| 43 | + class DuplicateSubBaseResource < JSONAPI::Resource |
| 44 | + resource_retrieval_strategy '#{strategy}' |
| 45 | + abstract |
| 46 | + end |
| 47 | + MODULE |
| 48 | + end |
| 49 | + assert_equal expected, VX::DuplicateSubBaseResource._resource_retrieval_strategy_loaded |
| 50 | + |
| 51 | + strategy = 'JSONAPI::ActiveRelationRetrievalV10' |
| 52 | + expected = "Resource retrieval strategy #{default} already loaded for VX::InvalidSubBaseResource. Cannot load #{strategy}" |
| 53 | + ex = assert_raises ArgumentError do |
| 54 | + ::VX.module_eval <<~MODULE |
| 55 | + class InvalidSubBaseResource < JSONAPI::Resource |
| 56 | + resource_retrieval_strategy '#{strategy}' |
| 57 | + abstract |
| 58 | + end |
| 59 | + MODULE |
| 60 | + end |
| 61 | + assert_equal expected, ex.message |
| 62 | + |
| 63 | + strategy = 'JSONAPI::ActiveRelationRetrievalV10' |
| 64 | + expected = 'JSONAPI::ActiveRelationRetrievalV10' |
| 65 | + assert_silent do |
| 66 | + ::VX.module_eval <<~MODULE |
| 67 | + class ValidCustomBaseResource |
| 68 | + include JSONAPI::ResourceCommon |
| 69 | + root_resource |
| 70 | + abstract |
| 71 | + immutable |
| 72 | + resource_retrieval_strategy '#{strategy}' |
| 73 | + end |
| 74 | + MODULE |
| 75 | + end |
| 76 | + assert_equal expected, VX::ValidCustomBaseResource._resource_retrieval_strategy_loaded |
| 77 | + end |
| 78 | +end |
0 commit comments