Skip to content

Commit

Permalink
Merge pull request #409 from euphoricpanda/master
Browse files Browse the repository at this point in the history
Added `#save!` method
  • Loading branch information
CloCkWeRX authored Jan 24, 2018
2 parents 8c5925b + 0d43cb4 commit 7de4747
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/xeroizer/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def message

end

class RecordInvalid < XeroizerError; end

class SettingTotalDirectlyNotSupported < XeroizerError

def initialize(attribute_name)
Expand Down
11 changes: 10 additions & 1 deletion lib/xeroizer/record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/unit/oauth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def setup

assert_raises Xeroizer::ApiException do
contact = @client.Contact.build(:name => 'Test Contact')
contact.save
contact.save!
end
end

Expand All @@ -109,7 +109,7 @@ def setup

assert_raises Xeroizer::UnparseableResponse do
contact = @client.Contact.build(:name => 'Test Contact')
contact.save
contact.save!
end
end

Expand Down
58 changes: 58 additions & 0 deletions test/unit/record/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
'<FakeRequest />')

@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

0 comments on commit 7de4747

Please sign in to comment.