From e23772f07b48354e87ad1f3040bf741912082ae1 Mon Sep 17 00:00:00 2001 From: Giallombardo Nathan Date: Tue, 30 Jul 2024 09:56:21 +0000 Subject: [PATCH 1/5] clean log --- spec/n1ql_spec.rb | 1 - 1 file changed, 1 deletion(-) 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 From efcc5534fe7140c047f6d82c1f955f4269bdd3ad Mon Sep 17 00:00:00 2001 From: Giallombardo Nathan Date: Tue, 30 Jul 2024 09:56:44 +0000 Subject: [PATCH 2/5] refacto collection_proxy_spec --- spec/collection_proxy_spec.rb | 184 +++++++++++++++++++++++++++++++--- 1 file changed, 171 insertions(+), 13 deletions(-) diff --git a/spec/collection_proxy_spec.rb b/spec/collection_proxy_spec.rb index 40fa401c..8d6969a2 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 '#remov_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 From d76254a13ae6c194bad78aaae7d0550f569feef2 Mon Sep 17 00:00:00 2001 From: Giallombardo Nathan Date: Tue, 30 Jul 2024 09:57:39 +0000 Subject: [PATCH 3/5] better handling error --- lib/couchbase-orm/proxies/collection_proxy.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/couchbase-orm/proxies/collection_proxy.rb b/lib/couchbase-orm/proxies/collection_proxy.rb index 5503fdf0..1686a0b5 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 @@ -53,7 +51,7 @@ def remove_multi(ids, **options) 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 From 16a1e86f7b5eb812a320128ef280ebadb4fc808d Mon Sep 17 00:00:00 2001 From: Giallombardo Nathan Date: Tue, 30 Jul 2024 09:57:57 +0000 Subject: [PATCH 4/5] add upsert_multi --- lib/couchbase-orm/proxies/collection_proxy.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/couchbase-orm/proxies/collection_proxy.rb b/lib/couchbase-orm/proxies/collection_proxy.rb index 1686a0b5..8080e28c 100644 --- a/lib/couchbase-orm/proxies/collection_proxy.rb +++ b/lib/couchbase-orm/proxies/collection_proxy.rb @@ -50,6 +50,19 @@ 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 ArgumentError.new('Must proxy a non nil object') if proxyfied.nil? From 90374fb00177e0c4e4d8276b1c9595aa699283ae Mon Sep 17 00:00:00 2001 From: Giallombardo Nathan Date: Tue, 30 Jul 2024 09:59:44 +0000 Subject: [PATCH 5/5] review --- spec/collection_proxy_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/collection_proxy_spec.rb b/spec/collection_proxy_spec.rb index 8d6969a2..bdda5c46 100644 --- a/spec/collection_proxy_spec.rb +++ b/spec/collection_proxy_spec.rb @@ -136,7 +136,7 @@ def upsert_multi(_key, _options = nil) end end - describe '#remov_multi!' do + describe '#remove_multi!' do subject(:ok_proxy) { described_class.new(OKProxyfied.new) } it { expect(ok_proxy.remove_multi!(['key'])).not_to be_nil }