Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activerecord / test / schema_dumper_test.rb
blobc6041e4f5c0197334bca11d48cdb238c73aa3c44
1 require 'abstract_unit'
2 require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper"
3 require 'stringio'
5 if ActiveRecord::Base.connection.respond_to?(:tables)
7   class SchemaDumperTest < Test::Unit::TestCase
8     def standard_dump
9       stream = StringIO.new
10       ActiveRecord::SchemaDumper.ignore_tables = []
11       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
12       stream.string
13     end
14     
15     def test_schema_dump
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
20     end
21     
22     def test_schema_dump_excludes_sqlite_sequence
23       output = standard_dump
24       assert_no_match %r{create_table "sqlite_sequence"}, output
25     end
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
31       matches.compact!
32       return assert(true) if matches.empty?
33       assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
34     end
36     def column_definition_lines(output = standard_dump)
37       output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
38     end
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+"/)
46             match[0].length
47           end
48         end
50         assert_equal 1, lengths.uniq.length
51       end
52     end
53     
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 => /)
59       end
60     end
61     
62     def test_no_dump_errors
63       output = standard_dump
64       assert_no_match %r{\# Could not dump table}, output
65     end
66     
67     def test_schema_dump_includes_not_null_columns
68       stream = StringIO.new
69       
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
74     end
76     def test_schema_dump_with_string_ignored_table
77       stream = StringIO.new
78       
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
85     end
88     def test_schema_dump_with_regexp_ignored_table
89       stream = StringIO.new
90       
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
97     end
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)
105       end
106     end
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
112       end
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"
119       end
120     end
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
128     end
129   end