Has anyone found a way to make fixtures work with foreign key constraints? I can manage to order the fixtures in a single unit test class in a way that no constraints are violated. That doesn''t help much, however, when in a following test one of those fixtures is deleted. I can''t set things up so that no constraints are violated in such a case. For now I''ve caved in and disable foreign key constraints for testing. I can''t say I like this stopgap solution. Michael -- Michael Schuerig Face reality and stare it down mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull, Silver River Turning http://www.schuerig.de/michael/
Michael Schuerig wrote:> Has anyone found a way to make fixtures work with foreign key > constraints? I can manage to order the fixtures in a single unit test > class in a way that no constraints are violated. That doesn''t help > much, however, when in a following test one of those fixtures is > deleted. I can''t set things up so that no constraints are violated in > such a case. > > For now I''ve caved in and disable foreign key constraints for testing. I > can''t say I like this stopgap solution. > > Michael >The only way I found (aluded to in previous mail) was to delete all fixture data in the teardown method. This is obviously very expensive though. I actually started taking a look yesterday at implementing a teardown method to test/unit that would run at the end of every testcase but didn''t get too far to be honest. Chris
I had a kludge solution to this. I made a base unit test class that loads all the fixtures in the correct order using the "fixtures" property. Then, I have my actual unit tests extend this base class-- so essentially each unit test is loading and deleting all the fixture data, but at least that is only being defined with one line of code in the base class. It''s not optimal, but I didn''t notice a performance hit. -jeff On Aug 18, 2005, at 8:59 AM, Chris Roos wrote:> Michael Schuerig wrote: > >> Has anyone found a way to make fixtures work with foreign key >> constraints? I can manage to order the fixtures in a single unit >> test class in a way that no constraints are violated. That doesn''t >> help much, however, when in a following test one of those fixtures >> is deleted. I can''t set things up so that no constraints are >> violated in such a case. >> For now I''ve caved in and disable foreign key constraints for >> testing. I can''t say I like this stopgap solution. >> Michael >> > The only way I found (aluded to in previous mail) was to delete all > fixture data in the teardown method. This is obviously very > expensive though. I actually started taking a look yesterday at > implementing a teardown method to test/unit that would run at the > end of every testcase but didn''t get too far to be honest. > > Chris > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Thursday 18 August 2005 20:57, Jeff Cole wrote:> I had a kludge solution to this. I made a base unit test class that > loads all the fixtures in the correct order using the "fixtures" > property. Then, I have my actual unit tests extend this base > class-- so essentially each unit test is loading and deleting all the > fixture data, but at least that is only being defined with one line > of code in the base class. It''s not optimal, but I didn''t notice a > performance hit.Sounds reasonable. However, I''m not sure if this solution is always applicable. There may be no linear ordering of fixtures. That is, looking at dependencies on a per-table basis can be too coarse-grained. Michael -- Michael Schuerig All good people read good books mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety
Chris Roos wrote:> Michael Schuerig wrote: > >> Has anyone found a way to make fixtures work with foreign key >> constraints? I can manage to order the fixtures in a single unit test >> class in a way that no constraints are violated. That doesn''t help >> much, however, when in a following test one of those fixtures is >> deleted. I can''t set things up so that no constraints are violated in >> such a case. >> >> For now I''ve caved in and disable foreign key constraints for testing. >> I can''t say I like this stopgap solution. >> >> Michael >> > The only way I found (aluded to in previous mail) was to delete all > fixture data in the teardown method. This is obviously very expensive > though. I actually started taking a look yesterday at implementing a > teardown method to test/unit that would run at the end of every testcase > but didn''t get too far to be honest. > > Chris >I''ve just spent some more time on this this morning and have come up with a way to have a class method run at the end of a TestCase (actually a TestSuite but this seems ok in my limited testing so far). You need to edit the run method in the Test::Unit::TestSuite class. This is in the testsuite.rb file in the test/unit directory. Your amended version should look as below, where the lines starting + have been added. It''s really quite limited in it''s current form but is certainly ok for what I needed it for - namely deleting the fixture data at the end of the TestCase rather than at the end of each TestMethod. All it does is try to call a class method called testcase_setup before any tests are run and then a class method called testcase_teardown after all tests are run. I''m guessing (i.e. haven''t even looked at it) that it shouldn''t be too difficult to get the fixture data removed automatically within testcase_teardown but I might save this for another time. BTW. This was tested using a single file with a TestCase, multiple TestCases in multiple files all ''required'' into one ''TestSuite'' file and running my rails unit tests from rake. Chris <code below> def run(result, &progress_block) yield(STARTED, name) + begin + eval "#{name}.testcase_setup" if Object.const_defined?name + rescue NameError + end @tests.each do |test| test.run(result, &progress_block) end + begin + eval "#{name}.testcase_teardown" if Object.const_defined?name + rescue NameError + end yield(FINISHED, name) end
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Could Kernel.const_get "ClassName" be used here to avoid the eval, which I''ve heard is a bit slow? Regards, - ------------------------------------------------------------------------ /*Ronny Hanssen*/ Chris Roos wrote:> Chris Roos wrote: > >> Michael Schuerig wrote: >> >>> Has anyone found a way to make fixtures work with foreign key >>> constraints? I can manage to order the fixtures in a single unit test >>> class in a way that no constraints are violated. That doesn''t help >>> much, however, when in a following test one of those fixtures is >>> deleted. I can''t set things up so that no constraints are violated in >>> such a case. >>> >>> For now I''ve caved in and disable foreign key constraints for >>> testing. I can''t say I like this stopgap solution. >>> >>> Michael >>> >> The only way I found (aluded to in previous mail) was to delete all >> fixture data in the teardown method. This is obviously very expensive >> though. I actually started taking a look yesterday at implementing a >> teardown method to test/unit that would run at the end of every >> testcase but didn''t get too far to be honest. >> >> Chris >> > I''ve just spent some more time on this this morning and have come up > with a way to have a class method run at the end of a TestCase (actually > a TestSuite but this seems ok in my limited testing so far). > > You need to edit the run method in the Test::Unit::TestSuite class. This > is in the testsuite.rb file in the test/unit directory. > > Your amended version should look as below, where the lines starting + > have been added. > > It''s really quite limited in it''s current form but is certainly ok for > what I needed it for - namely deleting the fixture data at the end of > the TestCase rather than at the end of each TestMethod. All it does is > try to call a class method called testcase_setup before any tests are > run and then a class method called testcase_teardown after all tests are > run. > > I''m guessing (i.e. haven''t even looked at it) that it shouldn''t be too > difficult to get the fixture data removed automatically within > testcase_teardown but I might save this for another time. > > BTW. This was tested using a single file with a TestCase, multiple > TestCases in multiple files all ''required'' into one ''TestSuite'' file and > running my rails unit tests from rake. > > Chris > > <code below> > > def run(result, &progress_block) > yield(STARTED, name) > > + begin > + eval "#{name}.testcase_setup" if Object.const_defined?name > + rescue NameError > + end > > @tests.each do |test| > test.run(result, &progress_block) > end > > + begin > + eval "#{name}.testcase_teardown" if Object.const_defined?name > + rescue NameError > + end > > yield(FINISHED, name) > end > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (MingW32) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFDBdQhMRRzQX3ma+kRAgr+AJ9JCfAYFvQbB1upOziXqv3UQQQ/JgCfaacE KS65IHJlbVHpVeETcwCchcc=ICi2 -----END PGP SIGNATURE-----
On Friday 19 August 2005 14:15, Chris Roos wrote:> Chris Roos wrote: > > The only way I found (aluded to in previous mail) was to delete all > > fixture data in the teardown method. This is obviously very > > expensive though. I actually started taking a look yesterday at > > implementing a teardown method to test/unit that would run at the > > end of every testcase but didn''t get too far to be honest. > > > > Chris > > I''ve just spent some more time on this this morning and have come up > with a way to have a class method run at the end of a TestCase > (actually a TestSuite but this seems ok in my limited testing so > far).This looks like a perfect case for decorators. One decorator that wraps around a single test case (class) and adorns it with methods that are run before and after the entire set of test methods from the class. Another decorator could do the same for all unit tests. JUnit has the former, I''m not sure about the latter. I''m surprised that Test::Unit doesn''t have the equivalent. A small matter of programming I presume. Michael -- Michael Schuerig Those people who smile a lot mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You
Yup. I just changed the eval lines to the following and it still works fine. (Kernel.const_get name).testcase_setup (Kernel.const_get name).testcase_teardown I''m not sure why I would use Kernel over Object or vice versa - they both seem to work fine for everything I''ve tested against them. Chris Ronny Hanssen wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Could Kernel.const_get "ClassName" be used here to avoid the eval, which > I''ve heard is a bit slow? > > Regards, > - ------------------------------------------------------------------------ > /*Ronny Hanssen*/ > > Chris Roos wrote: > >>Chris Roos wrote: >> >> >>>Michael Schuerig wrote: >>> >>> >>>>Has anyone found a way to make fixtures work with foreign key >>>>constraints? I can manage to order the fixtures in a single unit test >>>>class in a way that no constraints are violated. That doesn''t help >>>>much, however, when in a following test one of those fixtures is >>>>deleted. I can''t set things up so that no constraints are violated in >>>>such a case. >>>> >>>>For now I''ve caved in and disable foreign key constraints for >>>>testing. I can''t say I like this stopgap solution. >>>> >>>>Michael >>>> >>> >>>The only way I found (aluded to in previous mail) was to delete all >>>fixture data in the teardown method. This is obviously very expensive >>>though. I actually started taking a look yesterday at implementing a >>>teardown method to test/unit that would run at the end of every >>>testcase but didn''t get too far to be honest. >>> >>>Chris >>> >> >>I''ve just spent some more time on this this morning and have come up >>with a way to have a class method run at the end of a TestCase (actually >> a TestSuite but this seems ok in my limited testing so far). >> >>You need to edit the run method in the Test::Unit::TestSuite class. This >>is in the testsuite.rb file in the test/unit directory. >> >>Your amended version should look as below, where the lines starting + >>have been added. >> >>It''s really quite limited in it''s current form but is certainly ok for >>what I needed it for - namely deleting the fixture data at the end of >>the TestCase rather than at the end of each TestMethod. All it does is >>try to call a class method called testcase_setup before any tests are >>run and then a class method called testcase_teardown after all tests are >>run. >> >>I''m guessing (i.e. haven''t even looked at it) that it shouldn''t be too >>difficult to get the fixture data removed automatically within >>testcase_teardown but I might save this for another time. >> >>BTW. This was tested using a single file with a TestCase, multiple >>TestCases in multiple files all ''required'' into one ''TestSuite'' file and >>running my rails unit tests from rake. >> >>Chris >> >><code below> >> >>def run(result, &progress_block) >> yield(STARTED, name) >> >>+ begin >>+ eval "#{name}.testcase_setup" if Object.const_defined?name >>+ rescue NameError >>+ end >> >> @tests.each do |test| >> test.run(result, &progress_block) >> end >> >>+ begin >>+ eval "#{name}.testcase_teardown" if Object.const_defined?name >>+ rescue NameError >>+ end >> >> yield(FINISHED, name) >>end >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.0 (MingW32) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFDBdQhMRRzQX3ma+kRAgr+AJ9JCfAYFvQbB1upOziXqv3UQQQ/JgCfaacE > KS65IHJlbVHpVeETcwCchcc> =ICi2 > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >