Permalink
Browse files

behavioral tests for sqlite adapter

  • Loading branch information...
1 parent 80e47d7 commit 51783c0298d3789d49682f2ea94039ff2c1f1633 @tenderlove tenderlove committed Jul 10, 2010
Showing with 77 additions and 4 deletions.
  1. +77 −4 activerecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
View
81 activerecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
@@ -103,17 +103,90 @@ def test_tables_logs_name
end
end
+ def test_columns
+ columns = @ctx.columns('items').sort_by { |x| x.name }
+ assert_equal 2, columns.length
+ assert_equal %w{ id number }.sort, columns.map { |x| x.name }
+ assert_equal [nil, nil], columns.map { |x| x.default }
+ assert_equal [true, true], columns.map { |x| x.null }
+ end
+
+ def test_columns_with_default
+ @ctx.execute <<-eosql
+ CREATE TABLE columns_with_default (
+ id integer PRIMARY KEY AUTOINCREMENT,
+ number integer default 10
+ )
+ eosql
+ column = @ctx.columns('columns_with_default').find { |x|
+ x.name == 'number'
+ }
+ assert_equal 10, column.default
+ end
+
+ def test_columns_with_not_null
+ @ctx.execute <<-eosql
+ CREATE TABLE columns_with_default (
+ id integer PRIMARY KEY AUTOINCREMENT,
+ number integer not null
+ )
+ eosql
+ column = @ctx.columns('columns_with_default').find { |x|
+ x.name == 'number'
+ }
+ assert !column.null, "column should not be null"
+ end
+
+ def test_indexes_logs
+ intercept_logs_on @ctx
+ assert_difference('@ctx.logged.length') do
+ @ctx.indexes('items')
+ end
+ assert_match(/items/, @ctx.logged.last.first)
+ end
+
+ def test_no_indexes
+ assert_equal [], @ctx.indexes('items')
+ end
+
+ def test_index
+ @ctx.add_index 'items', 'id', :unique => true, :name => 'fun'
+ index = @ctx.indexes('items').find { |idx| idx.name == 'fun' }
+
+ assert_equal 'items', index.table
+ assert index.unique, 'index is unique'
+ assert_equal ['id'], index.columns
+ end
+
+ def test_non_unique_index
+ @ctx.add_index 'items', 'id', :name => 'fun'
+ index = @ctx.indexes('items').find { |idx| idx.name == 'fun' }
+ assert !index.unique, 'index is not unique'
+ end
+
+ def test_compound_index
+ @ctx.add_index 'items', %w{ id number }, :name => 'fun'
+ index = @ctx.indexes('items').find { |idx| idx.name == 'fun' }
+ assert_equal %w{ id number }.sort, index.columns.sort
+ end
+
+ private
+
def assert_logged logs
+ intercept_logs_on @ctx
+ yield
+ assert_equal logs, @ctx.logged
+ end
+
+ def intercept_logs_on ctx
@ctx.extend(Module.new {
- attr_reader :logged
+ attr_accessor :logged
def log sql, name
- @logged ||= []
@logged << [sql, name]
yield
end
})
- yield
- assert_equal logs, @ctx.logged
+ @ctx.logged = []
end
end
end

0 comments on commit 51783c0

Please sign in to comment.