1 require 'abstract_unit'
2 require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper"
5 if ActiveRecord::Base.connection.respond_to?(:tables)
7 class SchemaDumperTest < Test::Unit::TestCase
10 ActiveRecord::SchemaDumper.ignore_tables = []
11 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
16 output = standard_dump
17 assert_match %r{create_table "accounts"}, output
18 assert_match %r{create_table "authors"}, output
19 assert_no_match %r{create_table "schema_info"}, output
22 def test_schema_dump_excludes_sqlite_sequence
23 output = standard_dump
24 assert_no_match %r{create_table "sqlite_sequence"}, output
27 def assert_line_up(lines, pattern, required = false)
28 return assert(true) if lines.empty?
29 matches = lines.map { |line| line.match(pattern) }
30 assert matches.all? if required
32 return assert(true) if matches.empty?
33 assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
36 def column_definition_lines(output = standard_dump)
37 output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
40 def test_types_line_up
41 column_definition_lines.each do |column_set|
42 next if column_set.empty?
44 lengths = column_set.map do |column|
45 if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
50 assert_equal 1, lengths.uniq.length
54 def test_arguments_line_up
55 column_definition_lines.each do |column_set|
56 assert_line_up(column_set, /:default => /)
57 assert_line_up(column_set, /:limit => /)
58 assert_line_up(column_set, /:null => /)
62 def test_no_dump_errors
63 output = standard_dump
64 assert_no_match %r{\# Could not dump table}, output
67 def test_schema_dump_includes_not_null_columns
70 ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
71 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
72 output = stream.string
73 assert_match %r{:null => false}, output
76 def test_schema_dump_with_string_ignored_table
79 ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
80 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
81 output = stream.string
82 assert_no_match %r{create_table "accounts"}, output
83 assert_match %r{create_table "authors"}, output
84 assert_no_match %r{create_table "schema_info"}, output
88 def test_schema_dump_with_regexp_ignored_table
91 ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
92 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
93 output = stream.string
94 assert_no_match %r{create_table "accounts"}, output
95 assert_match %r{create_table "authors"}, output
96 assert_no_match %r{create_table "schema_info"}, output
100 def test_schema_dump_illegal_ignored_table_value
101 stream = StringIO.new
102 ActiveRecord::SchemaDumper.ignore_tables = [5]
103 assert_raise(StandardError) do
104 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
108 if current_adapter?(:MysqlAdapter)
109 def test_schema_dump_should_not_add_default_value_for_mysql_text_field
110 output = standard_dump
111 assert_match %r{t.text\s+"body",\s+:default => "",\s+:null => false$}, output
114 def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
115 output = standard_dump
116 match = output.match(%r{create_table "movies"(.*)do})
117 assert_not_nil(match, "nonstandardpk table not found")
118 assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
122 def test_schema_dump_includes_decimal_options
123 stream = StringIO.new
124 ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
125 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
126 output = stream.string
127 assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output