require 'test_helper'

class RailsOps::Operation::Model::CreateTest < ActiveSupport::TestCase
  include TestHelper

  BASIC_OP = Class.new(RailsOps::Operation::Model::Create) do
    model Group
  end

  def test_basic
    op = BASIC_OP.run!(group: { name: 'test', color: 'red' })

    assert_equal 'test', op.model.name
    assert_equal 'red', op.model.color
    assert op.model.persisted?
    refute op.model.changed?
  end

  def test_model_extension
    cls = Class.new(RailsOps::Operation::Model::Create) do
      model Group do
        validates :color, presence: true
      end
    end

    assert cls.new.model.class < Group

    assert_raises ActiveRecord::RecordInvalid do
      cls.run!(group: { name: 'test' })
    end
  end

  def test_parent_op
    op = BASIC_OP.new
    assert_equal op, op.model.parent_op
  end

  def test_always_extend_model_class
    assert RailsOps::Operation::Model::Create.always_extend_model_class?
  end

  def test_build
    op = BASIC_OP.new
    op.build_model

    assert op.instance_variable_get(:@model)

    assert_raises_with_message RuntimeError, 'Model can only be built once.' do
      op.build_model
    end
  end
end

Add test for model creation
Add test for model creation
