Sha256: b874aae62a7400b37e0ec84dc8a29fdf56cf52d120cc9e114d9be3808454742a

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require 'helper'

class AxleAttributes::HasAttributesTest < ActiveSupport::TestCase
  class TestModel
    include AxleAttributes::HasAttributes

    class << self
      def name
        'TestModel'
      end

      def reset!
        attributes.clear
        attribute_set.clear
      end
    end
  end

  setup do
    TestModel.reset!
  end

  test 'has_attributes' do
    TestModel.has_attributes foo: {}
    assert_kind_of AxleAttributes::Definition, TestModel.attributes['foo']

    TestModel.has_attributes({ bar: {} }, { editable: false })
    assert TestModel.attributes['bar'].readonly?

    TestModel.has_attributes({ baz: { editable: true } }, { editable: false })
    refute TestModel.attributes['baz'].readonly?
  end

  test 'has_attribute' do
    TestModel.has_attribute :foo
    assert_kind_of AxleAttributes::Definition, TestModel.attributes['foo']
    assert_equal ['foo'].to_set, TestModel.attribute_set
  end

  test 'has_mapping' do
    TestModel.has_attribute :foo
    TestModel.has_mapping :foo, %w(foo bar)
    expected = {'foo' => 'Foo', 'bar' => 'Bar'}
    assert_equal expected, TestModel.attributes['foo'].mappings
  end

  test 'has_attributes_file' do
    attributes_file = Tempfile.new(['attributes', '.json'])
    attributes_file.write({"speshal" => {"colors" => {"type" => "array"}}}.to_json)
    attributes_file.rewind
    TestModel.attributes_file_root = File.dirname(attributes_file.path)

    TestModel.has_attributes_file File.basename(attributes_file.path, '.json')

    assert_equal(:array, TestModel.attributes['colors'].type)
    assert_equal File.basename(attributes_file.path, '.json'), TestModel.attributes['colors'].options[:filename]
  end

  test 'attribute inheritance' do
    child_model = Class.new(TestModel)
    child_model.has_attribute :foo, {}

    assert child_model.attributes.key?('foo')
    assert child_model.attribute_set.include?('foo')

    assert !TestModel.attributes.key?('foo')
    assert !TestModel.attribute_set.include?('foo')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axle_attributes-1.13.2 test/lib/has_attributes_test.rb