Unit tests breaks with sqlite


I'm using an sqlite3 database with Rails and have some trouble running
unit tests

What I've done is
* rails my_app
* edited database.yml to use sqlite3 adapter, db/my_app_development
and db/my_app_test
* ruby script\generate model user
* edited model

    class User < ActiveRecord::Base
        validates_presence_of :name
        validates_uniqueness_of :name

* edited db/migrate/001_create_users.rb migration:

    def self.up
        create_table :users do |t|
          t.column :name, :string, :null => false
        User.create(:name => 'administrator')

* and:

    def self.down
        drop_table :users

* rake db:migrate
* rake db:test:prepare

At this point, the 2 DBs are created correctly.

* rake test:units

The problem is that, but when I want to run a (any) test, I get a "SQL
logic error or missing database (SQLite3::SQLException). I can however
access the DB via the sqlite3 client/rails console without any

Here's the exact error message I get:

  Loaded suite C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/
mswin32/lib/sqlite3/errors.rb:94:in `check': SQL logic error or
missing database (SQLite3::SQLException)
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/resultset.rb:76:in `check'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/resultset.rb:68:in `commence'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/resultset.rb:61:in `initialize'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/statement.rb:163:in `new'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/statement.rb:163:in `execute'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/database.rb:212:in `execute'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/database.rb:187:in `prepare'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-
ruby-1.2.1-mswin32/lib/sqlite3/database.rb:211:in `execute'
         ... 16 levels...
        from C:/InstantRails/ruby/lib/ruby/1.8/test/unit/autorunner.rb:
200:in `run'
        from C:/InstantRails/ruby/lib/ruby/1.8/test/unit/autorunner.rb:
13:in `run'
        from C:/InstantRails/ruby/lib/ruby/1.8/test/unit.rb:278
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/

I'm using Rails 1.2.3 with ruby 1.8.5, sqlite3.3.7 and sqlite3-ruby
Any help would be greatly appreciated!

Thanks in advance,

Frédéric Delanoy

I've seen that "logic error" where there was a COMMIT or ROLLBACK, but
no transaction was in flight. This is an Exception (not an Error), and
Test::Unit doesn't catch them, unfortunately.

Somewhere within this line:
        ... 16 levels...
is your unit test code.

To debug it, modify something like execute in sqlite3/database.rb to
do the following:

rescue Exception => e
  puts e.message
  puts e.backtrace

so that you can see those "16 levels".


I've finally found the source of the problem: it was due to the
default fixtures in use, i.e.

  id: 1
  id: 2

After adding a (mandatory in my model) name attribute in the fixture,
  id: 1
  name: foo
  name: bar

it worked.

However, what's odd is that evenif I commented the line
  fixtures :users
in the unit test file, I had the some error.

Maybe the fixtures are used automagically ???

In all cases, thank you for your help Alexey !