2005-09-23 09:29:33 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper"
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
if ActiveRecord::Base.connection.respond_to?(:tables)
|
|
|
|
|
2005-10-29 14:40:49 -04:00
|
|
|
class SchemaDumperTest < Test::Unit::TestCase
|
2006-04-26 02:15:51 -04:00
|
|
|
def standard_dump
|
2005-10-29 14:40:49 -04:00
|
|
|
stream = StringIO.new
|
2006-07-08 16:35:56 -04:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = []
|
2005-10-29 14:40:49 -04:00
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
2006-04-26 02:15:51 -04:00
|
|
|
stream.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump
|
|
|
|
output = standard_dump
|
2005-10-29 14:40:49 -04:00
|
|
|
assert_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_info"}, output
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2006-03-18 16:27:40 -05:00
|
|
|
|
2006-04-26 02:15:51 -04:00
|
|
|
def assert_line_up(lines, pattern, required = false)
|
|
|
|
return assert(true) if lines.empty?
|
|
|
|
matches = lines.map { |line| line.match(pattern) }
|
|
|
|
assert matches.all? if required
|
|
|
|
matches.compact!
|
|
|
|
return assert(true) if matches.empty?
|
|
|
|
assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_arguments_line_up
|
|
|
|
output = standard_dump
|
|
|
|
output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }.each do |column_set|
|
2006-07-08 16:35:56 -04:00
|
|
|
assert_line_up(column_set, /:(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)/, true)
|
2006-04-26 02:15:51 -04:00
|
|
|
assert_line_up(column_set, /:default => /)
|
|
|
|
assert_line_up(column_set, /:limit => /)
|
|
|
|
assert_line_up(column_set, /:null => /)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_dump_errors
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{\# Could not dump table}, output
|
|
|
|
end
|
|
|
|
|
2006-03-18 16:27:40 -05:00
|
|
|
def test_schema_dump_includes_not_null_columns
|
|
|
|
stream = StringIO.new
|
|
|
|
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^[^s]/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_match %r{:null => false}, output
|
|
|
|
end
|
2005-12-24 11:55:55 -05:00
|
|
|
|
|
|
|
def test_schema_dump_with_string_ignored_table
|
|
|
|
stream = StringIO.new
|
|
|
|
|
2005-12-24 12:31:26 -05:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
|
2005-12-24 11:55:55 -05:00
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_no_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_info"}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_dump_with_regexp_ignored_table
|
|
|
|
stream = StringIO.new
|
|
|
|
|
2005-12-24 12:31:26 -05:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
|
2005-12-24 11:55:55 -05:00
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_no_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_info"}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_dump_illegal_ignored_table_value
|
|
|
|
stream = StringIO.new
|
2005-12-24 12:31:26 -05:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [5]
|
2005-12-24 11:55:55 -05:00
|
|
|
assert_raise(StandardError) do
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
end
|
|
|
|
end
|
2006-07-08 16:35:56 -04:00
|
|
|
|
|
|
|
def test_schema_dump_includes_decimal_options
|
|
|
|
stream = StringIO.new
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
2006-07-24 13:14:48 -04:00
|
|
|
assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
|
2006-07-08 16:35:56 -04:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2005-10-29 14:40:49 -04:00
|
|
|
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|