Module: DataMapper::TestFixtures

Extended by:
ActiveSupport::Concern
Included in:
ActiveSupport::TestCase
Defined in:
lib/dm-fixtures/fixtures.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#enlist_fixture_connectionsObject



962
963
964
965
966
967
# File 'lib/dm-fixtures/fixtures.rb', line 962

def enlist_fixture_connections
  #ActiveRecord::Base.connection_handler.connection_pools.values.map(&:connection)
  DataMapper::Repository.adapters.values 
  #TODO think I actually want DataObjects stuff: https://groups.google.com/d/msg/datamapper/16DJGo9DLSI/oqjWvrnJp3oJ
  # or http://stackoverflow.com/a/11594219/1749924
end

#run_in_transaction?Boolean

Returns:

  • (Boolean)


889
890
891
892
# File 'lib/dm-fixtures/fixtures.rb', line 889

def run_in_transaction?
  use_transactional_fixtures && defined? DataMapper::Transaction && #make sure we have dm-transactions
    !self.class.uses_transaction?(method_name)
end

#setup_fixturesObject



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'lib/dm-fixtures/fixtures.rb', line 894

def setup_fixtures
  # Make sure we're connected to a DB
  # return unless !ActiveRecord::Base.configurations.blank?
  return if DataMapper::Repository.adapters.blank?

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

  @fixture_cache = {}
  @fixture_connections = []
  @@already_loaded_fixtures ||= {}
  
  # Load fixtures once and begin transaction.
  if run_in_transaction?
    if @@already_loaded_fixtures[self.class]
      @loaded_fixtures = @@already_loaded_fixtures[self.class]
    else
      @loaded_fixtures = load_fixtures
      @@already_loaded_fixtures[self.class] = @loaded_fixtures
    end
    @fixture_connections = enlist_fixture_connections
    @fixture_connections.each do |repository|
      # http://git.io/2QmaGg
      # connection.increment_open_transactions
      # connection.transaction_joinable = false
      # connection.begin_db_transaction
      transaction = DataMapper::Transaction.new(repository)
      transaction.begin
      repository.push_transaction(transaction)
    end
  # Load fixtures for every test.
  else
    DataMapper::Fixtures.reset_cache
    @@already_loaded_fixtures[self.class] = nil
    @loaded_fixtures = load_fixtures
  end

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

#teardown_fixturesObject



936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'lib/dm-fixtures/fixtures.rb', line 936

def teardown_fixtures
  #                                     ActiveRecord::Base.configurations.blank?
  return unless defined?(DataMapper) && !DataMapper::Repository.adapters.blank?

  # Rollback changes if a transaction is active.
  if run_in_transaction?
    @fixture_connections.each do |repository|
      # if connection.open_transactions != 0
      #   connection.rollback_db_transaction
      #   connection.decrement_open_transactions
      # end
      # adapter = repository.adapter
      while repository.current_transaction
        repository.current_transaction.rollback
        repository.pop_transaction
      end
    end
    @fixture_connections.clear
  else
    DataMapper::Fixtures.reset_cache
  end
  
  # 
  # ActiveRecord::Base.clear_active_connections!
end