Skip to content
This repository
tree: 508fc5aec2
Fetching contributors…

Cannot retrieve contributors at this time

file 117 lines (91 sloc) 2.665 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")

describe ActsAsArchive::Base::Destroy do
  
  before(:all) do
    establish_test_db
    Article.create_archive_table
  end
  
  describe 'delete_all!' do
    
    before(:all) do
      create_records
    end
    
    it "should really delete all records" do
      Article.delete_all!
      Article.count.should == 0
      Article::Archive.count.should == 0
    end
    
  end

  describe 'destroy!' do

    before(:all) do
      create_records
      @article = Article.first
    end

    it "should really destroy a records" do
      @article.destroy!
      Article::Archive.count.should == 0
    end

  end
  
  describe 'delete_all' do
    
    before(:all) do
      @articles = create_records
    end
    
    describe 'with conditions' do
      
      before(:all) do
        # Mini delete_all parameter test
        Article.delete_all [ 'id = ?', @articles[0].id ]
        Article.delete_all "id = #{@articles[1].id}"
      end
      
      it "should move some records to the archive table" do
        Article.count.should == 3
        Article::Archive.count.should == 2
      end
      
      it "should preserve record attributes" do
        2.times do |x|
          original = @articles[x]
          copy = Article::Archive.find(original.id)
          article_match?(original, copy)
        end
      end
    end
    
    describe 'without conditions' do
      
      before(:all) do
        Article.delete_all
      end
      
      it "should move all records to the archive table" do
        Article.count.should == 0
        Article::Archive.count.should == 5
      end
      
      it "should preserve record attributes" do
        5.times do |x|
          original = @articles[x]
          copy = Article::Archive.find(original.id)
          article_match?(original, copy)
        end
      end
    end
  end
  
  [ :destroy, :delete ].each do |d|
    
    describe d do
    
      before(:all) do
        @articles = create_records
        Article.find(@articles[0..1].collect(&:id)).each do |a|
          a.send(d)
        end
      end
    
      it "should move some records to the archive table" do
        Article.count.should == 3
        Article::Archive.count.should == 2
      end
    
      it "should preserve record attributes" do
        2.times do |x|
          original = @articles[x]
          copy = Article::Archive.find(original.id)
          article_match?(original, copy)
        end
      end
      
      it "should mark the object as destroyed" do
        @articles[3].send(d)
        @articles[3].destroyed?.should == true
      end
    end
  end
end
Something went wrong with that request. Please try again.