Unit tests breaks with sqlite

Hi,

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
    end

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

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

  • and:

    def self.down
    drop_table :users
    end

  • 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
problem.

Here’s the exact error message I get:

Loaded suite C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/
lib/rake/rake_test_loader
Started
EC:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1-
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:incheck’
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:ininitialize’
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:inexecute’
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:inprepare’
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:inrun’
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/
lib/rake/rake_test_loader.rb:5

I’m using Rails 1.2.3 with ruby 1.8.5, sqlite3.3.7 and sqlite3-ruby
(1.2.1)
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
raise
end

so that you can see those “16 levels”.

Alex

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

first
id: 1
second
id: 2

After adding a (mandatory in my model) name attribute in the fixture,
like
first
id: 1
name: foo
second
id:2
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 !

Frédéric

On 2 juin, 18:33, “Alexey V.” [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs