aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/datatype_test.rb
blob: 0725fde5ae93248c71279e35a7a9d0b2c930ab2b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require "cases/helper"
require "support/ddl_helper"

class PostgresqlTime < ActiveRecord::Base
end

class PostgresqlOid < ActiveRecord::Base
end

class PostgresqlLtree < ActiveRecord::Base
end

class PostgresqlDataTypeTest < ActiveRecord::PostgreSQLTestCase
  self.use_transactional_tests = false

  def setup
    @connection = ActiveRecord::Base.connection

    @connection.execute("INSERT INTO postgresql_times (id, time_interval, scaled_time_interval) VALUES (1, '1 year 2 days ago', '3 weeks ago')")
    @first_time = PostgresqlTime.find(1)

    @connection.execute("INSERT INTO postgresql_oids (id, obj_id) VALUES (1, 1234)")
    @first_oid = PostgresqlOid.find(1)
  end

  teardown do
    [PostgresqlTime, PostgresqlOid].each(&:delete_all)
  end

  def test_data_type_of_time_types
    assert_equal :interval, @first_time.column_for_attribute(:time_interval).type
    assert_equal :interval, @first_time.column_for_attribute(:scaled_time_interval).type
  end

  def test_data_type_of_oid_types
    assert_equal :oid, @first_oid.column_for_attribute(:obj_id).type
  end

  def test_time_values
    assert_equal "-1 years -2 days", @first_time.time_interval
    assert_equal "-21 days", @first_time.scaled_time_interval
  end

  def test_oid_values
    assert_equal 1234, @first_oid.obj_id
  end

  def test_update_time
    @first_time.time_interval = "2 years 3 minutes"
    assert @first_time.save
    assert @first_time.reload
    assert_equal "2 years 00:03:00", @first_time.time_interval
  end

  def test_update_oid
    new_value = 567890
    @first_oid.obj_id = new_value
    assert @first_oid.save
    assert @first_oid.reload
    assert_equal new_value, @first_oid.obj_id
  end

  def test_text_columns_are_limitless_the_upper_limit_is_one_GB
    assert_equal "text", @connection.type_to_sql(:text, limit: 100_000)
    assert_raise ActiveRecord::ActiveRecordError do
      @connection.type_to_sql(:text, limit: 4294967295)
    end
  end
end

class PostgresqlInternalDataTypeTest < ActiveRecord::PostgreSQLTestCase
  include DdlHelper

  setup do
    @connection = ActiveRecord::Base.connection
  end

  def test_name_column_type
    with_example_table @connection, "ex", "data name" do
      column = @connection.columns("ex").find { |col| col.name == "data" }
      assert_equal :string, column.type
    end
  end

  def test_char_column_type
    with_example_table @connection, "ex", 'data "char"' do
      column = @connection.columns("ex").find { |col| col.name == "data" }
      assert_equal :string, column.type
    end
  end
end