Skip to content

Commit

Permalink
Merge pull request #24 from jenskdsgn/issue-32/nil-cast
Browse files Browse the repository at this point in the history
Proper nil cast on numeric values
  • Loading branch information
shivanshgaur committed May 25, 2018
2 parents a4e3382 + e0e7072 commit aa92a11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/salesforce-orm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def map_from_keys(attributes)
def cast_to(value:, data_type:)
case data_type
when :integer
value.to_i
value.nil? ? nil : value.to_i
when :float
value.to_f
value.nil? ? nil : value.to_f
when :date_time
time_parse(value: value)
when :date
Expand Down
15 changes: 10 additions & 5 deletions spec/object_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require_relative 'fixtures/sample_object'

RSpec.describe SalesforceOrm::ObjectBase do

before(:all) do
@sample_field_map = {
field1: :fieldOne,
Expand Down Expand Up @@ -46,7 +45,7 @@ def assign_field_map(new_field_map = nil)
SalesforceOrm::ObjectMaker::DEFAULT_DATA_TYPE_MAP
)

custom_data_type_map = {yo: :date_time}
custom_data_type_map = { yo: :date_time }

SampleObject.data_type_map = custom_data_type_map

Expand All @@ -57,6 +56,15 @@ def assign_field_map(new_field_map = nil)
SampleObject.data_type_map = SalesforceOrm::ObjectMaker::DEFAULT_DATA_TYPE_MAP
end

it 'should represent nil numeric types as nil' do
SampleObject.data_type_map = { some_nil_int: :integer, some_nil_float: :float }

sobj = SampleObject.build(some_nil_int: :nil, some_nil_float: nil)

expect(sobj.some_nil_float).to be_nil
expect(sobj.some_nil_integer).to be_nil
end

it 'should allow to use record type' do
expect(SampleObject.record_type).to be_nil

Expand Down Expand Up @@ -84,7 +92,6 @@ def assign_field_map(new_field_map = nil)

expect(SampleObject).to receive(:record_type_id).and_return(record_type_id).exactly(4).times


expect(SalesforceOrm::RestforceClient.instance).to receive(:create!).with(
SampleObject.object_name,
SalesforceOrm::RecordTypeManager::FIELD_NAME => record_type_id
Expand Down Expand Up @@ -240,7 +247,6 @@ def assign_field_map(new_field_map = nil)

describe 'last' do
it 'should call order and and first of ORM' do

expect_any_instance_of(SalesforceOrm::Base).to receive(
:order
).with('created_at DESC').and_return(SalesforceOrm::Base.new(SampleObject))
Expand Down Expand Up @@ -352,5 +358,4 @@ def assign_field_map(new_field_map = nil)
sobj.destroy!
end
end

end

0 comments on commit aa92a11

Please sign in to comment.