# 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