如何测试rails中各种测试用例的postgres触发功能?
我有 Rails 应用程序,我使用 postgresql 作为数据库。当应用程序数据发生更改时,我使用触发器函数在数据库中进行一些更改。但我不知道如何用测试用例测试触发功能......
I have the rails application and i use postgresql for database. I use trigger functions to make some changes in the database while there is changes happens in the application data. But i dont have a good idea on how to test the trigger functions with test cases...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有类似的情况。我最终编写了涉及直接数据库插入/更新/删除语句以及直接选择语句来验证更改的单元测试。
作为提示,请考虑编写一些测试来一次操作多行,并验证返回的受影响行数是否有效。根据我的经验,后者有能力在事情复杂时揭示缺点(崩溃、竞争条件等)。
I'm in a similar boat on my end. I ended up writing unit tests that involved direct database insert/update/delete statements, and direct select statements to validate the changes.
As a tip, consider writing a few tests that manipulate multiple rows as once, and which verify that the number of affected rows returned is valid. In my experience the latter have a knack for revealing shortcomings (crashes, race conditions, etc.) when things are complex.
为什么不简单地执行与生产应用程序中相同的操作呢?
例如,如果您的触发器在插入和更新时增加列值,只需为这些事件编写一个测试,并通过检索并检查该值是否正确增加来测试该值是否正确增加。因此,如果在插入操作之前列值为“3”,之后为“4”,则您知道触发器已正确触发。
Why not simply do the same operation you do in the production application?
E.g. if your trigger incements a column value on an insert and update, just write a test for these events and test if the value was incremented correctly by retrieving it and checking it. So if a column value is "3" before the insert operation and "4" afterwards, you know that the trigger was fired correctly.