Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/fixtures.rb

Overview

:nodoc:

Constant Summary collapse

@@already_loaded_fixtures =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fixtures(*table_names) ⇒ Object



849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/active_record/fixtures.rb', line 849

def self.fixtures(*table_names)
  if table_names.first == :all
    table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"]
    table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }
  else
    table_names = table_names.flatten.map { |n| n.to_s }
  end

  self.fixture_table_names |= table_names
  require_fixture_classes(table_names)
  setup_fixture_accessors(table_names)
end

.method_added(method) ⇒ Object



960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# File 'lib/active_record/fixtures.rb', line 960

def self.method_added(method)
  case method.to_s
  when 'setup'
    unless method_defined?(:setup_without_fixtures)
      alias_method :setup_without_fixtures, :setup
      define_method(:setup) do
        setup_with_fixtures
        setup_without_fixtures
      end
    end
  when 'teardown'
    unless method_defined?(:teardown_without_fixtures)
      alias_method :teardown_without_fixtures, :teardown
      define_method(:teardown) do
        teardown_without_fixtures
        teardown_with_fixtures
      end
    end
  end
end

.require_fixture_classes(table_names = nil) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
# File 'lib/active_record/fixtures.rb', line 862

def self.require_fixture_classes(table_names = nil)
  (table_names || fixture_table_names).each do |table_name|
    file_name = table_name.to_s
    file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names
    begin
      require_dependency file_name
    rescue LoadError
      # Let's hope the developer has included it himself
    end
  end
end

.set_fixture_class(class_names = {}) ⇒ Object



845
846
847
# File 'lib/active_record/fixtures.rb', line 845

def self.set_fixture_class(class_names = {})
  self.fixture_class_names = self.fixture_class_names.merge(class_names)
end

.setup_fixture_accessors(table_names = nil) ⇒ Object



874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
# File 'lib/active_record/fixtures.rb', line 874

def self.setup_fixture_accessors(table_names = nil)
  (table_names || fixture_table_names).each do |table_name|
    table_name = table_name.to_s.tr('.', '_')

    define_method(table_name) do |*fixtures|
      force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload

      @fixture_cache[table_name] ||= {}

      instances = fixtures.map do |fixture|
        @fixture_cache[table_name].delete(fixture) if force_reload

        if @loaded_fixtures[table_name][fixture.to_s]
          @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find
        else
          raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
        end
      end

      instances.size == 1 ? instances.first : instances
    end
  end
end

.uses_transaction(*methods) ⇒ Object



898
899
900
901
# File 'lib/active_record/fixtures.rb', line 898

def self.uses_transaction(*methods)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.concat methods.map(&:to_s)
end

.uses_transaction?(method) ⇒ Boolean

Returns:

  • (Boolean)


903
904
905
906
# File 'lib/active_record/fixtures.rb', line 903

def self.uses_transaction?(method)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.include?(method.to_s)
end

Instance Method Details

#setup_with_fixturesObject Also known as: setup



913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/active_record/fixtures.rb', line 913

def setup_with_fixtures
  return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?

  if pre_loaded_fixtures && !use_transactional_fixtures
    raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
  end

  @fixture_cache = {}

  # Load fixtures once and begin transaction.
  if use_transactional_fixtures?
    if @@already_loaded_fixtures[self.class]
      @loaded_fixtures = @@already_loaded_fixtures[self.class]
    else
      load_fixtures
      @@already_loaded_fixtures[self.class] = @loaded_fixtures
    end
    ActiveRecord::Base.send :increment_open_transactions
    ActiveRecord::Base.connection.begin_db_transaction
  # Load fixtures for every test.
  else
    Fixtures.reset_cache
    @@already_loaded_fixtures[self.class] = nil
    load_fixtures
  end

  # Instantiate fixtures for every test if requested.
  instantiate_fixtures if use_instantiated_fixtures
end

#teardown_with_fixturesObject Also known as: teardown



944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/active_record/fixtures.rb', line 944

def teardown_with_fixtures
  return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?

  unless use_transactional_fixtures?
    Fixtures.reset_cache
  end

  # Rollback changes if a transaction is active.
  if use_transactional_fixtures? && Thread.current['open_transactions'] != 0
    ActiveRecord::Base.connection.rollback_db_transaction
    Thread.current['open_transactions'] = 0
  end
  ActiveRecord::Base.verify_active_connections!
end

#use_transactional_fixtures?Boolean

Returns:

  • (Boolean)


908
909
910
911
# File 'lib/active_record/fixtures.rb', line 908

def use_transactional_fixtures?
  use_transactional_fixtures &&
    !self.class.uses_transaction?(method_name)
end