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