Sha256: db04c92ca115f9cfc97ae6656ba65580f8a9ece88fb62dca952063ea665acdd6

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

require 'spec_helper'

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

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

  before do
    setup.relation(:users) do
      def by_name(name)
        where(name: name)
      end
    end

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

    rom.relations.users.insert(id: 2, name: 'Jane')
  end

  context '#transaction' do
    it 'delete in normal way if no error raised' do
      expect {
        users.delete.transaction do
          users.delete.by_name('Jane').call
        end
      }.to change { rom.relations.users.count }.by(-1)
    end

    it 'delete nothing if error was raised' do
      expect {
        users.delete.transaction do
          users.delete.by_name('Jane').call
          raise ROM::SQL::Rollback
        end
      }.to_not change { rom.relations.users.count }
    end
  end

  it 'raises error when tuple count does not match expectation' do
    result = users.try { users.delete.call }

    expect(result.value).to be(nil)
    expect(result.error).to be_instance_of(ROM::TupleCountMismatchError)
  end

  it 'deletes all tuples in a restricted relation' do
    result = users.try { users.delete.by_name('Jane').call }

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

  it 're-raises database error' do
    command = users.delete.by_name('Jane')

    expect(command.relation).to receive(:delete).and_raise(
      Sequel::DatabaseError, 'totally wrong'
    )

    expect {
      users.try { command.call }
    }.to raise_error(ROM::SQL::DatabaseError, /totally wrong/)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rom-sql-0.6.1 spec/integration/commands/delete_spec.rb
rom-sql-0.6.0 spec/integration/commands/delete_spec.rb
rom-sql-0.6.0.rc1 spec/integration/commands/delete_spec.rb
rom-sql-0.6.0.beta1 spec/integration/commands/delete_spec.rb
rom-sql-0.5.3 spec/integration/commands/delete_spec.rb
rom-sql-0.5.2 spec/integration/commands/delete_spec.rb
rom-sql-0.5.1 spec/integration/commands/delete_spec.rb
rom-sql-0.5.0 spec/integration/commands/delete_spec.rb