aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/datatype_test_postgresql.rb
blob: c4c3318de46bfdbc346c9d7a8096b005f4b4fda2 (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
require 'abstract_unit'

class PostgresqlDatatype < ActiveRecord::Base
end

class PGDataTypeTest < Test::Unit::TestCase
  self.use_transactional_fixtures = false

  TABLE_NAME = 'postgresql_datatypes'
  COLUMNS = [
    'id SERIAL PRIMARY KEY',
    'commission_by_quarter INTEGER[]',
    'nicknames TEXT[]'
  ]

  def setup
    @connection = ActiveRecord::Base.connection
    @connection.execute "CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
    @connection.execute "INSERT INTO #{TABLE_NAME} (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )"
    @first = PostgresqlDatatype.find( 1 )
  end

  def teardown
    @connection.execute "DROP TABLE #{TABLE_NAME}"
  end

  def test_data_type_of_array_types
    assert_equal :string, @first.column_for_attribute("commission_by_quarter").type
    assert_equal :string, @first.column_for_attribute("nicknames").type
  end

  def test_array_values
    assert_equal '{35000,21000,18000,17000}', @first.commission_by_quarter
    assert_equal '{foo,bar,baz}', @first.nicknames
  end

  def test_update_integer_array
    new_value = '{32800,95000,29350,17000}'
    assert @first.commission_by_quarter = new_value
    assert @first.save
    assert @first.reload
    assert_equal @first.commission_by_quarter, new_value
  end

  def test_update_text_array
    new_value = '{robby,robert,rob,robbie}'
    assert @first.nicknames = new_value
    assert @first.save
    assert @first.reload
    assert_equal @first.nicknames, new_value
  end
end