1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

pg, extract money tests into separate file.

- Added assertions about the column. Specifically scale.
- Move record insertion from setup into test method.
This commit is contained in:
Yves Senn 2014-05-21 09:46:37 +02:00
parent f69f20e4ff
commit 6b4f6d0064
2 changed files with 55 additions and 35 deletions

View file

@ -7,9 +7,6 @@ end
class PostgresqlTsvector < ActiveRecord::Base
end
class PostgresqlMoney < ActiveRecord::Base
end
class PostgresqlNumber < ActiveRecord::Base
end
@ -36,17 +33,11 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
def setup
@connection = ActiveRecord::Base.connection
@connection.execute("set lc_monetary = 'C'")
@connection.execute("INSERT INTO postgresql_tsvectors (id, text_vector) VALUES (1, ' ''text'' ''vector'' ')")
@first_tsvector = PostgresqlTsvector.find(1)
@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)
@connection.execute("INSERT INTO postgresql_numbers (id, single, double) VALUES (1, 123.456, 123456.789)")
@connection.execute("INSERT INTO postgresql_numbers (id, single, double) VALUES (2, '-Infinity', 'Infinity')")
@connection.execute("INSERT INTO postgresql_numbers (id, single, double) VALUES (3, 123.456, 'NaN')")
@ -70,7 +61,7 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
end
teardown do
[PostgresqlTsvector, PostgresqlMoney, PostgresqlNumber, PostgresqlTime, PostgresqlNetworkAddress,
[PostgresqlTsvector, PostgresqlNumber, PostgresqlTime, PostgresqlNetworkAddress,
PostgresqlBitString, PostgresqlOid, PostgresqlTimestampWithZone].each(&:delete_all)
end
@ -78,10 +69,6 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
assert_equal :tsvector, @first_tsvector.column_for_attribute(:text_vector).type
end
def test_data_type_of_money_types
assert_equal :decimal, @first_money.column_for_attribute(:wealth).type
end
def test_data_type_of_number_types
assert_equal :float, @first_number.column_for_attribute(:single).type
assert_equal :float, @first_number.column_for_attribute(:double).type
@ -111,19 +98,6 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
assert_equal "'text' 'vector'", @first_tsvector.text_vector
end
def test_money_values
assert_equal 567.89, @first_money.wealth
assert_equal(-567.89, @second_money.wealth)
end
def test_money_type_cast
column = PostgresqlMoney.columns_hash['wealth']
assert_equal(12345678.12, column.type_cast("$12,345,678.12"))
assert_equal(12345678.12, column.type_cast("$12.345.678,12"))
assert_equal(-1.15, column.type_cast("-$1.15"))
assert_equal(-2.25, column.type_cast("($2.25)"))
end
def test_update_tsvector
new_text_vector = "'new' 'text' 'vector'"
@first_tsvector.text_vector = new_text_vector
@ -166,14 +140,6 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase
assert_equal 1234, @first_oid.obj_id
end
def test_update_money
new_value = BigDecimal.new('123.45')
@first_money.wealth = new_value
assert @first_money.save
assert @first_money.reload
assert_equal new_value, @first_money.wealth
end
def test_update_number
new_single = 789.012
new_double = 789012.345

View file

@ -0,0 +1,54 @@
# encoding: utf-8
require "cases/helper"
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
class PostgresqlByteaTest < ActiveRecord::TestCase
class PostgresqlMoney < ActiveRecord::Base; end
setup do
@connection = ActiveRecord::Base.connection
@connection.execute("set lc_monetary = 'C'")
end
def test_column
column = PostgresqlMoney.columns_hash["wealth"]
assert_equal :decimal, column.type
assert_equal "money", column.sql_type
assert_equal 2, column.scale
assert column.number?
assert_not column.text?
assert_not column.binary?
assert_not column.array
end
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
column = PostgresqlMoney.columns_hash['wealth']
assert_equal(12345678.12, column.type_cast("$12,345,678.12"))
assert_equal(12345678.12, column.type_cast("$12.345.678,12"))
assert_equal(-1.15, column.type_cast("-$1.15"))
assert_equal(-2.25, column.type_cast("($2.25)"))
end
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
end