Skip to content

Commit

Permalink
cleanup testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio committed Jul 2, 2009
1 parent 663c18d commit bd25c95
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 62 deletions.
60 changes: 60 additions & 0 deletions test/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'rubygems'
require 'active_record'
require 'active_record/base'
require 'active_record/reflection'
require 'active_record/associations'
require 'ostruct'

module Associations
def companies
cs = OpenStruct.new
cs.with_companies = @with_companies
def cs.find(arg)
self.with_companies
end
return cs
end

def company
@with_company
end

def main_company
@with_main_company
end
end

# Mocking everything necesary to test the plugin.
class Company
def initialize(with_or_without)
@with_companies = with_or_without == :with_companies
@with_company = with_or_without == :with_company
@with_main_company = with_or_without == :with_main_company
self.class.send :before_destroy, nil
self.class.send :has_many, :companies, :dependent => :protect if @with_companies
self.class.send :has_one, :company, :dependent => :protect if @with_company
self.class.send :belongs_to, :main_company, :dependent => :protect, :class_name => 'Company' if @with_main_company
class_eval { include Associations }
end

def self.class_name
self.name
end

# not the real signature of the method, but forgive me
def self.before_destroy(s=nil)
@@before = s
end
# not the real signature of the method, but forgive me
def self.after_destroy(s=nil)
@@after = s
end

def destroy
eval(@@before) if @@before
end

include ActiveRecord::Reflection
include ActiveRecord::Associations
include DependentProtect
end
63 changes: 1 addition & 62 deletions test/dependent_protect_test.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
require 'rubygems'
require 'active_record'
require 'active_record/base'
require 'active_record/reflection'
require 'active_record/associations'
require 'test/unit'
require 'ostruct'
require File.join(File.dirname(__FILE__), '..', 'lib', 'dependent_protect')
require File.join(File.dirname(__FILE__), 'company')

class DependentProtectTest < Test::Unit::TestCase

module Associations
def companies
cs = OpenStruct.new
cs.with_companies = @with_companies
def cs.find(arg)
self.with_companies
end
return cs
end

def company
@with_company
end

def main_company
@with_main_company
end
end

# Mocking everything necesary to test the plugin.
class Company
def initialize(with_or_without)
@with_companies = with_or_without == :with_companies
@with_company = with_or_without == :with_company
@with_main_company = with_or_without == :with_main_company
self.class.send :before_destroy, nil
self.class.send :has_many, :companies, :dependent => :protect if @with_companies
self.class.send :has_one, :company, :dependent => :protect if @with_company
self.class.send :belongs_to, :main_company, :dependent => :protect, :class_name => 'Company' if @with_main_company
class_eval { include Associations }
end

def self.class_name
self.name
end

# not the real signature of the method, but forgive me
def self.before_destroy(s=nil)
@@before = s
end
# not the real signature of the method, but forgive me
def self.after_destroy(s=nil)
@@after = s
end

def destroy
eval(@@before) if @@before
end

include ActiveRecord::Reflection
include ActiveRecord::Associations
include DependentProtect
end

def test_destroy_protected_with_companies
puts "with copanies"
protected_firm = Company.new(:with_companies)
assert_raises(ActiveRecord::ReferentialIntegrityProtectionError) { protected_firm.destroy }
end
Expand Down

0 comments on commit bd25c95

Please sign in to comment.