diff options
Diffstat (limited to 'activerecord/test/cases/tasks/sqlite_rake_test.rb')
-rw-r--r-- | activerecord/test/cases/tasks/sqlite_rake_test.rb | 321 |
1 files changed, 174 insertions, 147 deletions
diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb index 0aea0c3b38..141048bfe7 100644 --- a/activerecord/test/cases/tasks/sqlite_rake_test.rb +++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb @@ -1,193 +1,220 @@ -require 'cases/helper' -require 'pathname' +require "cases/helper" +require "active_record/tasks/database_tasks" +require "pathname" if current_adapter?(:SQLite3Adapter) -module ActiveRecord - class SqliteDBCreateTest < ActiveRecord::TestCase - def setup - @database = 'db_create.sqlite3' - @connection = stub :connection - @configuration = { - 'adapter' => 'sqlite3', - 'database' => @database - } - - File.stubs(:exist?).returns(false) - ActiveRecord::Base.stubs(:connection).returns(@connection) - ActiveRecord::Base.stubs(:establish_connection).returns(true) - end + module ActiveRecord + class SqliteDBCreateTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @connection = stub :connection + @configuration = { + "adapter" => "sqlite3", + "database" => @database + } + + File.stubs(:exist?).returns(false) + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + + $stdout, @original_stdout = StringIO.new, $stdout + $stderr, @original_stderr = StringIO.new, $stderr + end - def test_db_checks_database_exists - File.expects(:exist?).with(@database).returns(false) + def teardown + $stdout, $stderr = @original_stdout, @original_stderr + end - ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' - end + def test_db_checks_database_exists + File.expects(:exist?).with(@database).returns(false) - def test_db_create_when_file_exists - File.stubs(:exist?).returns(true) + ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" + end - $stderr.expects(:puts).with("#{@database} already exists") + def test_when_db_created_successfully_outputs_info_to_stdout + ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" - ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' - end + assert_equal $stdout.string, "Created database '#{@database}'\n" + end - def test_db_create_with_file_does_nothing - File.stubs(:exist?).returns(true) - $stderr.stubs(:puts).returns(nil) + def test_db_create_when_file_exists + File.stubs(:exist?).returns(true) - ActiveRecord::Base.expects(:establish_connection).never + ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" - ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' - end + assert_equal $stderr.string, "Database '#{@database}' already exists\n" + end - def test_db_create_establishes_a_connection - ActiveRecord::Base.expects(:establish_connection).with(@configuration) + def test_db_create_with_file_does_nothing + File.stubs(:exist?).returns(true) + $stderr.stubs(:puts).returns(nil) - ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' - end + ActiveRecord::Base.expects(:establish_connection).never - def test_db_create_with_error_prints_message - ActiveRecord::Base.stubs(:establish_connection).raises(Exception) + ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" + end - $stderr.stubs(:puts).returns(true) - $stderr.expects(:puts). - with("Couldn't create database for #{@configuration.inspect}") + def test_db_create_establishes_a_connection + ActiveRecord::Base.expects(:establish_connection).with(@configuration) - assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' } - end - end + ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" + end - class SqliteDBDropTest < ActiveRecord::TestCase - def setup - @database = "db_create.sqlite3" - @path = stub(:to_s => '/absolute/path', :absolute? => true) - @configuration = { - 'adapter' => 'sqlite3', - 'database' => @database - } - - Pathname.stubs(:new).returns(@path) - File.stubs(:join).returns('/former/relative/path') - FileUtils.stubs(:rm).returns(true) - end + def test_db_create_with_error_prints_message + ActiveRecord::Base.stubs(:establish_connection).raises(Exception) - def test_creates_path_from_database - Pathname.expects(:new).with(@database).returns(@path) + $stderr.stubs(:puts).returns(true) + $stderr.expects(:puts). + with("Couldn't create database for #{@configuration.inspect}") - ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' + assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" } + end end - def test_removes_file_with_absolute_path - File.stubs(:exist?).returns(true) - @path.stubs(:absolute?).returns(true) + class SqliteDBDropTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @path = stub(to_s: "/absolute/path", absolute?: true) + @configuration = { + "adapter" => "sqlite3", + "database" => @database + } - FileUtils.expects(:rm).with('/absolute/path') + Pathname.stubs(:new).returns(@path) + File.stubs(:join).returns("/former/relative/path") + FileUtils.stubs(:rm).returns(true) - ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' - end + $stdout, @original_stdout = StringIO.new, $stdout + $stderr, @original_stderr = StringIO.new, $stderr + end - def test_generates_absolute_path_with_given_root - @path.stubs(:absolute?).returns(false) + def teardown + $stdout, $stderr = @original_stdout, @original_stderr + end - File.expects(:join).with('/rails/root', @path). - returns('/former/relative/path') + def test_creates_path_from_database + Pathname.expects(:new).with(@database).returns(@path) - ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' - end + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root" + end - def test_removes_file_with_relative_path - File.stubs(:exist?).returns(true) - @path.stubs(:absolute?).returns(false) + def test_removes_file_with_absolute_path + File.stubs(:exist?).returns(true) + @path.stubs(:absolute?).returns(true) - FileUtils.expects(:rm).with('/former/relative/path') + FileUtils.expects(:rm).with("/absolute/path") - ActiveRecord::Tasks::DatabaseTasks.drop @configuration, '/rails/root' - end - end + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root" + end - class SqliteDBCharsetTest < ActiveRecord::TestCase - def setup - @database = 'db_create.sqlite3' - @connection = stub :connection - @configuration = { - 'adapter' => 'sqlite3', - 'database' => @database - } - - File.stubs(:exist?).returns(false) - ActiveRecord::Base.stubs(:connection).returns(@connection) - ActiveRecord::Base.stubs(:establish_connection).returns(true) - end + def test_generates_absolute_path_with_given_root + @path.stubs(:absolute?).returns(false) - def test_db_retrieves_charset - @connection.expects(:encoding) - ActiveRecord::Tasks::DatabaseTasks.charset @configuration, '/rails/root' - end - end + File.expects(:join).with("/rails/root", @path). + returns("/former/relative/path") - class SqliteDBCollationTest < ActiveRecord::TestCase - def setup - @database = 'db_create.sqlite3' - @connection = stub :connection - @configuration = { - 'adapter' => 'sqlite3', - 'database' => @database - } - - File.stubs(:exist?).returns(false) - ActiveRecord::Base.stubs(:connection).returns(@connection) - ActiveRecord::Base.stubs(:establish_connection).returns(true) - end + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root" + end + + def test_removes_file_with_relative_path + File.stubs(:exist?).returns(true) + @path.stubs(:absolute?).returns(false) - def test_db_retrieves_collation - assert_raise NoMethodError do - ActiveRecord::Tasks::DatabaseTasks.collation @configuration, '/rails/root' + FileUtils.expects(:rm).with("/former/relative/path") + + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root" + end + + def test_when_db_dropped_successfully_outputs_info_to_stdout + ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root" + + assert_equal $stdout.string, "Dropped database '#{@database}'\n" end end - end - class SqliteStructureDumpTest < ActiveRecord::TestCase - def setup - @database = "db_create.sqlite3" - @configuration = { - 'adapter' => 'sqlite3', - 'database' => @database - } + class SqliteDBCharsetTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @connection = stub :connection + @configuration = { + "adapter" => "sqlite3", + "database" => @database + } + + File.stubs(:exist?).returns(false) + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_charset + @connection.expects(:encoding) + ActiveRecord::Tasks::DatabaseTasks.charset @configuration, "/rails/root" + end end - def test_structure_dump - dbfile = @database - filename = "awesome-file.sql" + class SqliteDBCollationTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @connection = stub :connection + @configuration = { + "adapter" => "sqlite3", + "database" => @database + } - ActiveRecord::Tasks::DatabaseTasks.structure_dump @configuration, filename, '/rails/root' - assert File.exist?(dbfile) - assert File.exist?(filename) - ensure - FileUtils.rm_f(filename) - FileUtils.rm_f(dbfile) + File.stubs(:exist?).returns(false) + ActiveRecord::Base.stubs(:connection).returns(@connection) + ActiveRecord::Base.stubs(:establish_connection).returns(true) + end + + def test_db_retrieves_collation + assert_raise NoMethodError do + ActiveRecord::Tasks::DatabaseTasks.collation @configuration, "/rails/root" + end + end end - end - class SqliteStructureLoadTest < ActiveRecord::TestCase - def setup - @database = "db_create.sqlite3" - @configuration = { - 'adapter' => 'sqlite3', - 'database' => @database - } + class SqliteStructureDumpTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @configuration = { + "adapter" => "sqlite3", + "database" => @database + } + end + + def test_structure_dump + dbfile = @database + filename = "awesome-file.sql" + + ActiveRecord::Tasks::DatabaseTasks.structure_dump @configuration, filename, "/rails/root" + assert File.exist?(dbfile) + assert File.exist?(filename) + ensure + FileUtils.rm_f(filename) + FileUtils.rm_f(dbfile) + end end - def test_structure_load - dbfile = @database - filename = "awesome-file.sql" + class SqliteStructureLoadTest < ActiveRecord::TestCase + def setup + @database = "db_create.sqlite3" + @configuration = { + "adapter" => "sqlite3", + "database" => @database + } + end + + def test_structure_load + dbfile = @database + filename = "awesome-file.sql" - open(filename, 'w') { |f| f.puts("select datetime('now', 'localtime');") } - ActiveRecord::Tasks::DatabaseTasks.structure_load @configuration, filename, '/rails/root' - assert File.exist?(dbfile) - ensure - FileUtils.rm_f(filename) - FileUtils.rm_f(dbfile) + open(filename, "w") { |f| f.puts("select datetime('now', 'localtime');") } + ActiveRecord::Tasks::DatabaseTasks.structure_load @configuration, filename, "/rails/root" + assert File.exist?(dbfile) + ensure + FileUtils.rm_f(filename) + FileUtils.rm_f(dbfile) + end end end end -end |