From 2acec4657752d441ab87b9f5862d7918843d6409 Mon Sep 17 00:00:00 2001 From: Roque Pinel Date: Mon, 15 Jun 2015 23:16:39 -0400 Subject: [PATCH] Allow fixtures YAML files to set the model class in the file itself Currently, `set_fixture_class` is only available using the `TestFixtures` concern and it is ignored for `rake db:fixtures:load`. Using the correct model class, it is possible for the fixture load to also load the associations from the YAML files (e.g., `:belongs_to` and `:has_many`). --- activerecord/CHANGELOG.md | 15 +++++++ .../lib/active_record/fixture_set/file.rb | 15 ++++++- activerecord/lib/active_record/fixtures.rb | 42 ++++++++++++++---- .../test/cases/fixture_set/file_test.rb | 10 +++++ activerecord/test/cases/fixtures_test.rb | 35 ++++++++++++++- activerecord/test/fixtures/.DS_Store | Bin 0 -> 6148 bytes activerecord/test/fixtures/bad_posts.yml | 10 +++++ activerecord/test/fixtures/other_comments.yml | 7 +++ activerecord/test/fixtures/other_posts.yml | 8 ++++ 9 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 activerecord/test/fixtures/.DS_Store create mode 100644 activerecord/test/fixtures/bad_posts.yml create mode 100644 activerecord/test/fixtures/other_comments.yml create mode 100644 activerecord/test/fixtures/other_posts.yml diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 554bec17d6..507e4038cd 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,18 @@ +* Allow fixtures YAML files to set the model class in the file itself. + + To load the fixtures file `accounts.yml` as the `User` model, use: + + ```yaml + _fixture: + model_class: User + david: + name: David + ``` + + Fixes #9516. + + *Roque Pinel* + * Correct query for PostgreSQL 8.2 compatibility. *Ben Murphy*, *Matthew Draper* diff --git a/activerecord/lib/active_record/fixture_set/file.rb b/activerecord/lib/active_record/fixture_set/file.rb index 8132310c91..9b74ed0ef7 100644 --- a/activerecord/lib/active_record/fixture_set/file.rb +++ b/activerecord/lib/active_record/fixture_set/file.rb @@ -18,23 +18,34 @@ module ActiveRecord def initialize(file) @file = file @rows = nil + @raw_rows = nil + @model_class = nil end def each(&block) rows.each(&block) end + def model_class + return @model_class if @model_class + row = raw_rows.find { |fixture_name, _| fixture_name == '_fixture' } + @model_class = row.last['model_class'] if row + end private def rows - return @rows if @rows + @rows ||= raw_rows.reject { |fixture_name, _| fixture_name == '_fixture' } + end + + def raw_rows + return @raw_rows if @raw_rows begin data = YAML.load(render(IO.read(@file))) 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 : [] + @raw_rows = data ? validate(data).to_a : [] end def render(content) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index f1dc56df63..e5d51fc5fb 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -395,6 +395,23 @@ module ActiveRecord # <<: *DEFAULTS # # Any fixture labeled "DEFAULTS" is safely ignored. + # + # == Support to custom model class + # + # You can also set the model class in your fixtures YAML file. + # This is helpful when fixtures are being loaded outside tests and + # you cannot use +set_fixture_class+, e.g., when running + # rake db:fixtures:load. + # + # To load the fixtures file +test/fixtures/accounts.yml+ as the +User+ + # model, use: + # + # _fixture: + # model_class: User + # david: + # name: David + # + # Any fixture labeled "_fixture" is safely ignored. class FixtureSet #-- # An instance of FixtureSet is normally stored in a single YAML file and @@ -578,21 +595,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 +773,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 diff --git a/activerecord/test/cases/fixture_set/file_test.rb b/activerecord/test/cases/fixture_set/file_test.rb index 92efa8aca7..79951c91c9 100644 --- a/activerecord/test/cases/fixture_set/file_test.rb +++ b/activerecord/test/cases/fixture_set/file_test.rb @@ -123,6 +123,16 @@ END end end + def test_fixture_model_class_and_rows + File.open(::File.join(FIXTURES_ROOT, 'other_posts.yml')) do |fh| + assert_equal 'Post', fh.model_class + + fixture_names = [] + fh.each { |fixture_name, _| fixture_names << fixture_name } + assert_equal ['second_welcome'], fixture_names + end + end + private def tmp_yaml(name, contents) t = Tempfile.new name diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 64759160dc..f7f2b69995 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -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' @@ -514,6 +515,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_table_method + 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_table_method2 + assert_kind_of Post, bad_posts(:bad_welcome) + end +end + class CheckSetTableNameFixturesTest < ActiveRecord::TestCase set_fixture_class :funny_jokes => Joke fixtures :funny_jokes diff --git a/activerecord/test/fixtures/.DS_Store b/activerecord/test/fixtures/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0