test/paranoia_test.rb in paranoia-2.0.3 vs test/paranoia_test.rb in paranoia-2.0.4
- old
+ new
@@ -9,10 +9,14 @@
end
require File.expand_path(File.dirname(__FILE__) + "/../lib/paranoia")
def connect!
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:'
+end
+
+def setup!
+ connect!
ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_anthor_class_name_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_foreign_key_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER)'
@@ -26,20 +30,19 @@
ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_sentinel_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME NOT NULL)'
ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)'
- ActiveRecord::Base.connection.execute 'CREATE TABLE idless_models (deleted_at DATETIME)'
end
class WithDifferentConnection < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: ':memory:'
connection.execute 'CREATE TABLE with_different_connections (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
acts_as_paranoid
end
-connect!
+setup!
class ParanoiaTest < test_framework
def setup
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute "DELETE FROM #{table}"
@@ -533,11 +536,32 @@
# Does it raise NoMethodException on restore of nil
hasOne.restore(:recursive => true)
assert hasOne.reload.deleted_at.nil?
end
-
+
+ # covers #185
+ def test_restoring_recursive_has_one_restores_correct_object
+ hasOnes = 2.times.map { ParanoidModelWithHasOne.create }
+ belongsTos = 2.times.map { ParanoidModelWithBelong.create }
+
+ hasOnes[0].update paranoid_model_with_belong: belongsTos[0]
+ hasOnes[1].update paranoid_model_with_belong: belongsTos[1]
+
+ hasOnes.each(&:destroy)
+
+ ParanoidModelWithHasOne.restore(hasOnes[1], :recursive => true)
+ hasOnes.each(&:reload)
+ belongsTos.each(&:reload)
+
+ # without #185, belongsTos[0] will be restored instead of belongsTos[1]
+ refute_nil hasOnes[0].deleted_at
+ refute_nil belongsTos[0].deleted_at
+ assert_nil hasOnes[1].deleted_at
+ assert_nil belongsTos[1].deleted_at
+ end
+
# covers #131
def test_has_one_really_destroy_with_nil
model = ParanoidModelWithHasOne.create
model.really_destroy!
@@ -601,11 +625,12 @@
ActiveRecord::Base.remove_connection # Disconnect the main connection
a = WithDifferentConnection.create
a.destroy!
a.restore!
# This test passes if no exception is raised
- connect! # Reconnect the main connection
+ ensure
+ setup! # Reconnect the main connection
end
def test_restore_clear_association_cache_if_associations_present
parent = ParentModel.create
3.times { parent.very_related_models.create }
@@ -619,14 +644,16 @@
assert_equal 3, parent.very_related_models.count
assert_equal 3, parent.very_related_models.size
end
- def test_model_without_primary_key
- assert_raises(RuntimeError) do
- IdlessModel.class_eval{ acts_as_paranoid }
- end
+ def test_model_without_db_connection
+ ActiveRecord::Base.remove_connection
+
+ NoConnectionModel.class_eval{ acts_as_paranoid }
+ ensure
+ setup!
end
private
def get_featureful_model
FeaturefulModel.new(:name => "not empty")
@@ -764,7 +791,7 @@
before_destroy do |r|
raise StandardError, 'ASPLODE!'
end
end
-class IdlessModel < ActiveRecord::Base
+class NoConnectionModel < ActiveRecord::Base
end