1 # The Engines testing extensions enable developers to load fixtures into specific
2 # tables irrespective of the name of the fixtures file. This work is heavily based on
3 # patches made by Duane Johnson (canadaduane), viewable at
4 # http://dev.rubyonrails.org/ticket/1911
6 # Engine developers should supply fixture files in the <engine>/test/fixtures directory
7 # as normal. Within their tests, they should load the fixtures using the 'fixture' command
8 # (rather than the normal 'fixtures' command). For example:
10 # class UserTest < Test::Unit::TestCase
11 # fixture :users, :table_name => LoginEngine.config(:user_table), :class_name => "User"
15 # This will ensure that the fixtures/users.yml file will get loaded into the correct
16 # table, and will use the correct model object class.
20 # A FixtureGroup is a set of fixtures identified by a name. Normally, this is the name of the
21 # corresponding fixture filename. For example, when you declare the use of fixtures in a
22 # TestUnit class, like so:
24 # you are creating a FixtureGroup whose name is 'users', and whose defaults are set such that the
25 # +class_name+, +file_name+ and +table_name+ are guessed from the FixtureGroup's name.
27 attr_accessor :table_name, :class_name, :connection
28 attr_reader :group_name, :file_name
30 def initialize(file_name, optional_names = {})
31 self.file_name = file_name
32 self.group_name = optional_names[:group_name] || file_name
33 if optional_names[:table_name]
34 self.table_name = optional_names[:table_name]
35 self.class_name = optional_names[:class_name] || Inflector.classify(@table_name.to_s.gsub('.','_'))
36 elsif optional_names[:class_name]
37 self.class_name = optional_names[:class_name]
38 if Object.const_defined?(@class_name)
39 model_class = Object.const_get(@class_name)
40 self.table_name = ActiveRecord::Base.table_name_prefix + model_class.table_name + ActiveRecord::Base.table_name_suffix
44 # In case either :table_name or :class_name was not set:
45 self.table_name ||= ActiveRecord::Base.table_name_prefix + @group_name.to_s + ActiveRecord::Base.table_name_suffix
46 self.class_name ||= Inflector.classify(@table_name.to_s.gsub('.','_'))
50 @file_name = name.to_s
54 @group_name = name.to_sym
58 Inflector.underscore(@class_name)
61 # Instantiate an array of FixtureGroup objects from an array of strings (table_names)
62 def self.array_from_names(names)
63 names.collect { |n| FixtureGroup.new(n) }
71 @group_name.eql? other.group_name
75 class Fixtures < YAML::Omap
77 def self.instantiate_fixtures(object, fixture_group_name, fixtures, load_instances=true)
78 old_logger_level = ActiveRecord::Base.logger.level
79 ActiveRecord::Base.logger.level = Logger::ERROR
81 # table_name.to_s.gsub('.','_') replaced by 'fixture_group_name'
82 object.instance_variable_set "@#{fixture_group_name}", fixtures
84 ActiveRecord::Base.silence do
85 fixtures.each do |name, fixture|
87 if model = fixture.find
88 object.instance_variable_set "@#{name}", model
90 rescue FixtureClassNotFound
91 # Let's hope the developer has included it himself
97 ActiveRecord::Base.logger.level = old_logger_level
100 # this doesn't really need to be overridden...
101 def self.instantiate_all_loaded_fixtures(object, load_instances=true)
102 all_loaded_fixtures.each do |fixture_group_name, fixtures|
103 Fixtures.instantiate_fixtures(object, fixture_group_name, fixtures, load_instances)
107 def self.create_fixtures(fixtures_directory, *fixture_groups)
108 connection = block_given? ? yield : ActiveRecord::Base.connection
109 fixture_groups.flatten!
111 # Backwards compatibility: Allow an array of table names to be passed in, but just use them
112 # to create an array of FixtureGroup objects
113 if not fixture_groups.empty? and fixture_groups.first.is_a?(String)
114 fixture_groups = FixtureGroup.array_from_names(fixture_groups)
117 ActiveRecord::Base.silence do
119 fixtures = fixture_groups.map do |group|
120 fixtures_map[group.group_name] = Fixtures.new(connection, fixtures_directory, group)
122 # Make sure all refs to all_loaded_fixtures use group_name as hash index, not table_name
123 all_loaded_fixtures.merge! fixtures_map
125 connection.transaction do
126 fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures }
127 fixtures.each { |fixture| fixture.insert_fixtures }
129 # Cap primary key sequences to max(pk).
130 if connection.respond_to?(:reset_pk_sequence!)
131 fixture_groups.each do |fg|
132 connection.reset_pk_sequence!(fg.table_name)
137 return fixtures.size > 1 ? fixtures : fixtures.first
142 attr_accessor :connection, :fixtures_directory, :file_filter
143 attr_accessor :fixture_group
145 def initialize(connection, fixtures_directory, fixture_group, file_filter = DEFAULT_FILTER_RE)
146 @connection, @fixtures_directory = connection, fixtures_directory
147 @fixture_group = fixture_group
148 @file_filter = file_filter
152 def delete_existing_fixtures
153 @connection.delete "DELETE FROM #{@fixture_group.table_name}", 'Fixture Delete'
157 values.each do |fixture|
158 @connection.execute "INSERT INTO #{@fixture_group.table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
163 def read_fixture_files
164 if File.file?(yaml_file_path)
165 read_yaml_fixture_files
166 elsif File.file?(csv_file_path)
167 read_csv_fixture_files
168 elsif File.file?(deprecated_yaml_file_path)
169 raise Fixture::FormatError, ".yml extension required: rename #{deprecated_yaml_file_path} to #{yaml_file_path}"
170 elsif File.directory?(single_file_fixtures_path)
171 read_standard_fixture_files
173 raise Fixture::FixtureError, "Couldn't find a yaml, csv or standard file to load at #{@fixtures_directory} (#{@fixture_group.file_name})."
177 def read_yaml_fixture_files
180 if yaml = YAML::load(erb_render(IO.read(yaml_file_path)))
181 yaml = yaml.value if yaml.respond_to?(:type_id) and yaml.respond_to?(:value)
182 yaml.each do |name, data|
183 self[name] = Fixture.new(data, fixture_group.class_name)
186 rescue Exception=>boom
187 raise Fixture::FormatError, "a YAML error occured parsing #{yaml_file_path}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{boom.class}: #{boom}"
191 def read_csv_fixture_files
193 reader = CSV::Reader.create(erb_render(IO.read(csv_file_path)))
194 header = reader.shift
198 row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip }
199 self["#{@fixture_group.class_file_name}_#{i+=1}"]= Fixture.new(data, @fixture_group.class_name)
203 def read_standard_fixture_files
205 path = File.join(@fixtures_directory, @fixture_group.file_name)
206 Dir.entries(path).each do |file|
207 path = File.join(@fixtures_directory, @fixture_group.file_name, file)
208 if File.file?(path) and file !~ @file_filter
209 self[file] = Fixture.new(path, @fixture_group.class_name)
215 fixture_path_with_extension ".yml"
218 def deprecated_yaml_file_path
219 fixture_path_with_extension ".yaml"
223 fixture_path_with_extension ".csv"
226 def single_file_fixtures_path
227 fixture_path_with_extension ""
230 def fixture_path_with_extension(ext)
231 File.join(@fixtures_directory, @fixture_group.file_name + ext)
234 def erb_render(fixture_content)
235 ERB.new(fixture_content).result
242 class TestCase #:nodoc:
243 cattr_accessor :fixtures_directory
244 class_inheritable_accessor :fixture_groups
245 class_inheritable_accessor :fixture_table_names
246 class_inheritable_accessor :use_transactional_fixtures
247 class_inheritable_accessor :use_instantiated_fixtures # true, false, or :no_instances
248 class_inheritable_accessor :pre_loaded_fixtures
250 self.fixture_groups = []
251 self.use_transactional_fixtures = false
252 self.use_instantiated_fixtures = true
253 self.pre_loaded_fixtures = false
255 @@already_loaded_fixtures = {}
257 # Backwards compatibility
258 def self.fixture_path=(path); self.fixtures_directory = path; end
259 def self.fixture_path; self.fixtures_directory; end
260 def fixture_group_names; fixture_groups.collect { |g| g.group_name }; end
261 def fixture_table_names; fixture_group_names; end
263 def self.fixture(file_name, options = {})
264 self.fixture_groups |= [FixtureGroup.new(file_name, options)]
265 require_fixture_classes
266 setup_fixture_accessors
269 def self.fixtures(*file_names)
270 self.fixture_groups |= FixtureGroup.array_from_names(file_names.flatten)
271 require_fixture_classes
272 setup_fixture_accessors
275 def self.require_fixture_classes(fixture_groups_override = nil)
276 (fixture_groups_override || fixture_groups).each do |group|
278 require group.class_file_name
280 # Let's hope the developer has included it himself
285 def self.setup_fixture_accessors(fixture_groups_override=nil)
286 (fixture_groups_override || fixture_groups).each do |group|
287 define_method(group.group_name) do |fixture, *optionals|
288 force_reload = optionals.shift
289 @fixture_cache[group.group_name] ||= Hash.new
290 @fixture_cache[group.group_name][fixture] = nil if force_reload
291 @fixture_cache[group.group_name][fixture] ||= @loaded_fixtures[group.group_name][fixture.to_s].find
298 @loaded_fixtures = {}
299 fixtures = Fixtures.create_fixtures(fixtures_directory, fixture_groups)
301 if fixtures.instance_of?(Fixtures)
302 @loaded_fixtures[fixtures.fixture_group.group_name] = fixtures
304 fixtures.each { |f| @loaded_fixtures[f.fixture_group.group_name] = f }
309 def instantiate_fixtures
310 if pre_loaded_fixtures
311 raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty?
312 unless @@required_fixture_classes
313 groups = Fixtures.all_loaded_fixtures.values.collect { |f| f.group_name }
314 self.class.require_fixture_classes groups
315 @@required_fixture_classes = true
317 Fixtures.instantiate_all_loaded_fixtures(self, load_instances?)
319 raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil?
320 @loaded_fixtures.each do |fixture_group_name, fixtures|
321 Fixtures.instantiate_fixtures(self, fixture_group_name, fixtures, load_instances?)