Skip to content

Commit

Permalink
add test helper and use them internally. Update Readme again.
Browse files Browse the repository at this point in the history
git-svn-id: http://llama/svn/trunk/ruby/acts_as_soft_deletable@238 bb26965d-a405-0410-8ce8-d62df5cd24e9
  • Loading branch information
(no author) committed Mar 13, 2008
1 parent 0bbb6e8 commit a3a974e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 35 deletions.
31 changes: 31 additions & 0 deletions README
Expand Up @@ -37,6 +37,8 @@ This turns out to be easy to solve. A migration helper is available (see below)

=== Setup ===

---+ Model

Any ActiveRecord class that wants the soft delete functionality should add
the following line to their class definition:

Expand All @@ -46,6 +48,8 @@ class SomeModel < ActiveRecord::Base
...
</pre>

---+ Migration

and setup the deleted table with the following migration:

<pre>
Expand All @@ -60,6 +64,33 @@ class AddActsAsSoftDeletable < ActiveRecord::Migration
end
</pre>

Any changes to the original table (such as adding a column) should be reflected in the deleted table. Use the update_columns method:

<pre>
class AddSkuColumn < ActiveRecord::Migration
def self.up
add_column 'items', 'sku', :string
Item::Deleted.update_columns # will add sku column
end

def self.down
remove_column 'items', 'sku'
Item::Deleted.update_columns # will remove sku column
end
end
</pre>

---+ Unit tests

A model's soft delete capabilities can be unit tests easily by using some provide asserts.

<pre>
def test_soft_delete_works
# will run the model through a destroy and undestroy while making sure all values were saved
assert_model_soft_deletes( items(:radar_detector) )
end
</pre>

=== TODO ===

make undestroying easier when there are lots of related rows to undestroy.
1 change: 1 addition & 0 deletions init.rb
@@ -1 +1,2 @@
require File.join(File.dirname(__FILE__), 'lib', 'acts_as_soft_deletable')
require File.join(File.dirname(__FILE__), 'lib', 'unit_test_helper.rb')
35 changes: 35 additions & 0 deletions lib/unit_test_helper.rb
@@ -0,0 +1,35 @@
module Test
module Unit
module ActsAsDeleted
# Takes a saved model and runs assertions testing whether soft deleting is working
def assert_model_soft_deletes(model)
klass = model.class
deleted_klass = model.class.deleted_class

assert_raises(ActiveRecord::RecordNotFound) { deleted_klass.find model.id }
model.destroy

assert(deleted = deleted_klass.find(model.id))
assert_raises(ActiveRecord::RecordNotFound) { klass.find model.id }

deleted.undestroy!

assert_soft_delete_models_are_equal deleted, klass.find(model.id)
assert_raises(ActiveRecord::RecordNotFound) { deleted_klass.find model.id }
end

# Asserts whether a two soft deleting models are equal. Intended to be passed
# an instance of a model and an instance of the deleted class's model. Checks that
# all attributes were saved off correctly.
def assert_soft_delete_models_are_equal(a, b, message = "models weren't equal")
reject_attrs = %q(deleted_at, updated_at)
assert_equal \
a.attributes.reject{|k,v| reject_attrs.include? k},
b.attributes.reject{|k,v| reject_attrs.include? k},
message
end
end
end
end

Test::Unit::TestCase.send(:include, Test::Unit::ActsAsDeleted)
6 changes: 4 additions & 2 deletions test/RUNNING_UNIT_TESTS
@@ -1,11 +1,13 @@
== Creating the test database(s)

The tests can run against several databases. See the related test/connections/#{adapter}/connection.rb file
for information about expected connection settings.
for information about expected connection settings.

The sqlite tests are especially easy to run once the required gems are installed.

== Other Prerequisites

Mocha must be installed.
The mocha gem must be installed.

== Running with Rake

Expand Down
11 changes: 1 addition & 10 deletions test/helper.rb
Expand Up @@ -7,7 +7,7 @@
require 'active_record'
require 'active_record/fixtures'

require 'acts_as_soft_deletable'
require File.join(File.dirname(__FILE__), '..', 'init')

begin
# pulls from one of test/connections/#{adapter}/connection.rb depending on how rake setup our lib paths
Expand Down Expand Up @@ -44,13 +44,4 @@ def run(*args)
super
end

private

def assert_models_equal(a, b, message = "models weren't equal")
reject_attrs = %q(deleted_at, updated_at)
assert_equal \
a.attributes.reject{|k,v| reject_attrs.include? k},
b.attributes.reject{|k,v| reject_attrs.include? k},
message
end
end
10 changes: 7 additions & 3 deletions test/test_acts_as_soft_deletable.rb
Expand Up @@ -7,7 +7,7 @@ def test_destroy_should_create_a_deleted_model
assert_nil Artist.find_by_name('Chick Corea')

deleted = Artist::Deleted.find_by_name('Chick Corea')
assert_models_equal artist, deleted
assert_soft_delete_models_are_equal artist, deleted
end

def test_deleted_model_should_be_able_to_undestroy
Expand All @@ -16,7 +16,7 @@ def test_deleted_model_should_be_able_to_undestroy

deleted.undestroy!

assert_models_equal deleted, Artist.find_by_name('Robert Walter')
assert_soft_delete_models_are_equal deleted, Artist.find_by_name('Robert Walter')
assert_nil Artist::Deleted.find_by_name('Robert Walter')
assert deleted.frozen?
end
Expand All @@ -39,6 +39,10 @@ def test_should_copy_decimals_correctly
end

restored = Decimal.find :first
assert_models_equal decimal, restored
assert_soft_delete_models_are_equal decimal, restored
end

def test_helper_should_work
assert_model_soft_deletes Artist.find_by_name('Chick Corea')
end
end
26 changes: 6 additions & 20 deletions test/test_migration.rb
Expand Up @@ -12,7 +12,8 @@ def test_should_create_deleted_table

migrate_up(3)

assert_soft_delete_works
t = Thing.create! :title => 'blah blah', :price => 123.45, :type => 'Thing'
assert_model_soft_deletes(t)

migrate_down

Expand All @@ -25,9 +26,8 @@ def test_should_help_when_adding_columns

migrate_up(4)

#t = Thing.create! :title => 'blah blah', :price => 123.45, :type => 'Thing'
#new_assert_soft_delete_works(t)
assert_soft_delete_works
t = Thing.create! :title => 'blah blah', :price => 123.45, :type => 'Thing'
assert_model_soft_deletes(t)

migrate_down

Expand Down Expand Up @@ -60,7 +60,7 @@ def migrate_down(version=nil)
end

# takes a saved model and runs assertions testing whether soft deleting is working
def new_assert_soft_delete_works(model)
def assert_model_soft_deletes(model)
klass = model.class
deleted_klass = model.class.deleted_class

Expand All @@ -72,23 +72,9 @@ def new_assert_soft_delete_works(model)

deleted.undestroy!

assert_models_equal deleted, klass.find(model.id)
assert_soft_delete_models_are_equal deleted, klass.find(model.id)
assert_raises(ActiveRecord::RecordNotFound) { deleted_klass.find model.id }
end

def assert_soft_delete_works
t = Thing.create! :title => 'blah blah', :price => 123.45, :type => 'Thing'

assert_nil Thing::Deleted.find_by_title('blah blah')
t.destroy

assert(deleted = Thing::Deleted.find_by_title('blah blah'))
assert_nil Thing.find_by_title('blah blah')

deleted.undestroy!

assert_models_equal deleted, Thing.find_by_title('blah blah')
assert_nil Thing::Deleted.find_by_title('blah blah')
end
end
end

0 comments on commit a3a974e

Please sign in to comment.