spec/paranoid_spec.rb in paranoid-0.0.1 vs spec/paranoid_spec.rb in paranoid-0.0.2
- old
+ new
@@ -3,97 +3,140 @@
LUKE = 'Luke Skywalker'
describe Paranoid do
before(:each) do
- Sticker.delete_all
- Place.delete_all
- Android.delete_all
- Person.delete_all
- Component.delete_all
+ # Sticker.delete_all
+ # Place.delete_all
+ # Android.delete_all
+ # Person.delete_all
+ # Component.delete_all
+ #
+ # @luke = Person.create(:name => LUKE)
+ # @r2d2 = Android.create(:name => 'R2D2', :owner_id => @luke.id)
+ # @c3p0 = Android.create(:name => 'C3P0', :owner_id => @luke.id)
+ #
+ # @r2d2.components.create(:name => 'Rotors')
+ #
+ # @r2d2.memories.create(:name => 'A pretty sunset')
+ # @c3p0.sticker = Sticker.create(:name => 'OMG, PONIES!')
+ # @tatooine = Place.create(:name => "Tatooine")
+ # @r2d2.places << @tatooine
+ end
- @luke = Person.create(:name => LUKE)
- @r2d2 = Android.create(:name => 'R2D2', :owner_id => @luke.id)
- @c3p0 = Android.create(:name => 'C3P0', :owner_id => @luke.id)
+ describe 'basic functionality' do
+ before(:each) do
+ Place.delete_all
+ @tatooine, @mos_eisley, @coruscant = Place.create([{:name => "Tatooine"}, {:name => 'Mos Eisley'}, {:name => 'Coruscant'}])
+ end
- @r2d2.components.create(:name => 'Rotors')
+ it 'should recognize a class as paranoid' do
+ Person.paranoid?.should be_false
+ Place.paranoid?.should be_true
+ end
- @r2d2.memories.create(:name => 'A pretty sunset')
- @c3p0.sticker = Sticker.create(:name => 'OMG, PONIES!')
- @tatooine = Place.create(:name => "Tatooine")
- @r2d2.places << @tatooine
- end
+ it 'should hide destroyed records' do
+ @tatooine.update_attribute('deleted_at', Time.now)
+ Place.first(:conditions => {:name => 'Tatooine'}).should be_nil
+ end
- it 'should recognize a class as paranoid' do
- Person.paranoid?.should be_false
- Place.paranoid?.should be_true
- end
+ it 'should reveal destroyed records when with_destroyed' do
+ @tatooine.update_attribute('deleted_at', Time.now)
+ Place.with_destroyed.first(:conditions => {:name => 'Tatooine'}).should_not be_nil
+ end
- it 'should hide destroyed records' do
- @tatooine.update_attribute('deleted_at', Time.now)
- Place.first(:conditions => {:name => 'Tatooine'}).should be_nil
- end
+ it 'should restore the destroyed record' do
+ @tatooine.update_attribute('deleted_at', Time.now)
- it 'should reveal destroyed records when with_destroyed' do
- @tatooine.update_attribute('deleted_at', Time.now)
- Place.with_destroyed.first(:conditions => {:name => 'Tatooine'}).should_not be_nil
- end
+ @tatooine = Place.with_destroyed.first(:conditions => {:name => 'Tatooine'})
+ @tatooine.restore
- it 'should restore the destroyed record' do
- @tatooine.update_attribute('deleted_at', Time.now)
+ Place.first(:conditions => {:name => 'Tatooine'}).should_not be_nil
+ end
- @tatooine = Place.with_destroyed.first(:conditions => {:name => 'Tatooine'})
- @tatooine.restore
+ it 'should soft delete paranoid records' do
+ @tatooine.destroy
- Place.first(:conditions => {:name => 'Tatooine'}).should_not be_nil
- end
+ record = Place.with_destroyed.first(:conditions => {:name => 'Tatooine'})
+ record.should_not be_nil
+ record.deleted_at.should_not be_nil
+ end
- it 'should soft delete paranoid records' do
- @tatooine.destroy
+ it 'should mark the record destroyed' do
+ @tatooine.destroy
+ @tatooine.destroyed?.should be_true
+ end
- record = Place.with_destroyed.first(:conditions => {:name => 'Tatooine'})
- record.should_not be_nil
- record.deleted_at.should_not be_nil
- end
+ it 'should set the deleted_field' do
+ @tatooine.destroy
+ @tatooine.deleted_at.should_not be_nil
+ end
- it 'should mark the record destroyed' do
- @tatooine.destroy
- @tatooine.destroyed?.should be_true
- end
+ it 'should show deleted_only' do
+ @tatooine.destroy
+ destroyed = Place.with_destroyed_only.all
+ destroyed.size.should == 1
+ destroyed[0].should == @tatooine
+ end
- it 'should set the deleted_field' do
- @tatooine.destroy
- @tatooine.deleted_at.should_not be_nil
+ it 'should properly count records' do
+ Place.count.should == 3
+
+ @tatooine.destroy
+ Place.count.should == 2
+ Place.with_destroyed.count.should == 3
+ Place.with_destroyed_only.count.should == 1
+ end
end
describe 'for alternate field information' do
before(:each) do
Ninja.delete_all
- @ninja = Ninja.create(:name => 'Steve')
+ @steve, @bob, @tim = Ninja.create([{:name => 'Steve', :visible => true}, {:name => 'Bob', :visible => true}, {:name => 'Tim', :visible => true}])
end
+ it 'should have 3 visible ninjas' do
+ Ninja.all.size.should == 3
+ end
+
it 'should vanish the ninja' do
- @ninja.destroy
+ @steve.destroy
record = Ninja.first(:conditions => {:name => 'Steve'})
record.should be_nil
end
it 'should not delete the ninja' do
- @ninja.destroy
+ @steve.destroy
record = Ninja.with_destroyed.first(:conditions => {:name => 'Steve'})
record.should_not be_nil
record.visible.should be_false
end
it 'should mark the ninja vanished' do
- @ninja.destroy
- @ninja.destroyed?.should be_true
+ @steve.destroy
+ @steve.destroyed?.should be_true
end
it 'should set visible to false' do
- @ninja.destroy
- @ninja.visible.should be_false
+ @steve.destroy
+ @steve.visible.should be_false
+ end
+
+ it 'should show deleted_only' do
+ @steve.destroy
+ destroyed = Ninja.with_destroyed_only.all
+ destroyed.size.should == 1
+ destroyed[0].should == @steve
+ end
+
+ it 'should properly count records' do
+ Ninja.count.should == 3
+
+ @steve.destroy
+ Ninja.count.should == 2
+ Ninja.with_destroyed.count.should == 3
+ Ninja.with_destroyed_only.count.should == 1
end
end
end