money_test.rb 2.8 KB
Newer Older
1
require "cases/helper"
2
require 'support/schema_dumping_helper'
3

4
class PostgresqlMoneyTest < ActiveRecord::TestCase
5 6
  include SchemaDumpingHelper

7 8 9 10 11
  class PostgresqlMoney < ActiveRecord::Base; end

  setup do
    @connection = ActiveRecord::Base.connection
    @connection.execute("set lc_monetary = 'C'")
12
    @connection.create_table('postgresql_moneys', force: true) do |t|
13 14
      t.money "wealth"
      t.money "depth", default: "150.55"
15 16 17 18 19
    end
  end

  teardown do
    @connection.execute 'DROP TABLE IF EXISTS postgresql_moneys'
20 21 22 23
  end

  def test_column
    column = PostgresqlMoney.columns_hash["wealth"]
24
    assert_equal :money, column.type
25 26
    assert_equal "money", column.sql_type
    assert_equal 2, column.scale
27
    assert_not column.array?
28 29 30

    type = PostgresqlMoney.type_for_attribute("wealth")
    assert_not type.binary?
31 32
  end

33
  def test_default
34
    assert_equal BigDecimal.new("150.55"), PostgresqlMoney.column_defaults['depth']
35 36 37
    assert_equal BigDecimal.new("150.55"), PostgresqlMoney.new.depth
  end

38 39 40 41 42 43 44 45 46 47 48
  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

  def test_money_type_cast
49 50 51 52 53
    type = PostgresqlMoney.type_for_attribute('wealth')
    assert_equal(12345678.12, type.type_cast_from_user("$12,345,678.12"))
    assert_equal(12345678.12, type.type_cast_from_user("$12.345.678,12"))
    assert_equal(-1.15, type.type_cast_from_user("-$1.15"))
    assert_equal(-2.25, type.type_cast_from_user("($2.25)"))
54 55
  end

56 57 58
  def test_schema_dumping
    output = dump_table_schema("postgresql_moneys")
    assert_match %r{t\.money\s+"wealth",\s+scale: 2$}, output
59
    assert_match %r{t\.money\s+"depth",\s+scale: 2,\s+default: 150\.55$}, output
60 61
  end

62 63 64 65 66 67 68 69 70 71
  def test_create_and_update_money
    money = PostgresqlMoney.create(wealth: "987.65")
    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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  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

  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

  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
96
end