From c7f64c46c9b94b42a8355243bd11026dcfdabaf8 Mon Sep 17 00:00:00 2001 From: Sean Edge Date: Thu, 2 Mar 2023 18:29:26 -0500 Subject: [PATCH] =?UTF-8?q?Add=20`find=5Fby!`=20method=20that=20raises=20`?= =?UTF-8?q?ActiveForce::RecordNotFound`=20if=20no=E2=80=A6=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add `find_by!` method that raises `ActiveForce::RecordNotFound` if nothing is found. * Update CHANGELOG. --- CHANGELOG.md | 1 + lib/active_force/active_query.rb | 7 +++++++ lib/active_force/sobject.rb | 2 +- spec/active_force/active_query_spec.rb | 7 +++++++ spec/active_force/sobject_spec.rb | 11 +++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 165dce2..2562d33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Not released * Fix `where` chaining on `ActiveQuery` (https://github.com/Beyond-Finance/active_force/pull/7) +* Add `find_by!` which raises `ActiveForce::RecordNotFound` if nothing is found. (https://github.com/Beyond-Finance/active_force/pull/8) ## 0.9.1 * Fix invalid error class (https://github.com/Beyond-Finance/active_force/pull/6) diff --git a/lib/active_force/active_query.rb b/lib/active_force/active_query.rb index b40da6c..cbf7555 100644 --- a/lib/active_force/active_query.rb +++ b/lib/active_force/active_query.rb @@ -4,6 +4,7 @@ module ActiveForce class PreparedStatementInvalid < ArgumentError; end + class RecordNotFound < StandardError; end class ActiveQuery < Query extend Forwardable @@ -54,6 +55,12 @@ def find_by conditions where(conditions).limit 1 end + def find_by! conditions + res = find_by(conditions) + raise RecordNotFound if res.nil? + res + end + def includes(*relations) relations.each do |relation| association = sobject.associations[relation] diff --git a/lib/active_force/sobject.rb b/lib/active_force/sobject.rb index 930cd9f..7ceb416 100644 --- a/lib/active_force/sobject.rb +++ b/lib/active_force/sobject.rb @@ -27,7 +27,7 @@ class SObject class << self extend Forwardable - def_delegators :query, :where, :first, :last, :all, :find, :find_by, :count, :includes, :limit, :order, :select, :none + def_delegators :query, :where, :first, :last, :all, :find, :find_by, :find_by!, :count, :includes, :limit, :order, :select, :none def_delegators :mapping, :table, :table_name, :custom_table?, :mappings private diff --git a/spec/active_force/active_query_spec.rb b/spec/active_force/active_query_spec.rb index 2bf9e4c..00635ce 100644 --- a/spec/active_force/active_query_spec.rb +++ b/spec/active_force/active_query_spec.rb @@ -170,6 +170,13 @@ end end + describe "#find_by!" do + it "raises if record not found" do + allow(client).to receive(:query).and_return(nil) + expect { active_query.find_by!(field: 123) }.to raise_error(ActiveForce::RecordNotFound) + end + end + describe "responding as an enumerable" do before do expect(active_query).to receive(:to_a).and_return([]) diff --git a/spec/active_force/sobject_spec.rb b/spec/active_force/sobject_spec.rb index 088878d..d125d4f 100644 --- a/spec/active_force/sobject_spec.rb +++ b/spec/active_force/sobject_spec.rb @@ -256,6 +256,17 @@ class IceCream < ActiveForce::SObject end end + describe "#find_by!" do + it "queries the client, with the SFDC field names and correctly enclosed values" do + expect(client).to receive(:query).with("SELECT #{Whizbang.fields.join ', '} FROM Whizbang__c WHERE (Id = 123) AND (Text_Label = 'foo') LIMIT 1").and_return([Restforce::Mash.new(Id: 123, text: 'foo')]) + Whizbang.find_by! id: 123, text: "foo" + end + it "raises if nothing found" do + expect(client).to receive(:query).with("SELECT #{Whizbang.fields.join ', '} FROM Whizbang__c WHERE (Id = 123) AND (Text_Label = 'foo') LIMIT 1") + expect { Whizbang.find_by! id: 123, text: "foo" }.to raise_error(ActiveForce::RecordNotFound) + end + end + describe '#reload' do let(:client) do double("sfdc_client", query: [Restforce::Mash.new(Id: 1, Name: 'Jeff')])