建议的方式是在每个QuarkusTest中重置内存中的H2数据库。

huangapple go评论50阅读模式
英文:

Recommended way to reset in memory H2 database for each QuarkusTest

问题

我正在实施一些使用RestAssured和内存中的H2数据库的BDD @QuarkusTest,我正在寻找在每个测试之后重置内存数据库(删除重建或删除所有实体)的最简单和推荐的方法。

我想避免任何需要手动运行的方式,比如创建自定义的DELETE脚本来在每个测试之后运行,或者表截断。是否有一种干净且与Quarkus兼容的方式来做到这一点?

编辑:请不要推荐与Flyway或其他第三方依赖相关的任何内容。

编辑2:我曾尝试通过手动删除所有实体来进行快速的初步尝试,但我正在寻找一种更简单和自动化的方法。

英文:

I'm implementing some BDD @QuarkusTests using RestAssured and an in-memory H2 database. I'm looking for the simplest and recommended way to reset the in memory database (either drop-recreate or delete all entities everywhere) after each test.

I'd like to avoid any hands on ways like create custom DELETE scripts that run manually after each test of table truncates. Is there a clean and quarkus-native way to do this?

EDIT: Please do not recommend anything related to Flyway or other 3rd party dependency.

EDIT 2: I did a quick initial hack by dropping all entities manually but I'm looking for something simpler and automatic

  @AfterEach
  void tearDown() {
    try (final EntityManager em = emf.createEntityManager()) {
      em.getTransaction().begin();
      em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
      em.getTransaction().commit();
      for (EntityType<?> entity : em.getMetamodel().getEntities()) {
        em.getTransaction().begin();
        final Class<?> managedType = entity.getBindableJavaType();
        em.createQuery("FROM " + managedType.getSimpleName(), managedType).getResultList().forEach(em::remove);
        em.getTransaction().commit();
      }
      em.getTransaction().begin();
      em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
      em.getTransaction().commit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

答案1

得分: 0

直到提供一个不同而更好的答案,我们将使用以下方法:

@AfterEach
void tearDown() {
    try (final EntityManager em = emf.createEntityManager()) {
        em.getTransaction().begin();
        em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
        em.getTransaction().commit();
        for (EntityType<?> entity : em.getMetamodel().getEntities()) {
            em.getTransaction().begin();
            final Class<?> managedType = entity.getBindableJavaType();
            em.createQuery("FROM " + managedType.getSimpleName(), managedType).getResultList().forEach(em::remove);
            em.getTransaction().commit();
        }
        em.getTransaction().begin();
        em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
        em.getTransaction().commit();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
英文:

Until a different and better answer is provided we'll go with the hack:

  @AfterEach
  void tearDown() {
    try (final EntityManager em = emf.createEntityManager()) {
      em.getTransaction().begin();
      em.createNativeQuery(&quot;SET REFERENTIAL_INTEGRITY FALSE&quot;).executeUpdate();
      em.getTransaction().commit();
      for (EntityType&lt;?&gt; entity : em.getMetamodel().getEntities()) {
        em.getTransaction().begin();
        final Class&lt;?&gt; managedType = entity.getBindableJavaType();
        em.createQuery(&quot;FROM &quot; + managedType.getSimpleName(), managedType).getResultList().forEach(em::remove);
        em.getTransaction().commit();
      }
      em.getTransaction().begin();
      em.createNativeQuery(&quot;SET REFERENTIAL_INTEGRITY FALSE&quot;).executeUpdate();
      em.getTransaction().commit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

huangapple
  • 本文由 发表于 2023年5月25日 00:18:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定