【问题标题】:DbUnit: Setting tolerance value - compare SQL Server vs SAP HANADbUnit:设置容差值 - 比较 SQL Server 与 SAP HANA
【发布时间】:2017-08-31 12:37:26
【问题描述】:

最重要的

DB Unit 返回第 78 行中双精度值的差异:

Exception in thread "main" junit.framework.ComparisonFailure: value (table=dataset, row=78, col=DirtyValue) expected:<4901232.27291950[7]> but was:<4901232.27291950[6]>

所以我假设 SQL Server 返回 4901232.272919507 而 HANA 返回 4901232.272919506
(根据JUnit assertEquals Changes String的回答)

然后我尝试根据常见问题解答Is there an equivalent to JUnit's assertEquals(double expected, double actual, double delta) to define a tolerance level when comparing numeric values? 设置容忍增量

但我仍然遇到同样的错误 - 有什么想法吗?

其他信息

也许这就是原因:?

[main] WARN org.dbunit.dataset.AbstractTableMetaData - Potential problem found: The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems with the current database 'Microsoft SQL Server' (e.g. some datatypes may not be supported properly). In rare cases you might see this message because the list of supported database products is incomplete (list=[derby]). If so please request a java-class update via the forums.If you are using your own IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override getValidDbProducts() to specify the supported database products.
[main] WARN org.dbunit.dataset.AbstractTableMetaData - Potential problem found: The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems with the current database 'HDB' (e.g. some datatypes may not be supported properly). In rare cases you might see this message because the list of supported database products is incomplete (list=[derby]). If so please request a java-class update via the forums.If you are using your own IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override getValidDbProducts() to specify the supported database products.
  • DbUnit 版本 2.5.4
  • DirtyValue 是根据两个系统中的 3 个双值计算得出的

SQL 服务器

SELECT TypeOfGroup, Segment, Portfolio, UniqueID, JobId, DirtyValue, PosUnits, FX_RATE, THEO_Value
FROM DATASET_PL
order by JobId, TypeOfGroup, Segment, Portfolio, UniqueID COLLATE Latin1_General_bin

汉娜

SELECT "TypeOfGroup", "Segment", "Portfolio", "UniqueID", "JobId", "DirtyValue", Pos_Units as "PosUnits", FX_RATE, THEO_Value as "THEO_Value"
FROM "_SYS_BIC"."meag.app.h4q.metadata.dataset.pnl/06_COMPARE_CUBES_AND_CALC_ATTR"
order by "JobId", "TypeOfGroup", "Segment", "Portfolio", "UniqueID"

【问题讨论】:

  • 您能否提供测试定义以及您使用的 DB Unit 版本?此外,所涉及的数据定义可能有助于了解差异的来源。最终,您可能希望在两个平台上获得相同的结果,而不管 double 的实现。

标签: sql-server double hana dbunit


【解决方案1】:

解决方法

使用 diffhandler 并处理那里的差异:

DiffCollectingFailureHandler diffHandler = new DiffCollectingFailureHandler();
Assertion.assertEquals(expectedTable, actualTable);

List<Difference> diffList = diffHandler.getDiffList();
for (Difference diff: diffList) {
    if (diff.getColumnName().equals("DirtyValue")) {
        double actual = (double) diff.getActualValue();
        double expected = (double) diff.getExpectedValue();
        if (Math.abs(Math.abs(actual) - Math.abs(expected)) > 0.00001) {
            logDiff(diff);
        } else {
            logDebugDiff(diff);
        }
    } else {
        logDiff(diff);
    }
}

private void logDiff(Difference diff) {
    logger.error(String.format("Diff found in row:%s, col:%s expected:%s, actual:%s", diff.getRowIndex(), diff.getColumnName(), diff.getExpectedValue(), diff.getActualValue()));
}

private void logDebugDiff(Difference diff) {
    logger.debug(String.format("Diff found in row:%s, col:%s expected:%s, actual:%s", diff.getRowIndex(), diff.getColumnName(), diff.getExpectedValue(), diff.getActualValue()));
}

【讨论】:

    【解决方案2】:

    问题是“有什么想法吗?”,所以也许有助于理解为什么会出现差异。

    如果需要,HANA 会截断,请参阅 "HANA SQL and System Views Reference", numeric types。在 HANA 中,以下语句导致 123.45:

    select cast( '123.456' as decimal(6,2)) from dummy;
    

    如果需要,SQL-Server 会进行四舍五入,至少在目标数据类型是数字的情况下,请参见例如here 在“截断和舍入结果”。 与上面相同的 SQL 语句在 SQL-Server 中的结果为 123.46。

    SQL-Standard 似乎将其保持打开状态,无论是舍入还是截断,请参阅 answer on SO 。 我不知道有任何设置会改变 HANA 中的舍入行为,但也许有。

    【讨论】:

    • 这是有关原因的有用信息 - 到目前为止 THX。然而,问题是如何让 DbUnit 忽略小于 .00001 的差异
    • 好吧,绝对 epsilon 为 0.00001 用于比较使用较低精度时会遇到相同问题的增量,但这可能与您的用例无关。
    猜你喜欢
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多