|
| 1 | +require File.expand_path('../../../test_helper', __FILE__) |
| 2 | + |
| 3 | +# Tests for backward compatibility with 0.9.x API |
| 4 | +class BackwardCompatibilityTest < ActiveSupport::TestCase |
| 5 | + # Test find_by_key with old signature: find_by_key(id, context) |
| 6 | + # New signature: find_by_key(key, options = {}) |
| 7 | + def test_find_by_key_with_old_signature |
| 8 | + # 0.9.x style: find_by_key(id, context) |
| 9 | + # In 0.9.x, context was passed directly as second argument |
| 10 | + # In 0.10+, context should be passed as options[:context] |
| 11 | + # For backward compatibility, if second arg is a Hash without :context key, |
| 12 | + # it should still work (context will be nil in resource) |
| 13 | + context = { current_user: 'test_user' } |
| 14 | + resource = PostResource.find_by_key(1, context) |
| 15 | + |
| 16 | + assert_not_nil resource |
| 17 | + assert_equal 1, resource.id |
| 18 | + end |
| 19 | + |
| 20 | + def test_find_by_key_with_new_signature |
| 21 | + # 0.10+ style: find_by_key(key, options = {}) |
| 22 | + context = { current_user: 'test_user' } |
| 23 | + resource = PostResource.find_by_key(1, context: context) |
| 24 | + |
| 25 | + assert_not_nil resource |
| 26 | + assert_equal 1, resource.id |
| 27 | + end |
| 28 | + |
| 29 | + def test_find_by_key_without_context |
| 30 | + # find_by_key without any context |
| 31 | + resource = PostResource.find_by_key(1) |
| 32 | + |
| 33 | + assert_not_nil resource |
| 34 | + assert_equal 1, resource.id |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +# Tests for ResourceSetOperationResult backward compatibility |
| 39 | +class ResourceSetOperationResultBackwardCompatibilityTest < ActiveSupport::TestCase |
| 40 | + def setup |
| 41 | + # Create a helper to build ResourceSet with resources |
| 42 | + end |
| 43 | + |
| 44 | + def test_resource_method_returns_first_resource |
| 45 | + # Create a ResourceSet with a single resource using the proper API |
| 46 | + post = Post.find(1) |
| 47 | + resource = PostResource.new(post, nil) |
| 48 | + |
| 49 | + # Use the ResourceSet with a resource (not nil) |
| 50 | + resource_set = JSONAPI::ResourceSet.new(resource) |
| 51 | + resource_set.mark_populated! |
| 52 | + |
| 53 | + result = JSONAPI::ResourceSetOperationResult.new(:ok, resource_set) |
| 54 | + |
| 55 | + # 0.9.x style: result.resource |
| 56 | + assert result.respond_to?(:resource), "ResourceSetOperationResult should respond to :resource for 0.9.x compatibility" |
| 57 | + assert_equal resource, result.resource |
| 58 | + end |
| 59 | + |
| 60 | + def test_resource_method_returns_nil_for_empty_resource_set |
| 61 | + # Create an empty resource set by passing an empty array |
| 62 | + resource_set = JSONAPI::ResourceSet.new([]) |
| 63 | + result = JSONAPI::ResourceSetOperationResult.new(:ok, resource_set) |
| 64 | + |
| 65 | + assert_nil result.resource |
| 66 | + end |
| 67 | + |
| 68 | + def test_resources_method_returns_all_resources |
| 69 | + # Create a ResourceSet with multiple resources |
| 70 | + post1 = Post.find(1) |
| 71 | + post2 = Post.find(2) |
| 72 | + resource1 = PostResource.new(post1, nil) |
| 73 | + resource2 = PostResource.new(post2, nil) |
| 74 | + |
| 75 | + # Use the ResourceSet with an array of resources |
| 76 | + resource_set = JSONAPI::ResourceSet.new([resource1, resource2]) |
| 77 | + resource_set.mark_populated! |
| 78 | + |
| 79 | + result = JSONAPI::ResourceSetOperationResult.new(:ok, resource_set) |
| 80 | + |
| 81 | + # 0.9.x style: result.resources (for collections) |
| 82 | + assert result.respond_to?(:resources), "ResourceSetOperationResult should respond to :resources for 0.9.x compatibility" |
| 83 | + assert_equal 2, result.resources.length |
| 84 | + assert_includes result.resources, resource1 |
| 85 | + assert_includes result.resources, resource2 |
| 86 | + end |
| 87 | +end |
0 commit comments