-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathprimary_keys_test_sqlserver.rb
103 lines (79 loc) · 3.21 KB
/
primary_keys_test_sqlserver.rb
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
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true
require "cases/helper_sqlserver"
require "support/schema_dumping_helper"
class PrimaryKeyUuidTypeTest < ActiveRecord::TestCase
include SchemaDumpingHelper
self.use_transactional_tests = false
class Barcode < ActiveRecord::Base
end
setup do
@connection = ActiveRecord::Base.lease_connection
@connection.create_table(:barcodes, primary_key: "code", id: :uuid, force: true)
end
teardown do
@connection.drop_table(:barcodes, if_exists: true)
end
def test_any_type_primary_key
assert_equal "code", Barcode.primary_key
column = Barcode.column_for_attribute(Barcode.primary_key)
assert_not column.null
assert_equal :uuid, column.type
assert_not_predicate column, :is_identity?
assert_predicate column, :is_primary?
ensure
Barcode.reset_column_information
end
test "schema dump primary key includes default" do
schema = dump_table_schema "barcodes"
assert_match %r/create_table "barcodes", primary_key: "code", id: :uuid, default: -> { "newid\(\)" }/, schema
end
end
class PrimaryKeyIntegerTest < ActiveRecord::TestCase
include SchemaDumpingHelper
self.use_transactional_tests = false
class Barcode < ActiveRecord::Base
end
class Widget < ActiveRecord::Base
end
setup do
@connection = ActiveRecord::Base.lease_connection
end
teardown do
@connection.drop_table :barcodes, if_exists: true
@connection.drop_table :widgets, if_exists: true
end
test "integer primary key without default" do
@connection.create_table(:widgets, id: :integer, force: true)
column = @connection.columns(:widgets).find { |c| c.name == "id" }
assert_predicate column, :is_primary?
assert_predicate column, :is_identity?
assert_equal :integer, column.type
assert_not_predicate column, :bigint?
schema = dump_table_schema "widgets"
assert_match %r/create_table "widgets", id: :integer, force: :cascade do/, schema
end
test "bigint primary key without default" do
@connection.create_table(:widgets, id: :bigint, force: true)
column = @connection.columns(:widgets).find { |c| c.name == "id" }
assert_predicate column, :is_primary?
assert_predicate column, :is_identity?
assert_equal :integer, column.type
assert_predicate column, :bigint?
schema = dump_table_schema "widgets"
assert_match %r/create_table "widgets", force: :cascade do/, schema
end
test "don't set identity to integer and bigint when there is a default" do
@connection.create_table(:barcodes, id: :integer, default: nil, force: true)
@connection.create_table(:widgets, id: :bigint, default: nil, force: true)
column = @connection.columns(:widgets).find { |c| c.name == "id" }
assert_predicate column, :is_primary?
assert_not_predicate column, :is_identity?
schema = dump_table_schema "widgets"
assert_match %r/create_table "widgets", id: :bigint, default: nil, force: :cascade do/, schema
column = @connection.columns(:barcodes).find { |c| c.name == "id" }
assert_predicate column, :is_primary?
assert_not_predicate column, :is_identity?
schema = dump_table_schema "barcodes"
assert_match %r/create_table "barcodes", id: :integer, default: nil, force: :cascade do/, schema
end
end