简体   繁体   English

您如何(单元)测试数据库架构?

[英]How do you (Unit) Test the database schema?

When there are a number of people working on a project, all of who could alter the database schema, what's the simplest way to unit test / test / verify it? 当有很多人在从事项目工作时,所有人都可以更改数据库架构,那么对单元进行测试/测试/验证的最简单方法是什么? The main suggestion we've had so far is to write tests for each table to verify column names, constraints, etc. 到目前为止,我们的主要建议是为每个表编写测试以验证列名,约束等。

Has anyone else done anything similar / simpler? 还有其他人做过类似/简单的事情吗? We're using C# with SQL Server, if that makes any real difference. 如果这真的有什么不同,我们将C#与SQL Server一起使用。

Updates: 更新:

  • The segment of the project we're working on is using SSIS packages to do the bulk of the work so there is very little C# code to write unit tests agains. 我们正在处理的项目部分是使用SSIS包来完成大部分工作,因此很少有C#代码可以再次编写单元测试。
  • The code for creating tables / stored procedures is spread across SQL files. 用于创建表/存储过程的代码分布在SQL文件中。 Because of the build system, we could maintain a separate VS DB project file as well, but I'm not sure how that would help us verify the schema either. 由于构建系统的原因,我们也可以维护一个单独的VS DB项目文件,但是我不确定这将如何帮助我们验证架构。

One possibly answer is to use Visual Studio for Database developers and keep your schema in source control with the rest of your code. 一种可能的答案是为数据库开发人员使用Visual Studio,并在其余代码中将架构保持在源代码控制中。 This allows you to see differences and you get a history of who changed what. 这使您可以看到差异,并获得有关谁更改内容的历史记录。

Alternatively you could use a tool like SQLCompare to see what has been modified in one database compared to another. 另外,您可以使用SQLCompare之类的工具来查看一个数据库与另一个数据库相比所修改的内容。

Your (relational) database does two things as far as I'm concerned: 1) Hold data and 2) Hold relations between data. 就我而言,您的(关系)数据库做了两件事:1)保存数据; 2)保存数据之间的关系。

Holding data is not a behavior so you would not test it 保留数据不是一种行为,因此您不会对其进行测试

And for ensuring relations just use constraints. 为了确保关系,只需使用约束即可。 Lots of constraints. 很多约束。 All over the place. 到处都是。

I've had to do this type of thing before, although not in C#. 我以前不得不做这种事情,尽管不是在C#中。 To begin with, I built a schema migration tool, based on the discussion at Ode to Code (page 1 of 5) (there are also existing tools to do similar things). 首先,基于Ode to Code(第1页,共5页)的讨论,我构建了一个模式迁移工具(也存在执行类似操作的现有工具)。 Importantly, the migration tool I built allowed you to specify the database you were applying the changes to and what version you wanted to apply. 重要的是,我构建的迁移工具允许您指定要应用更改的数据库以及要应用的版本。 Then, following a test first methodology, whenever I needed to make a schema change I would write a test script which would create a test database, apply version changes to the one before my target change script, add some data, apply the change script under test, and confirm that the data was in an expected state. 然后,按照测试优先的方法,每当我需要进行模式更改时,我都会编写一个测试脚本来创建一个测试数据库,将版本更改应用于目标更改脚本之前的版本,添加一些数据,然后在测试,并确认数据处于预期状态。

My main goal with this was to confirm that no data was lost or corrupted during schema migrations, not to check specifically that the schema was in a particular state. 我的主要目的是确认在模式迁移期间没有数据丢失或损坏,而不是专门检查模式是否处于特定状态。 A good awareness of your production data set is required, so you can write representative sample data for the tests. 需要对生产数据集有很好的了解,因此您可以为测试编写代表性的样本数据。

It's debatable if this should be considered unit testing or integration testing. 是否应将其视为单元测试或集成测试还是有争议的。 I would tend to consider it integration testing, based on the fact that I don't want to run old tests every time I iterate my code. 基于我不想在每次迭代代码时都运行旧的测试这一事实,我倾向于考虑进行集成测试。 Whatever you want to call it, I found it to be a useful tool for that situation. 无论您想称它为什么,我都发现它是解决这种情况的有用工具。

This is an old question but it appears that people are still landing here. 这是一个古老的问题,但看来人们仍然在这里着陆。 So the best tool I have found so far is "SQL Test" by Red Gate. 因此,到目前为止,我发现的最好的工具是Red Gate的“ SQL Test”。 It allows you to create scripts that run as transactions. 它允许您创建作为事务运行的脚本。 Allowing you to run "sandboxed" queries for checking the state of the database. 允许您运行“沙盒”查询以检查数据库状态。

That is an interesting question! 这是一个有趣的问题! There are lots of tools out there for testing stored procedures but not for testing the database schema. 有很多工具可以测试存储过程,但不能测试数据库模式。

Don't you find that the unit tests written for code generally find any problems with the database schema? 您是否发现为代码编写的单元测试通常不会发现数据库模式有任何问题?

One approach I have used is to write stored procedures to copy test data from the developer's schema to a test schema. 我使用的一种方法是编写存储过程,以将测试数据从开发人员的模式复制到测试模式。 This is pretty rough and ready as the stored procedures generally crash when they come across any differences between the schemas but it does alert you to any changes you haven't been told about. 这很粗糙而且很容易准备,因为存储过程在遇到架构之间的任何差异时通常会崩溃,但是它确实会提醒您任何尚未得知的更改。

And nominate someone to be the DBA who monitors changes to the schema? 并任命某人为负责监视架构更改的DBA吗?

This does not really fit the unit test paradigm. 这确实不适合单元测试范式。 I would suggest version controlling the schema and limiting write access to a single qualified team member such as the DBA or team lead, who can validate any requested changes against the entire application. 我建议对版本进行控制架构,并将写访问权限限制在单个合格的团队成员(例如DBA或团队负责人)上,他们可以针对整个应用程序验证任何请求的更改。 Schema changes should not be done haphazardly. 模式更改不应随意进行。

Don't you find that the unit tests written for code generally find any problems with the database schema? 您是否发现为代码编写的单元测试通常不会发现数据库模式有任何问题?

This assumes, of course, that your tests test everything. 当然,这假设您的测试可以测试所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM