如何有效地测试SQL / JPQL / HQL?

时间:2023-01-22 21:30:16

It is often said when unit testing to dont test the database as that is an integration test (see point 4).

当单元测试不测试数据库时,通常会说这是一个集成测试(参见第4点)。

However, SQL/JPQL/HQL encapsulate data store specific logic which is often in string format on how to access data. This free form string data access command can easily go wrong and hence needs to be tested.

但是,SQL / JPQL / HQL封装了数据存储特定的逻辑,这些逻辑通常采用字符串格式来表示如何访问数据。这种*格式字符串数据访问命令很容易出错,因此需要进行测试。

How do i efficiently test this sort of logic?

我如何有效地测试这种逻辑?

2 个解决方案

#1


2  

The closest you can get to running a unit test against an SQL (or similar framework) query, is to set up a SQLite database in memory, and run against it.

对SQL(或类似框架)查询运行单元测试最接近的是在内存中设置SQLite数据库,并针对它运行。

While that still is technically an integration test, it runs almost as fast as a unit test should.

虽然这在技术上仍然是一个集成测试,但它的运行速度几乎与单元测试一样快。

If you do so, just take care to note the slight differences between SQLite and your real database, and try to make your queries compatible with both.

如果您这样做,请注意注意SQLite和您的真实数据库之间的细微差别,并尝试使您的查询与两者兼容。

Hope this helps,
Assaf.

希望这有帮助,阿萨夫。

#2


1  

It is not a unit test, but there is nothing with using a unit testing framework like Nunit to test your sql. But it IS important that you keep it separated from the real unit tests. Real unit tests are fast and does not communicate with the outside ... nor do they attempt to alter it by updates, deletes and inserts.

它不是单元测试,但使用像Nunit这样的单元测试框架来测试你的sql没有任何意义。但重要的是你要将它与真实的单元测试分开。真正的单元测试很快,不与外部通信......也不会尝试通过更新,删除和插入来改变它。

#1


2  

The closest you can get to running a unit test against an SQL (or similar framework) query, is to set up a SQLite database in memory, and run against it.

对SQL(或类似框架)查询运行单元测试最接近的是在内存中设置SQLite数据库,并针对它运行。

While that still is technically an integration test, it runs almost as fast as a unit test should.

虽然这在技术上仍然是一个集成测试,但它的运行速度几乎与单元测试一样快。

If you do so, just take care to note the slight differences between SQLite and your real database, and try to make your queries compatible with both.

如果您这样做,请注意注意SQLite和您的真实数据库之间的细微差别,并尝试使您的查询与两者兼容。

Hope this helps,
Assaf.

希望这有帮助,阿萨夫。

#2


1  

It is not a unit test, but there is nothing with using a unit testing framework like Nunit to test your sql. But it IS important that you keep it separated from the real unit tests. Real unit tests are fast and does not communicate with the outside ... nor do they attempt to alter it by updates, deletes and inserts.

它不是单元测试,但使用像Nunit这样的单元测试框架来测试你的sql没有任何意义。但重要的是你要将它与真实的单元测试分开。真正的单元测试很快,不与外部通信......也不会尝试通过更新,删除和插入来改变它。