Skip to content

Commit

Permalink
Improve dumping and type casting specs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Jul 24, 2018
1 parent 0555c9b commit 89b5066
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 54 deletions.
70 changes: 21 additions & 49 deletions spec/dynamoid/dumping_spec.rb
Expand Up @@ -74,20 +74,16 @@
end

it 'saves time as :number' do
time = Time.now
time = Time.utc(2018, 7, 24, 22, 4, 30, 1).to_datetime
obj = klass.create(sent_at: time)
expect(reload(obj).sent_at).to eq(time)
expect(raw_attributes(obj)[:sent_at]).to eql(BigDecimal(format('%d.%09d', time.to_i, time.nsec)))
expect(reload(obj).sent_at).to eql(time)
expect(raw_attributes(obj)[:sent_at]).to eql(BigDecimal('1532469870.000001'))
end

it 'saves date as :number' do
date = Date.today
obj = klass.create(sent_at: date)
attributes = Dynamoid.adapter.get_item(klass.table_name, obj.hash_key)
expect(attributes[:sent_at]).to eql BigDecimal(format('%d.%09d', date.to_time.to_i, date.to_time.nsec))

expect(reload(obj).sent_at).to eq(date.to_time)
expect(raw_attributes(obj)[:sent_at]).to eql(BigDecimal(format('%d.%09d', date.to_time.to_i, date.to_time.nsec)))
obj = klass.create(sent_at: Date.new(2018, 7, 21))
expect(reload(obj).sent_at).to eq(DateTime.new(2018, 7, 21, 0, 0, 0))
expect(raw_attributes(obj)[:sent_at]).to eql(BigDecimal('1532131200.0'))
end

it 'does not loose precision and can be used as sort key', :bugfix do
Expand All @@ -114,27 +110,27 @@
end
end

context 'Stored in :string format' do
context 'Stored in :string ISO-8601 format' do
let(:klass) do
new_class do
field :sent_at, :datetime, store_as_string: true
end
end

it 'saves time as a :string and looses milliseconds' do
time = Time.now
time = DateTime.new(2018, 7, 24, 22, 4, 30)
obj = klass.create(sent_at: time)

expect(reload(obj).sent_at).to eq(time.change(usec: 0))
expect(raw_attributes(obj)[:sent_at]).to eql(time.iso8601)
expect(raw_attributes(obj)[:sent_at]).to eql('2018-07-24T22:04:30+00:00')
end

it 'saves date as :string' do
date = Date.today
it 'saves date as :string', application_timezone: :utc do
date = Date.new(2018, 7, 21)
obj = klass.create(sent_at: date)

expect(reload(obj).sent_at).to eq(date.to_time)
expect(raw_attributes(obj)[:sent_at]).to eql(date.to_time.iso8601)
expect(reload(obj).sent_at).to eq(DateTime.new(2018, 7, 21, 0, 0, 0, '+00:00'))
expect(raw_attributes(obj)[:sent_at]).to eql('2018-07-21T00:00:00+00:00')
end

it 'saves as :string if global option :store_date_time_as_string is true' do
Expand Down Expand Up @@ -213,8 +209,8 @@
field :signed_up_on, :date, store_as_string: true
end

obj = klass.create(signed_up_on: '25-09-2017'.to_date)
expect(reload(obj).signed_up_on).to eql('25-09-2017'.to_date)
obj = klass.create(signed_up_on: '2017-09-25'.to_date)
expect(reload(obj).signed_up_on).to eql('2017-09-25'.to_date)
expect(raw_attributes(obj)[:signed_up_on]).to eql('2017-09-25')
end

Expand All @@ -226,7 +222,7 @@
store_date_as_string = Dynamoid.config.store_date_as_string
Dynamoid.config.store_date_as_string = true

obj = klass.create(signed_up_on: '25-09-2017'.to_date)
obj = klass.create(signed_up_on: '2017-09-25'.to_date)
expect(raw_attributes(obj)[:signed_up_on]).to eql('2017-09-25')

Dynamoid.config.store_date_as_string = store_date_as_string
Expand All @@ -240,7 +236,7 @@
store_date_as_string = Dynamoid.config.store_date_as_string
Dynamoid.config.store_date_as_string = false

obj = klass.create(signed_up_on: '25-09-2017'.to_date)
obj = klass.create(signed_up_on: '2017-09-25'.to_date)
expect(raw_attributes(obj)[:signed_up_on]).to eql('2017-09-25')

Dynamoid.config.store_date_as_string = store_date_as_string
Expand All @@ -263,8 +259,8 @@
field :signed_up_on, :date, store_as_string: false
end

obj = klass.create(signed_up_on: '25-09-2017'.to_date)
expect(reload(obj).signed_up_on).to eql('25-09-2017'.to_date)
obj = klass.create(signed_up_on: '2017-09-25'.to_date)
expect(reload(obj).signed_up_on).to eql('2017-09-25'.to_date)
expect(raw_attributes(obj)[:signed_up_on]).to eql(17_434)
end

Expand All @@ -276,7 +272,7 @@
store_date_as_string = Dynamoid.config.store_date_as_string
Dynamoid.config.store_date_as_string = false

obj = klass.create(signed_up_on: '25-09-2017'.to_date)
obj = klass.create(signed_up_on: '2017-09-25'.to_date)
expect(raw_attributes(obj)[:signed_up_on]).to eql(17_434)

Dynamoid.config.store_date_as_string = store_date_as_string
Expand All @@ -290,7 +286,7 @@
store_date_as_string = Dynamoid.config.store_date_as_string
Dynamoid.config.store_date_as_string = true

obj = klass.create(signed_up_on: '25-09-2017'.to_date)
obj = klass.create(signed_up_on: '2017-09-25'.to_date)
expect(raw_attributes(obj)[:signed_up_on]).to eql(17_434)

Dynamoid.config.store_date_as_string = store_date_as_string
Expand Down Expand Up @@ -530,30 +526,6 @@
expect(raw_attributes(obj)[:count]).to eql(10)
end

it 'casts string value to integer' do
obj = klass.create(count: '10')
expect(reload(obj).count).to eql(10)
expect(raw_attributes(obj)[:count]).to eql(10)

expect do
klass.create(count: 'abc')
end.to raise_error(ArgumentError)
end

it 'casts numeric value to integer' do
obj = klass.create(count: 10.001)
expect(reload(obj).count).to eql(10)
expect(raw_attributes(obj)[:count]).to eql(10)

obj = klass.create(count: Rational(3, 2))
expect(reload(obj).count).to eql(1)
expect(raw_attributes(obj)[:count]).to eql(1)

obj = klass.create(count: BigDecimal('10'))
expect(reload(obj).count).to eql(10)
expect(raw_attributes(obj)[:count]).to eql(10)
end

it 'stores nil value' do
obj = klass.create(count: nil)
expect(reload(obj).count).to eql(nil)
Expand Down
10 changes: 5 additions & 5 deletions spec/dynamoid/type_casting_spec.rb
Expand Up @@ -230,11 +230,11 @@

it 'converts to Set with #to_set method' do
obj = klass.new(items: ['milk'])
expect(obj.attributes[:items]).to eql(Set.new(['milk']))
expect(obj.items).to eql(Set.new(['milk']))

struct = Struct.new(:name, :address, :zip)
obj = klass.new(items: struct.new('Joe Smith', '123 Maple, Anytown NC', 12_345))
expect(obj.attributes[:items]).to eql(Set.new(['Joe Smith', '123 Maple, Anytown NC', 12_345]))
expect(obj.items).to eql(Set.new(['Joe Smith', '123 Maple, Anytown NC', 12_345]))
end

it 'converts any random object to nil' do
Expand Down Expand Up @@ -266,14 +266,14 @@

it 'converts to Array with #to_a method' do
obj = klass.new(items: Set.new(['milk']))
expect(obj.attributes[:items]).to eql(['milk'])
expect(obj.items).to eql(['milk'])

obj = klass.new(items: { 'milk' => 13.60 })
expect(obj.attributes[:items]).to eql([['milk', 13.6]])
expect(obj.items).to eql([['milk', 13.6]])

struct = Struct.new(:name, :address, :zip)
obj = klass.new(items: struct.new('Joe Smith', '123 Maple, Anytown NC', 12_345))
expect(obj.attributes[:items]).to eql(['Joe Smith', '123 Maple, Anytown NC', 12_345])
expect(obj.items).to eql(['Joe Smith', '123 Maple, Anytown NC', 12_345])
end

it 'converts any random object to nil' do
Expand Down

0 comments on commit 89b5066

Please sign in to comment.