diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-09-11 13:14:37 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-09-11 16:47:52 +0200 |
commit | 395573b344b0bb0579a325d8f958f692f8f66cc8 (patch) | |
tree | aaaf3d49339737474042afa290a12eef95dcd503 /activerecord | |
parent | e40bf04a2f074557467d1a623bf4e34aacf2c701 (diff) | |
download | rails-395573b344b0bb0579a325d8f958f692f8f66cc8.tar.gz rails-395573b344b0bb0579a325d8f958f692f8f66cc8.tar.bz2 rails-395573b344b0bb0579a325d8f958f692f8f66cc8.zip |
reuse view test-cases for pg materialized view tests.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/view_test.rb | 67 | ||||
-rw-r--r-- | activerecord/test/cases/view_test.rb | 36 |
2 files changed, 36 insertions, 67 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/view_test.rb b/activerecord/test/cases/adapters/postgresql/view_test.rb index 47b7d38eda..f6f96bb3ba 100644 --- a/activerecord/test/cases/adapters/postgresql/view_test.rb +++ b/activerecord/test/cases/adapters/postgresql/view_test.rb @@ -1,67 +1,18 @@ 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 diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index 357265aa05..3aed90ba36 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -1,24 +1,28 @@ 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 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase 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 |