mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20574 from repinel/fix-db-fixtures-load
Allow fixtures YAML files to set the model class in the file itself Conflicts: activerecord/CHANGELOG.md
This commit is contained in:
commit
591a0bb87f
8 changed files with 130 additions and 14 deletions
|
@ -1,3 +1,16 @@
|
|||
* Allow fixtures files to set the model class in the YAML file itself.
|
||||
|
||||
To load the fixtures file `accounts.yml` as the `User` model, use:
|
||||
|
||||
_fixture:
|
||||
model_class: User
|
||||
david:
|
||||
name: David
|
||||
|
||||
Fixes #9516.
|
||||
|
||||
*Roque Pinel*
|
||||
|
||||
* Don't require a database connection to load a class which uses acceptance
|
||||
validations.
|
||||
|
||||
|
|
|
@ -17,24 +17,39 @@ module ActiveRecord
|
|||
|
||||
def initialize(file)
|
||||
@file = file
|
||||
@rows = nil
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
rows.each(&block)
|
||||
end
|
||||
|
||||
def model_class
|
||||
config_row['model_class']
|
||||
end
|
||||
|
||||
private
|
||||
def rows
|
||||
return @rows if @rows
|
||||
@rows ||= raw_rows.reject { |fixture_name, _| fixture_name == '_fixture' }
|
||||
end
|
||||
|
||||
begin
|
||||
def config_row
|
||||
@config_row ||= begin
|
||||
row = raw_rows.find { |fixture_name, _| fixture_name == '_fixture' }
|
||||
if row
|
||||
row.last
|
||||
else
|
||||
{'model_class': nil}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def raw_rows
|
||||
@raw_rows ||= begin
|
||||
data = YAML.load(render(IO.read(@file)))
|
||||
data ? validate(data).to_a : []
|
||||
rescue ArgumentError, Psych::SyntaxError => error
|
||||
raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. 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 #{error.class}: #{error}", error.backtrace
|
||||
end
|
||||
@rows = data ? validate(data).to_a : []
|
||||
end
|
||||
|
||||
def render(content)
|
||||
|
|
|
@ -395,6 +395,20 @@ module ActiveRecord
|
|||
# <<: *DEFAULTS
|
||||
#
|
||||
# Any fixture labeled "DEFAULTS" is safely ignored.
|
||||
#
|
||||
# == Configure the fixture model class
|
||||
#
|
||||
# It's possible to set the fixture's model class directly in the YAML file.
|
||||
# This is helpful when fixtures are loaded outside tests and
|
||||
# +set_fixture_class+ is not available (e.g.
|
||||
# when running <tt>rake db:fixtures:load</tt>).
|
||||
#
|
||||
# _fixture:
|
||||
# model_class: User
|
||||
# david:
|
||||
# name: David
|
||||
#
|
||||
# Any fixtures labeled "_fixture" are safely ignored.
|
||||
class FixtureSet
|
||||
#--
|
||||
# An instance of FixtureSet is normally stored in a single YAML file and
|
||||
|
@ -578,21 +592,16 @@ module ActiveRecord
|
|||
@name = name
|
||||
@path = path
|
||||
@config = config
|
||||
@model_class = nil
|
||||
|
||||
if class_name.is_a?(Class) # TODO: Should be an AR::Base type class, or any?
|
||||
@model_class = class_name
|
||||
else
|
||||
@model_class = class_name.safe_constantize if class_name
|
||||
end
|
||||
self.model_class = class_name
|
||||
|
||||
@fixtures = read_fixture_files(path)
|
||||
|
||||
@connection = connection
|
||||
|
||||
@table_name = ( model_class.respond_to?(:table_name) ?
|
||||
model_class.table_name :
|
||||
self.class.default_fixture_table_name(name, config) )
|
||||
|
||||
@fixtures = read_fixture_files path, @model_class
|
||||
end
|
||||
|
||||
def [](x)
|
||||
|
@ -761,13 +770,25 @@ module ActiveRecord
|
|||
@column_names ||= @connection.columns(@table_name).collect(&:name)
|
||||
end
|
||||
|
||||
def read_fixture_files(path, model_class)
|
||||
def model_class=(class_name)
|
||||
if class_name.is_a?(Class) # TODO: Should be an AR::Base type class, or any?
|
||||
@model_class = class_name
|
||||
else
|
||||
@model_class = class_name.safe_constantize if class_name
|
||||
end
|
||||
end
|
||||
|
||||
# Loads the fixtures from the YAML file at +path+.
|
||||
# If the file sets the +model_class+ and current instance value is not set,
|
||||
# it uses the file value.
|
||||
def read_fixture_files(path)
|
||||
yaml_files = Dir["#{path}/{**,*}/*.yml"].select { |f|
|
||||
::File.file?(f)
|
||||
} + [yaml_file_path(path)]
|
||||
|
||||
yaml_files.each_with_object({}) do |file, fixtures|
|
||||
FixtureSet::File.open(file) do |fh|
|
||||
self.model_class ||= fh.model_class if fh.model_class
|
||||
fh.each do |fixture_name, row|
|
||||
fixtures[fixture_name] = ActiveRecord::Fixture.new(row, model_class)
|
||||
end
|
||||
|
|
|
@ -123,6 +123,18 @@ END
|
|||
end
|
||||
end
|
||||
|
||||
def test_removes_fixture_config_row
|
||||
File.open(::File.join(FIXTURES_ROOT, 'other_posts.yml')) do |fh|
|
||||
assert_equal(['second_welcome'], fh.each.map { |name, _| name })
|
||||
end
|
||||
end
|
||||
|
||||
def test_extracts_model_class_from_config_row
|
||||
File.open(::File.join(FIXTURES_ROOT, 'other_posts.yml')) do |fh|
|
||||
assert_equal 'Post', fh.model_class
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def tmp_yaml(name, contents)
|
||||
t = Tempfile.new name
|
||||
|
|
|
@ -7,15 +7,16 @@ require 'models/binary'
|
|||
require 'models/book'
|
||||
require 'models/bulb'
|
||||
require 'models/category'
|
||||
require 'models/comment'
|
||||
require 'models/company'
|
||||
require 'models/computer'
|
||||
require 'models/course'
|
||||
require 'models/developer'
|
||||
require 'models/doubloon'
|
||||
require 'models/joke'
|
||||
require 'models/matey'
|
||||
require 'models/parrot'
|
||||
require 'models/pirate'
|
||||
require 'models/doubloon'
|
||||
require 'models/post'
|
||||
require 'models/randomly_named_c1'
|
||||
require 'models/reply'
|
||||
|
@ -516,6 +517,38 @@ class OverRideFixtureMethodTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
class FixtureWithSetModelClassTest < ActiveRecord::TestCase
|
||||
fixtures :other_posts, :other_comments
|
||||
|
||||
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
||||
# and thus takes into account the +set_model_class+.
|
||||
self.use_transactional_tests = false
|
||||
|
||||
def test_uses_fixture_class_defined_in_yaml
|
||||
assert_kind_of Post, other_posts(:second_welcome)
|
||||
end
|
||||
|
||||
def test_loads_the_associations_to_fixtures_with_set_model_class
|
||||
post = other_posts(:second_welcome)
|
||||
comment = other_comments(:second_greetings)
|
||||
assert_equal [comment], post.comments
|
||||
assert_equal post, comment.post
|
||||
end
|
||||
end
|
||||
|
||||
class SetFixtureClassPrevailsTest < ActiveRecord::TestCase
|
||||
set_fixture_class bad_posts: Post
|
||||
fixtures :bad_posts
|
||||
|
||||
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
|
||||
# and thus takes into account the +set_model_class+.
|
||||
self.use_transactional_tests = false
|
||||
|
||||
def test_uses_set_fixture_class
|
||||
assert_kind_of Post, bad_posts(:bad_welcome)
|
||||
end
|
||||
end
|
||||
|
||||
class CheckSetTableNameFixturesTest < ActiveRecord::TestCase
|
||||
set_fixture_class :funny_jokes => Joke
|
||||
fixtures :funny_jokes
|
||||
|
|
9
activerecord/test/fixtures/bad_posts.yml
vendored
Normal file
9
activerecord/test/fixtures/bad_posts.yml
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
# Please do not use this fixture without `set_fixture_class` as Post
|
||||
|
||||
_fixture:
|
||||
model_class: BadPostModel
|
||||
|
||||
bad_welcome:
|
||||
author_id: 1
|
||||
title: Welcome to the another weblog
|
||||
body: It's really nice today
|
6
activerecord/test/fixtures/other_comments.yml
vendored
Normal file
6
activerecord/test/fixtures/other_comments.yml
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
_fixture:
|
||||
model_class: Comment
|
||||
|
||||
second_greetings:
|
||||
post: second_welcome
|
||||
body: Thank you for the second welcome
|
7
activerecord/test/fixtures/other_posts.yml
vendored
Normal file
7
activerecord/test/fixtures/other_posts.yml
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
_fixture:
|
||||
model_class: Post
|
||||
|
||||
second_welcome:
|
||||
author_id: 1
|
||||
title: Welcome to the another weblog
|
||||
body: It's really nice today
|
Loading…
Reference in a new issue