Browse files

Update tests so that appraisals can be run in parallel

  • Loading branch information...
1 parent ead10c2 commit e738ba94d3bffffab8186fc104aeb36014c6ff58 @obrie obrie committed Mar 6, 2013
View
4 test/files/switch.rb
@@ -1,4 +1,8 @@
class Switch
+ def self.name
+ @name ||= "Switch_#{rand(1000000)}"
+ end
+
state_machine do
event :turn_on do
transition all => :on
View
11 test/unit/graph_test.rb
@@ -77,17 +77,20 @@ def test_should_set_default_font
class GraphOutputTest < Test::Unit::TestCase
def setup
- @graph = StateMachine::Graph.new('test')
+ @graph_name = "test_#{rand(1000000)}"
+ @graph = StateMachine::Graph.new(@graph_name)
@graph.add_nodes('parked', :shape => 'ellipse')
@graph.add_nodes('idling', :shape => 'ellipse')
@graph.add_edges('parked', 'idling', :label => 'ignite')
@graph.output
end
def test_should_save_file
- assert File.exist?('./test.png')
- ensure
- FileUtils.rm('./test.png')
+ assert File.exist?("./#{@graph_name}.png")
+ end
+
+ def teardown
+ FileUtils.rm Dir["./#{@graph_name}.png"]
end
end
rescue LoadError
View
36 test/unit/integrations/active_record_test.rb
@@ -36,14 +36,15 @@ def default_test
protected
# Creates a new ActiveRecord model (and the associated table)
def new_model(create_table = :foo, &block)
- table_name = create_table || :foo
+ name = create_table || :foo
+ table_name = "#{name}_#{rand(1000000)}"
model = Class.new(ActiveRecord::Base) do
- connection.create_table(table_name, :force => true) {|t| t.string(:state)} if create_table
self.table_name = table_name.to_s
+ connection.create_table(table_name, :force => true) {|t| t.string(:state)} if create_table
(class << self; self; end).class_eval do
- define_method(:name) { "ActiveRecordTest::#{table_name.to_s.capitalize}" }
+ define_method(:name) { "ActiveRecordTest::#{name.to_s.capitalize}" }
end
end
model.class_eval(&block) if block_given?
@@ -124,7 +125,7 @@ def setup
@model = new_model(false)
# Drop the table so that it definitely doesn't exist
- @model.connection.drop_table(:foo) if @model.table_exists?
+ @model.connection.drop_table(@model.table_name) if @model.table_exists?
end
def test_should_allow_machine_creation
@@ -390,7 +391,7 @@ def setup
@original_stderr, $stderr = $stderr, StringIO.new
@model = new_model do
- connection.add_column :foo, :status, :string, :default => 'parked'
+ connection.add_column table_name, :status, :string, :default => 'parked'
end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@record = @model.new
@@ -406,6 +407,7 @@ def test_should_not_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -414,7 +416,7 @@ def setup
@original_stderr, $stderr = $stderr, StringIO.new
@model = new_model do
- connection.add_column :foo, :status, :string, :default => 'idling'
+ connection.add_column table_name, :status, :string, :default => 'idling'
end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@record = @model.new
@@ -430,6 +432,7 @@ def test_should_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -438,7 +441,7 @@ def setup
@original_stderr, $stderr = $stderr, StringIO.new
@model = new_model do
- connection.add_column :foo, :status, :integer, :default => 0
+ connection.add_column table_name, :status, :integer, :default => 0
end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@machine.state :parked, :value => 1
@@ -455,6 +458,7 @@ def test_should_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -499,6 +503,7 @@ def test_should_output_warning_with_same_machine_attribute
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -648,6 +653,7 @@ def test_should_not_delegate_attribute_predicate_with_different_attribute
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -697,7 +703,7 @@ def test_should_use_default_state_if_protected
class MachineMultipleTest < BaseTestCase
def setup
@model = new_model do
- connection.add_column :foo, :status, :string
+ connection.add_column table_name, :status, :string
end
@state_machine = StateMachine::Machine.new(@model, :initial => :parked)
@status_machine = StateMachine::Machine.new(@model, :status, :initial => :idling)
@@ -713,7 +719,7 @@ def test_should_should_initialize_each_state
class MachineWithLoopbackTest < BaseTestCase
def setup
@model = new_model do
- connection.add_column :foo, :updated_at, :datetime
+ connection.add_column table_name, :updated_at, :datetime
end
@machine = StateMachine::Machine.new(@model, :initial => :parked)
@@ -796,7 +802,7 @@ def test_should_not_track_attribute_changes
class MachineWithDirtyAttributesAndCustomAttributeTest < BaseTestCase
def setup
@model = new_model do
- connection.add_column :foo, :status, :string
+ connection.add_column table_name, :status, :string
end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@machine.event :ignite
@@ -827,7 +833,7 @@ def test_should_not_reset_changes_on_multiple_transitions
class MachineWithDirtyAttributeAndCustomAttributesDuringLoopbackTest < BaseTestCase
def setup
@model = new_model do
- connection.add_column :foo, :status, :string
+ connection.add_column table_name, :status, :string
end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@machine.event :park
@@ -2071,7 +2077,7 @@ def setup
ActiveRecordTest.const_set('Company', @company)
@vehicle = new_model(:vehicle) do
- connection.add_column :vehicle, :company_id, :integer
+ connection.add_column table_name, :company_id, :integer
belongs_to :company, :class_name => 'ActiveRecordTest::Company'
end
ActiveRecordTest.const_set('Vehicle', @vehicle)
@@ -2085,18 +2091,20 @@ def setup
end
def test_should_find_records_in_with_scope
- assert_equal [@mustang], @vehicle.with_states(:parked).find(:all, :include => :company, :conditions => 'company.state = "active"')
+ assert_equal [@mustang], @vehicle.with_states(:parked).find(:all, :joins => :company, :conditions => "#{@company.table_name}.state = \"active\"")
end
def test_should_find_records_in_without_scope
- assert_equal [@mustang], @vehicle.without_states(:idling).find(:all, :include => :company, :conditions => 'company.state = "active"')
+ assert_equal [@mustang], @vehicle.without_states(:idling).find(:all, :joins => :company, :conditions => "#{@company.table_name}.state = \"active\"")
end
def teardown
ActiveRecordTest.class_eval do
remove_const('Vehicle')
remove_const('Company')
end
+ ActiveSupport::Dependencies.clear if defined?(ActiveSupport::Dependencies)
+ super
end
end
else
View
1 test/unit/integrations/base_test.rb
@@ -98,6 +98,7 @@ def version2_included?
def teardown
StateMachine::Integrations.send(:remove_const, 'Custom')
+ super
end
end
end
View
9 test/unit/integrations/data_mapper_test.rb
@@ -18,14 +18,16 @@ def default_test
end
def teardown
+ super
@resources.uniq.each {|resource| DataMapperTest.send(:remove_const, resource)} if instance_variable_defined?('@resources')
end
protected
# Creates a new DataMapper resource (and the associated table)
def new_resource(create_table = :foo, &block)
- table_name = create_table || :foo
- name = table_name.to_s.capitalize
+ base_table_name = create_table || :foo
+ name = base_table_name.to_s.capitalize
+ table_name = "#{base_table_name}_#{rand(1000000)}"
resource = Class.new
DataMapperTest.send(:remove_const, name) if DataMapperTest.const_defined?(name)
@@ -383,6 +385,7 @@ def test_should_not_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -407,6 +410,7 @@ def test_should_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -432,6 +436,7 @@ def test_should_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
View
16 test/unit/integrations/mongo_mapper_test.rb
@@ -11,14 +11,23 @@ class BaseTestCase < Test::Unit::TestCase
def default_test
end
+ def teardown
+ if @table_names
+ MongoMapper.database.collections.each {|c| c.drop if @table_names.include?(c.name)}
+ end
+ end
+
protected
# Creates a new MongoMapper model (and the associated table)
- def new_model(table_name = :foo, &block)
+ def new_model(name = :foo, &block)
+ table_name = "#{name}_#{rand(1000000)}"
+ @table_names ||= []
+ @table_names << table_name
model = Class.new do
(class << self; self; end).class_eval do
- define_method(:name) { "MongoMapperTest::#{table_name.to_s.capitalize}" }
- define_method(:to_s) { name }
+ define_method(:name) { "MongoMapperTest::#{name.to_s.capitalize}" }
+ define_method(:to_s) { self.name }
end
end
@@ -29,7 +38,6 @@ def new_model(table_name = :foo, &block)
key :state, String
end
model.class_eval(&block) if block_given?
- model.collection.remove
model
end
end
View
26 test/unit/integrations/mongoid_test.rb
@@ -5,7 +5,7 @@
# Establish database connection
Mongoid.configure do |config|
- if ::Mongoid::VERSION =~ /^2\./
+ if Mongoid::VERSION =~ /^2\./
config.master = Mongo::Connection.new('127.0.0.1', 27017, :slave_ok => true).db('test')
else
config.connect_to('test')
@@ -17,13 +17,24 @@ class BaseTestCase < Test::Unit::TestCase
def default_test
end
+ def teardown
+ if @table_names
+ db = Mongoid::VERSION =~ /^2\./ ? Mongoid.master : Mongoid::Sessions.default
+ db.collections.each {|c| c.drop if @table_names.include?(c.name)}
+ end
+ end
+
protected
# Creates a new Mongoid model (and the associated table)
- def new_model(table_name = :foo, &block)
+ def new_model(name = :foo, &block)
+ table_name = "#{name}_#{rand(1000000)}"
+ @table_names ||= []
+ @table_names << table_name
+
model = Class.new do
(class << self; self; end).class_eval do
- define_method(:name) { "MongoidTest::#{table_name.to_s.capitalize}" }
- define_method(:to_s) { name }
+ define_method(:name) { "MongoidTest::#{name.to_s.capitalize}" }
+ define_method(:to_s) { self.name }
end
end
@@ -38,7 +49,6 @@ def new_model(table_name = :foo, &block)
field :state, :type => String
end
model.class_eval(&block) if block_given?
- model.delete_all
model
end
@@ -383,6 +393,7 @@ def test_should_not_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -407,6 +418,7 @@ def test_should_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -432,6 +444,7 @@ def test_should_generate_a_warning
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -476,6 +489,7 @@ def test_should_output_warning_with_same_machine_attribute
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -646,6 +660,7 @@ def test_should_not_delegate_attribute_predicate_with_different_attribute
def teardown
$stderr = @original_stderr
+ super
end
end
@@ -1896,6 +1911,7 @@ def test_should_only_include_records_without_subclass_states_in_without_scope
def teardown
MongoidTest.send(:remove_const, 'SubFoo')
MongoidTest.send(:remove_const, 'Foo')
+ super
end
end
View
49 test/unit/integrations/sequel_test.rb
@@ -15,7 +15,8 @@ def default_test
protected
# Creates a new Sequel model (and the associated table)
def new_model(create_table = :foo, &block)
- table_name = create_table || :foo
+ name = create_table || :foo
+ table_name = "#{name}_#{rand(1000000)}"
table_identifier = ::Sequel::SQL::Identifier.new(table_name)
if !defined?(Sequel::VERSION) || Gem::Version.new(::Sequel::VERSION) <= Gem::Version.new('3.26.0')
@@ -32,7 +33,8 @@ def to_s(*args); args.empty? ? inspect : original_to_s(*args); end
model = Class.new(Sequel::Model) do
self.raise_on_save_failure = false
(class << self; self; end).class_eval do
- define_method(:name) { "SequelTest::#{table_name.to_s.capitalize}" }
+ define_method(:name) { "SequelTest::#{name.to_s.capitalize}" }
+ define_method(:table_identifier) { table_identifier }
end
set_dataset(DB[table_identifier])
@@ -344,12 +346,15 @@ class MachineWithSameColumnDefaultTest < BaseTestCase
def setup
@original_stderr, $stderr = $stderr, StringIO.new
- DB.create_table!(::Sequel::SQL::Identifier.new(:foo)) do
+ @model = new_model(false)
+ DB.create_table!(@model.table_identifier) do
primary_key :id
column :status, :string, :default => 'parked'
end
- @model = new_model(false)
- @model.class_eval { get_db_schema(true) }
+ @model.class_eval do
+ set_dataset(DB[table_identifier])
+ get_db_schema(true)
+ end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@record = @model.new
@@ -372,12 +377,15 @@ class MachineWithDifferentColumnDefaultTest < BaseTestCase
def setup
@original_stderr, $stderr = $stderr, StringIO.new
- DB.create_table!(::Sequel::SQL::Identifier.new(:foo)) do
+ @model = new_model(false)
+ DB.create_table!(@model.table_identifier) do
primary_key :id
column :status, :string, :default => 'idling'
end
- @model = new_model(false)
- @model.class_eval { get_db_schema(true) }
+ @model.class_eval do
+ set_dataset(DB[table_identifier])
+ get_db_schema(true)
+ end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@record = @model.new
@@ -400,12 +408,15 @@ class MachineWithDifferentIntegerColumnDefaultTest < BaseTestCase
def setup
@original_stderr, $stderr = $stderr, StringIO.new
- DB.create_table!(::Sequel::SQL::Identifier.new(:foo)) do
+ @model = new_model(false)
+ DB.create_table!(@model.table_identifier) do
primary_key :id
column :status, :integer, :default => 0
end
- @model = new_model(false)
- @model.class_eval { get_db_schema(true) }
+ @model.class_eval do
+ set_dataset(DB[table_identifier])
+ get_db_schema(true)
+ end
@machine = StateMachine::Machine.new(@model, :status, :initial => :parked)
@machine.state :parked, :value => 1
@@ -582,7 +593,7 @@ def test_should_use_default_state_if_protected
class MachineMultipleTest < BaseTestCase
def setup
@model = new_model
- DB.alter_table :foo do
+ DB.alter_table(@model.table_identifier) do
add_column :status, :string
end
@model.class_eval { get_db_schema(true) }
@@ -634,7 +645,7 @@ def setup
end
end
- DB.alter_table :foo do
+ DB.alter_table(@model.table_identifier) do
add_column :updated_at, :datetime
end
@model.class_eval { get_db_schema(true) }
@@ -697,7 +708,7 @@ def test_should_include_state_in_changed_attributes
class MachineWithDirtyAttributesAndCustomAttributeTest < BaseTestCase
def setup
@model = new_model
- DB.alter_table :foo do
+ DB.alter_table(@model.table_identifier) do
add_column :status, :string
end
@model.class_eval { get_db_schema(true) }
@@ -720,7 +731,7 @@ def test_should_include_state_in_changed_attributes
class MachineWithDirtyAttributeAndCustomAttributesDuringLoopbackTest < BaseTestCase
def setup
@model = new_model
- DB.alter_table :foo do
+ DB.alter_table(@model.table_identifier) do
add_column :status, :string
end
@model.class_eval { get_db_schema(true) }
@@ -1455,7 +1466,7 @@ def test_should_run_after_transitions_within_transaction
assert_equal 0, @model.count
end
-
+
def test_should_run_around_transition_within_transaction
@machine.around_transition {|block| block.call; self.class.create; raise Sequel::Error::Rollback }
@@ -1651,7 +1662,7 @@ def setup
@vehicle = new_model(:vehicle) do
many_to_one :company, :class => SequelTest::Company
end
- DB.alter_table :vehicle do
+ DB.alter_table(@vehicle.table_identifier) do
add_column :company_id, :integer
end
@vehicle.class_eval { get_db_schema(true) }
@@ -1666,11 +1677,11 @@ def setup
end
def test_should_find_records_in_with_scope
- assert_equal [@mustang], @vehicle.with_states(:parked).join(:company, :id => :company_id).filter(:company__state => 'active').select(:vehicle.*).all
+ assert_equal [@mustang], @vehicle.with_states(:parked).join(@company.table_identifier.value, :id => :company_id).filter(:"#{@company.table_identifier.value}__state" => 'active').select(@vehicle.table_identifier.value.to_sym.*).all
end
def test_should_find_records_in_without_scope
- assert_equal [@mustang], @vehicle.without_states(:idling).join(:company, :id => :company_id).filter(:company__state => 'active').select(:vehicle.*).all
+ assert_equal [@mustang], @vehicle.without_states(:idling).join(@company.table_identifier.value, :id => :company_id).filter(:"#{@company.table_identifier.value}__state" => 'active').select(@vehicle.table_identifier.value.to_sym.*).all
end
def teardown
View
60 test/unit/machine_test.rb
@@ -3216,7 +3216,7 @@ def test_should_set_custom_initial_state
class MachineDrawingTest < Test::Unit::TestCase
def setup
@klass = Class.new do
- def self.name; 'Vehicle'; end
+ def self.name; @name ||= "Vehicle_#{rand(1000000)}"; end
end
@machine = StateMachine::Machine.new(@klass, :initial => :parked)
@machine.event :ignite do
@@ -3230,22 +3230,26 @@ def test_should_raise_exception_if_invalid_option_specified
def test_should_save_file_with_class_name_by_default
@machine.draw
- assert File.exists?('./Vehicle_state.png')
+ assert File.exists?("./#{@klass.name}_state.png")
end
def test_should_allow_base_name_to_be_customized
- @machine.draw(:name => 'machine')
- assert File.exists?('./machine.png')
+ name = "machine_#{rand(1000000)}"
+ @machine.draw(:name => name)
+ @path = "./#{name}.png"
+ assert File.exists?(@path)
end
def test_should_allow_format_to_be_customized
@machine.draw(:format => 'jpg')
- assert File.exists?('./Vehicle_state.jpg')
+ @path = "./#{@klass.name}_state.jpg"
+ assert File.exists?(@path)
end
def test_should_allow_path_to_be_customized
@machine.draw(:path => "#{File.dirname(__FILE__)}/")
- assert File.exists?("#{File.dirname(__FILE__)}/Vehicle_state.png")
+ @path = "#{File.dirname(__FILE__)}/#{@klass.name}_state.png"
+ assert File.exists?(@path)
end
def test_should_allow_orientation_to_be_landscape
@@ -3274,14 +3278,14 @@ def test_should_allow_human_names_to_be_displayed
end
def teardown
- FileUtils.rm Dir["{.,#{File.dirname(__FILE__)}}/*.{png,jpg}"]
+ FileUtils.rm Dir[@path || "./#{@klass.name}_state.png"]
end
end
class MachineDrawingWithIntegerStatesTest < Test::Unit::TestCase
def setup
@klass = Class.new do
- def self.name; 'Vehicle'; end
+ def self.name; @name ||= "Vehicle_#{rand(1000000)}"; end
end
@machine = StateMachine::Machine.new(@klass, :state_id, :initial => :parked)
@machine.event :ignite do
@@ -3301,16 +3305,18 @@ def test_should_draw_all_events
end
def test_should_draw_machine
- assert File.exist?('./Vehicle_state_id.png')
- ensure
- FileUtils.rm('./Vehicle_state_id.png')
+ assert File.exist?("./#{@klass.name}_state_id.png")
+ end
+
+ def teardown
+ FileUtils.rm Dir["./#{@klass.name}_state_id.png"]
end
end
class MachineDrawingWithNilStatesTest < Test::Unit::TestCase
def setup
@klass = Class.new do
- def self.name; 'Vehicle'; end
+ def self.name; @name ||= "Vehicle_#{rand(1000000)}"; end
end
@machine = StateMachine::Machine.new(@klass, :initial => :parked)
@machine.event :ignite do
@@ -3329,16 +3335,18 @@ def test_should_draw_all_events
end
def test_should_draw_machine
- assert File.exist?('./Vehicle_state.png')
- ensure
- FileUtils.rm('./Vehicle_state.png')
+ assert File.exist?("./#{@klass.name}_state.png")
+ end
+
+ def teardown
+ FileUtils.rm Dir["./#{@klass.name}_state.png"]
end
end
class MachineDrawingWithDynamicStatesTest < Test::Unit::TestCase
def setup
@klass = Class.new do
- def self.name; 'Vehicle'; end
+ def self.name; @name ||= "Vehicle_#{rand(1000000)}"; end
end
@machine = StateMachine::Machine.new(@klass, :initial => :parked)
@machine.event :activate do
@@ -3357,16 +3365,18 @@ def test_should_draw_all_events
end
def test_should_draw_machine
- assert File.exist?('./Vehicle_state.png')
- ensure
- FileUtils.rm('./Vehicle_state.png')
+ assert File.exist?("./#{@klass.name}_state.png")
+ end
+
+ def teardown
+ FileUtils.rm Dir["./#{@klass.name}_state.png"]
end
end
class MachineClassDrawingTest < Test::Unit::TestCase
def setup
@klass = Class.new do
- def self.name; 'Vehicle'; end
+ def self.name; @name ||= "Vehicle_#{rand(1000000)}"; end
end
@machine = StateMachine::Machine.new(@klass)
@machine.event :ignite do
@@ -3382,15 +3392,15 @@ def test_should_raise_exception_if_no_class_names_specified
def test_should_load_files
StateMachine::Machine.draw('Switch', :file => File.expand_path("#{File.dirname(__FILE__)}/../files/switch.rb"))
assert defined?(::Switch)
- ensure
- FileUtils.rm('./Switch_state.png')
end
def test_should_allow_path_and_format_to_be_customized
StateMachine::Machine.draw('Switch', :file => File.expand_path("#{File.dirname(__FILE__)}/../files/switch.rb"), :path => "#{File.dirname(__FILE__)}/", :format => 'jpg')
- assert File.exist?("#{File.dirname(__FILE__)}/Switch_state.jpg")
- ensure
- FileUtils.rm("#{File.dirname(__FILE__)}/Switch_state.jpg")
+ assert File.exist?("#{File.dirname(__FILE__)}/#{Switch.name}_state.jpg")
+ end
+
+ def teardown
+ FileUtils.rm Dir["{.,#{File.dirname(__FILE__)}}/#{Switch.name}_state.{jpg,png}"]
end
end
rescue LoadError

0 comments on commit e738ba9

Please sign in to comment.