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

Ticket #4157 (reopened defect)

Opened 3 years ago

Last modified 5 months ago

[PATCH] Null values in a CSV format fixture show up a 0 in the test database

Reported by: josh@vectrice.com Assigned to: David
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: csv fixtures null, patch
Cc: bdimchef-rails@wieldim.com

Description

I'm running into the same problem that Steve describes in rails list. The solution can be to change:

row.each_with_index { |cell, j| puts data[header[j].to_s.strip] = cell.to_s.strip }

to:

row.each_with_index { |cell, j| puts data[header[j].to_s.strip] = cell.nil? ? cell : cell.to_s.strip }

in lib/active_record/fixtures.rb


Hi all,

I'm running into a strange problem. I'm trying to get null values to load from a fixture in CSV form into my test database, but they show up as 0 (zero).

I have a table people (simplified version below)

create table people ( id int not null auto_increment, target int null default null, name varchar(50) not null, primary key(id) );

that has a column target which can be null. In my development database many people have null values for target.

In my unit tests, I want to be able to use all the people (a couple of hundred), so I exported people from MySQL 4.1.12-standard in CSV format using the following command:

select * into outfile '/tmp/people.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from people;

That gives me a CSV file with null values represented as \N

According to the fixtures documentation, to get a null value inserted into your test database from a CSV, you should have nothing between the commas. "nulls can be achived by just placing a comma, for example, (1,sclausfalse,) minus the parenthesis of course." http://manuals.rubyonrails.com/read/chapter/26

So I went through and replaced \N with nothing ... as a result rows that previously looked like:

7,\N,"Steve"

now look like

7"Steve"

The rails unit test libraries load the fixture without any complaints.

BUT....

wherever I had a null for target in my development database, I now have a 0 my test database.

This is a problem because 0 is an illegal value for a target... targets must be positive or non-existant. As a result, the validation on my model (person.rb::validate) is failing and my unit tests are throwing errors.

As a work around, I commented out my validate, but I'd like to turn it back on.

I'm running the following: Mac OSX 10.4.5 Ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0] Rails 1.0.0

Thanks for any help. -Steve

Attachments

fix_null_csv_values.diff (1.7 kB) - added by rails-contrib@badfont.com on 04/24/06 08:03:19.
[PATCH] with test case
null_csv_values_tests.diff (1.2 kB) - added by josh on 04/23/07 23:22:35.
Passing tests [6553]
null_csv_values_tests2.diff (1.3 kB) - added by moofbong on 06/05/07 20:19:38.
Patch that works

Change History

04/24/06 08:03:19 changed by rails-contrib@badfont.com

  • attachment fix_null_csv_values.diff added.

[PATCH] with test case

04/24/06 08:06:05 changed by rails-contrib@badfont.com

  • summary changed from Null values in a CSV format fixture show up a 0 in the test database to [PATCH] Null values in a CSV format fixture show up a 0 in the test database.

Hello,

I've added a patch with a test case. I've tested it using rake test_mysql and it passes (using the Rails 1.1 trunk). This behaviour also exists in Rails 1.1 so I've updated the version too.

I apologise for the style or any extra test cases. This is my first patch submission!

04/24/06 08:06:34 changed by anonymous

  • version changed from 1.0.0 to 1.1.1.

05/15/06 04:43:18 changed by ryan allen <rails-contrib@badfont.com>

  • keywords changed from csv fixtures null to csv fixtures null, patch.

04/23/07 23:22:35 changed by josh

  • attachment null_csv_values_tests.diff added.

Passing tests [6553]

04/23/07 23:23:40 changed by josh

  • status changed from new to closed.
  • version changed from 1.1.1 to edge.
  • resolution set to fixed.
  • milestone set to 1.x.

Tests pass by themselves.

Maybe the second patch (tests only) should be applied for future safety.

06/05/07 20:19:38 changed by moofbong

  • attachment null_csv_values_tests2.diff added.

Patch that works

06/05/07 20:21:58 changed by moofbong

  • cc set to bdimchef-rails@wieldim.com.
  • status changed from closed to reopened.
  • resolution deleted.

I submitted a patch that will actually break the tests to show this bug. The old patch just used Fixtures.new which (as far as I can tell) doesn't actually load the fixtures. This one will load them using create_fixtures and will show that they get stored as 0 rather than NULL.

10/05/07 16:14:34 changed by dugsmith

  • milestone changed from 1.x to 1.2.4.

This didn't make it into 1.2.4 -- what can we do to get it in there?

02/06/09 18:39:59 changed by maz

  • milestone changed from 1.2.7 to 2.x.

This is still an issue in Active Record 2.3.

# Old
  row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip }

#New
  row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell && cell.to_s.strip }