Module: TestSeeds

Extended by:
ActiveSupport::Concern
Defined in:
lib/test_seeds.rb

Defined Under Namespace

Modules: ClassMethods Classes: SeedFixture

Instance Method Summary collapse

Instance Method Details

#load_fixturesObject

Load fixture is called once for each test class which is a good place to inject db transactions and to create the seeds.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/test_seeds.rb', line 115

def load_fixtures
  if ActiveRecord::Base.connection.open_transactions != 0
    ActiveRecord::Base.connection.decrement_open_transactions
    ActiveRecord::Base.connection.rollback_db_transaction
  end
  ActiveRecord::Base.clear_active_connections!

  # In Rails 3.0.x, load_fixtures sets the @loades_fixtures instance variable. In Rails 3.1, load_fixtures returns the 
  # fixtures which are then assigned to @loaded_fixtures by the caller. The following line ensures compability with
  # Rails 3.1. 
  result = super
  @loaded_fixtures = result if result.is_a? Hash

  if run_in_transaction?
    ActiveRecord::Base.connection.begin_db_transaction
    ActiveRecord::Base.connection.increment_open_transactions
    ActiveRecord::Base.connection.transaction_joinable = false
  end
  
  load_seed_fixtures
  
  @loaded_fixtures
end

#load_seed_fixturesObject



167
168
169
170
171
# File 'lib/test_seeds.rb', line 167

def load_seed_fixtures
  ActiveRecord::Base.connection.transaction(:requires_new => true) do
    _run_seed_callbacks
  end
end

#set_seed_fixture(seed_name, seed_model) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/test_seeds.rb', line 157

def set_seed_fixture(seed_name, seed_model)
  raise "Seed fixture must be an instance of ActiveRecord::Base" unless seed_model.is_a? ActiveRecord::Base
  
  seed_class = seed_model.class
  fixture = { seed_class.primary_key => seed_model.send(seed_class.primary_key) }
  @loaded_fixtures[seed_class.table_name][seed_name.to_s] = SeedFixture.new(fixture, seed_class)
  
  [ seed_class.table_name, seed_name.to_sym ]
end

#setup_fixturesObject

Re-implement setup_fixtures to use save points instead of transactions.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/test_seeds.rb', line 58

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

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

  @fixture_cache = {}
  @@already_loaded_fixtures ||= {}

  @loaded_seeds = []

  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

    # Use safe points
    ActiveRecord::Base.connection.create_savepoint
    ActiveRecord::Base.connection.increment_open_transactions
    ActiveRecord::Base.connection.transaction_joinable = false
    @created_save_point = true
    
    setup_seeds(nil)
  else
    fixtures_class_in_my_rails.reset_cache
    @@already_loaded_fixtures[self.class] = nil
    @loaded_fixtures = load_fixtures
  end

  instantiate_fixtures if use_instantiated_fixtures
end

#setup_seeds(*seeds) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/test_seeds.rb', line 139

def setup_seeds(*seeds)
  seeds.each do |seed|
    (self.class.defined_seeds[seed] || []).each do |seed_var, seed_accessor|
      instance_variable_set(seed_var, send(*seed_accessor))
    end
    @loaded_seeds << seed
  end
end

#teardown_fixturesObject

Re-implement setup_fixtures to use save points instead of transactions.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/test_seeds.rb', line 95

def teardown_fixtures
  teardown_seeds(*@loaded_seeds)
  
  return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?

  unless run_in_transaction?
    fixtures_class_in_my_rails.reset_cache
  end

  if run_in_transaction? && @created_save_point
    # Use safe points
    ActiveRecord::Base.connection.decrement_open_transactions
    ActiveRecord::Base.connection.rollback_to_savepoint
    @created_save_point = false
  end
  ActiveRecord::Base.clear_active_connections!
end

#teardown_seeds(*seeds) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/test_seeds.rb', line 148

def teardown_seeds(*seeds)
  seeds.each do |seed|
    (self.class.defined_seeds[seed] || []).each do |seed_var, seed_accessor|
      instance_variable_set(seed_var, nil)
    end
    @loaded_seeds.delete(seed)
  end
end