Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
508c679f
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
508c679f
编写于
4月 08, 2011
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
moving sqlite_adapter_tests to sqlite3_adapter_test so that the tests are actually run.
💣
上级
bd79172e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
215 addition
and
229 deletion
+215
-229
activerecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
...erecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
+0
-229
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
...ecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
+215
-0
未找到文件。
activerecord/test/cases/adapters/sqlite/sqlite_adapter_test.rb
已删除
100644 → 0
浏览文件 @
bd79172e
# encoding: utf-8
require
"cases/helper"
require
'models/binary'
module
ActiveRecord
module
ConnectionAdapters
class
SQLiteAdapterTest
<
ActiveRecord
::
TestCase
class
DualEncoding
<
ActiveRecord
::
Base
end
def
setup
@ctx
=
Base
.
sqlite3_connection
:database
=>
':memory:'
,
:adapter
=>
'sqlite3'
,
:timeout
=>
nil
@ctx
.
execute
<<-
eosql
CREATE TABLE items (
id integer PRIMARY KEY AUTOINCREMENT,
number integer
)
eosql
end
def
test_quote_binary_column_escapes_it
DualEncoding
.
connection
.
execute
(
<<-
eosql
)
CREATE TABLE dual_encodings (
id integer PRIMARY KEY AUTOINCREMENT,
name string,
data binary
)
eosql
str
=
"
\x80
"
.
force_encoding
(
"ASCII-8BIT"
)
binary
=
DualEncoding
.
new
:name
=>
'いただきます!'
,
:data
=>
str
binary
.
save!
assert_equal
str
,
binary
.
data
end
def
test_execute
@ctx
.
execute
"INSERT INTO items (number) VALUES (10)"
records
=
@ctx
.
execute
"SELECT * FROM items"
assert_equal
1
,
records
.
length
record
=
records
.
first
assert_equal
10
,
record
[
'number'
]
assert_equal
1
,
record
[
'id'
]
end
def
test_quote_string
assert_equal
"''"
,
@ctx
.
quote_string
(
"'"
)
end
def
test_insert_sql
2
.
times
do
|
i
|
rv
=
@ctx
.
insert_sql
"INSERT INTO items (number) VALUES (
#{
i
}
)"
assert_equal
(
i
+
1
,
rv
)
end
records
=
@ctx
.
execute
"SELECT * FROM items"
assert_equal
2
,
records
.
length
end
def
test_insert_sql_logged
sql
=
"INSERT INTO items (number) VALUES (10)"
name
=
"foo"
assert_logged
([[
sql
,
name
]])
do
@ctx
.
insert_sql
sql
,
name
end
end
def
test_insert_id_value_returned
sql
=
"INSERT INTO items (number) VALUES (10)"
idval
=
'vuvuzela'
id
=
@ctx
.
insert_sql
sql
,
nil
,
nil
,
idval
assert_equal
idval
,
id
end
def
test_select_rows
2
.
times
do
|
i
|
@ctx
.
create
"INSERT INTO items (number) VALUES (
#{
i
}
)"
end
rows
=
@ctx
.
select_rows
'select number, id from items'
assert_equal
[[
0
,
1
],
[
1
,
2
]],
rows
end
def
test_select_rows_logged
sql
=
"select * from items"
name
=
"foo"
assert_logged
([[
sql
,
name
]])
do
@ctx
.
select_rows
sql
,
name
end
end
def
test_transaction
count_sql
=
'select count(*) from items'
@ctx
.
begin_db_transaction
@ctx
.
create
"INSERT INTO items (number) VALUES (10)"
assert_equal
1
,
@ctx
.
select_rows
(
count_sql
).
first
.
first
@ctx
.
rollback_db_transaction
assert_equal
0
,
@ctx
.
select_rows
(
count_sql
).
first
.
first
end
def
test_tables
assert_equal
%w{ items }
,
@ctx
.
tables
@ctx
.
execute
<<-
eosql
CREATE TABLE people (
id integer PRIMARY KEY AUTOINCREMENT,
number integer
)
eosql
assert_equal
%w{ items people }
.
sort
,
@ctx
.
tables
.
sort
end
def
test_tables_logs_name
name
=
"hello"
assert_logged
[[
name
]]
do
@ctx
.
tables
(
name
)
assert_not_nil
@ctx
.
logged
.
first
.
shift
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
def
test_primary_key
assert_equal
'id'
,
@ctx
.
primary_key
(
'items'
)
@ctx
.
execute
<<-
eosql
CREATE TABLE foos (
internet integer PRIMARY KEY AUTOINCREMENT,
number integer not null
)
eosql
assert_equal
'internet'
,
@ctx
.
primary_key
(
'foos'
)
end
def
test_no_primary_key
@ctx
.
execute
'CREATE TABLE failboat (number integer not null)'
assert_nil
@ctx
.
primary_key
(
'failboat'
)
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_accessor
:logged
def
log
sql
,
name
@logged
<<
[
sql
,
name
]
yield
end
})
@ctx
.
logged
=
[]
end
end
end
end
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
浏览文件 @
508c679f
# encoding: utf-8
require
"cases/helper"
module
ActiveRecord
module
ConnectionAdapters
class
SQLite3AdapterTest
<
ActiveRecord
::
TestCase
class
DualEncoding
<
ActiveRecord
::
Base
end
def
setup
@conn
=
Base
.
sqlite3_connection
:database
=>
':memory:'
,
:adapter
=>
'sqlite3'
,
:timeout
=>
100
@conn
.
execute
<<-
eosql
CREATE TABLE items (
id integer PRIMARY KEY AUTOINCREMENT,
number integer
)
eosql
end
def
test_primary_key_returns_nil_for_no_pk
...
...
@@ -102,6 +112,211 @@ def test_exec_query_typecasts_bind_vals
assert_equal
[[
1
,
'foo'
]],
result
.
rows
end
def
test_quote_binary_column_escapes_it
DualEncoding
.
connection
.
execute
(
<<-
eosql
)
CREATE TABLE dual_encodings (
id integer PRIMARY KEY AUTOINCREMENT,
name string,
data binary
)
eosql
str
=
"
\x80
"
.
force_encoding
(
"ASCII-8BIT"
)
binary
=
DualEncoding
.
new
:name
=>
'いただきます!'
,
:data
=>
str
binary
.
save!
assert_equal
str
,
binary
.
data
end
def
test_execute
@conn
.
execute
"INSERT INTO items (number) VALUES (10)"
records
=
@conn
.
execute
"SELECT * FROM items"
assert_equal
1
,
records
.
length
record
=
records
.
first
assert_equal
10
,
record
[
'number'
]
assert_equal
1
,
record
[
'id'
]
end
def
test_quote_string
assert_equal
"''"
,
@conn
.
quote_string
(
"'"
)
end
def
test_insert_sql
2
.
times
do
|
i
|
rv
=
@conn
.
insert_sql
"INSERT INTO items (number) VALUES (
#{
i
}
)"
assert_equal
(
i
+
1
,
rv
)
end
records
=
@conn
.
execute
"SELECT * FROM items"
assert_equal
2
,
records
.
length
end
def
test_insert_sql_logged
sql
=
"INSERT INTO items (number) VALUES (10)"
name
=
"foo"
assert_logged
([[
sql
,
name
,
[]]])
do
@conn
.
insert_sql
sql
,
name
end
end
def
test_insert_id_value_returned
sql
=
"INSERT INTO items (number) VALUES (10)"
idval
=
'vuvuzela'
id
=
@conn
.
insert_sql
sql
,
nil
,
nil
,
idval
assert_equal
idval
,
id
end
def
test_select_rows
2
.
times
do
|
i
|
@conn
.
create
"INSERT INTO items (number) VALUES (
#{
i
}
)"
end
rows
=
@conn
.
select_rows
'select number, id from items'
assert_equal
[[
0
,
1
],
[
1
,
2
]],
rows
end
def
test_select_rows_logged
sql
=
"select * from items"
name
=
"foo"
assert_logged
([[
sql
,
name
,
[]]])
do
@conn
.
select_rows
sql
,
name
end
end
def
test_transaction
count_sql
=
'select count(*) from items'
@conn
.
begin_db_transaction
@conn
.
create
"INSERT INTO items (number) VALUES (10)"
assert_equal
1
,
@conn
.
select_rows
(
count_sql
).
first
.
first
@conn
.
rollback_db_transaction
assert_equal
0
,
@conn
.
select_rows
(
count_sql
).
first
.
first
end
def
test_tables
assert_equal
%w{ items }
,
@conn
.
tables
@conn
.
execute
<<-
eosql
CREATE TABLE people (
id integer PRIMARY KEY AUTOINCREMENT,
number integer
)
eosql
assert_equal
%w{ items people }
.
sort
,
@conn
.
tables
.
sort
end
def
test_tables_logs_name
name
=
"hello"
assert_logged
[[
name
,
[]]]
do
@conn
.
tables
(
name
)
assert_not_nil
@conn
.
logged
.
first
.
shift
end
end
def
test_columns
columns
=
@conn
.
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
@conn
.
execute
<<-
eosql
CREATE TABLE columns_with_default (
id integer PRIMARY KEY AUTOINCREMENT,
number integer default 10
)
eosql
column
=
@conn
.
columns
(
'columns_with_default'
).
find
{
|
x
|
x
.
name
==
'number'
}
assert_equal
10
,
column
.
default
end
def
test_columns_with_not_null
@conn
.
execute
<<-
eosql
CREATE TABLE columns_with_default (
id integer PRIMARY KEY AUTOINCREMENT,
number integer not null
)
eosql
column
=
@conn
.
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
@conn
assert_difference
(
'@conn.logged.length'
)
do
@conn
.
indexes
(
'items'
)
end
assert_match
(
/items/
,
@conn
.
logged
.
last
.
first
)
end
def
test_no_indexes
assert_equal
[],
@conn
.
indexes
(
'items'
)
end
def
test_index
@conn
.
add_index
'items'
,
'id'
,
:unique
=>
true
,
:name
=>
'fun'
index
=
@conn
.
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
@conn
.
add_index
'items'
,
'id'
,
:name
=>
'fun'
index
=
@conn
.
indexes
(
'items'
).
find
{
|
idx
|
idx
.
name
==
'fun'
}
assert
!
index
.
unique
,
'index is not unique'
end
def
test_compound_index
@conn
.
add_index
'items'
,
%w{ id number }
,
:name
=>
'fun'
index
=
@conn
.
indexes
(
'items'
).
find
{
|
idx
|
idx
.
name
==
'fun'
}
assert_equal
%w{ id number }
.
sort
,
index
.
columns
.
sort
end
def
test_primary_key
assert_equal
'id'
,
@conn
.
primary_key
(
'items'
)
@conn
.
execute
<<-
eosql
CREATE TABLE foos (
internet integer PRIMARY KEY AUTOINCREMENT,
number integer not null
)
eosql
assert_equal
'internet'
,
@conn
.
primary_key
(
'foos'
)
end
def
test_no_primary_key
@conn
.
execute
'CREATE TABLE failboat (number integer not null)'
assert_nil
@conn
.
primary_key
(
'failboat'
)
end
private
def
assert_logged
logs
intercept_logs_on
@conn
yield
assert_equal
logs
,
@conn
.
logged
end
def
intercept_logs_on
ctx
@conn
.
extend
(
Module
.
new
{
attr_accessor
:logged
def
log
sql
,
name
,
binds
=
[]
@logged
<<
[
sql
,
name
,
binds
]
yield
end
})
@conn
.
logged
=
[]
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录