|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../test_helper' |
| 4 | + |
| 5 | +class AssociationSourceTest < Minitest::Test |
| 6 | + class User |
| 7 | + attr_accessor :id, :name, :profile, :articles, :metadata |
| 8 | + |
| 9 | + def initialize(id, name) |
| 10 | + @id = id |
| 11 | + @name = name |
| 12 | + @articles = [] |
| 13 | + @metadata = {} |
| 14 | + end |
| 15 | + |
| 16 | + def custom_profile_data |
| 17 | + {email: "#{name.downcase}@example.com", bio: "Bio for #{name}"} |
| 18 | + end |
| 19 | + |
| 20 | + def filtered_articles(status = nil) |
| 21 | + return @articles unless status |
| 22 | + |
| 23 | + @articles.select { |article| article.status == status } |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + class Profile |
| 28 | + attr_accessor :id, :email, :bio |
| 29 | + |
| 30 | + def initialize(id, email, bio) |
| 31 | + @id = id |
| 32 | + @email = email |
| 33 | + @bio = bio |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + class Article |
| 38 | + attr_accessor :id, :title, :status |
| 39 | + |
| 40 | + def initialize(id, title, status = 'published') |
| 41 | + @id = id |
| 42 | + @title = title |
| 43 | + @status = status |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + class ProfileResource |
| 48 | + include Alba::Resource |
| 49 | + |
| 50 | + attributes :email, :bio |
| 51 | + end |
| 52 | + |
| 53 | + class ArticleResource |
| 54 | + include Alba::Resource |
| 55 | + |
| 56 | + attributes :id, :title, :status |
| 57 | + end |
| 58 | + |
| 59 | + def setup |
| 60 | + @user = User.new(1, 'John') |
| 61 | + @user.profile = Profile.new(1, '[email protected]', 'Software developer') |
| 62 | + @user.articles << Article.new(1, 'First Post', 'published') |
| 63 | + @user.articles << Article.new(2, 'Draft Post', 'draft') |
| 64 | + @user.articles << Article.new(3, 'Another Post', 'published') |
| 65 | + @user.metadata = {role: 'admin', department: 'engineering'} |
| 66 | + end |
| 67 | + |
| 68 | + # Test basic source functionality with one association |
| 69 | + class UserResourceWithSourceOne |
| 70 | + include Alba::Resource |
| 71 | + |
| 72 | + attributes :id, :name |
| 73 | + |
| 74 | + one :custom_profile, |
| 75 | + source: proc { custom_profile_data }, |
| 76 | + resource: ProfileResource |
| 77 | + end |
| 78 | + |
| 79 | + def test_one_association_with_basic_source |
| 80 | + expected = '{"id":1,"name":"John","custom_profile":{"email":"[email protected]","bio":"Bio for John"}}' |
| 81 | + assert_equal expected, UserResourceWithSourceOne.new(@user).serialize |
| 82 | + end |
| 83 | + |
| 84 | + # Test source with params access |
| 85 | + class UserResourceWithSourceAndParams |
| 86 | + include Alba::Resource |
| 87 | + |
| 88 | + attributes :id, :name |
| 89 | + |
| 90 | + many :filtered_articles, |
| 91 | + source: proc { |params| filtered_articles(params[:status]) }, |
| 92 | + resource: ArticleResource |
| 93 | + end |
| 94 | + |
| 95 | + def test_many_association_with_source_using_params |
| 96 | + expected = '{"id":1,"name":"John","filtered_articles":[{"id":1,"title":"First Post","status":"published"},{"id":3,"title":"Another Post","status":"published"}]}' # rubocop: disable Layout/LineLength |
| 97 | + result = UserResourceWithSourceAndParams.new(@user, params: {status: 'published'}).serialize |
| 98 | + assert_equal expected, result |
| 99 | + end |
| 100 | + |
| 101 | + def test_many_association_with_source_using_params_returns_all_when_no_status |
| 102 | + expected = '{"id":1,"name":"John","filtered_articles":[{"id":1,"title":"First Post","status":"published"},{"id":2,"title":"Draft Post","status":"draft"},{"id":3,"title":"Another Post","status":"published"}]}' # rubocop: disable Layout/LineLength |
| 103 | + result = UserResourceWithSourceAndParams.new(@user, params: {}).serialize |
| 104 | + assert_equal expected, result |
| 105 | + end |
| 106 | + |
| 107 | + # Test source with custom key |
| 108 | + class UserResourceWithSourceAndKey |
| 109 | + include Alba::Resource |
| 110 | + |
| 111 | + attributes :id, :name |
| 112 | + |
| 113 | + one :profile_info, |
| 114 | + source: proc { custom_profile_data }, |
| 115 | + key: :user_profile, |
| 116 | + resource: ProfileResource |
| 117 | + end |
| 118 | + |
| 119 | + def test_association_with_source_and_custom_key |
| 120 | + expected = '{"id":1,"name":"John","user_profile":{"email":"[email protected]","bio":"Bio for John"}}' |
| 121 | + assert_equal expected, UserResourceWithSourceAndKey.new(@user).serialize |
| 122 | + end |
| 123 | + |
| 124 | + # Test source with condition |
| 125 | + class UserResourceWithSourceAndCondition |
| 126 | + include Alba::Resource |
| 127 | + |
| 128 | + attributes :id, :name |
| 129 | + |
| 130 | + many :articles, |
| 131 | + proc { |articles, _params| articles.select { |a| a.status == 'published' } }, |
| 132 | + source: proc { @articles }, |
| 133 | + resource: ArticleResource |
| 134 | + end |
| 135 | + |
| 136 | + def test_association_with_source_and_condition |
| 137 | + expected = '{"id":1,"name":"John","articles":[{"id":1,"title":"First Post","status":"published"},{"id":3,"title":"Another Post","status":"published"}]}' |
| 138 | + assert_equal expected, UserResourceWithSourceAndCondition.new(@user).serialize |
| 139 | + end |
| 140 | + |
| 141 | + # Test source returning nil |
| 142 | + class UserResourceWithNilSource |
| 143 | + include Alba::Resource |
| 144 | + |
| 145 | + attributes :id, :name |
| 146 | + |
| 147 | + one :missing_profile, |
| 148 | + source: proc {}, |
| 149 | + resource: ProfileResource |
| 150 | + end |
| 151 | + |
| 152 | + class MetadataResource |
| 153 | + include Alba::Resource |
| 154 | + |
| 155 | + attributes :role, :department |
| 156 | + end |
| 157 | + |
| 158 | + def test_association_with_source_returning_nil |
| 159 | + expected = '{"id":1,"name":"John","missing_profile":null}' |
| 160 | + assert_equal expected, UserResourceWithNilSource.new(@user).serialize |
| 161 | + end |
| 162 | + |
| 163 | + # Test source accessing instance variables |
| 164 | + class UserResourceWithMetadataSource |
| 165 | + include Alba::Resource |
| 166 | + |
| 167 | + attributes :id, :name |
| 168 | + |
| 169 | + one :metadata, source: proc { @metadata }, resource: MetadataResource |
| 170 | + end |
| 171 | + |
| 172 | + def test_association_with_source_accessing_instance_variables |
| 173 | + expected = '{"id":1,"name":"John","metadata":{"role":"admin","department":"engineering"}}' |
| 174 | + assert_equal expected, UserResourceWithMetadataSource.new(@user).serialize |
| 175 | + end |
| 176 | + |
| 177 | + # Test source with block resource definition |
| 178 | + class UserResourceWithSourceAndBlock |
| 179 | + include Alba::Resource |
| 180 | + |
| 181 | + attributes :id, :name |
| 182 | + |
| 183 | + one :profile_summary, |
| 184 | + source: proc { {email: custom_profile_data[:email], name: @name} } do |
| 185 | + attributes :email, :name |
| 186 | + end |
| 187 | + end |
| 188 | + |
| 189 | + def test_association_with_source_and_block_resource |
| 190 | + expected = '{"id":1,"name":"John","profile_summary":{"email":"[email protected]","name":"John"}}' |
| 191 | + assert_equal expected, UserResourceWithSourceAndBlock.new(@user).serialize |
| 192 | + end |
| 193 | + |
| 194 | + # Test error handling when source proc raises an exception |
| 195 | + class UserResourceWithErrorSource |
| 196 | + include Alba::Resource |
| 197 | + |
| 198 | + attributes :id, :name |
| 199 | + |
| 200 | + one :error_profile, |
| 201 | + source: proc { raise StandardError, 'Source error' }, |
| 202 | + resource: ProfileResource |
| 203 | + end |
| 204 | + |
| 205 | + def test_association_with_source_that_raises_error |
| 206 | + assert_raises(StandardError) do |
| 207 | + UserResourceWithErrorSource.new(@user).serialize |
| 208 | + end |
| 209 | + end |
| 210 | +end |
0 commit comments