2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-22 01:11:45 -05:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/numeric_data"
|
|
|
|
|
|
|
|
class NumericDataTest < ActiveRecord::TestCase
|
|
|
|
def test_big_decimal_conditions
|
|
|
|
m = NumericData.new(
|
|
|
|
bank_balance: 1586.43,
|
|
|
|
big_bank_balance: BigDecimal("1000234000567.95"),
|
|
|
|
world_population: 6000000000,
|
|
|
|
my_house_population: 3
|
|
|
|
)
|
|
|
|
assert m.save
|
|
|
|
assert_equal 0, NumericData.where("bank_balance > ?", 2000.0).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_numeric_fields
|
|
|
|
m = NumericData.new(
|
|
|
|
bank_balance: 1586.43,
|
|
|
|
big_bank_balance: BigDecimal("1000234000567.95"),
|
2018-03-03 14:25:29 -05:00
|
|
|
world_population: 2**62,
|
2016-12-22 01:11:45 -05:00
|
|
|
my_house_population: 3
|
|
|
|
)
|
|
|
|
assert m.save
|
|
|
|
|
2018-11-11 13:39:28 -05:00
|
|
|
m1 = NumericData.find_by(
|
|
|
|
bank_balance: 1586.43,
|
|
|
|
big_bank_balance: BigDecimal("1000234000567.95")
|
|
|
|
)
|
2016-12-22 01:11:45 -05:00
|
|
|
|
|
|
|
assert_kind_of Integer, m1.world_population
|
2018-03-03 14:25:29 -05:00
|
|
|
assert_equal 2**62, m1.world_population
|
2016-12-22 01:11:45 -05:00
|
|
|
|
|
|
|
assert_kind_of Integer, m1.my_house_population
|
|
|
|
assert_equal 3, m1.my_house_population
|
|
|
|
|
|
|
|
assert_kind_of BigDecimal, m1.bank_balance
|
|
|
|
assert_equal BigDecimal("1586.43"), m1.bank_balance
|
|
|
|
|
|
|
|
assert_kind_of BigDecimal, m1.big_bank_balance
|
|
|
|
assert_equal BigDecimal("1000234000567.95"), m1.big_bank_balance
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_numeric_fields_with_scale
|
|
|
|
m = NumericData.new(
|
|
|
|
bank_balance: 1586.43122334,
|
|
|
|
big_bank_balance: BigDecimal("234000567.952344"),
|
2018-03-03 14:25:29 -05:00
|
|
|
world_population: 2**62,
|
2016-12-22 01:11:45 -05:00
|
|
|
my_house_population: 3
|
|
|
|
)
|
|
|
|
assert m.save
|
|
|
|
|
2018-11-11 13:39:28 -05:00
|
|
|
m1 = NumericData.find_by(
|
|
|
|
bank_balance: 1586.43122334,
|
|
|
|
big_bank_balance: BigDecimal("234000567.952344")
|
|
|
|
)
|
2016-12-22 01:11:45 -05:00
|
|
|
|
|
|
|
assert_kind_of Integer, m1.world_population
|
2018-03-03 14:25:29 -05:00
|
|
|
assert_equal 2**62, m1.world_population
|
2016-12-22 01:11:45 -05:00
|
|
|
|
|
|
|
assert_kind_of Integer, m1.my_house_population
|
|
|
|
assert_equal 3, m1.my_house_population
|
|
|
|
|
|
|
|
assert_kind_of BigDecimal, m1.bank_balance
|
|
|
|
assert_equal BigDecimal("1586.43"), m1.bank_balance
|
|
|
|
|
|
|
|
assert_kind_of BigDecimal, m1.big_bank_balance
|
|
|
|
assert_equal BigDecimal("234000567.95"), m1.big_bank_balance
|
|
|
|
end
|
2018-11-08 09:55:09 -05:00
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_numeric_fields_with_nan
|
|
|
|
m = NumericData.new(
|
|
|
|
bank_balance: BigDecimal("NaN"),
|
|
|
|
big_bank_balance: BigDecimal("NaN"),
|
|
|
|
world_population: 2**62,
|
|
|
|
my_house_population: 3
|
|
|
|
)
|
|
|
|
assert_predicate m.bank_balance, :nan?
|
|
|
|
assert_predicate m.big_bank_balance, :nan?
|
|
|
|
assert m.save
|
|
|
|
|
|
|
|
m1 = NumericData.find_by(
|
|
|
|
bank_balance: BigDecimal("NaN"),
|
|
|
|
big_bank_balance: BigDecimal("NaN")
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_predicate m1.bank_balance, :nan?
|
|
|
|
assert_predicate m1.big_bank_balance, :nan?
|
|
|
|
end
|
|
|
|
end
|
2016-12-22 01:11:45 -05:00
|
|
|
end
|