Sha256: 9401b65aa4b194aa37e70b7ade8e2f01e32f059196a4ea38c3b4bb5740a02af7

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'spec_helper'

describe 'Commands / Create' do
  include_context 'users and tasks'

  subject(:users) { rom.commands.users }

  before do
    setup.relation(:users)

    setup.commands(:users) do
      define(:create) do
        result :one
      end

      define(:create_many, type: :create) do
        result :many
      end
    end
  end

  it 'returns a single tuple when result is set to :one' do
    result = users.try { create(id: 2, name: 'Jane') }

    expect(result.value).to eql(id: 2, name: 'Jane')
  end

  it 'returns tuples when result is set to :many' do
    result = users.try do
      create_many([{ id: 2, name: 'Jane' }, { id: 3, name: 'Jack' }])
    end

    expect(result.value.to_a).to match_array([
      { id: 2, name: 'Jane' }, { id: 3, name: 'Jack' }
    ])
  end

  it 'handles not-null constraint violation error' do
    result = users.try { create(id: nil, name: 'Jane') }

    expect(result.error).to be_instance_of(ROM::SQL::ConstraintError)
    expect(result.error.message).to match(/not-null/)
  end

  it 'handles uniqueness constraint violation error' do
    result = users.try { create(id: 3, name: 'Piotr') }

    expect(result.error).to be_instance_of(ROM::SQL::ConstraintError)
    expect(result.error.message).to match(/unique/)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-sql-0.3.2 spec/integration/commands/create_spec.rb
rom-sql-0.3.1 spec/integration/commands/create_spec.rb