Dave Lee
2005-Mar-11 06:34 UTC
test_validates_numericality_of failing in rake test_postgresql
I have a patch to add postgresql schema support, but first I need to test it. I am running rake test_postgresql (using rails svn and postgresql 8.0.1) and test_validates_numericality_of is failing. The rake output is: 1) Error: test_validates_numericality_of(ValidationsTest): ActiveRecord::StatementInvalid: ERROR: invalid input syntax for integer: "10.0" : INSERT INTO topics ("title", "author_name", "type", "approved", "bonus_time", "replies_count", "author_email_address", "written_on", "content", "last_read", "parent_id") VALUES(''numeric test'', NULL, NULL, 1, NULL, ''10.0'', NULL, ''2005-03-10 23:37:47'', ''whatever'', NULL, NULL) Do I submit a ticket for a failing test like this? It seems the problem is that rails does not convert the string "10.0" into an integer and postgresql also won''t convert "10.0" to an interger, though it will convert "10" to an integer. Looking at the code, it appears write_attribute will blindly store any value to any attribute, even if the type of the value does not match the attribute''s database type, is this true? Dave
Sebastian Kanthak
2005-Mar-11 10:30 UTC
Re: test_validates_numericality_of failing in rake test_postgresql
Hi Dave, Dave Lee wrote:>I have a patch to add postgresql schema support, but first I need to >test it. I am running rake test_postgresql (using rails svn and >postgresql 8.0.1) and test_validates_numericality_of is failing. The >rake output is: > > 1) Error: >test_validates_numericality_of(ValidationsTest): >ActiveRecord::StatementInvalid: ERROR: invalid input syntax for integer: "10.0" >: INSERT INTO topics ("title", "author_name", "type", "approved", >"bonus_time", "replies_count", "author_email_address", "written_on", >"content", "last_read", "parent_id") VALUES(''numeric test'', NULL, >NULL, 1, NULL, ''10.0'', NULL, ''2005-03-10 23:37:47'', ''whatever'', NULL, >NULL) > >Do I submit a ticket for a failing test like this? > > >ups, I only tested the validates_numericality stuff on MySQL, so I take the blame for it. This is really a problem in the tests (as opposed to in rails), because we are using a field declared as an integer in the db to test validation of floating point numbers. Could you please submit a ticket for this, I''ll try to take care of it...>It seems the problem is that rails does not convert the string "10.0" >into an integer and postgresql also won''t convert "10.0" to an >interger, though it will convert "10" to an integer. Looking at the >code, it appears write_attribute will blindly store any value to any >attribute, even if the type of the value does not match the >attribute''s database type, is this true? > > >I think you''re right here. I guess this works well as long as you make sure (through the use of validation) that values have the right type before saving. Sebastian
Dave Lee
2005-Mar-11 23:25 UTC
Re: test_validates_numericality_of failing in rake test_postgresql
Sebastian Kanthak <sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org> wrote:> This is really a problem in the tests (as opposed to in rails), because > we are using a field declared as an integer in the db to test validation > of floating point numbers.actually, the problem is not because the field is of type integer but the input values are float. the problem is that the input values are all strings, and while postgresql will convert the string ''10'' to an integer, and it will convert the float 10.0 to an integer, it won''t convert the string ''10.0'' to an integer. after looking into this issue, I think this problem could be considered a bug in AR. for example: create table countries (id int not null primary key, name varchar, population int); class Country < ActiveRecorde::Base; end canada = Country.new(''name'' => ''Canada'', population => ''32144385'') canada.population.class # Fixnum the value of the canada.population is of type fixnum, not string. it strikes me as inconsistent that AR will convert values one way, but not the other way (when saving to the database). this can be easily be fixed by changing @attributes.inject to self.attributes.inject in ActiveRecord::Base#attributes_with_quotes, which allows the test_validates_numericality_of test to pass, though it did break two tests that are boolean to integer conversions, so I added a fix for that too, in the patch I submitted to the ticket.> Could you please submit a ticket for this, I''ll try to take care of it...http://dev.rubyonrails.com/ticket/820