提交 395573b3 编写于 作者: Y Yves Senn

reuse view test-cases for pg materialized view tests.

上级 e40bf04a
require "cases/helper"
require "cases/view_test"
module ViewTestConcern
extend ActiveSupport::Concern
included do
self.use_transactional_fixtures = false
mattr_accessor :view_type
end
SCHEMA_NAME = 'test_schema'
TABLE_NAME = 'things'
COLUMNS = [
'id integer',
'name character varying(50)',
'email character varying(50)',
'moment timestamp without time zone'
]
class ThingView < ActiveRecord::Base
end
def setup
super
ThingView.table_name = "#{SCHEMA_NAME}.#{view_type}_things"
if ActiveRecord::Base.connection.supports_materialized_views?
class MaterializedViewTest < ActiveRecord::TestCase
include ViewBehavior
@connection = ActiveRecord::Base.connection
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@connection.execute "CREATE #{view_type.humanize} #{ThingView.table_name} AS SELECT * FROM #{SCHEMA_NAME}.#{TABLE_NAME}"
private
def create_view(name, query)
@connection.execute "CREATE MATERIALIZED VIEW #{name} AS #{query}"
end
def teardown
super
@connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
end
def drop_view(name)
@connection.execute "DROP MATERIALIZED VIEW #{name}" if @connection.table_exists? name
def test_table_exists
name = ThingView.table_name
assert @connection.table_exists?(name), "'#{name}' table should exist"
end
def test_column_definitions
assert_nothing_raised do
assert_equal COLUMNS, columns(ThingView.table_name)
end
end
private
def columns(table_name)
@connection.send(:column_definitions, table_name).map do |name, type, default|
"#{name} #{type}" + (default ? " default #{default}" : '')
end
end
end
class ViewTest < ActiveRecord::TestCase
include ViewTestConcern
self.view_type = 'view'
end
if ActiveRecord::Base.connection.supports_materialized_views?
class MaterializedViewTest < ActiveRecord::TestCase
include ViewTestConcern
self.view_type = 'materialized_view'
end
end
require "cases/helper"
require "models/book"
if ActiveRecord::Base.connection.supports_views?
class ViewWithPrimaryKeyTest < ActiveRecord::TestCase
fixtures :books
module ViewBehavior
extend ActiveSupport::Concern
included do
fixtures :books
end
class Ebook < ActiveRecord::Base
self.primary_key = "id"
end
setup do
def setup
super
@connection = ActiveRecord::Base.connection
@connection.execute <<-SQL
CREATE VIEW ebooks
AS SELECT id, name, status FROM books WHERE format = 'ebook'
create_view "ebooks", <<-SQL
SELECT id, name, status FROM books WHERE format = 'ebook'
SQL
end
teardown do
@connection.execute "DROP VIEW ebooks" if @connection.table_exists? "ebooks"
def teardown
super
drop_view "ebooks"
end
def test_reading
......@@ -51,6 +55,20 @@ def test_does_not_assume_id_column_as_primary_key
end
end
if ActiveRecord::Base.connection.supports_views?
class ViewWithPrimaryKeyTest < ActiveRecord::TestCase
include ViewBehavior
private
def create_view(name, query)
@connection.execute "CREATE VIEW #{name} AS #{query}"
end
def drop_view(name)
@connection.execute "DROP VIEW #{name}" if @connection.table_exists? name
end
end
class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase
fixtures :books
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册