提交 bb0d7078 编写于 作者: Y yui-knk

Add view tests for MySQL

Basically view tests for MySQL are same with
`test/cases/adapters/postgresql/view_test.rb`.

So move `test/cases/adapters/postgresql/view_test.rb` to
`test/cases/view_test.rb` and make them only run if
`current_adapter` supports writable view.
上级 db69045f
require "cases/helper"
require "cases/view_test"
class UpdateableViewTest < ActiveRecord::PostgreSQLTestCase
fixtures :books
class PrintedBook < ActiveRecord::Base
self.primary_key = "id"
end
setup do
@connection = ActiveRecord::Base.connection
@connection.execute <<-SQL
CREATE VIEW printed_books
AS SELECT id, name, status, format FROM books WHERE format = 'paperback'
SQL
end
teardown do
@connection.execute "DROP VIEW printed_books" if @connection.table_exists? "printed_books"
end
def test_update_record
book = PrintedBook.first
book.name = "AWDwR"
book.save!
book.reload
assert_equal "AWDwR", book.name
end
def test_insert_record
PrintedBook.create! name: "Rails in Action", status: 0, format: "paperback"
new_book = PrintedBook.last
assert_equal "Rails in Action", new_book.name
end
def test_update_record_to_fail_view_conditions
book = PrintedBook.first
book.format = "ebook"
book.save!
assert_raises ActiveRecord::RecordNotFound do
book.reload
end
end
end
if ActiveRecord::Base.connection.respond_to?(:supports_materialized_views?) &&
ActiveRecord::Base.connection.supports_materialized_views?
class MaterializedViewTest < ActiveRecord::PostgreSQLTestCase
include ViewBehavior
private
def create_view(name, query)
@connection.execute "CREATE MATERIALIZED VIEW #{name} AS #{query}"
end
def drop_view(name)
@connection.execute "DROP MATERIALIZED VIEW #{name}" if @connection.table_exists? name
end
end
end
......@@ -110,4 +110,70 @@ def test_does_not_have_a_primary_key
assert_nil Paperback.primary_key
end
end
# sqlite dose not support CREATE, INSERT, and DELETE for VIEW
if current_adapter?(:MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter)
class UpdateableViewTest < ActiveRecord::TestCase
self.use_transactional_tests = false
fixtures :books
class PrintedBook < ActiveRecord::Base
self.primary_key = "id"
end
setup do
@connection = ActiveRecord::Base.connection
@connection.execute <<-SQL
CREATE VIEW printed_books
AS SELECT id, name, status, format FROM books WHERE format = 'paperback'
SQL
end
teardown do
@connection.execute "DROP VIEW printed_books" if @connection.table_exists? "printed_books"
end
def test_update_record
book = PrintedBook.first
book.name = "AWDwR"
book.save!
book.reload
assert_equal "AWDwR", book.name
end
def test_insert_record
PrintedBook.create! name: "Rails in Action", status: 0, format: "paperback"
new_book = PrintedBook.last
assert_equal "Rails in Action", new_book.name
end
def test_update_record_to_fail_view_conditions
book = PrintedBook.first
book.format = "ebook"
book.save!
assert_raises ActiveRecord::RecordNotFound do
book.reload
end
end
end
end # end fo `if current_adapter?(:MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter)`
end # end fo `if ActiveRecord::Base.connection.supports_views?`
if ActiveRecord::Base.connection.respond_to?(:supports_materialized_views?) &&
ActiveRecord::Base.connection.supports_materialized_views?
class MaterializedViewTest < ActiveRecord::PostgreSQLTestCase
include ViewBehavior
private
def create_view(name, query)
@connection.execute "CREATE MATERIALIZED VIEW #{name} AS #{query}"
end
def drop_view(name)
@connection.execute "DROP MATERIALIZED VIEW #{name}" if @connection.table_exists? name
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册