public
Description: Rails Plugin - soft-delete your ActiveRecord records with a deleted_at timestamp. This is a much more explicit version of ActsAsParanoid
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/permanent_records.git
permanent_records / test / permanent_records_test.rb
100644 81 lines (64 sloc) 2.159 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'test/unit'
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
require File.expand_path(File.dirname(__FILE__) + "/muskrat")
 
class PermanentRecordsTest < Test::Unit::TestCase
  
  def setup
    super
    Muskrat.delete_all
    @active = Muskrat.create!(:name => 'Wakko')
    @deleted = Muskrat.create!(:name => 'Yakko', :deleted_at => 4.days.ago)
    @the_girl = Muskrat.create!(:name => 'Dot')
    Kitty.delete_all
    @kitty = Kitty.create!(:name => 'Meow Meow')
  end
  
  def teardown
    setup
  end
  
  def test_destroy_should_return_the_record
    muskrat = @deleted
    assert_equal muskrat, muskrat.destroy
  end
 
  def test_destroy_should_set_deleted_at_attribute
    assert @active.destroy.deleted_at
  end
  
  def test_destroy_should_save_deleted_at_attribute
    assert Muskrat.find(@active.destroy.id).deleted_at
  end
  
  def test_destroy_should_freeze_record
    assert @active.destroy.frozen?
  end
  
  def test_destroy_should_not_really_remove_the_record
    assert Muskrat.find(@active.destroy.id)
  end
  
  def test_destroy_should_recognize_a_force_parameter
    assert_raises(ActiveRecord::RecordNotFound) { @active.destroy(:force).reload }
  end
  
  def test_destroy_should_ignore_other_parameters
    assert Muskrat.find(@active.destroy(:hula_dancer).id)
  end
  
  def test_revive_should_unfreeze_record
    assert !@deleted.revive.frozen?
  end
  
  def test_revive_should_unset_deleted_at
    assert !@deleted.revive.deleted_at
  end
  
  def test_revive_should_make_deleted_return_false
    assert !@deleted.revive.deleted?
  end
  
  def test_deleted_returns_true_for_deleted_records
    assert @deleted.deleted?
  end
  
  def test_with_deleted_limits_scope_to_deleted_records
    Muskrat.send :with_deleted do
      assert Muskrat.find(:all).all?(&:deleted?)
    end
  end
  
  def test_with_not_deleted_limits_scope_to_not_deleted_records
    Muskrat.send :with_not_deleted do
      assert !Muskrat.find(:all).any?(&:deleted?)
    end
  end
  
  def test_models_without_a_deleted_at_column_should_destroy_as_normal
    assert_raises(ActiveRecord::RecordNotFound) {@kitty.destroy.reload}
  end
end