aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/view_test.rb
diff options
context:
space:
mode:
authoryui-knk <spiketeika@gmail.com>2015-09-04 11:41:31 +0900
committeryui-knk <spiketeika@gmail.com>2015-09-08 20:09:41 +0900
commitbb0d70788a2cddb6598228806150b55d87e2e81d (patch)
tree5825a60fbddc0f9fdbf91c3066f148bcf8a9e85a /activerecord/test/cases/view_test.rb
parentdb69045f7df63afc4fda2eab7525a67408456044 (diff)
downloadrails-bb0d70788a2cddb6598228806150b55d87e2e81d.tar.gz
rails-bb0d70788a2cddb6598228806150b55d87e2e81d.tar.bz2
rails-bb0d70788a2cddb6598228806150b55d87e2e81d.zip
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.
Diffstat (limited to 'activerecord/test/cases/view_test.rb')
-rw-r--r--activerecord/test/cases/view_test.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb
index f9dca1e196..1eb1430065 100644
--- a/activerecord/test/cases/view_test.rb
+++ b/activerecord/test/cases/view_test.rb
@@ -110,4 +110,70 @@ class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase
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