test/test_document_schema.rb in populate-me-0.7.1 vs test/test_document_schema.rb in populate-me-0.8.0

- old
+ new

@@ -117,7 +117,146 @@ assert_equal ['averell', '4'], output[1] end end + describe 'Polymorphism' do + + class PolyBox < PopulateMe::Document + field :first_name + field :last_name + field :middle_name, only_for: ['Middle', 'Long name'] + field :nick_name, only_for: 'Funny' + field :third_name, only_for: 'Long name' + end + + class NotPoly < PopulateMe::Document + field :name + relationship :images + end + + class JustPoly < PopulateMe::Document + polymorphic + end + + class PolyCustom < PopulateMe::Document + polymorphic type: :text, custom_option: 'Custom' + end + + class PolyApplicable < PopulateMe::Document + field :name_1, only_for: 'Shape 1' + field :name_2, only_for: 'Shape 2' + field :position + end + + class PolyLabel < PopulateMe::Document + polymorphic + field :name + end + + class PolyRelationship < PopulateMe::Document + field :name + field :short_description, only_for: 'Chapter' + relationship :images, only_for: 'Slider' + relationship :paragraphs, only_for: 'Chapter' + end + + class PolyGroup < PopulateMe::Document + only_for 'Slider' do + field :name + relationship :images + end + field :no_only_for + only_for ['Try','This'] do + field :crazy + end + field :position + end + + it 'Creates a field for polymorphic type if it does not exist yet' do + assert_equal :polymorphic_type, PolyBox.fields[:polymorphic_type][:type] + assert_equal :polymorphic_type, JustPoly.fields[:polymorphic_type][:type] + assert_equal :polymorphic_type, PolyRelationship.fields[:polymorphic_type][:type] + end + + it 'By default polymorphic_type is hidden and not wrapped' do + assert_equal :hidden, PolyBox.fields[:polymorphic_type][:input_attributes][:type] + refute PolyBox.fields[:polymorphic_type][:wrap] + end + + it 'Does not create polymorphic type field if not required' do + assert_nil NotPoly.fields[:polymorphic_type] + end + + it 'Gathers all polymorphic type unique values' do + assert_equal ['Middle', 'Long name', 'Funny'], PolyBox.fields[:polymorphic_type][:values] + assert_equal [], JustPoly.fields[:polymorphic_type][:values] + assert_equal ['Chapter', 'Slider'], PolyRelationship.fields[:polymorphic_type][:values] + end + + it 'Adds or update field options for polymorphic type passed as arguments' do + assert_equal :text, PolyCustom.fields[:polymorphic_type][:type] + assert_equal 'Custom', PolyCustom.fields[:polymorphic_type][:custom_option] + end + + it 'Forces only_for field option to be an Array if String' do + assert_nil PolyBox.fields[:first_name][:only_for] + assert_equal ['Middle', 'Long name'], PolyBox.fields[:middle_name][:only_for] + assert_equal ['Funny'], PolyBox.fields[:nick_name][:only_for] + assert_equal ['Slider'], PolyRelationship.relationships[:images][:only_for] + end + + it 'Has a polymorphic? predicate' do + assert PolyBox.polymorphic? + refute NotPoly.polymorphic? + end + + it 'Knows when a field is applicable to a polymorphic_type' do + assert NotPoly.field_applicable?(:name, nil) # Not polymorphic + assert NotPoly.field_applicable?(:name, 'Fake') # Not polymorphic + refute NotPoly.field_applicable?(:non_existing_field, nil) # Not polymorphic + assert PolyApplicable.field_applicable?(:polymorphic_type, 'Shape 1') # no only_for + assert PolyApplicable.field_applicable?(:position, 'Shape 1') # no only_for + refute PolyApplicable.field_applicable?(:name_2, 'Shape 1') # not included + assert PolyApplicable.field_applicable?(:name_2, nil) # no type set yet + + assert NotPoly.new.field_applicable?(:name) # Not polymorphic + refute NotPoly.new.field_applicable?(:non_existing_field) # Not polymorphic + assert PolyApplicable.new(polymorphic_type: 'Shape 2').field_applicable?(:name_2) + refute PolyApplicable.new(polymorphic_type: 'Shape 2').field_applicable?(:name_1) + assert PolyApplicable.new.field_applicable?(:name_2) # No type set yet + assert PolyApplicable.new.field_applicable?(:name_1) # No type set yes + end + + it 'Knows when a relationship is applicable to a polymorphic_type' do + assert PolyRelationship.relationship_applicable?(:images, 'Slider') + refute PolyRelationship.relationship_applicable?(:images, 'Chapter') + assert PolyRelationship.new(polymorphic_type: 'Slider').relationship_applicable?(:images) + refute PolyRelationship.new(polymorphic_type: 'Chapter').relationship_applicable?(:images) + end + + it 'Ignores polymorphic_type when picking the default label_field' do + assert_equal :name, PolyLabel.label_field + end + + it 'Uses groups to define only_for option for all included fields and relationships' do + assert_equal :polymorphic_type, PolyGroup.fields[:polymorphic_type][:type] + assert_equal ['Slider', 'Try', 'This'], PolyGroup.fields[:polymorphic_type][:values] + assert_equal ['Slider'], PolyGroup.fields[:name][:only_for] + assert_equal ['Slider'], PolyGroup.relationships[:images][:only_for] + assert_equal ['Try','This'], PolyGroup.fields[:crazy][:only_for] + end + + it 'Group only_for option does not leak on special fields' do + refute PolyGroup.fields[:id].key?(:only_for) + refute PolyGroup.fields[:polymorphic_type].key?(:only_for) + end + + it 'Group only_for option does not leak outside of their scope' do + refute PolyGroup.fields[:no_only_for].key?(:only_for) + refute PolyGroup.fields[:position].key?(:only_for) + end + + end + end