|
| 1 | +require File.expand_path('../../../test_helper', __FILE__) |
| 2 | + |
| 3 | +# PORO (Plain Old Ruby Object) for testing |
| 4 | +class PoroModel |
| 5 | + attr_accessor :id, :name, :created_at |
| 6 | + |
| 7 | + def initialize(id, name) |
| 8 | + @id = id |
| 9 | + @name = name |
| 10 | + @created_at = Time.now |
| 11 | + end |
| 12 | + |
| 13 | + # Note: No .all method - this is a PORO, not ActiveRecord |
| 14 | +end |
| 15 | + |
| 16 | +# Resource for PORO model |
| 17 | +class PoroModelResource < JSONAPI::Resource |
| 18 | + model_name 'PoroModel' |
| 19 | + attributes :name |
| 20 | + |
| 21 | + class << self |
| 22 | + def find_by_key(key, options = {}) |
| 23 | + context = options[:context] |
| 24 | + model = PoroModel.new(key, "PORO Item #{key}") |
| 25 | + new(model, context) |
| 26 | + end |
| 27 | + |
| 28 | + def create(context) |
| 29 | + model = PoroModel.new(SecureRandom.uuid, "New PORO") |
| 30 | + new(model, context) |
| 31 | + end |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +class PoroResourceTest < ActiveSupport::TestCase |
| 36 | + def test_poro_model_does_not_respond_to_all |
| 37 | + # Verify our test PORO doesn't have .all method |
| 38 | + refute PoroModel.respond_to?(:all), |
| 39 | + "PORO model should not respond to :all" |
| 40 | + end |
| 41 | + |
| 42 | + def test_find_fragments_works_with_poro |
| 43 | + # find_fragments should work with PORO by falling back to find_by_key |
| 44 | + filters = {} |
| 45 | + options = { |
| 46 | + context: {}, |
| 47 | + primary_keys: ['key1', 'key2'] |
| 48 | + } |
| 49 | + |
| 50 | + fragments = PoroModelResource.find_fragments(filters, options) |
| 51 | + |
| 52 | + assert_equal 2, fragments.length, "Should return 2 fragments" |
| 53 | + |
| 54 | + # Verify fragments have correct structure |
| 55 | + fragments.each do |identity, fragment| |
| 56 | + assert_kind_of JSONAPI::ResourceIdentity, identity |
| 57 | + assert_kind_of JSONAPI::ResourceFragment, fragment |
| 58 | + assert_not_nil fragment.resource, "Fragment should have resource for PORO" |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def test_find_fragments_returns_empty_for_poro_without_keys |
| 63 | + filters = {} |
| 64 | + options = { |
| 65 | + context: {}, |
| 66 | + primary_keys: nil |
| 67 | + } |
| 68 | + |
| 69 | + fragments = PoroModelResource.find_fragments(filters, options) |
| 70 | + |
| 71 | + assert_equal({}, fragments, "Should return empty hash when no keys") |
| 72 | + end |
| 73 | + |
| 74 | + def test_find_fragments_with_single_key_for_poro |
| 75 | + filters = {} |
| 76 | + options = { |
| 77 | + context: {}, |
| 78 | + primary_keys: 'single_key' |
| 79 | + } |
| 80 | + |
| 81 | + fragments = PoroModelResource.find_fragments(filters, options) |
| 82 | + |
| 83 | + assert_equal 1, fragments.length, "Should return 1 fragment" |
| 84 | + end |
| 85 | + |
| 86 | + def test_active_record_resource_still_uses_original_find_fragments |
| 87 | + # Ensure ActiveRecord resources still use the optimized pluck-based implementation |
| 88 | + # This test verifies we didn't break existing behavior |
| 89 | + filters = { PostResource._primary_key => [1, 2] } |
| 90 | + options = { context: {} } |
| 91 | + |
| 92 | + # PostResource uses ActiveRecord, should work normally |
| 93 | + fragments = PostResource.find_fragments(filters, options) |
| 94 | + |
| 95 | + assert_equal 2, fragments.length |
| 96 | + fragments.each do |identity, fragment| |
| 97 | + assert_kind_of JSONAPI::ResourceIdentity, identity |
| 98 | + assert_kind_of JSONAPI::ResourceFragment, fragment |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments