Hello, I''m using foreign keys in postgres and I''m having problems running unit tests. One unit test has something like: fixtures :authors, :songs There is a referential constraint in the ''songs'' table definition like: author_id int references authors(id) The first time I run the test with empty tables, things work. Trying to run the test again, or trying to run another test that uses only the :authors fixture, produces errors because postgres won''t let me delete ''authors'' records referenced by ''songs'' records. Anyone else been up against something similar? Any suggestions? I guess I could a) not use foreign keys in the test environment or b) try to order my tests in a specific way, but I''d like to a) have the tests verify referential integrity (I''m converting a legacy database to run with AR), and b) have the flexibility to run each test on its own. Maybe if there was a way to have the fixtures do a cascading delete when they reload, and make sure to reload in the the order of arguments to the ''fixtures'' method... Thanks Zach
On Thu, 31 Mar 2005 14:22:06 -0800, Zach Thompson <lists-DZILUfagCPWukZHgTAicrQ@public.gmane.org> wrote:> The first time I run the test with empty tables, things work. Trying > to run the test again, or trying to run another test that uses only the > :authors fixture, produces errors because postgres won''t let me delete > ''authors'' records referenced by ''songs'' records. > > Anyone else been up against something similar? Any suggestions?Yup, that''s normal. Easy fix, make a teardown function that deletes crap in the proper order. Here''s an example from my unit tests: def teardown Article.find_all.each do |art| art.destroy end end (in my code, categories contain articles, so you have to delete all the articles first, otherwise postgres complains that it can''t delete the categories because that violates referential integrity. I didn''t have to delete the Categories manually since rails handles that itself. I just had to delete the articles explicitly in the teardown method so they''d be gone before rails attempted to delete the categories. -- Urban Artography http://artography.ath.cx
On Apr 1, 2005, at 4:00 AM, Rob Park wrote:> On Thu, 31 Mar 2005 14:22:06 -0800, Zach Thompson > <lists-DZILUfagCPWukZHgTAicrQ@public.gmane.org> wrote: >> The first time I run the test with empty tables, things work. Trying >> to run the test again, or trying to run another test that uses only >> the >> :authors fixture, produces errors because postgres won''t let me delete >> ''authors'' records referenced by ''songs'' records. >> >> Anyone else been up against something similar? Any suggestions? > > Yup, that''s normal. Easy fix, make a teardown function that deletes > crap in the proper order. Here''s an example from my unit tests: >Actually, it''s not normal it''s a bug that was introduced a couple versions ago. They used to delete in the proper order, see: http://dev.rubyonrails.com/ticket/890 -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for the comments - they helped me get things working. The teardown workaround does the trick, but I also applied the patch and it has the added benefits of running faster, and of leaving data in the tables after the tests are done. On Apr 1, 2005, at 5:57 AM, Scott Barron wrote:> > On Apr 1, 2005, at 4:00 AM, Rob Park wrote: > >> On Thu, 31 Mar 2005 14:22:06 -0800, Zach Thompson >> <lists-DZILUfagCPWukZHgTAicrQ@public.gmane.org> wrote: >>> The first time I run the test with empty tables, things work. Trying >>> to run the test again, or trying to run another test that uses only >>> the >>> :authors fixture, produces errors because postgres won''t let me >>> delete >>> ''authors'' records referenced by ''songs'' records. >>> >>> Anyone else been up against something similar? Any suggestions? >> >> Yup, that''s normal. Easy fix, make a teardown function that deletes >> crap in the proper order. Here''s an example from my unit tests: >> > > Actually, it''s not normal it''s a bug that was introduced a couple > versions ago. They used to delete in the proper order, see: > http://dev.rubyonrails.com/ticket/890 > > -Scott > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Apr 1, 2005, at 4:03 PM, Zach Thompson wrote:> Thanks for the comments - they helped me get things working. The > teardown workaround does the trick, but I also applied the patch and > it has the added benefits of running faster, and of leaving data in > the tables after the tests are done. >Another comment, if you are going to go the teardown route (for anyone), I''d recommend executing the SQL directly (via the execute method of the connection object). It''ll be a bit faster, especially if you''re instantiating AR objects to destroy relations or anything like that. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails