Sha256: f93c404584f16127c7b6475b5c6f2a5c43689ded03b7e49280d4112276dd7fcf

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 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)
        restrict(name: name)
      end
    end
  end

  it 'deletes all tuples when there is no restriction' do
    setup.commands(:users) do
      define(:delete)
    end

    result = users.try { users.delete.call }

    expect(result).to match_array([
      { name: 'Jane', email: 'jane@doe.org' },
      { name: 'Joe', email: 'joe@doe.org' }
    ])
  end

  it 'deletes tuples matching restriction' do
    setup.commands(:users) do
      define(:delete)
    end

    result = users.try { users.delete.by_name('Joe').call }

    expect(result).to match_array([{ name: 'Joe', email: 'joe@doe.org' }])
  end

  it 'returns untouched relation if there are no tuples to delete' do
    setup.commands(:users) do
      define(:delete)
    end

    result = users.try { users.delete.by_name('Not here').call }

    expect(result).to match_array([])
  end

  it 'returns deleted tuple when result is set to :one' do
    setup.commands(:users) do
      define(:delete_one, type: :delete) do
        result :one
      end
    end

    result = users.try { users.delete_one.by_name('Jane').call }

    expect(result.value).to eql(name: 'Jane', email: 'jane@doe.org')
  end

  it 'raises when result is set to :one and relation contains more tuples' do
    setup.commands(:users) do
      define(:delete) do
        result :one
      end
    end

    result = users.try { users.delete.call }

    expect(result.error).to be_instance_of(ROM::TupleCountMismatchError)

    expect(rom.relations.users.to_a).to match_array([
      { name: 'Jane', email: 'jane@doe.org' },
      { name: 'Joe', email: 'joe@doe.org' }
    ])
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rom-0.7.1 spec/integration/commands/delete_spec.rb
rom-0.7.0 spec/integration/commands/delete_spec.rb
rom-0.6.2 spec/integration/commands/delete_spec.rb
rom-0.6.1 spec/integration/commands/delete_spec.rb
rom-0.6.0 spec/integration/commands/delete_spec.rb
rom-0.6.0.rc1 spec/integration/commands/delete_spec.rb
rom-0.6.0.beta3 spec/integration/commands/delete_spec.rb
rom-0.6.0.beta2 spec/integration/commands/delete_spec.rb
rom-0.6.0.beta1 spec/integration/commands/delete_spec.rb