Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / schema_dumper_test.rb
1 require "cases/helper"
2 require 'active_record/schema_dumper'
3 require 'stringio'
4
5
6 class SchemaDumperTest < ActiveRecord::TestCase
7 def standard_dump
8 stream = StringIO.new
9 ActiveRecord::SchemaDumper.ignore_tables = []
10 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
11 stream.string
12 end
13
14 def test_schema_dump
15 output = standard_dump
16 assert_match %r{create_table "accounts"}, output
17 assert_match %r{create_table "authors"}, output
18 assert_no_match %r{create_table "schema_migrations"}, output
19 end
20
21 def test_schema_dump_excludes_sqlite_sequence
22 output = standard_dump
23 assert_no_match %r{create_table "sqlite_sequence"}, output
24 end
25
26 def assert_line_up(lines, pattern, required = false)
27 return assert(true) if lines.empty?
28 matches = lines.map { |line| line.match(pattern) }
29 assert matches.all? if required
30 matches.compact!
31 return assert(true) if matches.empty?
32 assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
33 end
34
35 def column_definition_lines(output = standard_dump)
36 output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
37 end
38
39 def test_types_line_up
40 column_definition_lines.each do |column_set|
41 next if column_set.empty?
42
43 lengths = column_set.map do |column|
44 if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
45 match[0].length
46 end
47 end
48
49 assert_equal 1, lengths.uniq.length
50 end
51 end
52
53 def test_arguments_line_up
54 column_definition_lines.each do |column_set|
55 assert_line_up(column_set, /:default => /)
56 assert_line_up(column_set, /:limit => /)
57 assert_line_up(column_set, /:null => /)
58 end
59 end
60
61 def test_no_dump_errors
62 output = standard_dump
63 assert_no_match %r{\# Could not dump table}, output
64 end
65
66 def test_schema_dump_includes_not_null_columns
67 stream = StringIO.new
68
69 ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
70 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
71 output = stream.string
72 assert_match %r{:null => false}, output
73 end
74
75 def test_schema_dump_includes_limit_constraint_for_integer_columns
76 stream = StringIO.new
77
78 ActiveRecord::SchemaDumper.ignore_tables = [/^(?!integer_limits)/]
79 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
80 output = stream.string
81
82 if current_adapter?(:PostgreSQLAdapter)
83 assert_match %r{c_int_1.*:limit => 2}, output
84 assert_match %r{c_int_2.*:limit => 2}, output
85
86 # int 3 is 4 bytes in postgresql
87 assert_match %r{c_int_3.*}, output
88 assert_no_match %r{c_int_3.*:limit}, output
89
90 assert_match %r{c_int_4.*}, output
91 assert_no_match %r{c_int_4.*:limit}, output
92 elsif current_adapter?(:MysqlAdapter)
93 assert_match %r{c_int_1.*:limit => 1}, output
94 assert_match %r{c_int_2.*:limit => 2}, output
95 assert_match %r{c_int_3.*:limit => 3}, output
96
97 assert_match %r{c_int_4.*}, output
98 assert_no_match %r{c_int_4.*:limit}, output
99 elsif current_adapter?(:SQLiteAdapter)
100 assert_match %r{c_int_1.*:limit => 1}, output
101 assert_match %r{c_int_2.*:limit => 2}, output
102 assert_match %r{c_int_3.*:limit => 3}, output
103 assert_match %r{c_int_4.*:limit => 4}, output
104 end
105 assert_match %r{c_int_without_limit.*}, output
106 assert_no_match %r{c_int_without_limit.*:limit}, output
107
108 if current_adapter?(:SQLiteAdapter)
109 assert_match %r{c_int_5.*:limit => 5}, output
110 assert_match %r{c_int_6.*:limit => 6}, output
111 assert_match %r{c_int_7.*:limit => 7}, output
112 assert_match %r{c_int_8.*:limit => 8}, output
113 else
114 assert_match %r{c_int_5.*:limit => 8}, output
115 assert_match %r{c_int_6.*:limit => 8}, output
116 assert_match %r{c_int_7.*:limit => 8}, output
117 assert_match %r{c_int_8.*:limit => 8}, output
118 end
119 end
120
121 def test_schema_dump_with_string_ignored_table
122 stream = StringIO.new
123
124 ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
125 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
126 output = stream.string
127 assert_no_match %r{create_table "accounts"}, output
128 assert_match %r{create_table "authors"}, output
129 assert_no_match %r{create_table "schema_migrations"}, output
130 end
131
132 def test_schema_dump_with_regexp_ignored_table
133 stream = StringIO.new
134
135 ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
136 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
137 output = stream.string
138 assert_no_match %r{create_table "accounts"}, output
139 assert_match %r{create_table "authors"}, output
140 assert_no_match %r{create_table "schema_migrations"}, output
141 end
142
143 def test_schema_dump_illegal_ignored_table_value
144 stream = StringIO.new
145 ActiveRecord::SchemaDumper.ignore_tables = [5]
146 assert_raise(StandardError) do
147 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
148 end
149 end
150
151 if current_adapter?(:MysqlAdapter)
152 def test_schema_dump_should_not_add_default_value_for_mysql_text_field
153 output = standard_dump
154 assert_match %r{t.text\s+"body",\s+:null => false$}, output
155 end
156
157 def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
158 output = standard_dump
159 match = output.match(%r{create_table "movies"(.*)do})
160 assert_not_nil(match, "nonstandardpk table not found")
161 assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
162 end
163
164 def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
165 output = standard_dump
166 assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
167 assert_match %r{t.binary\s+"normal_blob"$}, output
168 assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
169 assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
170 assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
171 assert_match %r{t.text\s+"normal_text"$}, output
172 assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
173 assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
174 end
175 end
176
177 def test_schema_dump_includes_decimal_options
178 stream = StringIO.new
179 ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
180 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
181 output = stream.string
182 assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
183 end
184 end