在Database Rider中是否有减少@DataSet配置属性重复的方法?

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

Is there any way in Database Rider to reduce duplication of @DataSet configuration properties?

问题

在Database Rider中是否有任何方法可以减少*@DataSet*配置属性的重复?

例如,假设我们有以下代码:

@DataSet(value = "yml/test1.yml", cleanAfter = true, skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
public void test1() {
  //...
}

@DataSet(value = "yml/test2.yml", cleanAfter = true, skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
public void test2() {
  //...
}

正如您所看到的,cleanAfterskipCleaningFor是重复的。这只是一个示例。skipCleaningFor列表甚至可能更长。拥有成千上万个具有相同跳过表列表的测试是一场噩梦。想象一下,如果我们需要在成千上万个地方更改此列表。

将此列表提取到某个static final变量中不是一个选项,因为注释的参数只能是内联常量(否则会出现编译错误)。

元数据集是减少重复的一种方式,但它们包括所有属性,并且无法覆盖某些属性(例如我示例中的value属性)。

合并数据集看起来很有希望,但不太确定...

英文:

Is there any way in Database Rider to reduce duplication of @DataSet configuration properties?

For example, suppose we have:

@DataSet(value = "yml/test1.yml", cleanAfter = true, skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
public void test1() {
  //...
}

@DataSet(value = "yml/test2.yml", cleanAfter = true, skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
public void test2() {
  //...
}

Here as you can see cleanAfter and skipCleaningFor are duplicated. And this is just an example. The list of skipCleaningFor can be even longer. Having thousands of tests with the same list of skipped tables is a nightmare. Imagine we need to change this list, in thousands of places.

Extracting this list to some static final variable is not an option, because parameters to annotation can be only true inline constants (you will get compilation error).

Meta datasets are one of the ways of reducing duplication but they include ALL properties, and it's not possible to override some of the properties (like value in my example)

Merge datasets looks promising, but not sure...

答案1

得分: 2

看起来我找到了如何使用合并数据集来完成这个任务。
首先,我们需要为所有测试创建一些BaseTest,其中包含skipCleaningFor的配置:

@DataSet(value = "yml/empty.yml", skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
class BaseTest

其中empty.yml只是一个空数据集,以避免在测试执行期间出现错误。然后,在测试类中,我们添加以下内容:

@DbUnit(mergeDataSets = true)
@DBRider
class MyTest extends BaseTest {
   @DataSet(value = "yml/test1.yml", cleanAfter = true)
   public void test1() {
     //...
   }

   @DataSet(value = "yml/test2.yml", cleanAfter = true)
   public void test2() {
     //...
   }
}

就是这样 - 配置不再重复!

英文:

Seems I figured out how to do it using merging datasets.
First, we need some BaseTest for all our tests with the config of skipCleaningFor :

@DataSet(value = "yml/empty.yml", skipCleaningFor = {"zone_code", "product_code", "product_type", "user_type"})
class BaseTest

Where empty.yml it's just some empty dataset to avoid error during tests execution.
Then, in the test class, we put

@DbUnit(mergeDataSets = true)
@DBRider
class MyTest extends BaseTest {
   @DataSet(value = "yml/test1.yml", cleanAfter = true)
   public void test1() {
     //...
   }

   @DataSet(value = "yml/test2.yml", cleanAfter = true)
   public void test2() {
     //...
   }

And that's it - the config is no longer duplicated!

huangapple
  • 本文由 发表于 2020年9月25日 01:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64051481.html
匿名

发表评论

匿名网友

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

确定