Sha256: cdd769156f413e1328680f369af30fa9b515341af3fbbc9635b264ed563e0038

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'test_helper'

require 'upgrow/model'

module Upgrow
  class ModelTest < ActiveSupport::TestCase
    class SampleModel < Model
    end

    class SubSampleModel < SampleModel
    end

    class SampleModelWithAttribute < Model
      attribute :phone
    end

    class SampleModelRecord
      def self.attribute_names
        ['id', 'name']
      end
    end

    class SubSampleModelRecord
      def self.attribute_names
        ['email']
      end
    end

    class SampleModelWithAttributeRecord
      def self.attribute_names
        ['id']
      end
    end

    class OrphanedModel < Model
    end

    test '.schema is inferred from the Active Record with the Model name' do
      assert_equal [:id, :name], SampleModel.schema.attribute_names
    end

    test '.schema is populated with inherited attributes' do
      assert_equal [:email, :id, :name], SubSampleModel.schema.attribute_names
    end

    test '.schema includes explicit attributes as well as Active Record attributes' do
      assert_equal [:id, :phone],
        SampleModelWithAttribute.schema.attribute_names
    end

    test '.new raises a Name Error if the Record class is undefined' do
      error = assert_raises(NameError) do
        OrphanedModel.new
      end

      assert_includes(
        error.message,
        'uninitialized constant Upgrow::ModelTest::OrphanedModelRecord'
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
upgrow-0.0.5 test/upgrow/model_test.rb
upgrow-0.0.4 test/upgrow/model_test.rb
upgrow-0.0.3 test/upgrow/model_test.rb