diff --git a/lib/couchbase-orm/proxies/collection_proxy.rb b/lib/couchbase-orm/proxies/collection_proxy.rb index 5503fdf0..8080e28c 100644 --- a/lib/couchbase-orm/proxies/collection_proxy.rb +++ b/lib/couchbase-orm/proxies/collection_proxy.rb @@ -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 @@ -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 diff --git a/spec/collection_proxy_spec.rb b/spec/collection_proxy_spec.rb index 40fa401c..bdda5c46 100644 --- a/spec/collection_proxy_spec.rb +++ b/spec/collection_proxy_spec.rb @@ -3,7 +3,7 @@ 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 @@ -11,26 +11,184 @@ def get(_key, _options = nil) 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 diff --git a/spec/n1ql_spec.rb b/spec/n1ql_spec.rb index 7b68b7fe..8f1334a0 100644 --- a/spec/n1ql_spec.rb +++ b/spec/n1ql_spec.rb @@ -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