diff --git a/lib/xeroizer/exceptions.rb b/lib/xeroizer/exceptions.rb index 714866f5..2f65e0b6 100644 --- a/lib/xeroizer/exceptions.rb +++ b/lib/xeroizer/exceptions.rb @@ -97,6 +97,8 @@ def message end + class RecordInvalid < XeroizerError; end + class SettingTotalDirectlyNotSupported < XeroizerError def initialize(attribute_name) diff --git a/lib/xeroizer/record/base.rb b/lib/xeroizer/record/base.rb index 24a8afaa..65bd5e5e 100644 --- a/lib/xeroizer/record/base.rb +++ b/lib/xeroizer/record/base.rb @@ -105,12 +105,21 @@ def download_complete_record! end def save - return false unless valid? + save! + true + rescue XeroizerError => e + log "[ERROR SAVING] (#{__FILE__}:#{__LINE__}) - #{e.message}" + false + end + + def save! + raise RecordInvalid unless valid? if new_record? create else update end + saved! end diff --git a/test/unit/oauth_test.rb b/test/unit/oauth_test.rb index 30b8e806..fd349c45 100644 --- a/test/unit/oauth_test.rb +++ b/test/unit/oauth_test.rb @@ -99,7 +99,7 @@ def setup assert_raises Xeroizer::ApiException do contact = @client.Contact.build(:name => 'Test Contact') - contact.save + contact.save! end end @@ -109,7 +109,7 @@ def setup assert_raises Xeroizer::UnparseableResponse do contact = @client.Contact.build(:name => 'Test Contact') - contact.save + contact.save! end end diff --git a/test/unit/record/base_test.rb b/test/unit/record/base_test.rb index 5832613a..97cd2c0e 100644 --- a/test/unit/record/base_test.rb +++ b/test/unit/record/base_test.rb @@ -103,4 +103,62 @@ class Xeroizer::Record::ExampleRecordClassModel < Xeroizer::Record::BaseModel; e an_example_instance.save end end + + context 'saving' do + context 'invalid record' do + setup do + @contact.stubs(:valid?).returns(false) + end + + must 'raise an exception saving with #save!' do + assert_raise(Xeroizer::RecordInvalid) do + @contact.save! + end + end + + must 'return false saving with #save' do + assert_equal(false, @contact.save) + end + end + + context 'api error received' do + setup do + response = get_file_as_string('api_exception.xml') + doc = Nokogiri::XML(response) + exception = Xeroizer::ApiException.new(doc.root.xpath("Type").text, + doc.root.xpath("Message").text, + response, + doc, + '') + + @contact.stubs(:valid?).returns(true) + @contact.stubs(:create).raises(exception) + @contact.stubs(:update).raises(exception) + end + + must 'raise an exception creating records with #save!' do + @contact.stubs(:new_record?).returns(true) + assert_raise(Xeroizer::ApiException) do + @contact.save! + end + end + + must 'raise an exception updating records with #save!' do + @contact.stubs(:new_record?).returns(false) + assert_raise(Xeroizer::ApiException) do + @contact.save! + end + end + + must 'return false creating records with #save' do + @contact.stubs(:new_record?).returns(true) + assert_equal(false, @contact.save) + end + + must 'return false updating records with #save' do + @contact.stubs(:new_record?).returns(false) + assert_equal(false, @contact.save) + end + end + end end