forked from matthuhiggins/foreigner
/
mysql_adapter_test.rb
83 lines (70 loc) · 2.55 KB
/
mysql_adapter_test.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
require File.dirname(__FILE__) + '/helper'
class MysqlAdapterTest < ActiveRecord::TestCase
include Foreigner::MysqlAdapter
def test_add_without_options
assert_equal(
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
add_foreign_key(:employees, :companies)
)
end
def test_add_with_name
assert_equal(
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id)",
add_foreign_key(:employees, :companies, :name => 'favorite_company_fk')
)
end
def test_add_with_column
assert_equal(
"ALTER TABLE `employees` ADD CONSTRAINT `employees_last_employer_id_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
add_foreign_key(:employees, :companies, :column => 'last_employer_id')
)
end
def test_add_with_column_and_name
assert_equal(
"ALTER TABLE `employees` ADD CONSTRAINT `favorite_company_fk` FOREIGN KEY (`last_employer_id`) REFERENCES `companies`(id)",
add_foreign_key(:employees, :companies, :column => 'last_employer_id', :name => 'favorite_company_fk')
)
end
def test_add_with_delete_dependency
assert_equal(
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
"ON DELETE CASCADE",
add_foreign_key(:employees, :companies, :dependent => :delete)
)
end
def test_add_with_nullify_dependency
assert_equal(
"ALTER TABLE `employees` ADD CONSTRAINT `employees_company_id_fk` FOREIGN KEY (`company_id`) REFERENCES `companies`(id) " +
"ON DELETE SET NULL",
add_foreign_key(:employees, :companies, :dependent => :nullify)
)
end
def test_remove_by_table
assert_equal(
"ALTER TABLE `suppliers` DROP FOREIGN KEY `suppliers_company_id_fk`",
remove_foreign_key(:suppliers, :companies)
)
end
def test_remove_by_name
assert_equal(
"ALTER TABLE `suppliers` DROP FOREIGN KEY `belongs_to_supplier`",
remove_foreign_key(:suppliers, :name => "belongs_to_supplier")
)
end
def test_remove_by_column
assert_equal(
"ALTER TABLE `suppliers` DROP FOREIGN KEY `suppliers_ship_to_id_fk`",
remove_foreign_key(:suppliers, :column => "ship_to_id")
)
end
private
def execute(sql, name = nil)
sql
end
def quote_column_name(name)
"`#{name}`"
end
def quote_table_name(name)
quote_column_name(name).gsub('.', '`.`')
end
end