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.1.17 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.16 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.15 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.14 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.13 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.12 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.11 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.10 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.9 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.8 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.7 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.6 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.5 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.4 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.3 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.2 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.1 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.1.0 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.0.21 test/unit/rails_ops/mixins/model/nesting.rb
rails_ops-1.0.20 test/unit/rails_ops/mixins/model/nesting.rb