2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/post'
|
|
|
|
require 'models/binary'
|
|
|
|
require 'models/topic'
|
|
|
|
require 'models/computer'
|
|
|
|
require 'models/developer'
|
|
|
|
require 'models/company'
|
|
|
|
require 'models/task'
|
|
|
|
require 'models/reply'
|
|
|
|
require 'models/joke'
|
|
|
|
require 'models/course'
|
|
|
|
require 'models/category'
|
|
|
|
require 'models/parrot'
|
|
|
|
require 'models/pirate'
|
|
|
|
require 'models/treasure'
|
|
|
|
require 'models/matey'
|
|
|
|
require 'models/ship'
|
2008-07-13 21:01:52 -04:00
|
|
|
require 'models/book'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class FixturesTest < ActiveRecord::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
self.use_instantiated_fixtures = true
|
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
|
2007-05-21 14:54:51 -04:00
|
|
|
fixtures :topics, :developers, :accounts, :tasks, :categories, :funny_jokes, :binaries
|
2004-12-09 10:52:54 -05:00
|
|
|
|
2007-05-21 14:54:51 -04:00
|
|
|
FIXTURES = %w( accounts binaries companies customers
|
2004-11-23 20:04:44 -05:00
|
|
|
developers developers_projects entrants
|
2005-04-07 02:54:25 -04:00
|
|
|
movies projects subscribers topics tasks )
|
2004-11-23 20:04:44 -05:00
|
|
|
MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-_\w]*/
|
|
|
|
|
|
|
|
def test_clean_fixtures
|
|
|
|
FIXTURES.each do |name|
|
|
|
|
fixtures = nil
|
|
|
|
assert_nothing_raised { fixtures = create_fixtures(name) }
|
|
|
|
assert_kind_of(Fixtures, fixtures)
|
|
|
|
fixtures.each { |name, fixture|
|
|
|
|
fixture.each { |key, value|
|
|
|
|
assert_match(MATCH_ATTRIBUTE_NAME, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiple_clean_fixtures
|
|
|
|
fixtures_array = nil
|
|
|
|
assert_nothing_raised { fixtures_array = create_fixtures(*FIXTURES) }
|
|
|
|
assert_kind_of(Array, fixtures_array)
|
|
|
|
fixtures_array.each { |fixtures| assert_kind_of(Fixtures, fixtures) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attributes
|
|
|
|
topics = create_fixtures("topics")
|
|
|
|
assert_equal("The First Topic", topics["first"]["title"])
|
|
|
|
assert_nil(topics["second"]["author_email_address"])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inserts
|
|
|
|
topics = create_fixtures("topics")
|
2007-09-28 10:56:07 -04:00
|
|
|
first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'David'")
|
|
|
|
assert_equal("The First Topic", first_row["title"])
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-09-28 10:56:07 -04:00
|
|
|
second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'Mary'")
|
|
|
|
assert_nil(second_row["author_email_address"])
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-11-16 03:16:54 -05:00
|
|
|
if ActiveRecord::Base.connection.supports_migrations?
|
|
|
|
def test_inserts_with_pre_and_suffix
|
2007-09-28 10:56:07 -04:00
|
|
|
# Reset cache to make finds on the new table work
|
2007-09-28 10:05:03 -04:00
|
|
|
Fixtures.reset_cache
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2005-11-16 03:16:54 -05:00
|
|
|
ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t|
|
|
|
|
t.column :title, :string
|
|
|
|
t.column :author_name, :string
|
|
|
|
t.column :author_email_address, :string
|
|
|
|
t.column :written_on, :datetime
|
|
|
|
t.column :bonus_time, :time
|
|
|
|
t.column :last_read, :date
|
2006-03-01 11:01:53 -05:00
|
|
|
t.column :content, :string
|
2005-11-16 03:16:54 -05:00
|
|
|
t.column :approved, :boolean, :default => true
|
|
|
|
t.column :replies_count, :integer, :default => 0
|
|
|
|
t.column :parent_id, :integer
|
|
|
|
t.column :type, :string, :limit => 50
|
|
|
|
end
|
|
|
|
|
|
|
|
# Store existing prefix/suffix
|
|
|
|
old_prefix = ActiveRecord::Base.table_name_prefix
|
|
|
|
old_suffix = ActiveRecord::Base.table_name_suffix
|
|
|
|
|
|
|
|
# Set a prefix/suffix we can test against
|
|
|
|
ActiveRecord::Base.table_name_prefix = 'prefix_'
|
|
|
|
ActiveRecord::Base.table_name_suffix = '_suffix'
|
|
|
|
|
|
|
|
topics = create_fixtures("topics")
|
|
|
|
|
2007-09-28 10:56:07 -04:00
|
|
|
first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'David'")
|
|
|
|
assert_equal("The First Topic", first_row["title"])
|
2005-11-16 03:16:54 -05:00
|
|
|
|
2007-09-28 10:56:07 -04:00
|
|
|
second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'")
|
|
|
|
assert_nil(second_row["author_email_address"])
|
2008-04-30 17:59:22 -04:00
|
|
|
|
|
|
|
# This checks for a caching problem which causes a bug in the fixtures
|
|
|
|
# class-level configuration helper.
|
|
|
|
assert_not_nil topics, "Fixture data inserted, but fixture objects not returned from create"
|
2005-11-16 03:16:54 -05:00
|
|
|
ensure
|
2006-03-01 11:01:53 -05:00
|
|
|
# Restore prefix/suffix to its previous values
|
2007-09-28 10:56:07 -04:00
|
|
|
ActiveRecord::Base.table_name_prefix = old_prefix
|
|
|
|
ActiveRecord::Base.table_name_suffix = old_suffix
|
2006-03-01 11:01:53 -05:00
|
|
|
|
2005-11-16 03:16:54 -05:00
|
|
|
ActiveRecord::Base.connection.drop_table :prefix_topics_suffix rescue nil
|
2005-09-02 06:51:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-07 02:54:25 -04:00
|
|
|
def test_insert_with_datetime
|
|
|
|
topics = create_fixtures("tasks")
|
|
|
|
first = Task.find(1)
|
|
|
|
assert first
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_logger_level_invariant
|
|
|
|
level = ActiveRecord::Base.logger.level
|
|
|
|
create_fixtures('topics')
|
|
|
|
assert_equal level, ActiveRecord::Base.logger.level
|
|
|
|
end
|
2004-12-09 10:52:54 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_instantiation
|
|
|
|
topics = create_fixtures("topics")
|
|
|
|
assert_kind_of Topic, topics["first"].find
|
|
|
|
end
|
2004-12-09 10:52:54 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_complete_instantiation
|
2008-03-23 22:50:02 -04:00
|
|
|
assert_equal 4, @topics.size
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "The First Topic", @first.title
|
|
|
|
end
|
2004-12-09 10:52:54 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_fixtures_from_root_yml_with_instantiation
|
|
|
|
# assert_equal 2, @accounts.size
|
|
|
|
assert_equal 50, @unknown.credit_limit
|
|
|
|
end
|
2004-12-09 10:52:54 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_erb_in_fixtures
|
2006-01-03 22:43:28 -05:00
|
|
|
assert_equal 11, @developers.size
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "fixture_5", @dev_5.name
|
|
|
|
end
|
2004-12-14 19:46:26 -05:00
|
|
|
|
|
|
|
def test_empty_yaml_fixture
|
2008-01-21 12:20:51 -05:00
|
|
|
assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/yml/accounts")
|
2004-12-14 19:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_yaml_fixture_with_a_comment_in_it
|
2008-01-21 12:20:51 -05:00
|
|
|
assert_not_nil Fixtures.new( Account.connection, "companies", 'Company', FIXTURES_ROOT + "/naked/yml/companies")
|
2004-12-14 19:46:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dirty_dirty_yaml_file
|
|
|
|
assert_raises(Fixture::FormatError) do
|
2008-01-21 12:20:51 -05:00
|
|
|
Fixtures.new( Account.connection, "courses", 'Course', FIXTURES_ROOT + "/naked/yml/courses")
|
2004-12-14 19:46:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_csv_fixtures
|
2008-01-21 12:20:51 -05:00
|
|
|
assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', FIXTURES_ROOT + "/naked/csv/accounts")
|
2004-12-14 19:46:26 -05:00
|
|
|
end
|
2005-10-14 22:01:38 -04:00
|
|
|
|
|
|
|
def test_omap_fixtures
|
|
|
|
assert_nothing_raised do
|
2008-01-21 12:20:51 -05:00
|
|
|
fixtures = Fixtures.new(Account.connection, 'categories', 'Category', FIXTURES_ROOT + "/categories_ordered")
|
2005-10-14 22:01:38 -04:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
fixtures.each do |name, fixture|
|
|
|
|
assert_equal "fixture_no_#{i}", name
|
|
|
|
assert_equal "Category #{i}", fixture['name']
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
end
|
2005-10-15 23:45:39 -04:00
|
|
|
end
|
2006-02-27 00:14:57 -05:00
|
|
|
|
|
|
|
def test_yml_file_in_subdirectory
|
2006-02-28 15:39:21 -05:00
|
|
|
assert_equal(categories(:sub_special_1).name, "A special category in a subdir file")
|
|
|
|
assert_equal(categories(:sub_special_1).class, SpecialCategory)
|
2006-02-27 00:14:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_subsubdir_file_with_arbitrary_name
|
2006-02-28 15:39:21 -05:00
|
|
|
assert_equal(categories(:sub_special_3).name, "A special category in an arbitrarily named subsubdir file")
|
|
|
|
assert_equal(categories(:sub_special_3).class, SpecialCategory)
|
2006-02-27 00:14:57 -05:00
|
|
|
end
|
|
|
|
|
2007-05-21 14:54:51 -04:00
|
|
|
def test_binary_in_fixtures
|
|
|
|
assert_equal 1, @binaries.size
|
2008-04-01 02:58:52 -04:00
|
|
|
data = File.read(ASSETS_ROOT + "/flowers.jpg")
|
|
|
|
data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
|
|
|
|
data.freeze
|
2007-05-21 14:54:51 -04:00
|
|
|
assert_equal data, @flowers.data
|
|
|
|
end
|
2005-10-18 19:52:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if Account.connection.respond_to?(:reset_pk_sequence!)
|
2008-01-21 12:20:51 -05:00
|
|
|
class FixturesResetPkSequenceTest < ActiveRecord::TestCase
|
2005-10-18 19:52:07 -04:00
|
|
|
fixtures :accounts
|
2005-11-12 06:59:54 -05:00
|
|
|
fixtures :companies
|
2005-10-15 23:45:39 -04:00
|
|
|
|
2005-11-12 06:59:54 -05:00
|
|
|
def setup
|
|
|
|
@instances = [Account.new(:credit_limit => 50), Company.new(:name => 'RoR Consulting')]
|
2007-09-28 10:56:07 -04:00
|
|
|
Fixtures.reset_cache # make sure tables get reinitialized
|
2005-11-12 06:59:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_resets_to_min_pk_with_specified_pk_and_sequence
|
|
|
|
@instances.each do |instance|
|
|
|
|
model = instance.class
|
|
|
|
model.delete_all
|
|
|
|
model.connection.reset_pk_sequence!(model.table_name, model.primary_key, model.sequence_name)
|
2005-10-15 23:45:39 -04:00
|
|
|
|
2005-11-12 06:59:54 -05:00
|
|
|
instance.save!
|
|
|
|
assert_equal 1, instance.id, "Sequence reset for #{model.table_name} failed."
|
|
|
|
end
|
2005-10-15 23:45:39 -04:00
|
|
|
end
|
|
|
|
|
2005-11-12 06:59:54 -05:00
|
|
|
def test_resets_to_min_pk_with_default_pk_and_sequence
|
|
|
|
@instances.each do |instance|
|
|
|
|
model = instance.class
|
|
|
|
model.delete_all
|
|
|
|
model.connection.reset_pk_sequence!(model.table_name)
|
|
|
|
|
|
|
|
instance.save!
|
|
|
|
assert_equal 1, instance.id, "Sequence reset for #{model.table_name} failed."
|
2005-10-15 23:45:39 -04:00
|
|
|
end
|
2005-11-12 06:59:54 -05:00
|
|
|
end
|
2005-10-15 23:45:39 -04:00
|
|
|
|
2007-09-28 10:56:07 -04:00
|
|
|
def test_create_fixtures_resets_sequences_when_not_cached
|
2005-11-12 06:59:54 -05:00
|
|
|
@instances.each do |instance|
|
|
|
|
max_id = create_fixtures(instance.class.table_name).inject(0) do |max_id, (name, fixture)|
|
|
|
|
fixture_id = fixture['id'].to_i
|
|
|
|
fixture_id > max_id ? fixture_id : max_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# Clone the last fixture to check that it gets the next greatest id.
|
|
|
|
instance.save!
|
|
|
|
assert_equal max_id + 1, instance.id, "Sequence reset for #{instance.class.table_name} failed."
|
|
|
|
end
|
2005-10-15 23:45:39 -04:00
|
|
|
end
|
2005-10-14 22:01:38 -04:00
|
|
|
end
|
2005-03-06 08:51:55 -05:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
|
2005-03-06 08:51:55 -05:00
|
|
|
self.use_instantiated_fixtures = false
|
|
|
|
fixtures :topics, :developers, :accounts
|
|
|
|
|
|
|
|
def test_without_complete_instantiation
|
|
|
|
assert_nil @first
|
2005-03-20 08:42:35 -05:00
|
|
|
assert_nil @topics
|
|
|
|
assert_nil @developers
|
|
|
|
assert_nil @accounts
|
2005-03-06 08:51:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fixtures_from_root_yml_without_instantiation
|
|
|
|
assert_nil @unknown
|
|
|
|
end
|
2005-06-03 07:49:34 -04:00
|
|
|
|
|
|
|
def test_accessor_methods
|
|
|
|
assert_equal "The First Topic", topics(:first).title
|
|
|
|
assert_equal "Jamis", developers(:jamis).name
|
|
|
|
assert_equal 50, accounts(:signals37).credit_limit
|
|
|
|
end
|
2007-06-04 23:47:02 -04:00
|
|
|
|
|
|
|
def test_accessor_methods_with_multiple_args
|
|
|
|
assert_equal 2, topics(:first, :second).size
|
|
|
|
assert_raise(StandardError) { topics([:first, :second]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
uses_mocha 'reloading_fixtures_through_accessor_methods' do
|
|
|
|
def test_reloading_fixtures_through_accessor_methods
|
|
|
|
assert_equal "The First Topic", topics(:first).title
|
|
|
|
@loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
|
|
|
|
assert_equal "Fresh Topic!", topics(:first, true).title
|
|
|
|
end
|
|
|
|
end
|
2005-03-06 08:51:55 -05:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class FixturesWithoutInstanceInstantiationTest < ActiveRecord::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
self.use_instantiated_fixtures = true
|
2005-03-20 08:42:35 -05:00
|
|
|
self.use_instantiated_fixtures = :no_instances
|
2005-06-10 10:58:02 -04:00
|
|
|
|
2005-03-20 08:42:35 -05:00
|
|
|
fixtures :topics, :developers, :accounts
|
|
|
|
|
|
|
|
def test_without_instance_instantiation
|
|
|
|
assert_nil @first
|
|
|
|
assert_not_nil @topics
|
|
|
|
assert_not_nil @developers
|
|
|
|
assert_not_nil @accounts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class TransactionalFixturesTest < ActiveRecord::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
self.use_instantiated_fixtures = true
|
2005-03-06 08:51:55 -05:00
|
|
|
self.use_transactional_fixtures = true
|
2005-06-10 10:58:02 -04:00
|
|
|
|
2005-03-06 08:51:55 -05:00
|
|
|
fixtures :topics
|
|
|
|
|
|
|
|
def test_destroy
|
|
|
|
assert_not_nil @first
|
|
|
|
@first.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy_just_kidding
|
|
|
|
assert_not_nil @first
|
|
|
|
end
|
2004-12-01 07:25:04 -05:00
|
|
|
end
|
2005-03-17 14:36:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class MultipleFixturesTest < ActiveRecord::TestCase
|
2005-03-17 14:36:44 -05:00
|
|
|
fixtures :topics
|
|
|
|
fixtures :developers, :accounts
|
|
|
|
|
|
|
|
def test_fixture_table_names
|
2005-10-23 15:02:38 -04:00
|
|
|
assert_equal %w(topics developers accounts), fixture_table_names
|
2005-03-17 14:36:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class SetupTest < ActiveRecord::TestCase
|
2007-12-19 17:49:27 -05:00
|
|
|
# fixtures :topics
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-12-19 17:49:27 -05:00
|
|
|
def setup
|
|
|
|
@first = true
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-12-19 17:49:27 -05:00
|
|
|
def test_nothing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SetupSubclassTest < SetupTest
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@second = true
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-12-19 17:49:27 -05:00
|
|
|
def test_subclassing_should_preserve_setups
|
|
|
|
assert @first
|
|
|
|
assert @second
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class OverlappingFixturesTest < ActiveRecord::TestCase
|
2005-03-17 14:36:44 -05:00
|
|
|
fixtures :topics, :developers
|
|
|
|
fixtures :developers, :accounts
|
|
|
|
|
|
|
|
def test_fixture_table_names
|
2005-10-23 15:02:38 -04:00
|
|
|
assert_equal %w(topics developers accounts), fixture_table_names
|
2005-03-17 14:36:44 -05:00
|
|
|
end
|
|
|
|
end
|
2005-04-18 03:52:58 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ForeignKeyFixturesTest < ActiveRecord::TestCase
|
2005-04-18 03:52:58 -04:00
|
|
|
fixtures :fk_test_has_pk, :fk_test_has_fk
|
|
|
|
|
|
|
|
# if foreign keys are implemented and fixtures
|
|
|
|
# are not deleted in reverse order then this test
|
|
|
|
# case will raise StatementInvalid
|
|
|
|
|
|
|
|
def test_number1
|
|
|
|
assert true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_number2
|
|
|
|
assert true
|
|
|
|
end
|
|
|
|
end
|
2006-02-27 15:29:28 -05:00
|
|
|
|
2008-03-28 16:21:20 -04:00
|
|
|
class CheckSetTableNameFixturesTest < ActiveRecord::TestCase
|
2006-02-27 15:29:28 -05:00
|
|
|
set_fixture_class :funny_jokes => 'Joke'
|
|
|
|
fixtures :funny_jokes
|
2008-03-28 07:55:01 -04:00
|
|
|
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
|
|
|
# and thus takes into account our set_fixture_class
|
|
|
|
self.use_transactional_fixtures = false
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2006-02-27 15:29:28 -05:00
|
|
|
def test_table_method
|
|
|
|
assert_kind_of Joke, funny_jokes(:a_joke)
|
|
|
|
end
|
|
|
|
end
|
2006-03-06 18:03:35 -05:00
|
|
|
|
2008-07-13 21:01:52 -04:00
|
|
|
class FixtureNameIsNotTableNameFixturesTest < ActiveRecord::TestCase
|
|
|
|
set_fixture_class :items => Book
|
|
|
|
fixtures :items
|
|
|
|
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
|
|
|
# and thus takes into account our set_fixture_class
|
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
|
|
|
|
def test_named_accessor
|
|
|
|
assert_kind_of Book, items(:dvd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FixtureNameIsNotTableNameMultipleFixturesTest < ActiveRecord::TestCase
|
|
|
|
set_fixture_class :items => Book, :funny_jokes => Joke
|
|
|
|
fixtures :items, :funny_jokes
|
|
|
|
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
|
|
|
# and thus takes into account our set_fixture_class
|
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
|
|
|
|
def test_named_accessor_of_differently_named_fixture
|
|
|
|
assert_kind_of Book, items(:dvd)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_accessor_of_same_named_fixture
|
|
|
|
assert_kind_of Joke, funny_jokes(:a_joke)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class CustomConnectionFixturesTest < ActiveRecord::TestCase
|
2007-01-23 20:29:49 -05:00
|
|
|
set_fixture_class :courses => Course
|
|
|
|
fixtures :courses
|
2008-03-28 07:55:01 -04:00
|
|
|
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
|
|
|
# and thus takes into account our set_fixture_class
|
|
|
|
self.use_transactional_fixtures = false
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2007-01-23 20:29:49 -05:00
|
|
|
def test_connection
|
|
|
|
assert_kind_of Course, courses(:ruby)
|
|
|
|
assert_equal Course.connection, courses(:ruby).connection
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InvalidTableNameFixturesTest < ActiveRecord::TestCase
|
2006-03-06 18:03:35 -05:00
|
|
|
fixtures :funny_jokes
|
2008-03-28 07:55:01 -04:00
|
|
|
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
|
|
|
# and thus takes into account our lack of set_fixture_class
|
|
|
|
self.use_transactional_fixtures = false
|
2006-03-06 18:03:35 -05:00
|
|
|
|
|
|
|
def test_raises_error
|
|
|
|
assert_raises FixtureClassNotFound do
|
|
|
|
funny_jokes(:a_joke)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-08-11 19:38:46 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class CheckEscapedYamlFixturesTest < ActiveRecord::TestCase
|
2006-08-23 21:50:24 -04:00
|
|
|
set_fixture_class :funny_jokes => 'Joke'
|
|
|
|
fixtures :funny_jokes
|
2008-03-28 07:55:01 -04:00
|
|
|
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
|
|
|
# and thus takes into account our set_fixture_class
|
|
|
|
self.use_transactional_fixtures = false
|
2006-08-23 21:50:24 -04:00
|
|
|
|
|
|
|
def test_proper_escaped_fixture
|
|
|
|
assert_equal "The \\n Aristocrats\nAte the candy\n", funny_jokes(:another_joke).name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-28 10:56:07 -04:00
|
|
|
class DevelopersProject; end
|
2008-01-21 12:20:51 -05:00
|
|
|
class ManyToManyFixturesWithClassDefined < ActiveRecord::TestCase
|
2006-08-11 19:38:46 -04:00
|
|
|
fixtures :developers_projects
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2006-08-11 19:38:46 -04:00
|
|
|
def test_this_should_run_cleanly
|
|
|
|
assert true
|
|
|
|
end
|
2006-10-09 03:48:27 -04:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class FixturesBrokenRollbackTest < ActiveRecord::TestCase
|
2006-10-09 03:48:27 -04:00
|
|
|
def blank_setup; end
|
2008-01-05 08:34:15 -05:00
|
|
|
alias_method :ar_setup_fixtures, :setup_fixtures
|
|
|
|
alias_method :setup_fixtures, :blank_setup
|
2006-10-09 03:48:27 -04:00
|
|
|
alias_method :setup, :blank_setup
|
|
|
|
|
|
|
|
def blank_teardown; end
|
2008-01-05 08:34:15 -05:00
|
|
|
alias_method :ar_teardown_fixtures, :teardown_fixtures
|
|
|
|
alias_method :teardown_fixtures, :blank_teardown
|
2006-10-09 03:48:27 -04:00
|
|
|
alias_method :teardown, :blank_teardown
|
|
|
|
|
|
|
|
def test_no_rollback_in_teardown_unless_transaction_active
|
2008-07-02 00:01:26 -04:00
|
|
|
assert_equal 0, ActiveRecord::Base.connection.open_transactions
|
2008-01-05 08:34:15 -05:00
|
|
|
assert_raise(RuntimeError) { ar_setup_fixtures }
|
2008-07-02 00:01:26 -04:00
|
|
|
assert_equal 0, ActiveRecord::Base.connection.open_transactions
|
2008-01-05 08:34:15 -05:00
|
|
|
assert_nothing_raised { ar_teardown_fixtures }
|
2008-07-02 00:01:26 -04:00
|
|
|
assert_equal 0, ActiveRecord::Base.connection.open_transactions
|
2006-10-09 03:48:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def load_fixtures
|
|
|
|
raise 'argh'
|
|
|
|
end
|
|
|
|
end
|
2007-02-25 12:31:43 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class LoadAllFixturesTest < ActiveRecord::TestCase
|
|
|
|
self.fixture_path = FIXTURES_ROOT + "/all"
|
2007-02-25 12:31:43 -05:00
|
|
|
fixtures :all
|
|
|
|
|
|
|
|
def test_all_there
|
|
|
|
assert_equal %w(developers people tasks), fixture_table_names.sort
|
|
|
|
end
|
|
|
|
end
|
2007-09-28 10:05:03 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class FasterFixturesTest < ActiveRecord::TestCase
|
2007-09-28 10:05:03 -04:00
|
|
|
fixtures :categories, :authors
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2007-09-28 10:05:03 -04:00
|
|
|
def load_extra_fixture(name)
|
|
|
|
fixture = create_fixtures(name)
|
|
|
|
assert fixture.is_a?(Fixtures)
|
|
|
|
@loaded_fixtures[fixture.table_name] = fixture
|
|
|
|
end
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2007-09-28 10:05:03 -04:00
|
|
|
def test_cache
|
|
|
|
assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'categories')
|
|
|
|
assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'authors')
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2007-09-28 10:05:03 -04:00
|
|
|
assert_no_queries do
|
|
|
|
create_fixtures('categories')
|
|
|
|
create_fixtures('authors')
|
|
|
|
end
|
2007-09-28 10:56:07 -04:00
|
|
|
|
2007-09-28 10:05:03 -04:00
|
|
|
load_extra_fixture('posts')
|
|
|
|
assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'posts')
|
|
|
|
self.class.setup_fixture_accessors('posts')
|
|
|
|
assert_equal 'Welcome to the weblog', posts(:welcome).title
|
|
|
|
end
|
|
|
|
end
|
2007-10-26 01:56:46 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class FoxyFixturesTest < ActiveRecord::TestCase
|
2007-11-26 17:45:43 -05:00
|
|
|
fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers
|
2007-10-26 01:56:46 -04:00
|
|
|
|
|
|
|
def test_identifies_strings
|
|
|
|
assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo"))
|
|
|
|
assert_not_equal(Fixtures.identify("foo"), Fixtures.identify("FOO"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_identifies_symbols
|
|
|
|
assert_equal(Fixtures.identify(:foo), Fixtures.identify(:foo))
|
|
|
|
end
|
|
|
|
|
|
|
|
TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on)
|
|
|
|
|
|
|
|
def test_populates_timestamp_columns
|
|
|
|
TIMESTAMP_COLUMNS.each do |property|
|
|
|
|
assert_not_nil(parrots(:george).send(property), "should set #{property}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
def test_does_not_populate_timestamp_columns_if_model_has_set_record_timestamps_to_false
|
|
|
|
TIMESTAMP_COLUMNS.each do |property|
|
|
|
|
assert_nil(ships(:black_pearl).send(property), "should not set #{property}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-10-26 01:56:46 -04:00
|
|
|
def test_populates_all_columns_with_the_same_time
|
|
|
|
last = nil
|
|
|
|
|
|
|
|
TIMESTAMP_COLUMNS.each do |property|
|
|
|
|
current = parrots(:george).send(property)
|
|
|
|
last ||= current
|
|
|
|
|
|
|
|
assert_equal(last, current)
|
|
|
|
last = current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_only_populates_columns_that_exist
|
|
|
|
assert_not_nil(pirates(:blackbeard).created_on)
|
|
|
|
assert_not_nil(pirates(:blackbeard).updated_on)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_preserves_existing_fixture_data
|
|
|
|
assert_equal(2.weeks.ago.to_date, pirates(:redbeard).created_on.to_date)
|
|
|
|
assert_equal(2.weeks.ago.to_date, pirates(:redbeard).updated_on.to_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generates_unique_ids
|
|
|
|
assert_not_nil(parrots(:george).id)
|
|
|
|
assert_not_equal(parrots(:george).id, parrots(:louis).id)
|
|
|
|
end
|
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
def test_automatically_sets_primary_key
|
|
|
|
assert_not_nil(ships(:black_pearl))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_preserves_existing_primary_key
|
|
|
|
assert_equal(2, ships(:interceptor).id)
|
|
|
|
end
|
|
|
|
|
2007-10-26 01:56:46 -04:00
|
|
|
def test_resolves_belongs_to_symbols
|
|
|
|
assert_equal(parrots(:george), pirates(:blackbeard).parrot)
|
|
|
|
end
|
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
def test_ignores_belongs_to_symbols_if_association_and_foreign_key_are_named_the_same
|
|
|
|
assert_equal(developers(:david), computers(:workstation).developer)
|
|
|
|
end
|
|
|
|
|
2007-10-26 01:56:46 -04:00
|
|
|
def test_supports_join_tables
|
|
|
|
assert(pirates(:blackbeard).parrots.include?(parrots(:george)))
|
|
|
|
assert(pirates(:blackbeard).parrots.include?(parrots(:louis)))
|
|
|
|
assert(parrots(:george).pirates.include?(pirates(:blackbeard)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_supports_inline_habtm
|
|
|
|
assert(parrots(:george).treasures.include?(treasures(:diamond)))
|
|
|
|
assert(parrots(:george).treasures.include?(treasures(:sapphire)))
|
|
|
|
assert(!parrots(:george).treasures.include?(treasures(:ruby)))
|
|
|
|
end
|
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
def test_supports_inline_habtm_with_specified_id
|
|
|
|
assert(parrots(:polly).treasures.include?(treasures(:ruby)))
|
|
|
|
assert(parrots(:polly).treasures.include?(treasures(:sapphire)))
|
|
|
|
assert(!parrots(:polly).treasures.include?(treasures(:diamond)))
|
|
|
|
end
|
|
|
|
|
2007-10-26 01:56:46 -04:00
|
|
|
def test_supports_yaml_arrays
|
|
|
|
assert(parrots(:louis).treasures.include?(treasures(:diamond)))
|
|
|
|
assert(parrots(:louis).treasures.include?(treasures(:sapphire)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strips_DEFAULTS_key
|
|
|
|
assert_raise(StandardError) { parrots(:DEFAULTS) }
|
|
|
|
|
|
|
|
# this lets us do YAML defaults and not have an extra fixture entry
|
|
|
|
%w(sapphire ruby).each { |t| assert(parrots(:davey).treasures.include?(treasures(t))) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_supports_label_interpolation
|
|
|
|
assert_equal("frederick", parrots(:frederick).name)
|
|
|
|
end
|
2007-11-20 16:53:22 -05:00
|
|
|
|
|
|
|
def test_supports_polymorphic_belongs_to
|
|
|
|
assert_equal(pirates(:redbeard), treasures(:sapphire).looter)
|
|
|
|
assert_equal(parrots(:louis), treasures(:ruby).looter)
|
|
|
|
end
|
2007-11-26 17:46:11 -05:00
|
|
|
|
2007-11-25 17:08:38 -05:00
|
|
|
def test_only_generates_a_pk_if_necessary
|
|
|
|
m = Matey.find(:first)
|
|
|
|
m.pirate = pirates(:blackbeard)
|
|
|
|
m.target = pirates(:redbeard)
|
|
|
|
end
|
2007-11-26 17:46:11 -05:00
|
|
|
|
|
|
|
def test_supports_sti
|
|
|
|
assert_kind_of DeadParrot, parrots(:polly)
|
|
|
|
assert_equal pirates(:blackbeard), parrots(:polly).killer
|
|
|
|
end
|
2007-10-26 01:56:46 -04:00
|
|
|
end
|
2007-10-31 01:43:52 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ActiveSupportSubclassWithFixturesTest < ActiveRecord::TestCase
|
2007-10-31 01:43:52 -04:00
|
|
|
fixtures :parrots
|
|
|
|
|
|
|
|
# This seemingly useless assertion catches a bug that caused the fixtures
|
|
|
|
# setup code call nil[]
|
|
|
|
def test_foo
|
|
|
|
assert_equal parrots(:louis), Parrot.find_by_name("King Louis")
|
|
|
|
end
|
2007-11-26 17:45:43 -05:00
|
|
|
end
|
2008-03-17 17:48:28 -04:00
|
|
|
|
|
|
|
class FixtureLoadingTest < ActiveRecord::TestCase
|
2008-05-13 15:22:48 -04:00
|
|
|
uses_mocha 'reloading_fixtures_through_accessor_methods' do
|
|
|
|
def test_logs_message_for_failed_dependency_load
|
2008-11-07 16:22:56 -05:00
|
|
|
ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError)
|
2008-05-13 15:22:48 -04:00
|
|
|
ActiveRecord::Base.logger.expects(:warn)
|
2008-11-07 16:22:56 -05:00
|
|
|
ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist)
|
2008-05-13 15:22:48 -04:00
|
|
|
end
|
2008-03-17 17:48:28 -04:00
|
|
|
|
2008-05-13 15:22:48 -04:00
|
|
|
def test_does_not_logs_message_for_successful_dependency_load
|
2008-11-07 16:22:56 -05:00
|
|
|
ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine)
|
2008-05-13 15:22:48 -04:00
|
|
|
ActiveRecord::Base.logger.expects(:warn).never
|
2008-11-07 16:22:56 -05:00
|
|
|
ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine)
|
2008-05-13 15:22:48 -04:00
|
|
|
end
|
2008-03-17 17:48:28 -04:00
|
|
|
end
|
|
|
|
end
|