Forum: Ruby on Rails Tests not tiding up after themselves?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B1d59a804bd67487c964bc505a8eb892?d=identicon&s=25 Thiago Arrais (Guest)
on 2006-03-27 19:21
(Received via mailing list)
Hello,

My tests are messing with one another because it seems that they
aren't cleaning the database after execution. I have the tables
headlines and articles. And the articles table has a foreign key to
the headlines table. My schema structure is created by this script

----
drop table if exists changes;
drop table if exists articles;
drop table if exists headlines;
create table headlines (
	id int not null auto_increment,
	author varchar(40) not null,
	title varchar(100) not null,
	happened_at timestamp not null,
	reported_by varchar(40) not null,
    -- An id that identifies the revision to the reporter
	rid varchar(40) not null,
	primary key (id)
);
create table articles (
	id int not null auto_increment,
	headline_id int not null,
	description text,
	constraint fk_article_headline foreign key (headline_id) references
headlines(id),
	primary key (id)
);
create table changes (
	id int not null auto_increment,
	article_id int not null,
	summary text not null,
	diff text,
	constraint fk_change_article foreign key (article_id) references
articles(id),
	primary key (id)
);
----

The Headline class declares a "has_one" relationship to the Article
class. And the Article class declares a "has_many" relationship to the
Change class. I have tests for the Article and Headline classes, and
if I run the article tests before the headline tests without
explicitly cleaning the database, I get a foreing key constraint
error. For example:

----
$ ruby test/unit/headline_test.rb
Loaded suite test/unit/headline_test
Started
...........
Finished in 0.312 seconds.

11 tests, 14 assertions, 0 failures, 0 errors
----

This runs nicely. But if I had run the article tests before, I get this

----
$ ruby test/unit/article_test.rb
Loaded suite test/unit/article_test
Started
.
Finished in 0.203 seconds.


1 tests, 1 assertions, 0 failures, 0 errors
$ C:\devel\motiro>ruby test/unit/headline_test.rb | more
Loaded suite test/unit/headline_test
Started
EEEEEEEEEEE
Finished in 0.531 seconds.

  1) Error:
test_cache_already_recorded(HeadlineTest):
ActiveRecord::StatementInvalid: Mysql::Error: #23000Cannot delete or
update a pa
rent row: a foreign key constraint fails: DELETE FROM articles
----

The article tests use the :headlines, :articles and :changes fixtures
but the headline tests only need the :headlines and :articles
fixtures. If I add the changes fixtures to the headline tests,
everything runs smoothly. I think the database cleanup is done before
running the tests on the fixtures used for that test case only, but
because the previous test left the database dirty there are foreign
key constraints when doing this. The headline test should clean all
the three tables, but it tries to clean only those it knows about.

I have tried to use :dependent and :exclusively_dependent on the
article-change relationship, but it didn't help.

For the sake of completeness, I am using MySQL 4.1.

Should I be doing anything differently? Do I have to use all three
fixtures on all my test cases?

Cheers,

Thiago Arrais
317aad7f9f0b69a440faca74bbe22b20?d=identicon&s=25 Jakob L. Skjerning (Guest)
on 2006-03-27 20:19
(Received via mailing list)
Thiago Arrais wrote:
> My tests are messing with one another because it seems that they
> aren't cleaning the database after execution.

Sounds like you might be running with transactional_fixtures on a
database that doesn't support transactions. You can read more about this
at http://www.clarkware.com/cgi/blosxom/2005/10/24.
This topic is locked and can not be replied to.