Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
671e997e
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,发现更多精彩内容 >>
提交
671e997e
编写于
2月 26, 2014
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
extract common code in `uuid_test.rb`.
上级
50be1d0d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
57 addition
and
60 deletion
+57
-60
activerecord/test/cases/adapters/postgresql/uuid_test.rb
activerecord/test/cases/adapters/postgresql/uuid_test.rb
+57
-60
未找到文件。
activerecord/test/cases/adapters/postgresql/uuid_test.rb
浏览文件 @
671e997e
...
...
@@ -4,22 +4,40 @@
require
'active_record/base'
require
'active_record/connection_adapters/postgresql_adapter'
module
PostgresqlUUIDHelper
def
connection
@connection
||=
ActiveRecord
::
Base
.
connection
end
def
enable_uuid_ossp
unless
connection
.
extension_enabled?
(
'uuid-ossp'
)
connection
.
enable_extension
'uuid-ossp'
connection
.
commit_db_transaction
end
connection
.
reconnect!
end
def
drop_table
(
name
)
connection
.
execute
"drop table if exists
#{
name
}
"
end
end
class
PostgresqlUUIDTest
<
ActiveRecord
::
TestCase
include
PostgresqlUUIDHelper
class
UUIDType
<
ActiveRecord
::
Base
self
.
table_name
=
"uuid_data_type"
end
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
transaction
do
@connection
.
create_table
"uuid_data_type"
do
|
t
|
t
.
uuid
'guid'
end
setup
do
connection
.
create_table
"uuid_data_type"
do
|
t
|
t
.
uuid
'guid'
end
end
def
teardown
@connection
.
execute
'drop table if exists uuid_data_type'
teardown
do
drop_table
"uuid_data_type"
end
def
test_data_type_of_uuid_types
...
...
@@ -40,30 +58,23 @@ def test_uuid_formats
end
class
PostgresqlUUIDGenerationTest
<
ActiveRecord
::
TestCase
include
PostgresqlUUIDHelper
class
UUID
<
ActiveRecord
::
Base
self
.
table_name
=
'pg_uuids'
end
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
setup
do
enable_uuid_ossp
unless
@connection
.
extension_enabled?
(
'uuid-ossp'
)
@connection
.
enable_extension
'uuid-ossp'
@connection
.
commit_db_transaction
end
@connection
.
reconnect!
@connection
.
transaction
do
@connection
.
create_table
(
'pg_uuids'
,
id: :uuid
,
default:
'uuid_generate_v1()'
)
do
|
t
|
t
.
string
'name'
t
.
uuid
'other_uuid'
,
default:
'uuid_generate_v4()'
end
connection
.
create_table
(
'pg_uuids'
,
id: :uuid
,
default:
'uuid_generate_v1()'
)
do
|
t
|
t
.
string
'name'
t
.
uuid
'other_uuid'
,
default:
'uuid_generate_v4()'
end
end
def
teardown
@connection
.
execute
'drop table if exists pg_uuids'
teardown
do
drop_table
"pg_uuids"
end
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
...
...
@@ -84,14 +95,14 @@ def test_auto_create_uuid
end
def
test_pk_and_sequence_for_uuid_primary_key
pk
,
seq
=
@
connection
.
pk_and_sequence_for
(
'pg_uuids'
)
pk
,
seq
=
connection
.
pk_and_sequence_for
(
'pg_uuids'
)
assert_equal
'id'
,
pk
assert_equal
nil
,
seq
end
def
test_schema_dumper_for_uuid_primary_key
schema
=
StringIO
.
new
ActiveRecord
::
SchemaDumper
.
dump
(
@
connection
,
schema
)
ActiveRecord
::
SchemaDumper
.
dump
(
connection
,
schema
)
assert_match
(
/\bcreate_table "pg_uuids", id: :uuid, default: "uuid_generate_v1\(\)"/
,
schema
.
string
)
assert_match
(
/t\.uuid "other_uuid", default: "uuid_generate_v4\(\)"/
,
schema
.
string
)
end
...
...
@@ -99,34 +110,24 @@ def test_schema_dumper_for_uuid_primary_key
end
class
PostgresqlUUIDTestNilDefault
<
ActiveRecord
::
TestCase
class
UUID
<
ActiveRecord
::
Base
self
.
table_name
=
'pg_uuids'
end
include
PostgresqlUUIDHelper
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
reconnect!
setup
do
enable_uuid_ossp
unless
@connection
.
extension_enabled?
(
'uuid-ossp'
)
@connection
.
enable_extension
'uuid-ossp'
@connection
.
commit_db_transaction
end
@connection
.
transaction
do
@connection
.
create_table
(
'pg_uuids'
,
id:
false
)
do
|
t
|
t
.
primary_key
:id
,
:uuid
,
default:
nil
t
.
string
'name'
end
connection
.
create_table
(
'pg_uuids'
,
id:
false
)
do
|
t
|
t
.
primary_key
:id
,
:uuid
,
default:
nil
t
.
string
'name'
end
end
def
teardown
@connection
.
execute
'drop table if exists pg_uuids'
teardown
do
drop_table
"pg_uuids"
end
if
ActiveRecord
::
Base
.
connection
.
supports_extensions?
def
test_id_allows_default_override_via_nil
col_desc
=
@
connection
.
execute
(
"SELECT pg_get_expr(d.adbin, d.adrelid) as default
col_desc
=
connection
.
execute
(
"SELECT pg_get_expr(d.adbin, d.adrelid) as default
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attname='id' AND a.attrelid = 'pg_uuids'::regclass"
).
first
...
...
@@ -136,6 +137,8 @@ def test_id_allows_default_override_via_nil
end
class
PostgresqlUUIDTestInverseOf
<
ActiveRecord
::
TestCase
include
PostgresqlUUIDHelper
class
UuidPost
<
ActiveRecord
::
Base
self
.
table_name
=
'pg_uuid_posts'
has_many
:uuid_comments
,
inverse_of: :uuid_post
...
...
@@ -146,30 +149,24 @@ class UuidComment < ActiveRecord::Base
belongs_to
:uuid_post
end
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
reconnect!
unless
@connection
.
extension_enabled?
(
'uuid-ossp'
)
@connection
.
enable_extension
'uuid-ossp'
@connection
.
commit_db_transaction
end
setup
do
enable_uuid_ossp
@
connection
.
transaction
do
@
connection
.
create_table
(
'pg_uuid_posts'
,
id: :uuid
)
do
|
t
|
connection
.
transaction
do
connection
.
create_table
(
'pg_uuid_posts'
,
id: :uuid
)
do
|
t
|
t
.
string
'title'
end
@
connection
.
create_table
(
'pg_uuid_comments'
,
id: :uuid
)
do
|
t
|
connection
.
create_table
(
'pg_uuid_comments'
,
id: :uuid
)
do
|
t
|
t
.
uuid
:uuid_post_id
,
default:
'uuid_generate_v4()'
t
.
string
'content'
end
end
end
def
teardown
@
connection
.
transaction
do
@connection
.
execute
'drop table if exists pg_uuid_comments'
@connection
.
execute
'drop table if exists pg_uuid_posts'
teardown
do
connection
.
transaction
do
drop_table
"pg_uuid_comments"
drop_table
"pg_uuid_posts"
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录