Rails 测试尝试删除 sql 视图
Posted
技术标签:
【中文标题】Rails 测试尝试删除 sql 视图【英文标题】:Rails tests trying to delete sql views 【发布时间】:2015-07-03 02:23:57 【问题描述】:我正在尝试为我的应用程序编写单元和集成测试,但是当我运行 rake 测试时,我看到了很多错误,所有这些错误都是由系统试图删除只读的 sql 视图引起的。当测试只是为了验证数据创建时,为什么要尝试删除视图,以及如何让我的测试通过这个?该应用程序运行良好,但我无法让测试正常运行。 (在添加 sql 视图之前,所有测试都运行良好)。
这是我看到的错误:
Finished in 0.779585s, 39.7647 runs/s, 0.0000 assertions/s.
1) Error:
UsersControllerTest#test_should_get_new:
ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR: cannot delete from view "playabilitysummary"
DETAIL: Views that do not select from a single table or view are not automatically updatable.
HINT: To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
: DELETE FROM "playabilitysummary"
Error:
UsersControllerTest#test_should_get_new:
NoMethodError: undefined method `each' for nil:NilClass
2) Error:
StaticPagesControllerTest#test_should_get_contactus:
ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR: cannot delete from view "playabilitysummary"
DETAIL: Views that do not select from a single table or view are not automatically updatable.
HINT: To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
: DELETE FROM "playabilitysummary"
可玩性总结模型:
class PlayabilitySummary < ActiveRecord::Base
self.table_name = 'playabilitysummary'
after_initialize :readonly!
end
可玩性摘要迁移:
class CreatePlayabilitySummaries < ActiveRecord::Migration
def up
execute <<-SQL
CREATE VIEW PlayabilitySummary AS
SELECT date,user_id,speed,firmness,moisture FROM (((SELECT distinct(schedules.date) as date, speed.user_id as user_id, speed.speed as speed FROM schedules
join
(SELECT user_id, measure_date as speed_date, avg(speed) as speed FROM greens group by user_id, measure_date order by user_id, measure_date asc) as speed
on schedules.date = speed.speed_date) as avg1
JOIN
(SELECT user_id as fuser_id, measure_date as firm_date, avg(firmness) as firmness FROM greens group by user_id, measure_date order by user_id, measure_date asc) as firmness
ON avg1.user_id = firmness.fuser_id AND avg1.date = firmness.firm_date) as avg2
JOIN
(SELECT user_id as muser_id, measure_date as moist_date, avg(moisture) as moisture FROM greens group by user_id, measure_date order by user_id, measure_date asc) as moisture
ON avg2.fuser_id = moisture.muser_id AND avg2.date = moisture.moist_date)
SQL
end
def down
execute 'DROP VIEW PlayabilitySummary'
end
end
示例测试(user_controller):
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end
【问题讨论】:
【参考方案1】:我在最近一次 rails/rspec 更新后遇到了这个问题,并认为以下内容可能有用。 如果您在迁移中执行任意 SQL 命令(例如创建视图或函数),这些可能会在运行测试时丢失,因为测试从 schema.rb 加载。
将format of the schema type 更改为 :sql 允许将整个结构转储到一个文件中,这意味着测试运行程序现在将选择这些:
# config/application.rb
config.active_record.schema_type = :sql
运行rake db:schema:dump
以生成必要的db/structure.sql
将停止在测试运行之间删除您的视图和函数。
【讨论】:
【参考方案2】:通过创建 rake db:views 任务来创建 sql 视图并从迁移中删除这些视图,此问题已得到解决。这使测试数据库准备不会尝试删除定义为只读的表。
此外,db 用户被授予超级用户访问权限。这两个步骤都成功消除了删除错误。
【讨论】:
谢谢,这里有一篇文章解释了如何做到这一点rietta.com/blog/2015/03/30/…,但我仍然遇到同样的错误以上是关于Rails 测试尝试删除 sql 视图的主要内容,如果未能解决你的问题,请参考以下文章