Rake vs Ruby for running tests (error discrepency)

I’m having (to me) a strange problem with errors when running my tests
with rake as opposed to using ruby. If I do rake test:units I get this
error for several tests, but not all:

  1. Error:
    test_player_has_game_statistics_for_season(PlayerSeasonTest):
    ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘22’ for
    key 1: INSERT INTO positions (name, id, position) VALUES
    (‘athlete’, ‘22’, ‘ATH’)
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
    log' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/mysql_adapter.rb:185:inexecute’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:288:in
    insert_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:287:ininsert_fixtures’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:257:in
    create_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:257:increate_fixtures’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
    transaction' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:255:increate_fixtures’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:794:in
    silence' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:248:increate_fixtures’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:565:in
    load_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:512:insetup_with_fixtures’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:547:in
    `setup’

However if I use “ruby test/unit/player_season_test.rb” I don’t get any
errors. Same thing happens with funtional tests. Using rake to run the
tests gives me this error about duplicate keys but running the
functionals individually with Ruby passes fine. Any
hints/suggestions/solutions? Seems like some kind of issue with the
fixtures but I have no idea what I can or should be doing here. Thanks
for any help.

I had this issue too. Turned out my user model was calling my country
model somewhere (though nowhere I could think of – although it did in
the main layout – go figure!). I added the countries fixtures to the
tests and it sorted the prob. Suspect something similar is happening
with you. Not sure why there’s the discrepancy. Please post back if you
ever find out…

Chris T wrote:

I had this issue too. Turned out my user model was calling my country
model somewhere (though nowhere I could think of – although it did in
the main layout – go figure!). I added the countries fixtures to the
tests and it sorted the prob. Suspect something similar is happening
with you. Not sure why there’s the discrepancy. Please post back if you
ever find out…

I did have that as a similar problem but with different errors. I
finally figured that out and had added the fixtures that were needed for
the association. This is just stranger because it looks like its trying
to load the fixtures and can’t because of the ID being a primary key.
However the positions table (table/fixture in question here) are empty
to begin with. I’ve even tried to preload them but using rake they just
get zapped anyway. Admittedly my knowledge of how the rake task for
running tests works is low as is my knowledge of fixtures beyond making
them correctly formatted yaml and calling them in my tests. This just
seems like a potential bug but I can’t even figure how or what exactly
could be happening. My understanding is that loading a fixture removes
any prexisting data in the table before loading it so this error
shouldn’t occur. It’s not like it’s a foreign key error its a primary
key error and I’ve double checked that the fixture doesn’t have
duplicate entries.

Something I noticed is that if I have say 3 of this error they will
progress through the IDs:

  1. Error:
    ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘18’ for
    key 1:

  2. Error:
    ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘19’ for
    key 1:

  3. Error:
    ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘20’ for
    key 1:

and so on. Strange that it always ascends through the ID’s. Maybe
that’s a clue.

On Jun 24, 2006, at 10:54 AM, Paul C. wrote:

tests gives me this error about duplicate keys but running the
functionals individually with Ruby passes fine. Any
hints/suggestions/solutions? Seems like some kind of issue with the
fixtures but I have no idea what I can or should be doing here.
Thanks
for any help.

Take a look at your config/environment.rb file. Do you have a line
that looks like this?

config.active_record.schema_format = :ruby

If so, change “:ruby” to “:sql” and try again. The problem you are
having might be related to the lack of transactional support in
MySQL’s default database engine. If you overrode your engine choice
to use InnoDB, the ruby schema format doesn’t retain this
characteristic when reloading your test database (via the test:*
commands).

Anyway, check it out.

cr

Nice one Chris T.
I think my problem was closer to Chris’ one than the one of this post
(no MySQL error for me).
But same effect: test run with ruby, ok. Same test run with rake
test:units, failed!
I did add a missing fixture/model to the test and it passed!
Thanks again.

The thread is old, however we got similar error recently with rails 2
and mysql 5.1. It was related to missing fixtures, however the problem
with mysql

our installation is mysql 5.1.22 on CentOS 5 and 5.1.30 on Debian
we were able to reproduce the error with simple seqence of sql commands

select version();

DROP TABLE IF EXISTS test_inc;
CREATE TABLE test_inc (
id int(11) NOT NULL AUTO_INCREMENT,
account_id int(11) NOT NULL,
PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

select ‘insert explicit 9. must become 10’;
insert into test_inc ( id, account_id) values (9,1);
select * from test_inc;
show create table test_inc \G
select ‘delete all. stays 10’;
delete from test_inc;
select * from test_inc;
show create table test_inc \G

select ‘insert autoincrement. must become 11’;
insert into test_inc ( account_id) values (1) ;
select * from test_inc ;
show create table test_inc \G
select ‘delete all. stays 11’;
delete from test_inc ;
select * from test_inc ;
show create table test_inc \G

select ‘insert explicit 11. must become 12, but stays 11’;
insert into test_inc ( id, account_id) values (11,1);
select * from test_inc;
show create table test_inc \G

/*
select ‘insert explicit 12. must become 13’;
insert into test_inc ( id, account_id) values (12,1);
select * from test_inc;
show create table test_inc \G
*/

select * from test_inc;
show create table test_inc \G
select ‘insert auto-increment’;
insert into test_inc ( account_id) values (1);
select * from test_inc;
show create table test_inc \G

I’ve opened a bug with mysql, however they ‘closed’ it as unable to
reproduce

http://ep.blogware.com/blog/_archives/2009/1/9/4051185.html

http://bugs.mysql.com/bug.php?id=41984