aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/domain_test.rb
blob: ba25a64fac89ba2a78f904a312497cdc91376222 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
require "cases/helper"
require 'support/postgresql_helper'
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'

class PostgresqlDomainTest < ActiveRecord::TestCase
  include PostgresqlHelper

  class PostgresqlDomain < ActiveRecord::Base
    self.table_name = "postgresql_domains"
  end

  def setup
    @connection = ActiveRecord::Base.connection
    @connection.transaction do
      @connection.execute "CREATE DOMAIN custom_money as numeric(8,2)"
      @connection.create_table('postgresql_domains') do |t|
        t.column :price, :custom_money
      end
    end

    # reload type map after creating the enum type
    @connection.send(:reload_type_map)
  end

  teardown do
    @connection.execute 'DROP TABLE IF EXISTS postgresql_domains'
    @connection.execute 'DROP DOMAIN IF EXISTS custom_money'
    reset_pg_session
  end

  def test_column
    column = PostgresqlDomain.columns_hash["price"]
    assert_equal :decimal, column.type
    assert_equal "custom_money", column.sql_type
    assert column.number?
    assert_not column.text?
    assert_not column.binary?
    assert_not column.array
  end

  def test_domain_acts_like_basetype
    PostgresqlDomain.create price: ""
    record = PostgresqlDomain.first
    assert_nil record.price

    record.price = "34.15"
    record.save!

    assert_equal BigDecimal.new("34.15"), record.reload.price
  end
end