Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / active_schema_test_mysql.rb
1 require "cases/helper"
2
3 class ActiveSchemaTest < ActiveRecord::TestCase
4 def setup
5 ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
6 alias_method :execute_without_stub, :execute
7 def execute(sql, name = nil) return sql end
8 end
9 end
10
11 def teardown
12 ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
13 remove_method :execute
14 alias_method :execute, :execute_without_stub
15 end
16 end
17
18 def test_drop_table
19 assert_equal "DROP TABLE `people`", drop_table(:people)
20 end
21
22 if current_adapter?(:MysqlAdapter)
23 def test_create_mysql_database_with_encoding
24 assert_equal "CREATE DATABASE `matt` DEFAULT CHARACTER SET `utf8`", create_database(:matt)
25 assert_equal "CREATE DATABASE `aimonetti` DEFAULT CHARACTER SET `latin1`", create_database(:aimonetti, {:charset => 'latin1'})
26 assert_equal "CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `big5` COLLATE `big5_chinese_ci`", create_database(:matt_aimonetti, {:charset => :big5, :collation => :big5_chinese_ci})
27 end
28
29 def test_recreate_mysql_database_with_encoding
30 create_database(:luca, {:charset => 'latin1'})
31 assert_equal "CREATE DATABASE `luca` DEFAULT CHARACTER SET `latin1`", recreate_database(:luca, {:charset => 'latin1'})
32 end
33 end
34
35 def test_add_column
36 assert_equal "ALTER TABLE `people` ADD `last_name` varchar(255)", add_column(:people, :last_name, :string)
37 end
38
39 def test_add_column_with_limit
40 assert_equal "ALTER TABLE `people` ADD `key` varchar(32)", add_column(:people, :key, :string, :limit => 32)
41 end
42
43 def test_drop_table_with_specific_database
44 assert_equal "DROP TABLE `otherdb`.`people`", drop_table('otherdb.people')
45 end
46
47 def test_add_timestamps
48 with_real_execute do
49 begin
50 ActiveRecord::Base.connection.create_table :delete_me do |t|
51 end
52 ActiveRecord::Base.connection.add_timestamps :delete_me
53 assert column_present?('delete_me', 'updated_at', 'datetime')
54 assert column_present?('delete_me', 'created_at', 'datetime')
55 ensure
56 ActiveRecord::Base.connection.drop_table :delete_me rescue nil
57 end
58 end
59 end
60
61 def test_remove_timestamps
62 with_real_execute do
63 begin
64 ActiveRecord::Base.connection.create_table :delete_me do |t|
65 t.timestamps
66 end
67 ActiveRecord::Base.connection.remove_timestamps :delete_me
68 assert !column_present?('delete_me', 'updated_at', 'datetime')
69 assert !column_present?('delete_me', 'created_at', 'datetime')
70 ensure
71 ActiveRecord::Base.connection.drop_table :delete_me rescue nil
72 end
73 end
74 end
75
76 private
77 def with_real_execute
78 #we need to actually modify some data, so we make execute point to the original method
79 ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
80 alias_method :execute_with_stub, :execute
81 alias_method :execute, :execute_without_stub
82 end
83 yield
84 ensure
85 #before finishing, we restore the alias to the mock-up method
86 ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
87 alias_method :execute, :execute_with_stub
88 end
89 end
90
91
92 def method_missing(method_symbol, *arguments)
93 ActiveRecord::Base.connection.send(method_symbol, *arguments)
94 end
95
96 def column_present?(table_name, column_name, type)
97 results = ActiveRecord::Base.connection.select_all("SHOW FIELDS FROM #{table_name} LIKE '#{column_name}'")
98 results.first && results.first['Type'] == type
99 end
100 end