gpt4 book ai didi

postgresql - DBUnit 和 Postgres UUID 主键

转载 作者:行者123 更新时间:2023-11-29 11:33:31 26 4
gpt4 key购买 nike

我正在尝试使用 DBUnit 运行集成测试,但我发现自己无法插入主键列,这显然不适用于稍后在文件中引用主键的外键。

例如,我有以下DDL:

CREATE TABLE attributes(
attribute_id UUID NOT NULL DEFAULT uuid_generate_v4(),
attribute VARCHAR(64) NOT NULL,
description TEXT NOT NULL,
PRIMARY KEY(attribute_id)
);

DBUnit 设置 XML 如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<attributes attribute_id="233bc966-4fcd-4b46-88e6-3e07090f322d" attribute="Empathy" description="Empathy Description" />
</dataset>

当我尝试运行测试时,失败了:

    org.dbunit.dataset.NoSuchColumnException: attributes.ATTRIBUTE_ID -  (Non-uppercase input column: attribute_id) in ColumnNameToIndexes cache map. Note that the
ap's column names are NOT case sensitive.

这是正在运行的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@DbUnitConfiguration(dataSetLoader = TestConfiguration.FlatXmlDataLoaderProxy.class)
@ContextConfiguration(classes = {ApplicationConfiguration.class, TestConfiguration.class})
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
public class ApplicationAssessmentJobTest {
@Autowired
private ApplicationAssessmentJob applicationAssessmentJob;

@Test
@DatabaseSetup("/dbunit/ApplicationAssessmentJobTestSetup.xml")
@DatabaseTearDown("dbunit/ApplicationAssessmentJobTestTearDown.xml")
public void testJob() {
ApplicationAssessmentJobModel model = new ApplicationAssessmentJobModel();
model.setApplicationId(UUID.fromString("41fa1d51-c1ee-482b-80a7-a6eefda64436"));

applicationAssessmentJob.receiveMessage(model);
}
}

显示的这个错误似乎与根本问题没有直接关系。如果我从 XML 中删除 attribute_id 列,则会插入记录。

最佳答案

我遇到了同样的问题,我终于找到了解决办法。如文档中所述,您需要覆盖默认的 dbunit DatabaseConfig 以设置特定于 PostgreSQL 的 IDataTypeFactory。

这是我的测试配置:

@Autowired
private DataSource dataSource;

@Bean
public DatabaseConfigBean databaseConfigBean() {
DatabaseConfigBean databaseConfigBean = new DatabaseConfigBean();
databaseConfigBean.setDatatypeFactory(new PostgresqlDataTypeFactory());
return databaseConfigBean;
}

@Bean(name = "dbUnitDatabaseConnection")
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection() throws SQLException, DatabaseUnitException, IOException {
DatabaseDataSourceConnectionFactoryBean databaseDataSourceConnectionFactoryBean = new DatabaseDataSourceConnectionFactoryBean();
databaseDataSourceConnectionFactoryBean.setDatabaseConfig(databaseConfigBean());
databaseDataSourceConnectionFactoryBean.setDataSource(dataSource);
databaseDataSourceConnectionFactoryBean.setSchema("public");
return databaseDataSourceConnectionFactoryBean;
}

然后,您需要指定您将在测试中使用自定义 IDatabaseConnection。这些连接将由 bean“dbUnitDatabaseConnection”创建。例如,这是我声明我的 spring 测试配置的方式:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringRepositoryConfigurationTest.class})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DbUnitConfiguration(databaseConnection = {"dbUnitDatabaseConnection"})

关于postgresql - DBUnit 和 Postgres UUID 主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28866878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com