Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fcc2231a
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,发现更多精彩内容 >>
提交
fcc2231a
编写于
7月 15, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Stop using mocha on dbconsole_test
上级
a83efa4a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
68 addition
and
44 deletion
+68
-44
railties/test/commands/dbconsole_test.rb
railties/test/commands/dbconsole_test.rb
+68
-44
未找到文件。
railties/test/commands/dbconsole_test.rb
浏览文件 @
fcc2231a
require
'abstract_unit'
require
'abstract_unit'
require
'minitest/mock'
require
'rails/commands/dbconsole'
require
'rails/commands/dbconsole'
class
Rails::DBConsoleTest
<
ActiveSupport
::
TestCase
class
Rails::DBConsoleTest
<
ActiveSupport
::
TestCase
...
@@ -26,20 +27,21 @@ def test_config_with_db_config_only
...
@@ -26,20 +27,21 @@ def test_config_with_db_config_only
"timeout"
=>
"3000"
"timeout"
=>
"3000"
}
}
}
}
app_db_config
(
config_sample
)
app_db_config
(
config_sample
)
do
assert_equal
Rails
::
DBConsole
.
new
.
config
,
config_sample
[
"test"
]
assert_equal
Rails
::
DBConsole
.
new
.
config
,
config_sample
[
"test"
]
end
end
end
def
test_config_with_no_db_config
def
test_config_with_no_db_config
app_db_config
(
nil
)
app_db_config
(
nil
)
do
assert_raise
(
ActiveRecord
::
AdapterNotSpecified
)
{
assert_raise
(
ActiveRecord
::
AdapterNotSpecified
)
{
Rails
::
DBConsole
.
new
.
config
Rails
::
DBConsole
.
new
.
config
}
}
end
end
end
def
test_config_with_database_url_only
def
test_config_with_database_url_only
ENV
[
'DATABASE_URL'
]
=
'postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000'
ENV
[
'DATABASE_URL'
]
=
'postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000'
app_db_config
(
nil
)
expected
=
{
expected
=
{
"adapter"
=>
"postgresql"
,
"adapter"
=>
"postgresql"
,
"host"
=>
"localhost"
,
"host"
=>
"localhost"
,
...
@@ -50,7 +52,10 @@ def test_config_with_database_url_only
...
@@ -50,7 +52,10 @@ def test_config_with_database_url_only
"pool"
=>
"5"
,
"pool"
=>
"5"
,
"timeout"
=>
"3000"
"timeout"
=>
"3000"
}.
sort
}.
sort
assert_equal
expected
,
Rails
::
DBConsole
.
new
.
config
.
sort
app_db_config
(
nil
)
do
assert_equal
expected
,
Rails
::
DBConsole
.
new
.
config
.
sort
end
end
end
def
test_config_choose_database_url_if_exists
def
test_config_choose_database_url_if_exists
...
@@ -68,8 +73,9 @@ def test_config_choose_database_url_if_exists
...
@@ -68,8 +73,9 @@ def test_config_choose_database_url_if_exists
"timeout"
=>
"3000"
"timeout"
=>
"3000"
}
}
}
}
app_db_config
(
sample_config
)
app_db_config
(
sample_config
)
do
assert_equal
host
,
Rails
::
DBConsole
.
new
.
config
[
"host"
]
assert_equal
host
,
Rails
::
DBConsole
.
new
.
config
[
"host"
]
end
end
end
def
test_env
def
test_env
...
@@ -78,58 +84,65 @@ def test_env
...
@@ -78,58 +84,65 @@ def test_env
ENV
[
'RAILS_ENV'
]
=
nil
ENV
[
'RAILS_ENV'
]
=
nil
ENV
[
'RACK_ENV'
]
=
nil
ENV
[
'RACK_ENV'
]
=
nil
Rails
.
stub
s
(
:respond_to?
).
with
(
:env
).
returns
(
false
)
Rails
.
stub
(
:respond_to?
,
false
)
do
assert_equal
Rails
::
DBConsole
.
new
.
environment
,
"development"
assert_equal
Rails
::
DBConsole
.
new
.
environment
,
"development"
ENV
[
'RACK_ENV'
]
=
"rack_env"
ENV
[
'RACK_ENV'
]
=
"rack_env"
assert_equal
Rails
::
DBConsole
.
new
.
environment
,
"rack_env"
assert_equal
Rails
::
DBConsole
.
new
.
environment
,
"rack_env"
ENV
[
'RAILS_ENV'
]
=
"rails_env"
ENV
[
'RAILS_ENV'
]
=
"rails_env"
assert_equal
Rails
::
DBConsole
.
new
.
environment
,
"rails_env"
assert_equal
Rails
::
DBConsole
.
new
.
environment
,
"rails_env"
end
ensure
ensure
ENV
[
'RAILS_ENV'
]
=
"test"
ENV
[
'RAILS_ENV'
]
=
"test"
end
end
def
test_rails_env_is_development_when_argument_is_dev
def
test_rails_env_is_development_when_argument_is_dev
Rails
::
DBConsole
.
stubs
(
:available_environments
).
returns
([
'development'
,
'test'
])
dbconsole
=
Rails
::
DBConsole
.
new
options
=
Rails
::
DBConsole
.
new
.
send
(
:parse_arguments
,
[
'dev'
])
assert_match
(
'development'
,
options
[
:environment
])
dbconsole
.
stub
(
:available_environments
,
[
'development'
,
'test'
])
do
options
=
dbconsole
.
send
(
:parse_arguments
,
[
'dev'
])
assert_match
(
'development'
,
options
[
:environment
])
end
end
end
def
test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
def
test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
Rails
::
DBConsole
.
stubs
(
:available_environments
).
returns
([
'dev'
])
dbconsole
=
Rails
::
DBConsole
.
new
options
=
Rails
::
DBConsole
.
new
.
send
(
:parse_arguments
,
[
'dev'
])
assert_match
(
'dev'
,
options
[
:environment
])
dbconsole
.
stub
(
:available_environments
,
[
'dev'
])
do
options
=
dbconsole
.
send
(
:parse_arguments
,
[
'dev'
])
assert_match
(
'dev'
,
options
[
:environment
])
end
end
end
def
test_mysql
def
test_mysql
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
%w[mysql mysql5]
,
'db'
)
start
(
adapter:
'mysql'
,
database:
'db'
)
start
(
adapter:
'mysql'
,
database:
'db'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
%w[mysql mysql5]
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_mysql_full
def
test_mysql_full
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
%w[mysql mysql5]
,
'--host=locahost'
,
'--port=1234'
,
'--socket=socket'
,
'--user=user'
,
'--default-character-set=UTF-8'
,
'-p'
,
'db'
)
start
(
adapter:
'mysql'
,
database:
'db'
,
host:
'locahost'
,
port:
1234
,
socket:
'socket'
,
username:
'user'
,
password:
'qwerty'
,
encoding:
'UTF-8'
)
start
(
adapter:
'mysql'
,
database:
'db'
,
host:
'locahost'
,
port:
1234
,
socket:
'socket'
,
username:
'user'
,
password:
'qwerty'
,
encoding:
'UTF-8'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
%w[mysql mysql5]
,
'--host=locahost'
,
'--port=1234'
,
'--socket=socket'
,
'--user=user'
,
'--default-character-set=UTF-8'
,
'-p'
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_mysql_include_password
def
test_mysql_include_password
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
%w[mysql mysql5]
,
'--user=user'
,
'--password=qwerty'
,
'db'
)
start
({
adapter:
'mysql'
,
database:
'db'
,
username:
'user'
,
password:
'qwerty'
},
[
'-p'
])
start
({
adapter:
'mysql'
,
database:
'db'
,
username:
'user'
,
password:
'qwerty'
},
[
'-p'
])
assert
!
aborted
assert
!
aborted
assert_equal
[
%w[mysql mysql5]
,
'--user=user'
,
'--password=qwerty'
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_postgresql
def
test_postgresql
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'psql'
,
'db'
)
start
(
adapter:
'postgresql'
,
database:
'db'
)
start
(
adapter:
'postgresql'
,
database:
'db'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
'psql'
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_postgresql_full
def
test_postgresql_full
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'psql'
,
'db'
)
start
(
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
,
host:
'host'
,
port:
5432
)
start
(
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
,
host:
'host'
,
port:
5432
)
assert
!
aborted
assert
!
aborted
assert_equal
[
'psql'
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
assert_equal
'user'
,
ENV
[
'PGUSER'
]
assert_equal
'user'
,
ENV
[
'PGUSER'
]
assert_equal
'host'
,
ENV
[
'PGHOST'
]
assert_equal
'host'
,
ENV
[
'PGHOST'
]
assert_equal
'5432'
,
ENV
[
'PGPORT'
]
assert_equal
'5432'
,
ENV
[
'PGPORT'
]
...
@@ -137,60 +150,60 @@ def test_postgresql_full
...
@@ -137,60 +150,60 @@ def test_postgresql_full
end
end
def
test_postgresql_include_password
def
test_postgresql_include_password
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'psql'
,
'db'
)
start
({
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
},
[
'-p'
])
start
({
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
},
[
'-p'
])
assert
!
aborted
assert
!
aborted
assert_equal
[
'psql'
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
assert_equal
'user'
,
ENV
[
'PGUSER'
]
assert_equal
'user'
,
ENV
[
'PGUSER'
]
assert_equal
'q1w2e3'
,
ENV
[
'PGPASSWORD'
]
assert_equal
'q1w2e3'
,
ENV
[
'PGPASSWORD'
]
end
end
def
test_sqlite
def
test_sqlite
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite'
,
'db'
)
start
(
adapter:
'sqlite'
,
database:
'db'
)
start
(
adapter:
'sqlite'
,
database:
'db'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
'sqlite'
,
'db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_sqlite3
def
test_sqlite3
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
Rails
.
root
.
join
(
'db.sqlite3'
).
to_s
)
start
(
adapter:
'sqlite3'
,
database:
'db.sqlite3'
)
start
(
adapter:
'sqlite3'
,
database:
'db.sqlite3'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
'sqlite3'
,
Rails
.
root
.
join
(
'db.sqlite3'
).
to_s
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_sqlite3_mode
def
test_sqlite3_mode
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
'-html'
,
Rails
.
root
.
join
(
'db.sqlite3'
).
to_s
)
start
({
adapter:
'sqlite3'
,
database:
'db.sqlite3'
},
[
'--mode'
,
'html'
])
start
({
adapter:
'sqlite3'
,
database:
'db.sqlite3'
},
[
'--mode'
,
'html'
])
assert
!
aborted
assert
!
aborted
assert_equal
[
'sqlite3'
,
'-html'
,
Rails
.
root
.
join
(
'db.sqlite3'
).
to_s
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_sqlite3_header
def
test_sqlite3_header
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
'-header'
,
Rails
.
root
.
join
(
'db.sqlite3'
).
to_s
)
start
({
adapter:
'sqlite3'
,
database:
'db.sqlite3'
},
[
'--header'
])
start
({
adapter:
'sqlite3'
,
database:
'db.sqlite3'
},
[
'--header'
])
assert_equal
[
'sqlite3'
,
'-header'
,
Rails
.
root
.
join
(
'db.sqlite3'
).
to_s
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_sqlite3_db_absolute_path
def
test_sqlite3_db_absolute_path
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
'/tmp/db.sqlite3'
)
start
(
adapter:
'sqlite3'
,
database:
'/tmp/db.sqlite3'
)
start
(
adapter:
'sqlite3'
,
database:
'/tmp/db.sqlite3'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
'sqlite3'
,
'/tmp/db.sqlite3'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_sqlite3_db_without_defined_rails_root
def
test_sqlite3_db_without_defined_rails_root
Rails
.
stub
s
(
:respond_to?
)
Rails
.
stub
(
:respond_to?
,
false
)
do
Rails
.
expects
(
:respond_to?
).
with
(
:root
).
once
.
returns
(
false
)
start
(
adapter:
'sqlite3'
,
database:
'config/db.sqlite3'
)
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
Rails
.
root
.
join
(
'../config/db.sqlite3'
).
to_s
)
assert
!
aborted
start
(
adapter:
'sqlite3'
,
database:
'config/db.sqlite3'
)
assert_equal
[
'sqlite3'
,
Rails
.
root
.
join
(
'../config/db.sqlite3'
).
to_s
],
dbconsole
.
find_cmd_and_exec_args
assert
!
aborte
d
en
d
end
end
def
test_oracle
def
test_oracle
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlplus'
,
'user@db'
)
start
(
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
)
start
(
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
)
assert
!
aborted
assert
!
aborted
assert_equal
[
'sqlplus'
,
'user@db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_oracle_include_password
def
test_oracle_include_password
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlplus'
,
'user/secret@db'
)
start
({
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
},
[
'-p'
])
start
({
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
},
[
'-p'
])
assert
!
aborted
assert
!
aborted
assert_equal
[
'sqlplus'
,
'user/secret@db'
],
dbconsole
.
find_cmd_and_exec_args
end
end
def
test_unknown_command_line_client
def
test_unknown_command_line_client
...
@@ -223,16 +236,27 @@ def test_print_help_long
...
@@ -223,16 +236,27 @@ def test_print_help_long
private
private
def
app_db_config
(
results
)
def
app_db_config
(
results
)
Rails
.
application
.
config
.
stubs
(
:database_configuration
).
returns
(
results
||
{})
Rails
.
application
.
config
.
stub
(
:database_configuration
,
results
||
{})
do
yield
end
end
end
def
dbconsole
def
dbconsole
@dbconsole
||=
Rails
::
DBConsole
.
new
(
nil
)
@dbconsole
||=
Class
.
new
(
Rails
::
DBConsole
)
do
attr_reader
:find_cmd_and_exec_args
def
find_cmd_and_exec
(
*
args
)
@find_cmd_and_exec_args
=
args
end
end
.
new
(
nil
)
end
end
def
start
(
config
=
{},
argv
=
[])
def
start
(
config
=
{},
argv
=
[])
dbconsole
.
stubs
(
config:
config
.
stringify_keys
,
arguments:
argv
)
dbconsole
.
stub
(
:config
,
config
.
stringify_keys
)
do
capture_abort
{
dbconsole
.
start
}
dbconsole
.
stub
(
:arguments
,
argv
)
do
capture_abort
{
dbconsole
.
start
}
end
end
end
end
def
capture_abort
def
capture_abort
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录