Methods
N
T
Constants
FIXTURES = %w( accounts binaries companies customers developers developers_projects entrants movies projects subscribers topics tasks )
 
MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-\w]*/
 
Class Public methods
name()
# File activerecord/test/cases/fixtures_test.rb, line 133
def self.name
  "OtherTopic"
end
Instance Public methods
test_attributes()
# File activerecord/test/cases/fixtures_test.rb, line 90
def test_attributes
  topics = create_fixtures("topics").first
  assert_equal("The First Topic", topics["first"]["title"])
  assert_nil(topics["second"]["author_email_address"])
end
test_binary_in_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 252
def test_binary_in_fixtures
  data = File.open(ASSETS_ROOT + "/flowers.jpg", 'rb') { |f| f.read }
  data.force_encoding('ASCII-8BIT')
  data.freeze
  assert_equal data, @flowers.data
end
test_broken_yaml_exception()
# File activerecord/test/cases/fixtures_test.rb, line 55
def test_broken_yaml_exception
  badyaml = Tempfile.new ['foo', '.yml']
  badyaml.write 'a: : '
  badyaml.flush

  dir  = File.dirname badyaml.path
  name = File.basename badyaml.path, '.yml'
  assert_raises(ActiveRecord::Fixture::FormatError) do
    ActiveRecord::FixtureSet.create_fixtures(dir, name)
  end
ensure
  badyaml.close
  badyaml.unlink
end
test_clean_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 42
def test_clean_fixtures
  FIXTURES.each do |name|
    fixtures = nil
    assert_nothing_raised { fixtures = create_fixtures(name).first }
    assert_kind_of(ActiveRecord::FixtureSet, fixtures)
    fixtures.each { |_name, fixture|
      fixture.each { |key, value|
        assert_match(MATCH_ATTRIBUTE_NAME, key)
      }
    }
  end
end
test_complete_instantiation()
# File activerecord/test/cases/fixtures_test.rb, line 182
def test_complete_instantiation
  assert_equal "The First Topic", @first.title
end
test_create_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 70
def test_create_fixtures
  fixtures = ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, "parrots")
  assert Parrot.find_by_name('Curious George'), 'George is not in the database'
  assert fixtures.detect { |f| f.name == 'parrots' }, "no fixtures named 'parrots' in #{fixtures.map(&:name).inspect}"
end
test_create_symbol_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 83
def test_create_symbol_fixtures
  fixtures = ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, :collections, :collections => Course) { Course.connection }

  assert Course.find_by_name('Collection'), 'course is not in the database'
  assert fixtures.detect { |f| f.name == 'collections' }, "no fixtures named 'collections' in #{fixtures.map(&:name).inspect}"
end
test_dirty_dirty_yaml_file()
# File activerecord/test/cases/fixtures_test.rb, line 214
def test_dirty_dirty_yaml_file
  assert_raise(ActiveRecord::Fixture::FormatError) do
    ActiveRecord::FixtureSet.new( Account.connection, "courses", Course, FIXTURES_ROOT + "/naked/yml/courses")
  end
end
test_empty_yaml_fixture()
# File activerecord/test/cases/fixtures_test.rb, line 195
def test_empty_yaml_fixture
  assert_not_nil ActiveRecord::FixtureSet.new( Account.connection, "accounts", Account, FIXTURES_ROOT + "/naked/yml/accounts")
end
test_empty_yaml_fixture_with_a_comment_in_it()
# File activerecord/test/cases/fixtures_test.rb, line 199
def test_empty_yaml_fixture_with_a_comment_in_it
  assert_not_nil ActiveRecord::FixtureSet.new( Account.connection, "companies", Company, FIXTURES_ROOT + "/naked/yml/companies")
end
test_erb_in_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 191
def test_erb_in_fixtures
  assert_equal "fixture_5", @dev_5.name
end
test_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 270
def test_fixtures
  assert accounts(:signals37)
end
test_fixtures_are_set_up_with_database_env_variable()
# File activerecord/test/cases/fixtures_test.rb, line 263
def test_fixtures_are_set_up_with_database_env_variable
  db_url_tmp = ENV['DATABASE_URL']
  ENV['DATABASE_URL'] = "sqlite3::memory:"
  ActiveRecord::Base.stubs(:configurations).returns({})
  test_case = Class.new(ActiveRecord::TestCase) do
    fixtures :accounts

    def test_fixtures
      assert accounts(:signals37)
    end
  end

  result = test_case.new(:test_fixtures).run

  assert result.passed?, "Expected #{result.name} to pass:\n#{result}"
ensure
  ENV['DATABASE_URL'] = db_url_tmp
end
test_fixtures_from_root_yml_with_instantiation()
# File activerecord/test/cases/fixtures_test.rb, line 186
def test_fixtures_from_root_yml_with_instantiation
  # assert_equal 2, @accounts.size
  assert_equal 50, @unknown.credit_limit
end
test_insert_with_datetime()
# File activerecord/test/cases/fixtures_test.rb, line 165
def test_insert_with_datetime
  create_fixtures("tasks")
  first = Task.find(1)
  assert first
end
test_inserts()
# File activerecord/test/cases/fixtures_test.rb, line 96
def test_inserts
  create_fixtures("topics")
  first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'David'")
  assert_equal("The First Topic", first_row["title"])

  second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'Mary'")
  assert_nil(second_row["author_email_address"])
end
test_inserts_with_pre_and_suffix()
# File activerecord/test/cases/fixtures_test.rb, line 106
def test_inserts_with_pre_and_suffix
  # Reset cache to make finds on the new table work
  ActiveRecord::FixtureSet.reset_cache

  ActiveRecord::Base.connection.create_table :prefix_other_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
    t.column :content, :string
    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'

  other_topic_klass = Class.new(ActiveRecord::Base) do
    def self.name
      "OtherTopic"
    end
  end

  topics = [create_fixtures("other_topics")].flatten.first

  # 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"

  first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'David'")
  assert_not_nil first_row, "The prefix_other_topics_suffix table appears to be empty despite create_fixtures: the row with author_name = 'David' was not found"
  assert_equal("The First Topic", first_row["title"])

  second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'Mary'")
  assert_nil(second_row["author_email_address"])

  assert_equal :prefix_other_topics_suffix, topics.table_name.to_sym
  # This assertion should preferably be the last in the list, because calling
  # other_topic_klass.table_name sets a class-level instance variable
  assert_equal :prefix_other_topics_suffix, other_topic_klass.table_name.to_sym

ensure
  # Restore prefix/suffix to its previous values
  ActiveRecord::Base.table_name_prefix = old_prefix
  ActiveRecord::Base.table_name_suffix = old_suffix

  ActiveRecord::Base.connection.drop_table :prefix_other_topics_suffix rescue nil
end
test_instantiation()
# File activerecord/test/cases/fixtures_test.rb, line 177
def test_instantiation
  topics = create_fixtures("topics").first
  assert_kind_of Topic, topics["first"].find
end
test_logger_level_invariant()
# File activerecord/test/cases/fixtures_test.rb, line 171
def test_logger_level_invariant
  level = ActiveRecord::Base.logger.level
  create_fixtures('topics')
  assert_equal level, ActiveRecord::Base.logger.level
end
test_multiple_clean_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 76
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(ActiveRecord::FixtureSet, fixtures) }
end
test_nonexistent_fixture_file()
# File activerecord/test/cases/fixtures_test.rb, line 203
def test_nonexistent_fixture_file
  nonexistent_fixture_path = FIXTURES_ROOT + "/imnothere"

  #sanity check to make sure that this file never exists
  assert Dir[nonexistent_fixture_path+"*"].empty?

  assert_raise(Errno::ENOENT) do
    ActiveRecord::FixtureSet.new( Account.connection, "companies", Company, nonexistent_fixture_path)
  end
end
test_omap_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 231
def test_omap_fixtures
  assert_nothing_raised do
    fixtures = ActiveRecord::FixtureSet.new(Account.connection, 'categories', Category, FIXTURES_ROOT + "/categories_ordered")

    fixtures.each.with_index do |(name, fixture), i|
      assert_equal "fixture_no_#{i}", name
      assert_equal "Category #{i}", fixture['name']
    end
  end
end
test_serialized_fixtures()
# File activerecord/test/cases/fixtures_test.rb, line 259
def test_serialized_fixtures
  assert_equal ["Green", "Red", "Orange"], traffic_lights(:uk).state
end
test_subsubdir_file_with_arbitrary_name()
# File activerecord/test/cases/fixtures_test.rb, line 247
def test_subsubdir_file_with_arbitrary_name
  assert_equal(categories(:sub_special_3).name, "A special category in an arbitrarily named subsubdir file")
  assert_equal(categories(:sub_special_3).class, SpecialCategory)
end
test_yaml_file_with_invalid_column()
# File activerecord/test/cases/fixtures_test.rb, line 220
def test_yaml_file_with_invalid_column
  e = assert_raise(ActiveRecord::Fixture::FixtureError) do
    ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT + "/naked/yml", "parrots")
  end
  assert_equal(%Q(table "parrots" has no column named "arrr".), e.message)
end
test_yaml_file_with_symbol_columns()
# File activerecord/test/cases/fixtures_test.rb, line 227
def test_yaml_file_with_symbol_columns
  ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT + "/naked/yml", "trees")
end
test_yml_file_in_subdirectory()
# File activerecord/test/cases/fixtures_test.rb, line 242
def test_yml_file_in_subdirectory
  assert_equal(categories(:sub_special_1).name, "A special category in a subdir file")
  assert_equal(categories(:sub_special_1).class, SpecialCategory)
end