I'm not suggesting that it is wrong for your specific project, but this is why you shouldn't use transaction-wrapping as a substitute for schema teardown ;) (unless your database supports nested transactions, which MySQL InnoDB does not).
mysql> create table tbl (test int not null default 0) engine=InnoDB; Query OK, 0 rows affected (0.06 sec) mysql> begin; /* Test runner starts a txn */ Query OK, 0 rows affected (0.00 sec) mysql> insert into tbl values (1); /* Some data is created during test */ Query OK, 1 row affected (0.00 sec) mysql> begin; /* application logic starts a txn */ Query OK, 0 rows affected (0.00 sec) mysql> insert into tbl value (2); /* more data is inserted */ Query OK, 1 row affected (0.00 sec) mysql> commit; /* application logic commits, what it thinks is its own txn */ Query OK, 0 rows affected (0.00 sec) mysql> rollback; /* Test runner rolls back a txn that has already been commited */ Query OK, 0 rows affected (0.00 sec) mysql> select * from tbl; /* Your environment is dirty */ +------+ | test | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) mysql> Some databases do support nested transactions, so this doesn't apply to everyone, but we actually run on a combination of InnoDB and MyISAM tables. El 10/05/2011, a las 16:49, Ian MacLeod escribió: > I can't directly answer why it's not tearing down your test database, > but I've had a lot of luck using https://github.com/bmabey/database_cleaner > to ensure a clean slate between each test - and it's quick about it! > It starts a transaction at the start of a test, and rolls back when > the test is over > > -Ian > > On May 9, 11:43 pm, Chris Corbyn <[email protected]> wrote: >> Hi All, >> >> I'm not sure what I'm missing, but I had a project that started with >> ActiveRecord, which I then subsequently swapped to use DataMapper, by adding >> the gem and only requiring selected parts of rails into my application.rb. >> >> I have these included in application.rb: >> >> require "action_controller/railtie" >> require "action_mailer/railtie" >> require "rails/test_unit/railtie" >> >> When I run my unit tests, any data inserted into the database during the >> test is left there, dirtying the state for the next test. I can't see what >> I've missed, comparing my project with a blank one created using: >> >> rails new datamapper_project -mhttp://datamapper.org/templates/rails.rb >> >> Can anyone shed any light on this? I had disabled fixtures (because I don't >> use them), but this should affect it. Oddly, when I put "fixtures :all" >> back in the test_helper.rb, the test runner crashes with: >> >> /home/ccorbyn/projects/flippa-rails/test/test_helper.rb:6:in >> `<class:TestCase>': undefined method `fixtures' for >> ActiveSupport::TestCase:Class (NoMethodError) >> from /home/ccorbyn/projects/flippa-rails/test/test_helper.rb:5:in >> `<top (required)>' >> from test/unit/user_test.rb:1:in `require' >> from test/unit/user_test.rb:1:in `<top (required)>' >> from >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load >> er.rb:5:in `load' >> from >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load >> er.rb:5:in `block in <main>' >> from >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load >> er.rb:5:in `each' >> from >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake/rake_test_load >> er.rb:5:in `<main>' >> >> Surely projects without AR still have their test databases torn down after >> each test, right? >> >> class ActiveSupport::TestCase >> fixtures :all >> end >> >> Here's a little test showing the issue: >> >> class UserTest < ActiveSupport::TestCase >> test "user should be created" do >> user = User.new >> user.username = "testing" >> user.full_name = "Test User" >> assert user.save, "User should be saved" >> end >> >> test "no users should exist by default" do >> assert_equal 0, User.count, "No users should exist by >> default" >> end >> end >> >> Which fails with: >> >> 1) Failure: >> test_no_users_should_exist_by_default(UserTest) [test/unit/user_test.rb:12]: >> No users should exist by default. >> <0> expected but was >> <69>. >> >> 2) Error: >> test_user_should_be_created(UserTest): >> DataObjects::IntegrityError: Duplicate entry 'testing' for key 2 >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap >> ter/adapter.rb:114:in `execute_non_query' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap >> ter/adapter.rb:114:in `block (2 levels) in create' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap >> ter/adapter.rb:276:in `with_connection' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap >> ter/adapter.rb:113:in `block in create' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap >> ter/adapter.rb:85:in `each' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-do-adapter-1.1.0/lib/dm-do-adap >> ter/adapter.rb:85:in `create' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/reposito >> ry.rb:146:in `create' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> /state/transient.rb:61:in `create_resource' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> /state/transient.rb:25:in `commit' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:957:in `_persist' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:971:in `block in create_with_hooks' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:968:in `catch' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:968:in `create_with_hooks' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:1022:in `save_self' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:1007:in `block in _save' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:1223:in `run_once' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:1006:in `_save' >> >> /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/resource >> .rb:406:in `save' >> test/unit/user_test.rb:8:in `block in <class:UserTest>' >> >> Something really weird is happening. That number <69> goes up by one every >> time I run this test. >> >> Cheers, >> >> Chris > > -- > You received this message because you are subscribed to the Google Groups > "DataMapper" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/datamapper?hl=en. > -- You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
