Max Schubert
2011-Nov-06 17:37 UTC
Rails 3.0.7 - validates uniqueness, factory girl, and transactions with rspec
Hi, We have a number of rspec tests that started failing with uniqueness validation failures once we added uniqueness constraints to our models despite their being no uniqueness violations at the DB level - we have unique constraints in the db and the same tests without the model-level validations work well - even negative tests that cause mysql to issue uuniqueness exceptions. We use factory girl for creating models for tests (with create not build). A teammate of mine determined this had to do with transactions within rspec, so for now we can work around the failing specs by turning transactions off before the tests and then on again afterwards, but this obviously has the disadvantage that if these non-transactional tests fail we have a database with test data artifacts left over in it. We have tried moving tests set up and tear down code from before all to before each and even within each it block itself - no change. The behavior in the tests is unique to the tests, we see no unique constraint issues in our integration nor production environments. Anyone else here encouter this behavior and have a resolution that does not involve turning off transactions for rspec or risk losing the idempotency of tests? If your response adds value to the thread I don''t care if you top post or not :) Max On 11/4/11, JavierQQ <jquarites-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I hope someone can help me with this > > I have a model Question, and its controller .. what I''ve done to > validate that there''s no evaluation with 2 question with the same > number is > > validates_uniqueness_of :qnumber, :scope > => :evaluation_id > > but when I try to update a question''s content I got an error message > that says that "#{qnumber} has already been taken" > and that means that the validation works, but not as I want > > Can I obtain wich "def" is using ? because I want to do this > - Question Model > > IF ("def create" or something) > do the validation > elsif ("def update") > don''t do it > end > > I hope someone can help me > > Thanks in advance > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2011-Nov-06 19:25 UTC
Re: Rails 3.0.7 - validates uniqueness, factory girl, and transactions with rspec
On Sun, Nov 6, 2011 at 12:37, Max Schubert <max.schubert-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> We have a number of rspec tests that started failing with uniqueness > validation failures once we added uniqueness constraints to our modelsAs lazy students like to say, "post teh codez pls!" :-) If you show us the relevant bits of the models, tests, etc., maybe we can spot something amiss.> if these non-transactional tests fail > we have a database with test data artifacts left over in it.So do like the purists say to: completely rebuild the test database from scratch for each test, which should test only one thing. -Dave PS: Same Max I knew at Comcast? Tell the (remaining) Tools guys I said hi! -- LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. Where: Northern Virginia, Washington DC (near Orange Line), and remote work. See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence). Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Schubert
2011-Nov-06 22:29 UTC
Re: Rails 3.0.7 - validates uniqueness, factory girl, and transactions with rspec
Dave, On Sun, Nov 6, 2011 at 2:25 PM, Dave Aronson <googlegroups2dave-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> If you show us the relevant bits of the models, tests, etc., maybe we > can spot something amiss.Trimmed down and sanitized model used in the spec class Widget < DnpElement before_save do |widget| widget.gadgets.each do |s| if s.locket_id != widget.locket_id raise ActiveRecord::AssociationTypeMismatch.new( %Q{Gadget with id #{s.id} not part of locket #{widget.peergroup_id}} ) end end end has_and_belongs_to_many \ :gadgets, \ :foreign_key => "widget_id", \ :association_foreign_key => "gadget_id", :join_table => "widgets_gadgets" validates :id, :name, :networkid, :uniqueness => { :message => ''must be a uniq ue value'' } validates :name, :id, :presence => true validates :name, :length => { :maximum => 64 } end The spec itself just uses Factory Girl to create instances of this model - again, without transactions in rspec on the code works fine, with them on we get unique key violations. Transactions in rspec rollback the database state after each test to ensure it stays clean regardless of whether the test passed or failed, which is a good thing - without them we have to write manual clean up code.> So do like the purists say to: completely rebuild the test database > from scratch for each test, which should test only one thing.That is not a reasonable solution for us as our test database is built by importing parts of another database we pull data from - rebuilding from scratch between each test would take 1-2 minutes per test - i''d think that for most larger projects, transactional clean up is a lot more desirable than rebuild from scratch as well for the same reasons. There is a database cleaner Gem that does what rspec now does - we used to use it before rspec had the transactional database sweetener it does now - it might record the database work done in a different way so that might be worth checking out for us, just thought I would check on the rails list for this one since this is probably the first Rails / Rspec issue we have run into as a team that we couldn''t resolve ourselves :). https://github.com/bmabey/database_cleaner Looks like it might be a good solution until the issues with threading and Rspec''s built in cleaner are resolved as the database_cleaner gem lets you choose a number of different strategies for cleaning. Yeah, Dave, same Max from Comcast - and will do! - Max -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Schubert
2011-Nov-07 22:13 UTC
Re: Rails 3.0.7 - validates uniqueness, factory girl, and transactions with rspec
Turned out that an earlier mail thread you had responded on, Dave, had the answer - I hadn''t looked into the model code deeply enough till today to realize our uniqueness constraints were missing :scope conditions to constrain them properly! - Max -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.