处理单元测试中模拟本体对象的小数字段

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

Handling decimal-fields of mocked ontology-objects in unit-tests

问题

根据您的要求,以下是翻译好的内容:

我正在尝试为Foundry中的TypeScript函数存储库编写单元测试。根据Palantir-Foundry文档,我想使用Objects.create()来模拟我的测试对象以及whenObjectSet(objectSetOperation).thenReturn(objects)来进行注入。

然而,在我的测试中设置模拟对象的十进制字段会导致异常:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^
[...]
SafeError: Setting property type is not supported.
args: [
    {
      type: 'UNSAFE',
      name: 'type',
      value: '{"version":2,"propertyTypeId":"confirmed_quantity_menge","type":{"type":"decimal"}}'
    }
  ],
  type: 'UserCausedError',
  errorInstanceId: 'd4e336a8-791b-48e3-8ea8-feb5405ace53',
  result: {
    runtimeError: {
      message: 'Setting property type is not supported.',
      stacktrace: 'SafeError: Setting property type is not supported.\n' +
        '    at UserCausedError.SafeError [as constructor] (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/witchcraft-logging-api/src/args/safeError.ts:26:9)\n' +
        '    at new UserCausedError (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-isolate-api/src/UserCausedError.ts:19:5)\n' +
        '    at Object.settingPropertyTypeNotSupported (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/Errors.ts:81:12)\n' +
        '    at ObjectPropertyStore.throwIfSettingPropertyTypeUnsupported (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/properties/ObjectPropertyStore.ts:63:22)\n' +
        '    at ObjectPropertyStore.set (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/properties/ObjectPropertyStore.ts:26:14)\n' +
        '    at AnnotatingObjectStorageProvider.setPropertyValue (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/DefaultObjectStorageProvider.ts:39:28)\n
[...]
      parameters: {
        type: '{"version":2,"propertyTypeId":"confirmed_quantity_menge","type":{"type":"decimal"}}'
      }
    },
    type: 'runtimeError'
  }
}

在Foundry文档中,我找到了关于decimal类型的以下信息:

decimal: 无法在操作类型中使用此类型的属性,因为在更新此数据类型时无法保证精度,这是由于JSON和Java之间的转换。此类型也不受Object Storage V2支持。

但是,我觉得这个限制在单元测试时实际上不应该适用(毕竟我们只是在这个上下文中谈论模拟数据)。有没有人知道如何解决这个问题,而无需更改属性类型(从而更改本体)?

英文:

I am trying to write unit tests for a typescript function repo in foundry. According to the palantir-foundry documentation, I'd like to use Objects.create() to mock objects for my tests and whenObjectSet(objectSetOperation).thenReturn(objects) for injection.

However, setting decimal fields of mocked objects in my tests results in the exception:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^
[...]
SafeError: Setting property type is not supported.
args: [
    {
      type: 'UNSAFE',
      name: 'type',
      value: '{"version":2,"propertyTypeId":"confirmed_quantity_menge","type":{"type":"decimal"}}'
    }
  ],
  type: 'UserCausedError',
  errorInstanceId: 'd4e336a8-791b-48e3-8ea8-feb5405ace53',
  result: {
    runtimeError: {
      message: 'Setting property type is not supported.',
      stacktrace: 'SafeError: Setting property type is not supported.\n' +
        '    at UserCausedError.SafeError [as constructor] (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/witchcraft-logging-api/src/args/safeError.ts:26:9)\n' +
        '    at new UserCausedError (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-isolate-api/src/UserCausedError.ts:19:5)\n' +
        '    at Object.settingPropertyTypeNotSupported (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/Errors.ts:81:12)\n' +
        '    at ObjectPropertyStore.throwIfSettingPropertyTypeUnsupported (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/properties/ObjectPropertyStore.ts:63:22)\n' +
        '    at ObjectPropertyStore.set (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/properties/ObjectPropertyStore.ts:26:14)\n' +
        '    at AnnotatingObjectStorageProvider.setPropertyValue (/scratch/standalone/53c47a40-195f-4d78-9e75-53ee67c1e813/code-assist/contents/functions-typescript/node_modules/@foundry/functions-typescript-runtime-storage/src/DefaultObjectStorageProvider.ts:39:28)\n
[...]
      parameters: {
        type: '{"version":2,"propertyTypeId":"confirmed_quantity_menge","type":{"type":"decimal"}}'
      }
    },
    type: 'runtimeError'
  }
}

In the foundry documentation I found the following information regarding the decimal type:

> decimal: Properties of this type cannot be used within action
> types as the precision cannot be guaranteed when updating this data
> type due to the conversion between JSON and Java. This type is also
> not supported in Object Storage V2.

However, I feel like this restriction really should not apply when it comes unit-testing (after all we are only talking about mocked data in this context). Does someone know a workaround for that problem without having to change the property type (and thus the ontology)?

答案1

得分: 0

函数和对象上的操作的单元测试在与生产环境中的函数运行相同的运行时框架内运行。目前,没有可用于单元测试的替代运行时环境,该环境具有替代的检查集,允许您绕过此限制。

英文:

The unit testing for functions and actions on objects runs within the same runtime framework in which a published function would run in production. At the moment, there aren't any alternative runtimes available for unit tests that would have an alternative set of checks to allow you to bypass this.

huangapple
  • 本文由 发表于 2023年4月11日 16:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983918.html
匿名

发表评论

匿名网友

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

确定