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

Ticket #3938 (closed defect: untested)

Opened 3 years ago

Last modified 1 year ago

[PATCH] ActiveRecord should not wipe out prefilled data

Reported by: bloritsch@d-haven.org Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 1.0.0
Severity: major Keywords: test fixture
Cc:

Description

ActiveRecord currently wipes clean all data in the database before it starts testing. When using migrations to prefill data, you can't test that the prefilled data exists properly.

For example, I have a system that manages projects. The migration I set up prefills the Projects table with a project for the site. When I tried to write a unit test to verify that the migration worked properly, I found out that the prefilled data was removed. Part of that prefilled data was an image, so it won't fit well in YAML. And that's not the point--I shouldn't have to put that record in YAML in the first place. It's not an expected result.

I have a patch so that the Fixtures class in the ActiveRecord package will delete all records by ID for any given table. So if the YAML fixtures for my Project tests had 100 records starting with 100 and counting up to 199, only those records would be removed. Any records prefilled by the migrations would remain in tact--allowing me to write unit tests against it.

I'll submit the patch separately, but for now I will include it in line:

--- fixtures.rb 2006-02-22 22:31:27.890625000 -0500 +++ fixtures.rb.bak 2006-02-22 22:33:30.859375000 -0500 @@ -276,9 +276,7 @@

end

def delete_existing_fixtures

- values.each do |fixture| - @connection.delete "DELETE FROM #{@table_name} WHERE ID=#{fixtureid?}", 'Fixture Delete' - end + @connection.delete "DELETE FROM #{@table_name}", 'Fixture Delete'

end

def insert_fixtures

Attachments

fixtures.diff (410 bytes) - added by bloritsch@d-haven.org on 02/23/06 03:48:57.
Patch to fixtures.rb to protect pre-filled data

Change History

02/23/06 03:48:57 changed by bloritsch@d-haven.org

  • attachment fixtures.diff added.

Patch to fixtures.rb to protect pre-filled data

04/21/07 19:09:55 changed by josh

  • status changed from new to closed.
  • resolution set to untested.