如何在 H2 数据库中伪造 ENUM 列以进行单元测试? [英] How to fake ENUM columns in the H2 database for play unit testing?

查看:28
本文介绍了如何在 H2 数据库中伪造 ENUM 列以进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套 Play!我一直在针对 H2 数据库运行的单元测试.我在模型中添加了一些枚举列,但由于用于创建模型表的 sql 语句,测试现在失败了.

错误信息是:

14:42:10,435 错误 ~ 未知数据类型:ENUM";SQL语句:

一些搜索表明在 H2 中有一些方法可以模拟枚举(例如:http://groups.google.com/group/h2-database/search?group=h2-database&q=enum&qt_g=Search+this+group)

将 Play 设置为在测试模式下使用 H2Dialect 并不能解决问题.似乎根本原因是 H2 不支持枚举,或者 H2Dialect 可能不知道 H2 的伪枚举.

Play 中有没有办法在 H2 中测试带有枚举的模型?

解决方案

这里有一个适用于带有 H2 的 spring-boot 的解决方法 -它不依赖于任何一个,所以你可以做类似的游戏.

请注意,这是一个假的,并不是真的允许您完全测试枚举,但它允许您运行测试针对预先存在的生产数据库(您不能只是去改变模式),而不必自己编写整个 DDL.

因此,不要让您的测试框架为内存数据库设置连接字符串,而是自己配置 H2 连接字符串.

这里是我的情况下魔法设置的样子:

# 下一行很重要,它为 ddl 工作命名# H2 不支持枚举# 为了虚假支持他们,我们必须声明一个# 名为 enum 的域并将其映射到 varchar - 大小# 我是随机挑选的,但现在足够好"了.# H2 将在休眠创建模式之前运行它,并且# 那么schema创建就会成功spring.datasource.url=jdbc:h2:mem:test;MODE=MySQL;INIT=CREATE DOMAIN IF NOT EXISTS enum as VARCHAR(255);DB_CLOSE_ON_EXIT=FALSE

神奇之处在于:

CREATE DOMAIN IF NOT EXISTS enum as VARCHAR(255)

这告诉 H2 将自定义(域)数据类型枚举视为 varchar - 您显然可以将大小更改为任何大小.

它就像一个 INIT 原因,确保它在 ddl 的第一位被任何框架对其执行之前执行

因此,在 Play! 的情况下,设置将是 db.default.jdbcUrl - 或者您定义测试数据库连接(例如作为特征)

I have a set of Play! unit tests that I have been running against an H2 database. I have added some enum columns to my model, and the tests now fail because of the sql statements used to create my model tables.

The error message is:

14:42:10,435 ERROR ~ Unknown data type: "ENUM"; SQL statement:

Some searching shows that there are ways to emulate enums in H2 (eg: http://groups.google.com/group/h2-database/search?group=h2-database&q=enum&qt_g=Search+this+group)

Setting Play to use the H2Dialect for test mode does not fix the issue. It seems like the root cause is that H2 does not support enums, or maybe that the H2Dialect doesn't know about H2's pseudo-enums.

Is there a way in Play to test models with enums in H2?

解决方案

Here a workaround that works for spring-boot with H2 - it doesn't depend on either though, so you can do something similar for play.

Please note that this is a fake and doesn't really allow you to test enums fully but it allows you to run tests against a pre-existing production DBs (where you cannot just go and change the schema) without having to write the entire DDL yourself.

So, instead of letting your test framework setup the connecting string for the in-memory db, do configure the H2 connection string yourself.

Here how the magic setting looks in my case:

# the next line is very important it names the ddl work
# H2 does not support enums
# In order to fake support for them we have to declare a
# domain called enum and mapped it to a varchar - the size
# I picked at random but it is "good enough" for now.
# H2 will run this before hibernate creates the schema and
# then the schema creation will succeed
spring.datasource.url=jdbc:h2:mem:test;MODE=MySQL;INIT=CREATE DOMAIN IF NOT EXISTS enum as VARCHAR(255);DB_CLOSE_ON_EXIT=FALSE

The magic is this:

CREATE DOMAIN IF NOT EXISTS enum as VARCHAR(255)

this tells H2 to treat the custom (domain) datatype enum as varchar - you can obviously change the size to whatever.

It as done as an INIT cause that makes sure it is executed before the first bit of ddl is executed against it by any framework

So, in case of Play! the setting would be db.default.jdbcUrl - or however you define your test database connection (e.g. as a trait)

这篇关于如何在 H2 数据库中伪造 ENUM 列以进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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