class PostgresqlMoneyTest

Public Instance Methods

test_column() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 24
def test_column
  column = PostgresqlMoney.columns_hash["wealth"]
  assert_equal :money, column.type
  assert_equal "money", column.sql_type
  assert_equal 2, column.scale
  assert_not column.array?

  type = PostgresqlMoney.type_for_attribute("wealth")
  assert_not type.binary?
end
test_create_and_update_money() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 64
def test_create_and_update_money
  money = PostgresqlMoney.create(wealth: "987.65".dup)
  assert_equal 987.65, money.wealth

  new_value = BigDecimal.new("123.45")
  money.wealth = new_value
  money.save!
  money.reload
  assert_equal new_value, money.wealth
end
test_default() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 35
def test_default
  assert_equal BigDecimal.new("150.55"), PostgresqlMoney.column_defaults["depth"]
  assert_equal BigDecimal.new("150.55"), PostgresqlMoney.new.depth
end
test_money_type_cast() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 50
def test_money_type_cast
  type = PostgresqlMoney.type_for_attribute("wealth")
  assert_equal(12345678.12, type.cast("$12,345,678.12".dup))
  assert_equal(12345678.12, type.cast("$12.345.678,12".dup))
  assert_equal(-1.15, type.cast("-$1.15".dup))
  assert_equal(-2.25, type.cast("($2.25)".dup))
end
test_money_values() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 40
def test_money_values
  @connection.execute("INSERT INTO postgresql_moneys (id, wealth) VALUES (1, '567.89'::money)")
  @connection.execute("INSERT INTO postgresql_moneys (id, wealth) VALUES (2, '-567.89'::money)")

  first_money = PostgresqlMoney.find(1)
  second_money = PostgresqlMoney.find(2)
  assert_equal 567.89, first_money.wealth
  assert_equal(-567.89, second_money.wealth)
end
test_schema_dumping() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 58
def test_schema_dumping
  output = dump_table_schema("postgresql_moneys")
  assert_match %r{t\.money\s+"wealth",\s+scale: 2$}, output
  assert_match %r{t\.money\s+"depth",\s+scale: 2,\s+default: "150\.55"$}, output
end
test_update_all_with_money_big_decimal() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 83
def test_update_all_with_money_big_decimal
  money = PostgresqlMoney.create!
  PostgresqlMoney.update_all(wealth: "123.45".to_d)
  money.reload

  assert_equal 123.45, money.wealth
end
test_update_all_with_money_numeric() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 91
def test_update_all_with_money_numeric
  money = PostgresqlMoney.create!
  PostgresqlMoney.update_all(wealth: 123.45)
  money.reload

  assert_equal 123.45, money.wealth
end
test_update_all_with_money_string() click to toggle source
# File activerecord/test/cases/adapters/postgresql/money_test.rb, line 75
def test_update_all_with_money_string
  money = PostgresqlMoney.create!
  PostgresqlMoney.update_all(wealth: "987.65")
  money.reload

  assert_equal 987.65, money.wealth
end