# Unit tests breaks with sqlite

**URL:** <https://discuss.rubyonrails.org/t/unit-tests-breaks-with-sqlite/13862>\
**Category:** rubyonrails-talk\
**Created:** [June 2, 2007, 2:18pm UTC](https://discuss.rubyonrails.org/t/unit-tests-breaks-with-sqlite/13862 "2007-06-02T14:18:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nexos](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nexos](https://discuss.rubyonrails.org/u/Nexos)\
**Post date:** [June 2, 2007, 2:18pm UTC](https://discuss.rubyonrails.org/t/unit-tests-breaks-with-sqlite/13862/1 "2007-06-02T14:18:07Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;class User \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validates\_presence\_of :name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validates\_uniqueness\_of :name &nbsp;&nbsp;&nbsp;&nbsp;end

\* edited db/migrate/001\_create\_users.rb migration:

&nbsp;&nbsp;&nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_table :users do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :name, :string, :null =\> false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User.create(:name =\> 'administrator') &nbsp;&nbsp;&nbsp;&nbsp;end

\* and:

&nbsp;&nbsp;&nbsp;&nbsp;def self.down &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drop\_table :users &nbsp;&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;Loaded suite C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/ lib/rake/rake\_test\_loader &nbsp;&nbsp;Started &nbsp;&nbsp;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) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/resultset.rb:76:in `check' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/resultset.rb:68:in `commence' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/resultset.rb:61:in `initialize' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/statement.rb:163:in `new' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/statement.rb:163:in `execute' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/database.rb:212:in `execute' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/database.rb:187:in `prepare' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/sqlite3- ruby-1.2.1-mswin32/lib/sqlite3/database.rb:211:in `execute' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... 16 levels... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/1.8/test/unit/autorunner.rb: 200:in `run' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/1.8/test/unit/autorunner.rb: 13:in `run' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/InstantRails/ruby/lib/ruby/1.8/test/unit.rb:278 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Alexey\_Verkhovsky](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexey\_Verkhovsky](https://discuss.rubyonrails.org/u/Alexey_Verkhovsky)\
**Post date:** [June 2, 2007, 4:33pm UTC](https://discuss.rubyonrails.org/t/unit-tests-breaks-with-sqlite/13862/2 "2007-06-02T16:33:41Z")

</div>

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: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... 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 &nbsp;&nbsp;puts e.message &nbsp;&nbsp;puts e.backtrace &nbsp;&nbsp;raise end

so that you can see those "16 levels".

Alex

---

<div class="post-metadata">

**Author:** ![Nexos](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nexos](https://discuss.rubyonrails.org/u/Nexos)\
**Post date:** [June 3, 2007, 7:18am UTC](https://discuss.rubyonrails.org/t/unit-tests-breaks-with-sqlite/13862/3 "2007-06-03T07:18:10Z")

</div>

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

first &nbsp;&nbsp;id: 1 second &nbsp;&nbsp;id: 2

After adding a (mandatory in my model) name attribute in the fixture, like first &nbsp;&nbsp;id: 1 &nbsp;&nbsp;name: foo second &nbsp;&nbsp;id:2 &nbsp;&nbsp;name: bar

it worked.

However, what's odd is that evenif I commented the line &nbsp;&nbsp;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
