mirror of
				https://github.com/DatabaseCleaner/database_cleaner
				synced 2023-03-27 23:22:03 -04:00 
			
		
		
		
	Add basic coverage for deletion strategy (ActiveRecord) in MySQL, PG
and SQLite
This commit is contained in:
		
							parent
							
								
									ef9e62adc3
								
							
						
					
					
						commit
						6bc0c1db4d
					
				
					 4 changed files with 144 additions and 0 deletions
				
			
		
							
								
								
									
										28
									
								
								spec/database_cleaner/active_record/deletion/mysql2_spec.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								spec/database_cleaner/active_record/deletion/mysql2_spec.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
require 'spec_helper'
 | 
			
		||||
require 'active_record'
 | 
			
		||||
require 'support/active_record/mysql2_setup'
 | 
			
		||||
require 'database_cleaner/active_record/deletion'
 | 
			
		||||
 | 
			
		||||
module ActiveRecord
 | 
			
		||||
  module AbstractMysqlAdapter
 | 
			
		||||
    describe do
 | 
			
		||||
      before(:all) { active_record_mysql2_setup }
 | 
			
		||||
 | 
			
		||||
      let(:connection) { active_record_mysql2_connection }
 | 
			
		||||
 | 
			
		||||
      subject { DatabaseCleaner::ActiveRecord::Deletion.new }
 | 
			
		||||
 | 
			
		||||
      describe "#delete_table" do
 | 
			
		||||
        it "should delete the table" do
 | 
			
		||||
          2.times { User.create }
 | 
			
		||||
 | 
			
		||||
          allow(subject).to receive(:tables_with_new_rows).and_return(["users"])
 | 
			
		||||
 | 
			
		||||
          subject.clean
 | 
			
		||||
 | 
			
		||||
          User.count.should eq 0
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										23
									
								
								spec/database_cleaner/active_record/deletion/mysql_spec.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								spec/database_cleaner/active_record/deletion/mysql_spec.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
require 'spec_helper'
 | 
			
		||||
require 'active_record'
 | 
			
		||||
require 'support/active_record/mysql_setup'
 | 
			
		||||
require 'database_cleaner/active_record/deletion'
 | 
			
		||||
 | 
			
		||||
module ActiveRecord
 | 
			
		||||
  module AbstractMysqlAdapter
 | 
			
		||||
    describe do
 | 
			
		||||
      before(:all) { active_record_mysql_setup }
 | 
			
		||||
 | 
			
		||||
      let(:connection) { active_record_mysql_connection }
 | 
			
		||||
 | 
			
		||||
      describe "#delete_table" do
 | 
			
		||||
        it "should delete the table" do
 | 
			
		||||
          2.times { User.create }
 | 
			
		||||
          DatabaseCleaner::ActiveRecord::Deletion.new.clean
 | 
			
		||||
 | 
			
		||||
          User.count.should eq 0
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,62 @@
 | 
			
		|||
require 'spec_helper'
 | 
			
		||||
require 'active_record'
 | 
			
		||||
require 'support/active_record/postgresql_setup'
 | 
			
		||||
require 'database_cleaner/active_record/deletion'
 | 
			
		||||
 | 
			
		||||
module ActiveRecord
 | 
			
		||||
  module ConnectionAdapters
 | 
			
		||||
    describe "schema_migrations table" do
 | 
			
		||||
      it "is not deleted" do
 | 
			
		||||
        active_record_pg_migrate
 | 
			
		||||
        DatabaseCleaner::ActiveRecord::Deletion.new.clean
 | 
			
		||||
        result = active_record_pg_connection.execute("select count(*) from schema_migrations;")
 | 
			
		||||
        result.values.first.should eq ["2"]
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    describe do
 | 
			
		||||
      before(:all) { active_record_pg_setup }
 | 
			
		||||
 | 
			
		||||
      let(:connection) do
 | 
			
		||||
        active_record_pg_connection
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      before(:each) do
 | 
			
		||||
        connection.tables.each do |table|
 | 
			
		||||
          connection.delete_table table
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      describe "#delete_table" do
 | 
			
		||||
        it "deletes the table" do
 | 
			
		||||
          2.times { User.create }
 | 
			
		||||
 | 
			
		||||
          connection.delete_table('users')
 | 
			
		||||
          User.count.should eq 0
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        it "deletes the table without id sequence" do
 | 
			
		||||
          2.times { Agent.create }
 | 
			
		||||
 | 
			
		||||
          connection.delete_table('agents')
 | 
			
		||||
          Agent.count.should eq 0
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      describe ":except option cleanup" do
 | 
			
		||||
        it "should not delete the tables specified in the :except option" do
 | 
			
		||||
          2.times { User.create }
 | 
			
		||||
 | 
			
		||||
          ::DatabaseCleaner::ActiveRecord::Deletion.new(:except => ['users']).clean
 | 
			
		||||
          expect( User.count ).to eq 2
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      describe '#database_cleaner_table_cache' do
 | 
			
		||||
        it 'should default to the list of tables with their schema' do
 | 
			
		||||
          connection.database_cleaner_table_cache.first.should match(/^public\./)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										31
									
								
								spec/database_cleaner/active_record/deletion/sqlite3_spec.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								spec/database_cleaner/active_record/deletion/sqlite3_spec.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,31 @@
 | 
			
		|||
require 'spec_helper'
 | 
			
		||||
require 'active_record'
 | 
			
		||||
require 'support/active_record/sqlite3_setup'
 | 
			
		||||
require 'database_cleaner/active_record/deletion'
 | 
			
		||||
 | 
			
		||||
module ActiveRecord
 | 
			
		||||
  module ConnectionAdapters
 | 
			
		||||
    describe do
 | 
			
		||||
      before(:all) { active_record_sqlite3_setup }
 | 
			
		||||
 | 
			
		||||
      let(:connection) do
 | 
			
		||||
        active_record_sqlite3_connection
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      before(:each) do
 | 
			
		||||
        connection.tables.each do |table|
 | 
			
		||||
          connection.delete_table table
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      describe "#delete_table" do
 | 
			
		||||
        it "deletes the table" do
 | 
			
		||||
          2.times { User.create }
 | 
			
		||||
 | 
			
		||||
          connection.delete_table('users')
 | 
			
		||||
          User.count.should eq 0
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue