Sha256: ade4f4ede7c777898a370995eab4c6d643545c67ec799b246a6bcf817361c7f7

Contents?: true

Size: 1.28 KB

Versions: 51

Compression:

Stored size: 1.28 KB

Contents

require 'test_helper'

class RailsOps::Mixins::Model::NestingTest < ActiveSupport::TestCase
  include TestHelper

  GROUP_CREATION_OP = Class.new RailsOps::Operation::Model::Create do
    model Group do
      validates :name, presence: true
    end
  end

  BASIC_OP = Class.new RailsOps::Operation::Model::Create do
    model User do
    end
    nest_model_op :group, GROUP_CREATION_OP
  end

  def test_has_many_basic
    BASIC_OP.run!(
      user: {
        name: 'test',
        group_attributes: {
          name: 'g1',
          color: 'blue'
        }
      }
    )

    assert_equal 1, User.count
    assert_equal 1, Group.count
    assert_equal 'test', User.first.name
    assert_equal 'g1', Group.first.name
    assert_equal Group.first, User.first.group
    assert_equal [User.first], Group.first.users
  end

  def test_has_many_validation
    op = BASIC_OP.new(
      user: {
        name: 'test',
        group_attributes: {
          color: 'blue'
        }
      }
    )

    assert_raises ActiveRecord::RecordInvalid do
      op.run!
    end

    refute op.model.persisted?
    refute op.model.group.persisted?

    assert_equal BASIC_OP.model, op.model.class
    assert_equal GROUP_CREATION_OP.model, op.model.group.class

    refute op.model.group.valid?
    refute op.model.valid?
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
rails_ops-1.0.17.1 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.3.0 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.2.3 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.2.2 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.2.1 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.2.0 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.31 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.30 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.29 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.28 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.27 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.26 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.25 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.24 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.23 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.22 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.21 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.20 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.19 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.18 test/unit/rails_ops/mixins/model/nesting.rb