From 8913c698df078e9bdb5d2d6942a8c09449b15e75 Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Wed, 8 Feb 2023 10:48:48 -0500 Subject: [PATCH 1/3] deserialize fixture instead --- features/step_definitions/request_steps.ts | 23 +++++++++-------- features/support/templating.ts | 30 +++++++++++++++++----- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/features/step_definitions/request_steps.ts b/features/step_definitions/request_steps.ts index 3f4755de2309..cd5571254c13 100644 --- a/features/step_definitions/request_steps.ts +++ b/features/step_definitions/request_steps.ts @@ -2,7 +2,7 @@ import { Given, Then, When, AfterAll } from "@cucumber/cucumber"; import { expect } from "chai"; import { World } from "../support/world"; -import { getProperty, pathLookup } from "../support/templating"; +import { getProperty, pathLookup, getTypeForValue } from "../support/templating"; import { Store } from "../support/store"; import { buildUndoFor, UndoActions } from "../support/undo"; import * as datadogApiClient from "../../index"; @@ -12,6 +12,7 @@ import path from "path"; import { compressSync } from "zstd.ts"; import log from "loglevel"; import { ScenariosModelMappings } from "../support/scenarios_model_mapping"; +import _ from "lodash"; const logger = log.getLogger("testing") logger.setLevel(process.env.DEBUG ? logger.levels.DEBUG : logger.levels.INFO); @@ -134,13 +135,6 @@ When("the request is sent", async function (this: World) { this.response = await apiInstance[this.operationId.toOperationName()](); } - const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer; - this.response = objectSerializer.serialize( - this.response, - ScenariosModelMappings[`${this.apiVersion}.${this.operationId}`]["operationResponseType"], - "", - ) - if (undoAction.undo.type == "unsafe") { this.undo.push( buildUndoFor( @@ -262,9 +256,16 @@ Then( Then( /the response "([^"]+)" is equal to (.*)/, function (this: World, responsePath: string, value: string) { - expect(pathLookup(this.response, responsePath)).to.deep.equal( - JSON.parse(value.templated(this.fixtures)) - ); + const pathResult = pathLookup(this.response, responsePath) + const _type = getTypeForValue(pathResult) + const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer; + let templatedFixtureValue = JSON.parse(value.templated(this.fixtures)) + + if (_type) { + templatedFixtureValue = objectSerializer.deserialize(templatedFixtureValue, _type, "") + } + + expect(pathResult).to.deep.equal(templatedFixtureValue); } ); diff --git a/features/support/templating.ts b/features/support/templating.ts index a77c4a429208..cd3ed5b028d6 100644 --- a/features/support/templating.ts +++ b/features/support/templating.ts @@ -1,3 +1,5 @@ +import { UnparsedObject } from "../../packages/datadog-api-client-common/util"; + declare global { interface String { templated(data: { [key: string]: any }): string; @@ -68,11 +70,8 @@ function pathLookup(data: any, dottedPath: string): any { result = value[part]; } else if (part.toAttributeName() in value) { result = value[part.toAttributeName()]; - } else if ( - "unparsedObject" in value && - part in value["unparsedObject"] - ) { - result = value["unparsedObject"][part]; + } else if (value instanceof UnparsedObject && part in value["_data"]) { + result = value["_data"][part]; } else { throw new Error( `${part} not found in ${JSON.stringify( @@ -81,11 +80,30 @@ function pathLookup(data: any, dottedPath: string): any { ); } } + if (result instanceof UnparsedObject) { + result = result["_data"] + } } } + return result; } +function getTypeForValue(pathResult: any): string { + let _type: string = ""; + + if (pathResult?.constructor?.name) { + if (pathResult.constructor?.name == "Array") { + if (pathResult[0]?.constructor?.name) { + _type = `Array<${pathResult[0].constructor.name}>` + } + } else { + _type = pathResult.constructor.name + } + } + return _type; +} + String.prototype.templated = function (data: { [key: string]: any }): string { const regexp = /{{ *([^{}]+|'[^']+'|"[^"]+") *}}/g; const function_re = /^(.+)\((.*)\)$/; @@ -120,4 +138,4 @@ function getProperty(obj: T, name: string): T[K] { return obj[key]; } -export { pathLookup, getProperty }; +export { pathLookup, getProperty, getTypeForValue }; From 54658474d58f4becc05ff81d1b462c9498fb7307 Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Wed, 8 Feb 2023 10:53:05 -0500 Subject: [PATCH 2/3] remove lodash import --- features/step_definitions/request_steps.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/features/step_definitions/request_steps.ts b/features/step_definitions/request_steps.ts index cd5571254c13..6332ef9bfdeb 100644 --- a/features/step_definitions/request_steps.ts +++ b/features/step_definitions/request_steps.ts @@ -12,7 +12,6 @@ import path from "path"; import { compressSync } from "zstd.ts"; import log from "loglevel"; import { ScenariosModelMappings } from "../support/scenarios_model_mapping"; -import _ from "lodash"; const logger = log.getLogger("testing") logger.setLevel(process.env.DEBUG ? logger.levels.DEBUG : logger.levels.INFO); From 25b76189000f6c7424cb680020e8e9980df25158 Mon Sep 17 00:00:00 2001 From: Sherzod Karimov Date: Wed, 8 Feb 2023 10:53:59 -0500 Subject: [PATCH 3/3] small cleanup --- features/step_definitions/request_steps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/step_definitions/request_steps.ts b/features/step_definitions/request_steps.ts index 6332ef9bfdeb..7ccb6e5389e7 100644 --- a/features/step_definitions/request_steps.ts +++ b/features/step_definitions/request_steps.ts @@ -257,10 +257,10 @@ Then( function (this: World, responsePath: string, value: string) { const pathResult = pathLookup(this.response, responsePath) const _type = getTypeForValue(pathResult) - const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer; let templatedFixtureValue = JSON.parse(value.templated(this.fixtures)) if (_type) { + const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer; templatedFixtureValue = objectSerializer.deserialize(templatedFixtureValue, _type, "") }