Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upsert multi #99

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/couchbase-orm/proxies/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def remove(id, **options)
def remove_multi!(ids, **options)
result = @proxyfied.remove_multi(ids, Couchbase::Options::RemoveMulti.new(**options))
first_result_with_error = result.find(&:error)
if first_result_with_error
raise CouchbaseOrm::Error::DocumentNotFound
end
raise first_result_with_error.error if first_result_with_error

result
end
Expand All @@ -52,8 +50,21 @@ def remove_multi(ids, **options)
result.reject(&:error)
end

def upsert_multi!(id_content, **options)
result = @proxyfied.upsert_multi(id_content, Couchbase::Options::UpsertMulti.new(**options))
first_result_with_error = result.find(&:error)
raise first_result_with_error.error if first_result_with_error

result
end

def upsert_multi(id_content, **options)
result = @proxyfied.upsert_multi(id_content, Couchbase::Options::UpsertMulti.new(**options))
result.reject(&:error)
end

def initialize(proxyfied)
raise 'Must proxy a non nil object' if proxyfied.nil?
raise ArgumentError.new('Must proxy a non nil object') if proxyfied.nil?

@proxyfied = proxyfied
end
Expand Down
184 changes: 171 additions & 13 deletions spec/collection_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,192 @@
require File.expand_path('support', __dir__)
require File.expand_path('../lib/couchbase-orm/proxies/collection_proxy', __dir__)

class Proxyfied
class KOProxyfied
def get(_key, _options = nil)
raise Couchbase::Error::DocumentNotFound
end

def remove(_key, _options = nil)
raise Couchbase::Error::DocumentNotFound
end

def get_multi(_key, _options = nil)
[OpenStruct.new(error: Couchbase::Error::DocumentNotFound.new)]
end

def remove_multi(_key, _options = nil)
[OpenStruct.new(error: Couchbase::Error::DocumentNotFound.new)]
end

def upsert_multi(_key, _options = nil)
[OpenStruct.new(error: Couchbase::Error::CouchbaseError.new)]
end
end

class OKProxyfied
def get(_key, _options = nil)
Object.new
end

def remove(_key, _options = nil)
Object.new
end

def get_multi(_key, _options = nil)
[OpenStruct.new(error: nil)]
end

def remove_multi(_key, _options = nil)
[OpenStruct.new(error: nil)]
end

def upsert_multi(_key, _options = nil)
[OpenStruct.new(error: nil)]
end
end

describe CouchbaseOrm::CollectionProxy do
it 'raises an error when get is called with bang version' do
expect {
CouchbaseOrm::CollectionProxy.new(Proxyfied.new).get!('key')
}.to raise_error(Couchbase::Error::DocumentNotFound)
describe '#initialize' do
it { expect { described_class.new(nil) }.to raise_error(ArgumentError) }
end

describe '#get!' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.get('key')).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.get!('key') }.to raise_error(Couchbase::Error::DocumentNotFound)
end
end
end

it 'does not raise an error when get is called with non bang version' do
expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).get('key') }.not_to raise_error
describe '#get' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.get('key')).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.get('key') }.not_to raise_error
end
end
end

it 'raises an error when remove is called with bang version' do
expect {
CouchbaseOrm::CollectionProxy.new(Proxyfied.new).remove!('key')
}.to raise_error(Couchbase::Error::DocumentNotFound)
describe '#get_multi!' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.get_multi!('key')).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.get_multi!('key') }.to raise_error(Couchbase::Error::DocumentNotFound)
end
end
end

it 'does not raise an error when remove is called with non bang version' do
expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).remove('key') }.not_to raise_error
describe '#get_multi' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.get_multi('key')).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.get_multi('key') }.not_to raise_error
end
end
end

describe '#remove!' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.remove!('key')).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.remove!('key') }.to raise_error(Couchbase::Error::DocumentNotFound)
end
end
end

describe '#remove' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.remove('key')).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.remove('key') }.not_to raise_error
end
end
end

describe '#remove_multi!' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.remove_multi!(['key'])).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.remove!('key') }.to raise_error(Couchbase::Error::DocumentNotFound)
end
end
end

describe '#remove_multi' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.remove_multi(['key'])).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.remove_multi(['key']) }.not_to raise_error
end
end
end

describe '#upsert_multi!' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.upsert_multi!(['foo', {foo: 'bar'}, 'bar', {bar: 'some value'}])).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.upsert_multi!(['foo', {foo: 'bar'}, 'bar', {bar: 'some value'}]) }.to raise_error(Couchbase::Error::CouchbaseError)
end
end
end

describe '#upsert_multi' do
subject(:ok_proxy) { described_class.new(OKProxyfied.new) }

it { expect(ok_proxy.upsert_multi(['foo', {foo: 'bar'}, 'bar', {bar: 'some value'}])).not_to be_nil }

context 'with error' do
subject(:ko_proxy) { described_class.new(KOProxyfied.new) }

it 'raises an error' do
expect { ko_proxy.upsert_multi(['foo', {foo: 'bar'}, 'bar', {bar: 'some value'}]) }.not_to raise_error
end
end
end
end
1 change: 0 additions & 1 deletion spec/n1ql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class N1QLTest < CouchbaseOrm::Base
expect(N1QLTest.find(t.id).name).to eq(special_name)
expect(N1QLTest.by_name(key: special_name).to_a.first).to eq(t)
expect(N1QLTest.where(name: special_name).to_a.first).to eq(t)
puts N1QLTest.where(name: special_name).to_n1ql
end

it 'returns matching results with custom n1ql query' do
Expand Down
Loading