|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +module ForestAdminDatasourceCustomizer |
| 4 | + module Decorators |
| 5 | + module RenameCollection |
| 6 | + include ForestAdminDatasourceToolkit |
| 7 | + include ForestAdminDatasourceToolkit::Components::Query |
| 8 | + include ForestAdminDatasourceToolkit::Components::Query::ConditionTree |
| 9 | + include ForestAdminDatasourceToolkit::Decorators |
| 10 | + include ForestAdminDatasourceToolkit::Schema |
| 11 | + |
| 12 | + describe RenameCollectionDecorator do |
| 13 | + include_context 'with caller' |
| 14 | + |
| 15 | + before do |
| 16 | + @collection_transaction = build_collection( |
| 17 | + name: 'transaction', |
| 18 | + schema: { |
| 19 | + fields: { |
| 20 | + 'id' => build_numeric_primary_key, |
| 21 | + 'amount' => build_column, |
| 22 | + 'subject_id' => build_column, |
| 23 | + 'subject_type' => build_column, |
| 24 | + 'subject' => Relations::PolymorphicManyToOneSchema.new( |
| 25 | + foreign_key_type_field: 'subject_type', |
| 26 | + foreign_collections: ['income', 'expense'], |
| 27 | + foreign_key_targets: { 'income' => 'id', 'expense' => 'id' }, |
| 28 | + foreign_key: 'subject_id' |
| 29 | + ) |
| 30 | + } |
| 31 | + } |
| 32 | + ) |
| 33 | + |
| 34 | + @collection_income = build_collection( |
| 35 | + name: 'income', |
| 36 | + schema: { |
| 37 | + fields: { |
| 38 | + 'id' => build_numeric_primary_key, |
| 39 | + 'description' => build_column, |
| 40 | + 'transactions' => Relations::PolymorphicOneToManySchema.new( |
| 41 | + origin_key: 'subject_id', |
| 42 | + foreign_collection: 'transaction', |
| 43 | + origin_key_target: 'id', |
| 44 | + origin_type_field: 'subject_type', |
| 45 | + origin_type_value: 'income' |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + @collection_expense = build_collection( |
| 52 | + name: 'expense', |
| 53 | + schema: { |
| 54 | + fields: { |
| 55 | + 'id' => build_numeric_primary_key, |
| 56 | + 'description' => build_column, |
| 57 | + 'transactions' => Relations::PolymorphicOneToManySchema.new( |
| 58 | + origin_key: 'subject_id', |
| 59 | + foreign_collection: 'transaction', |
| 60 | + origin_key_target: 'id', |
| 61 | + origin_type_field: 'subject_type', |
| 62 | + origin_type_value: 'expense' |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + @datasource = RenameCollectionDatasourceDecorator.new( |
| 69 | + build_datasource_with_collections([ |
| 70 | + @collection_transaction, |
| 71 | + @collection_income, |
| 72 | + @collection_expense |
| 73 | + ]) |
| 74 | + ) |
| 75 | + end |
| 76 | + |
| 77 | + describe '#name' do |
| 78 | + it 'returns the renamed collection name' do |
| 79 | + @datasource.rename_collection('income', 'renamed_income') |
| 80 | + collection = @datasource.get_collection('renamed_income') |
| 81 | + |
| 82 | + expect(collection.name).to eq('renamed_income') |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + describe '#list' do |
| 87 | + context 'with polymorphic relations' do |
| 88 | + it 'transforms polymorphic type values in returned records' do |
| 89 | + @datasource.rename_collection('income', 'renamed_income') |
| 90 | + collection = @datasource.get_collection('transaction') |
| 91 | + |
| 92 | + # Mock the child collection to return records with old collection names |
| 93 | + allow(collection.instance_variable_get(:@child_collection)).to receive(:list).and_return([ |
| 94 | + { 'id' => 1, 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'income' } |
| 95 | + ]) |
| 96 | + |
| 97 | + result = collection.list(caller, nil, nil) |
| 98 | + |
| 99 | + expect(result).to eq([ |
| 100 | + { 'id' => 1, 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'renamed_income' } |
| 101 | + ]) |
| 102 | + end |
| 103 | + |
| 104 | + it 'handles arrays of records' do |
| 105 | + @datasource.rename_collection('income', 'renamed_income') |
| 106 | + @datasource.rename_collection('expense', 'renamed_expense') |
| 107 | + collection = @datasource.get_collection('transaction') |
| 108 | + |
| 109 | + allow(collection.instance_variable_get(:@child_collection)).to receive(:list).and_return([ |
| 110 | + { 'id' => 1, 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'income' }, |
| 111 | + { 'id' => 2, 'amount' => 200, 'subject_id' => 2, 'subject_type' => 'expense' } |
| 112 | + ]) |
| 113 | + |
| 114 | + result = collection.list(caller, nil, nil) |
| 115 | + |
| 116 | + expect(result).to eq([ |
| 117 | + { 'id' => 1, 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'renamed_income' }, |
| 118 | + { 'id' => 2, 'amount' => 200, 'subject_id' => 2, 'subject_type' => 'renamed_expense' } |
| 119 | + ]) |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe '#create' do |
| 125 | + context 'with polymorphic relations' do |
| 126 | + it 'transforms polymorphic type values before creating' do |
| 127 | + @datasource.rename_collection('income', 'renamed_income') |
| 128 | + collection = @datasource.get_collection('transaction') |
| 129 | + child_collection = collection.instance_variable_get(:@child_collection) |
| 130 | + |
| 131 | + # Setup spy for child collection |
| 132 | + allow(child_collection).to receive(:create).with( |
| 133 | + caller, |
| 134 | + { 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'income' } |
| 135 | + ).and_return( |
| 136 | + { 'id' => 1, 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'income' } |
| 137 | + ) |
| 138 | + |
| 139 | + result = collection.create(caller, { 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'renamed_income' }) |
| 140 | + |
| 141 | + expect(result).to eq({ 'id' => 1, 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'renamed_income' }) |
| 142 | + expect(child_collection).to have_received(:create).with( |
| 143 | + caller, |
| 144 | + { 'amount' => 100, 'subject_id' => 1, 'subject_type' => 'income' } |
| 145 | + ) |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + describe '#update' do |
| 151 | + context 'with polymorphic relations' do |
| 152 | + it 'transforms polymorphic type values in patch' do |
| 153 | + @datasource.rename_collection('income', 'renamed_income') |
| 154 | + collection = @datasource.get_collection('transaction') |
| 155 | + child_collection = collection.instance_variable_get(:@child_collection) |
| 156 | + filter = Filter.new |
| 157 | + |
| 158 | + allow(child_collection).to receive(:update) |
| 159 | + |
| 160 | + collection.update(caller, filter, { 'subject_type' => 'renamed_income' }) |
| 161 | + |
| 162 | + expect(child_collection).to have_received(:update).with( |
| 163 | + caller, |
| 164 | + filter, |
| 165 | + { 'subject_type' => 'income' } |
| 166 | + ) |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + describe '#refine_filter' do |
| 172 | + context 'with polymorphic type fields in condition tree' do |
| 173 | + it 'transforms renamed collection names to original names' do |
| 174 | + @datasource.rename_collection('income', 'renamed_income') |
| 175 | + collection = @datasource.get_collection('transaction') |
| 176 | + |
| 177 | + filter = Filter.new( |
| 178 | + condition_tree: Nodes::ConditionTreeLeaf.new('subject_type', Operators::EQUAL, 'renamed_income') |
| 179 | + ) |
| 180 | + |
| 181 | + refined_filter = collection.refine_filter(caller, filter) |
| 182 | + |
| 183 | + expect(refined_filter.condition_tree.value).to eq('income') |
| 184 | + end |
| 185 | + |
| 186 | + it 'handles array values (IN operator)' do |
| 187 | + @datasource.rename_collection('income', 'renamed_income') |
| 188 | + @datasource.rename_collection('expense', 'renamed_expense') |
| 189 | + collection = @datasource.get_collection('transaction') |
| 190 | + |
| 191 | + filter = Filter.new( |
| 192 | + condition_tree: Nodes::ConditionTreeLeaf.new('subject_type', Operators::IN, ['renamed_income', 'renamed_expense']) |
| 193 | + ) |
| 194 | + |
| 195 | + refined_filter = collection.refine_filter(caller, filter) |
| 196 | + |
| 197 | + expect(refined_filter.condition_tree.value).to eq(['income', 'expense']) |
| 198 | + end |
| 199 | + |
| 200 | + it 'leaves non-type fields unchanged' do |
| 201 | + @datasource.rename_collection('income', 'renamed_income') |
| 202 | + collection = @datasource.get_collection('transaction') |
| 203 | + |
| 204 | + filter = Filter.new( |
| 205 | + condition_tree: Nodes::ConditionTreeLeaf.new('amount', Operators::GREATER_THAN, 100) |
| 206 | + ) |
| 207 | + |
| 208 | + refined_filter = collection.refine_filter(caller, filter) |
| 209 | + |
| 210 | + expect(refined_filter.condition_tree.value).to eq(100) |
| 211 | + end |
| 212 | + end |
| 213 | + end |
| 214 | + |
| 215 | + describe '#refine_schema' do |
| 216 | + context 'with PolymorphicManyToOne relations' do |
| 217 | + it 'renames foreign_collections' do |
| 218 | + @datasource.rename_collection('income', 'renamed_income') |
| 219 | + @datasource.rename_collection('expense', 'renamed_expense') |
| 220 | + collection = @datasource.get_collection('transaction') |
| 221 | + |
| 222 | + expect(collection.schema[:fields]['subject'].foreign_collections).to eq(['renamed_income', 'renamed_expense']) |
| 223 | + expect(collection.schema[:fields]['subject'].foreign_key_targets.keys).to eq(['renamed_income', 'renamed_expense']) |
| 224 | + end |
| 225 | + end |
| 226 | + |
| 227 | + context 'with PolymorphicOneToMany relations' do |
| 228 | + it 'updates origin_type_value when collection is renamed' do |
| 229 | + @datasource.rename_collection('income', 'renamed_income') |
| 230 | + collection = @datasource.get_collection('renamed_income') |
| 231 | + |
| 232 | + expect(collection.schema[:fields]['transactions'].origin_type_value).to eq('renamed_income') |
| 233 | + end |
| 234 | + |
| 235 | + it 'updates foreign_collection' do |
| 236 | + @datasource.rename_collection('transaction', 'renamed_transaction') |
| 237 | + collection = @datasource.get_collection('income') |
| 238 | + |
| 239 | + expect(collection.schema[:fields]['transactions'].foreign_collection).to eq('renamed_transaction') |
| 240 | + end |
| 241 | + end |
| 242 | + end |
| 243 | + end |
| 244 | + end |
| 245 | + end |
| 246 | +end |
0 commit comments