Skip to content

Commit

Permalink
Added specs for schema, migrate_from_acts_as_paranoid, restore_all, a…
Browse files Browse the repository at this point in the history
…nd bang (!) destroy methods
  • Loading branch information
winton committed Jan 4, 2011
1 parent 1b16ce8 commit 678edf2
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/acts_as_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class Archive < ActiveRecord::Base
EVAL
self.reflect_on_all_associations.each do |association|
if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
options = association.options.dup
options[:class_name] = "::#{association.class_name}::Archive"
options[:foreign_key] = association.primary_key_name
klass.send association.macro, association.name, options
opts = association.options.dup
opts[:class_name] = "::#{association.class_name}::Archive"
opts[:foreign_key] = association.primary_key_name
klass.send association.macro, association.name, opts
end
end
unless options[:migrate] == false
Expand All @@ -128,22 +128,22 @@ def destroy_all!(*args)
def migrate_from_acts_as_paranoid
time = Benchmark.measure do
ActsAsArchive.find(self).each do |config|
config = config.dup
config[:options][:copy] = false
ActsAsArchive.move(
config,
"#{config[:options][:magic]} IS NOT NULL",
"`#{config[:options][:magic]}` IS NOT NULL",
:migrate => true
)
end
end
$stdout.puts "-- #{self.class}.migrate_from_acts_as_paranoid"
$stdout.puts "-- #{self}.migrate_from_acts_as_paranoid"
$stdout.puts " -> #{"%.4fs" % time.real}"
end

def restore_all(*args)
ActsAsArchive.deprecate "#{self}.restore_all is deprecated, please use #{self}::Archive.delete_all."
if defined?(self::Archive)
self::Archive.delete_all *args
end
ActsAsArchive.deprecate "#{self}.restore_all is deprecated, please use #{self}.delete_all."
self.delete_all *args
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ ActsAsArchive::Gems.activate :active_wrapper
require 'active_wrapper/tasks'

ActiveWrapper::Tasks.new(
:base => File.dirname(__FILE__),
:base => File.dirname(__FILE__) + "/fixtures",
:env => ENV['ENV']
)
48 changes: 48 additions & 0 deletions spec/acts_as_archive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@
before(:each) do
before_each
end

it "should have valid schema" do
should_have_valid_schema
end

it "should create records" do
should_create_records
end

describe :migrate_from_acts_as_paranoid do
it "should migrate record and preserve deleted_at" do
should_migrate_record_and_preserve_deleted_at
end
end

describe :restore_all do
it "should emulate delete_all" do
should_emulate_delete_all
end
end

%w(delete delete_all destroy destroy_all).each do |type|
describe type do
Expand All @@ -21,6 +37,12 @@
should_move_records_back_to_original_tables(type)
end
end

describe "#{type}!" do
it "should delete records without archiving" do
should_delete_records_without_archiving(type)
end
end
end
end
end
Expand All @@ -43,10 +65,29 @@ def app
last_response.body.should == '1'
end

it "should have valid schema" do
get "/should_have_valid_schema_action"
last_response.body.should == '1'
end

it "should create records" do
get "/should_create_records_action"
last_response.body.should == '1'
end

describe :migrate_from_acts_as_paranoid do
it "should migrate record and preserve deleted_at" do
get "/should_migrate_record_and_preserve_deleted_at_action"
last_response.body.should == '1'
end
end

describe :restore_all do
it "should emulate delete_all" do
get "/should_emulate_delete_all_action"
last_response.body.should == '1'
end
end

%w(delete delete_all destroy destroy_all).each do |type|
describe type do
Expand All @@ -60,6 +101,13 @@ def app
last_response.body.should == '1'
end
end

describe "#{type}!" do
it "should delete records without archiving" do
get "/should_delete_records_without_archiving_action", :type => type
last_response.body.should == '1'
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/001_belongs_tos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def self.up
create_table :belongs_tos do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/002_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :belongs_to_id
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/003_has_ones.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/004_has_manies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/005_has_many_through_throughs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def self.up
t.integer :integer, :default => '1'
t.integer :has_many_through_id
t.integer :record_id
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/006_has_many_throughs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def self.up
create_table :has_many_throughs do |t|
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/007_has_one_through_throughs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :record_id
t.datetime :restored_at
t.timestamps
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/db/migrate/008_has_one_throughs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.up
t.string :string, :default => 'string'
t.integer :integer, :default => '1'
t.integer :has_one_through_through_id
t.datetime :restored_at
t.timestamps
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/fixtures/frameworks/rails2/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@ def pulse
render :text => '1'
end

def should_have_valid_schema_action
before_each false, true
should_have_valid_schema
render :text => '1'
end

def should_create_records_action
before_each false, true
should_create_records
render :text => '1'
end

def should_migrate_record_and_preserve_deleted_at_action
before_each false, true
should_migrate_record_and_preserve_deleted_at
render :text => '1'
end

def should_emulate_delete_all_action
before_each false, true
should_emulate_delete_all
render :text => '1'
end

def should_move_records_back_to_original_tables_action
before_each false, true
should_move_records_back_to_original_tables(params[:type])
Expand All @@ -31,4 +49,10 @@ def should_move_records_to_archive_tables_action
should_move_records_to_archive_tables(params[:type])
render :text => '1'
end

def should_delete_records_without_archiving_action
before_each false, true
should_delete_records_without_archiving(params[:type])
render :text => '1'
end
end
24 changes: 24 additions & 0 deletions spec/fixtures/frameworks/rails3/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ def pulse
render :text => '1'
end

def should_have_valid_schema_action
before_each false, true
should_have_valid_schema
render :text => '1'
end

def should_create_records_action
before_each false, true
should_create_records
render :text => '1'
end

def should_migrate_record_and_preserve_deleted_at_action
before_each false, true
should_migrate_record_and_preserve_deleted_at
render :text => '1'
end

def should_emulate_delete_all_action
before_each false, true
should_emulate_delete_all
render :text => '1'
end

def should_move_records_back_to_original_tables_action
before_each false, true
should_move_records_back_to_original_tables(params[:type])
Expand All @@ -24,4 +42,10 @@ def should_move_records_to_archive_tables_action
should_move_records_to_archive_tables(params[:type])
render :text => '1'
end

def should_delete_records_without_archiving_action
before_each false, true
should_delete_records_without_archiving(params[:type])
render :text => '1'
end
end
24 changes: 24 additions & 0 deletions spec/fixtures/frameworks/sinatra/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@ class Application < Sinatra::Base
'1'
end

get "/should_have_valid_schema_action" do
before_each false, true
should_have_valid_schema
'1'
end

get "/should_create_records_action" do
before_each false, true
should_create_records
'1'
end

get "/should_migrate_record_and_preserve_deleted_at_action" do
before_each false, true
should_migrate_record_and_preserve_deleted_at
'1'
end

get "/should_emulate_delete_all_action" do
before_each false, true
should_emulate_delete_all
'1'
end

get "/should_move_records_back_to_original_tables_action" do
before_each false, true
should_move_records_back_to_original_tables(params[:type])
Expand All @@ -31,4 +49,10 @@ class Application < Sinatra::Base
should_move_records_to_archive_tables(params[:type])
'1'
end

get "/should_delete_records_without_archiving_action" do
before_each false, true
should_delete_records_without_archiving(params[:type])
'1'
end
end
53 changes: 53 additions & 0 deletions spec/fixtures/helpers/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def all_records
def before_each(migrate=true, setup=true)
if migrate
[ 8, 0, 8 ].each { |v| $db.migrate(v) }
Record.reset_column_information
end
if setup
@record, @lengths, @zero_lengths = setup_records
Expand Down Expand Up @@ -72,6 +73,58 @@ def should_create_records
verify_attributes original
end

def should_delete_records_without_archiving(type)
type = "#{type}!"
case type
when 'delete!', 'destroy!'
@record.send type
when 'delete_all!', 'destroy_all!'
Record.send type
end

Record.count.should == 0
Record::Archive.count.should == 0
end

def should_emulate_delete_all
id = @record.id
@record.destroy
Record.count.should == 0
Record::Archive.count.should == 1
Record::Archive.restore_all [ "id = ?", id ]
Record.count.should == 1
end

def should_have_valid_schema
[
BelongsTo, Record, HasOne, HasMany, HasManyThroughThrough,
HasManyThrough, HasOneThroughThrough, HasOneThrough
].each do |klass|
cols = [ 'string', 'integer', 'restored_at', 'created_at', 'updated_at' ]
archive_cols = [ 'string', 'integer', 'created_at', 'updated_at', 'deleted_at' ]
(klass.column_names & cols).should == cols
(klass::Archive.column_names & archive_cols).should == archive_cols
end
end

def should_migrate_record_and_preserve_deleted_at
Record.connection.execute <<-SQL
ALTER TABLE records ADD deleted_at DATETIME
SQL
Record.reset_column_information

time = Time.now.utc - 24 * 60 * 60
first = Record.first
first.update_attribute :deleted_at, time
Record.create

Record.migrate_from_acts_as_paranoid
Record::Archive.first.deleted_at.to_s.should == time.to_s
Record::Archive.first.id.should == first.id
Record::Archive.count.should == 1
Record.count.should == 1
end

def should_move_records_back_to_original_tables(type)
case type
when 'delete', 'destroy'
Expand Down
Loading

0 comments on commit 678edf2

Please sign in to comment.