Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1911 (new enhancement)

Opened 3 years ago

Last modified 1 year ago

[PATCH] flexible fixtures

Reported by: duane.johnson@gmail.com Assigned to: Jeremy Kemper <rails@bitsweat.net>
Priority: normal Milestone: 1.2.7
Component: ActiveRecord Version: edge
Severity: normal Keywords: fixtures yaml fixture needy
Cc: a-ruby-rails@harper.nu, james.adam@gmail.com, michael@schuerig.de, marcel@ioni.st, magicmat@gmail.com, aaron.pfeifer@gmail.com, orion@mh2o.com

Description (Last modified by bitsweat)

At present, the only way to easily load a fixture in to a TestUnit is with the "fixtures" class method. While convenient, this method assumes several things about your tests that may not be true; for example, that you have only one set of fixture data per table for the entire application.

Rather than introduce backwards incompatibilities to the 'fixtures' method, this patch introduces a new "fixture" class method that will give developers more flexibility if necessary. For example:

  class MixSingularAndPluralFixturesTest < Test::Unit::TestCase
    fixtures :topics, :developers, :accounts
    fixture :additional_topics, :table_name => "topics", :file_name => "topics2"
    ...
  end

In order to accomplish this, a class called FixtureGroup has been created to represent the idea of a "group" of fixtures. So, for example, the "fixtures" class method above is creating three fixture groups named "topics", "developers" and "accounts". These groups then use sensible defaults to set their class names, table names and fixture file names.

When the developer needs to control these attributes at a finer level, she can choose to use the "fixture" class method. Like the 'fixtures' method, 'fixture' will assume settings appropriately, but only if attributes are not set. For example, the following line would assume the 'accounts' table and the Account class name, but would load a separate yaml file instead of the default "accounts.yml":

  fixture :accounts, :file_name => "accounts_in_process"

Additionally, multiple fixture files can be loaded in to the same table:

  fixture :topics_set_one, :table_name => "topics", :file_name => "topics"
  fixture :topics_set_two, :table_name => "topics", :file_name => "topics2"

Each of the above sets of fixtures can be accessed within the test class via @topics_set_one and @topics_set_two. An error will be raised if the data sets overlap (e.g. if both fixture files define a row in the database table with id '5', an insert error will occur).

NOTE: Some tidying and fixing of the fixtures also took place in this patch. The deprecated yaml test was not reporting errors for .yaml files properly. In addition, there is now a "fixture not found" error message when there are NO matching fixtures (rather than causing error after error during unit tests). Finally, some code was re-arranged so that it matches the "class << self" tidy-looking pattern for a group of class methods.

Attachments

more_flexible_fixture_architecture.patch (21.2 kB) - added by duane.johnson@gmail.com on 08/05/05 11:55:11.
more_flexible_fixture_architecture_without_breakpoint.patch (21.1 kB) - added by duane.johnson@gmail.com on 08/05/05 12:06:09.
Removed an errant breakpoint and nearby comment
more_flexible_fixture_architecture-2.patch (22.4 kB) - added by duane.johnson@gmail.com on 08/08/05 12:16:04.
This patch makes a more intelligent assumption about fixtures: that the file names are almost always the same as the group names. It also uses the model to guess the table name, rather than relying solely on inflection.
more_flexible_fixture_architecture-3b.patch (22.5 kB) - added by duane.johnson@gmail.com on 10/10/05 02:23:57.
Updated patch, resolves single file fixtures problem, patch no longer stale

Change History

08/05/05 11:55:11 changed by duane.johnson@gmail.com

  • attachment more_flexible_fixture_architecture.patch added.

08/05/05 12:06:09 changed by duane.johnson@gmail.com

  • attachment more_flexible_fixture_architecture_without_breakpoint.patch added.

Removed an errant breakpoint and nearby comment

08/08/05 12:16:04 changed by duane.johnson@gmail.com

  • attachment more_flexible_fixture_architecture-2.patch added.

This patch makes a more intelligent assumption about fixtures: that the file names are almost always the same as the group names. It also uses the model to guess the table name, rather than relying solely on inflection.

08/21/05 16:16:22 changed by juergen+ror@strobel.info

more_flexible_fixture_architecture-2.patch breaks fixture files in subdirectories. before that, I could use file_name => 'subdir/myfixtures'. In FixtureGroup#initialize, now that group_name seems to be gone the new code tries to instantiate @subdir/myfixtures which fails.

08/21/05 16:17:04 changed by juergen+ror@strobel.info

more_flexible_fixture_architecture-2.patch breaks fixture files in subdirectories. before that, I could use file_name => 'subdir/myfixtures'. In FixtureGroup#initialize, now that group_name seems to be gone the new code tries to instantiate @subdir/myfixtures which fails.

08/21/05 16:18:19 changed by juergen+ror@strobel.info

sorry double post, new to this :)

09/20/05 17:47:36 changed by mschuerig

  • cc set to michael@schuerig.de.

09/28/05 09:29:36 changed by david

  • keywords changed from fixtures yaml fixture to fixtures yaml fixture fd.

10/10/05 02:23:57 changed by duane.johnson@gmail.com

  • attachment more_flexible_fixture_architecture-3b.patch added.

Updated patch, resolves single file fixtures problem, patch no longer stale

10/10/05 02:26:13 changed by duane.johnson@gmail.com

  • cc changed from michael@schuerig.de to michael@schuerig.de, marcel@ioni.st.

Marcel, this should be ready for you to apply. It also addresses the problem identified by juergen above.

10/10/05 16:25:05 changed by bitsweat

  • owner changed from David to Jeremy Kemper <rails@bitsweat.net>.
  • summary changed from [PATCH] Adds a "FixtureGroup" class that gives TestUnit fixture declarations more flexibility to [PATCH] flexible fixtures.
  • severity changed from normal to enhancement.
  • milestone set to 1.0.

Hi Duane, I've been reworking fixtures and Marcel pointed me to your patch: very nice work. My refactoring has some similar patterns to yours. I hope to merge the best of each with some bits from #2292 and apply shortly. Thank you!

10/15/05 02:12:14 changed by bitsweat

  • keywords changed from fixtures yaml fixture fd to fixtures yaml fixture.

Cleared fd tag. This is a disruptive change so I'm going to hold off until just after 1.0.

11/11/05 07:54:59 changed by james.adam@gmail.com

  • cc changed from michael@schuerig.de, marcel@ioni.st to james.adam@gmail.com,michael@schuerig.de, marcel@ioni.st.

11/21/05 16:31:51 changed by bitsweat

  • description changed.
  • milestone changed from 1.0 to 1.1.

03/05/06 17:09:26 changed by a-ruby-rails@harper.nu

  • cc changed from james.adam@gmail.com,michael@schuerig.de, marcel@ioni.st to a-ruby-rails@harper.nu,james.adam@gmail.com,michael@schuerig.de, marcel@ioni.st.

03/06/06 14:56:10 changed by anonymous

When will this be merged with trunk?

03/19/06 12:05:16 changed by magicmat@gmail.com

  • cc changed from a-ruby-rails@harper.nu,james.adam@gmail.com,michael@schuerig.de, marcel@ioni.st to a-ruby-rails@harper.nu,james.adam@gmail.com,michael@schuerig.de, marcel@ioni.st, magicmat@gmail.com.

Any news on if this will make 1.1?

05/09/06 23:01:54 changed by anonymous

Any update on this? This patch, or something that enables similar functionality, would be very helpful, especially in large complex systems testing. Is this even on the roadmap? Anyone? Bueller?

06/01/06 20:27:44 changed by rsanheim@gmail.com

Agree'd, any word on this? Would be very useful for legacy db's and testing.

(in reply to: ↑ description ) 09/15/06 16:20:45 changed by obrie

  • cc changed from a-ruby-rails@harper.nu,james.adam@gmail.com,michael@schuerig.de, marcel@ioni.st, magicmat@gmail.com to a-ruby-rails@harper.nu, james.adam@gmail.com, michael@schuerig.de, marcel@ioni.st, magicmat@gmail.com, aaron.pfeifer@gmail.com.

(in reply to: ↑ description ) 12/15/06 14:59:33 changed by delwaterman

  • cc changed from a-ruby-rails@harper.nu, james.adam@gmail.com, michael@schuerig.de, marcel@ioni.st, magicmat@gmail.com, aaron.pfeifer@gmail.com to a-ruby-rails@harper.nu, james.adam@gmail.com, michael@schuerig.de, marcel@ioni.st, magicmat@gmail.com, aaron.pfeifer@gmail.com, orion@mh2o.com.

Replying to duane.johnson@gmail.com:

At present, the only way to easily load a fixture in to a TestUnit is with the "fixtures" class method. While convenient, this method assumes several things about your tests that may not be true; for example, that you have only one set of fixture data per table for the entire application. Rather than introduce backwards incompatibilities to the 'fixtures' method, this patch introduces a new "fixture" class method that will give developers more flexibility if necessary. For example: {{{ class MixSingularAndPluralFixturesTest < Test::Unit::TestCase fixtures :topics, :developers, :accounts fixture :additional_topics, :table_name => "topics", :file_name => "topics2" ... end }}} In order to accomplish this, a class called FixtureGroup has been created to represent the idea of a "group" of fixtures. So, for example, the "fixtures" class method above is creating three fixture groups named "topics", "developers" and "accounts". These groups then use sensible defaults to set their class names, table names and fixture file names. When the developer needs to control these attributes at a finer level, she can choose to use the "fixture" class method. Like the 'fixtures' method, 'fixture' will assume settings appropriately, but only if attributes are not set. For example, the following line would assume the 'accounts' table and the Account class name, but would load a separate yaml file instead of the default "accounts.yml": {{{ fixture :accounts, :file_name => "accounts_in_process" }}} Additionally, multiple fixture files can be loaded in to the same table: {{{ fixture :topics_set_one, :table_name => "topics", :file_name => "topics" fixture :topics_set_two, :table_name => "topics", :file_name => "topics2" }}} Each of the above sets of fixtures can be accessed within the test class via @topics_set_one and @topics_set_two. An error will be raised if the data sets overlap (e.g. if both fixture files define a row in the database table with id '5', an insert error will occur). NOTE: Some tidying and fixing of the fixtures also took place in this patch. The deprecated yaml test was not reporting errors for .yaml files properly. In addition, there is now a "fixture not found" error message when there are NO matching fixtures (rather than causing error after error during unit tests). Finally, some code was re-arranged so that it matches the "class << self" tidy-looking pattern for a group of class methods.

02/25/07 19:18:44 changed by josh

  • keywords changed from fixtures yaml fixture to fixtures yaml fixture needy.
  • version changed from 0.13.1 to edge.

Patch needs to be updated to patch against edge.

06/18/07 19:25:44 changed by dasil003

+1 on this patch or something similar. I am testing a productized app against multiple view_paths, and having all the necessary data for all the tests in a single fixture really kills test performance. I've worked around it by adding the shared fixture information to each fixture file, but that is horrible.