aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/serial_test.rb
blob: 8abe064bf158bb56b422f107a1899c63d38dacea (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
require "cases/helper"
require 'support/schema_dumping_helper'

class PostgresqlSerialTest < ActiveRecord::PostgreSQLTestCase
  include SchemaDumpingHelper

  class PostgresqlSerial < ActiveRecord::Base; end

  setup do
    @connection = ActiveRecord::Base.connection
    @connection.create_table "postgresql_serials", force: true do |t|
      t.serial :seq
      t.integer :serials_id, default: -> { "nextval('postgresql_serials_id_seq')" }
    end
  end

  teardown do
    @connection.drop_table "postgresql_serials", if_exists: true
  end

  def test_serial_column
    column = PostgresqlSerial.columns_hash["seq"]
    assert_equal :integer, column.type
    assert_equal "integer", column.sql_type
    assert column.serial?
  end

  def test_not_serial_column
    column = PostgresqlSerial.columns_hash["serials_id"]
    assert_equal :integer, column.type
    assert_equal "integer", column.sql_type
    assert_not column.serial?
  end

  def test_schema_dump_with_shorthand
    output = dump_table_schema "postgresql_serials"
    assert_match %r{t\.serial\s+"seq",\s+null: false$}, output
  end

  def test_schema_dump_with_not_serial
    output = dump_table_schema "postgresql_serials"
    assert_match %r{t\.integer\s+"serials_id",\s+default: -> \{ "nextval\('postgresql_serials_id_seq'::regclass\)" \}$}, output
  end
end

class PostgresqlBigSerialTest < ActiveRecord::PostgreSQLTestCase
  include SchemaDumpingHelper

  class PostgresqlBigSerial < ActiveRecord::Base; end

  setup do
    @connection = ActiveRecord::Base.connection
    @connection.create_table "postgresql_big_serials", force: true do |t|
      t.bigserial :seq
      t.bigint :serials_id, default: -> { "nextval('postgresql_big_serials_id_seq')" }
    end
  end

  teardown do
    @connection.drop_table "postgresql_big_serials", if_exists: true
  end

  def test_bigserial_column
    column = PostgresqlBigSerial.columns_hash["seq"]
    assert_equal :integer, column.type
    assert_equal "bigint", column.sql_type
    assert column.serial?
  end

  def test_not_bigserial_column
    column = PostgresqlBigSerial.columns_hash["serials_id"]
    assert_equal :integer, column.type
    assert_equal "bigint", column.sql_type
    assert_not column.serial?
  end

  def test_schema_dump_with_shorthand
    output = dump_table_schema "postgresql_big_serials"
    assert_match %r{t\.bigserial\s+"seq",\s+null: false$}, output
  end

  def test_schema_dump_with_not_bigserial
    output = dump_table_schema "postgresql_big_serials"
    assert_match %r{t\.bigint\s+"serials_id",\s+default: -> \{ "nextval\('postgresql_big_serials_id_seq'::regclass\)" \}$}, output
  end
end