如何让dbunit与MySQL枚举数据类型一起玩?

时间:2022-06-25 15:33:15

I'm trying to use dbunit to test some our database access code and I'm running into a problem. We are using MySQL 5 something or other as the database itself. I exported a small set of data to a FlatXmlDataSet and when I setup the test case, it throws an exception which says "Data truncated for column 'FHEIGHT_FLAG' at row 1". The column FHEIGHT_FLAG is defined as

我正在尝试使用dbunit测试一些我们的数据库访问代码,我遇到了问题。我们使用MySQL 5或其他作为数据库本身。我将一小组数据导出到FlatXmlDataSet中,当我设置测试用例时,它会抛出一个异常,说明“第1行的列'FHEIGHT_FLAG'的数据被截断”。列FHEIGHT_FLAG定义为

enum('t','f') default NULL

The way the enum data is inserted to the database, if the real value for the source of the data is not true, then the column actually contains "" (empty string). The code using the database is just making the assumption that if the value provided from the database is not 't', then it must be false. How can I make this go? I can't fix the source data, so I have to try to do something with the test case.

枚举数据插入数据库的方式,如果数据源的实际值不为真,则该列实际包含“”(空字符串)。使用数据库的代码只是假设如果数据库提供的值不是't',那么它必须是false。我怎么能这样做?我无法修复源数据,因此我必须尝试使用​​测试用例。

2 个解决方案

#1


Export a new dataset, using "select case when FHEIGHT_FLAG = 't' then 't' else 'f' end as FHEIGHT_FLAG, ....(all other columns) .... from tablename";

导出一个新的数据集,使用“选择情况,当FHEIGHT_FLAG ='t'然后't',否则'f'结束为FHEIGHT_FLAG,....(所有其他列)....来自tablename”;

(But order the columns as they are in the real table, of course.)

(当然,在实际表格中对列进行排序。)

final String select = "select case when FHEIGHT_FLAG = 't' then 't' else 'f' end as FHEIGHT_FLAG, ....(all other columns) .... from tablename";
// database connection
Class driverClass = Class.forName("package.of.jdbcDriver");
Connection jdbcConnection = DriverManager.getConnection(
        "jdbc:url:of:db", "user", "pass");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

// partial database export
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("Tablename", select);

FlatXmlDataSet.write(partialDataSet, new FileOutputStream("mydataset.xml"));

#2


I ended up modifying the test database FHEIGHT_FLAG column to be

我最终修改了测试数据库FHEIGHT_FLAG列

enum('','t','f') default NULL

that way all of the empty string values were allowed to be inserted back.

这样就允许插回所有空字符串值。

#1


Export a new dataset, using "select case when FHEIGHT_FLAG = 't' then 't' else 'f' end as FHEIGHT_FLAG, ....(all other columns) .... from tablename";

导出一个新的数据集,使用“选择情况,当FHEIGHT_FLAG ='t'然后't',否则'f'结束为FHEIGHT_FLAG,....(所有其他列)....来自tablename”;

(But order the columns as they are in the real table, of course.)

(当然,在实际表格中对列进行排序。)

final String select = "select case when FHEIGHT_FLAG = 't' then 't' else 'f' end as FHEIGHT_FLAG, ....(all other columns) .... from tablename";
// database connection
Class driverClass = Class.forName("package.of.jdbcDriver");
Connection jdbcConnection = DriverManager.getConnection(
        "jdbc:url:of:db", "user", "pass");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

// partial database export
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("Tablename", select);

FlatXmlDataSet.write(partialDataSet, new FileOutputStream("mydataset.xml"));

#2


I ended up modifying the test database FHEIGHT_FLAG column to be

我最终修改了测试数据库FHEIGHT_FLAG列

enum('','t','f') default NULL

that way all of the empty string values were allowed to be inserted back.

这样就允许插回所有空字符串值。