用于在存储库测试中创建相同表的 Sql 脚本 [英] Sql scripts for creation of same table in repository tests

查看:25
本文介绍了用于在存储库测试中创建相同表的 Sql 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 Spring 数据存储库,它们与同一个表有一些连接,比如 Client 表.因此,为了不重复为每个存储库测试创建 Client 表的 sql 脚本(这将是一个不好的做法),将这个表的创建外部化在单个 sql 文件中并导入它是否是一个好习惯?在测试中他们需要它吗?

I have several Spring data repositories which have some joins to the same table, lets say, Client table. So in order to not repeat the sql script for the creation of the Client table for each repository test (it would be a bad practice), would it be a good practice to externalize the creation of this table in a single sql file and import it in the tests they need it?

谢谢!

推荐答案

通常的做法不是为每个测试创建自定义架构,而是使用您将使用的相同脚本/机制为所有测试创建完整架构生产中.

The normal practice is not to create a custom schema for each test, but create the full schema for all tests using the same script/mechanism that you'll use in production.

数据库架构不是模块化的,它们到处都有依赖关系.这些依赖项以外键的形式出现,但也以触发器或物化视图和约束的形式出现.

Database schema aren't modular, they have dependencies all over the place. These dependencies come in the form of foreign keys, but also as triggers or materialised views and constraints.

如果您的架构小而简单,那么将其分解成碎片是没有意义的.如果它很大,很容易遗漏一些东西,并且测试不能正确代表生产环境.

If your schema is small and simple there is no point in going through the hassle of breaking it into pieces. If it is large it becomes easy to miss something and have tests that don't properly represent the production environment.

这篇关于用于在存储库测试中创建相同表的 Sql 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆