From ec6eee5db0994f92ac2c3c6d2a922c6edd35b2b1 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 12:00:01 +0200 Subject: [PATCH] add test-cases for primary-key-less-views. Closes #16555. --- activerecord/test/cases/view_test.rb | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index b79dd03676..2f23d12de5 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -37,5 +37,48 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase ["name", :string], ["status", :integer]], Ebook.columns.map { |c| [c.name, c.type] }) end + + def test_attributes + assert_equal({"id" => 2, "name" => "Ruby for Rails", "status" => 0}, + Ebook.first.attributes) + end +end + +class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase + fixtures :books + + class Paperback < ActiveRecord::Base; end + + setup do + @connection = ActiveRecord::Base.connection + @connection.execute <<-SQL + CREATE VIEW paperbacks + AS SELECT name, status FROM books WHERE format = 'paperback' + SQL + end + + teardown do + @connection.execute "DROP VIEW IF EXISTS paperbacks" + end + + def test_reading + books = Paperback.all + assert_equal ["Agile Web Development with Rails"], books.map(&:name) + end + + def test_table_exists + view_name = Paperback.table_name + assert @connection.table_exists?(view_name), "'#{view_name}' table should exist" + end + + def test_column_definitions + assert_equal([["name", :string], + ["status", :integer]], Paperback.columns.map { |c| [c.name, c.type] }) + end + + def test_attributes + assert_equal({"name" => "Agile Web Development with Rails", "status" => 0}, + Paperback.first.attributes) + end end end