|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class HierarchyInheritanceTest < ActiveSupport::TestCase |
| 6 | + # Test for issue #392: Hierarchy class should inherit from same abstract base as model |
| 7 | + # but NOT from STI parent (to avoid inheriting validations/callbacks) |
| 8 | + test 'MetalHierarchy inherits from same connection class as Metal' do |
| 9 | + # Force MetalHierarchy to be loaded |
| 10 | + Metal._ct |
| 11 | + |
| 12 | + # Metal < ApplicationRecord (abstract connection class) |
| 13 | + # Adamantium < Metal (STI child) |
| 14 | + # MetalHierarchy should inherit from ApplicationRecord, NOT Metal |
| 15 | + assert_equal Metal.superclass, MetalHierarchy.superclass, |
| 16 | + "MetalHierarchy should inherit from same abstract base as Metal (#{Metal.superclass})" |
| 17 | + |
| 18 | + # Verify it's the abstract class, not the STI parent |
| 19 | + assert MetalHierarchy.superclass.abstract_class?, |
| 20 | + "MetalHierarchy should inherit from abstract class" |
| 21 | + |
| 22 | + # The hierarchy class should NOT inherit validations from Metal |
| 23 | + assert_not_equal Metal.validators.size, MetalHierarchy.validators.size, |
| 24 | + "MetalHierarchy should not inherit validations from Metal" |
| 25 | + end |
| 26 | + |
| 27 | + test 'Adamantium inherits has_closure_tree and uses same hierarchy as Metal' do |
| 28 | + # Adamantium < Metal (STI - should inherit has_closure_tree) |
| 29 | + |
| 30 | + # Verify Adamantium inherited has_closure_tree |
| 31 | + assert_respond_to Adamantium, :_ct, "Adamantium should inherit has_closure_tree from Metal" |
| 32 | + |
| 33 | + # Both should use the same hierarchy class (MetalHierarchy) |
| 34 | + assert_equal Metal.hierarchy_class, Adamantium.hierarchy_class, |
| 35 | + "Adamantium should use same hierarchy class as Metal (STI)" |
| 36 | + |
| 37 | + # The hierarchy class should inherit from ApplicationRecord, NOT Metal |
| 38 | + assert_equal ApplicationRecord, MetalHierarchy.superclass, |
| 39 | + "MetalHierarchy should inherit from ApplicationRecord, not Metal" |
| 40 | + end |
| 41 | +end |
0 commit comments