Skip to content

Commit

Permalink
Improve types for mocking (#3082)
Browse files Browse the repository at this point in the history
* Improve types for mocking

* Fix untransformBody for arrays

* Remove executeGetPermissionsFormulaFromPackDef

* Update changelog

* Rebuild

* Fix validation for non object types

* Improve changelog
  • Loading branch information
oleg-codaio authored Oct 7, 2024
1 parent f8d387b commit 8422fa6
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 68 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ This changelog keeps track of all changes to the Packs SDK. We follow convention

- Add continuation for get permissions request and response

### Changed

- Improve types used for testing, specifically on `MockExecutionContext` returned by `newMockExecutionContext()`.
`fetcher.fetch`, `temporaryBlobStorage.storeUrl`, and `temporaryBlobStorage.storeBlob` should be stubbed using
`resolves()` and `rejects()` since the underlying methods return promises
- Fix `untransformBody` helper for array inputs

## [1.7.19] - 2024-09-27

### Changed
Expand Down
4 changes: 2 additions & 2 deletions dist/handler_templates.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions dist/testing/execution.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions dist/testing/mocks.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion dist/testing/mocks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/reference/sdk/interfaces/testing.MockExecutionContext.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions handler_templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ function mapKeys(obj: {[key: string]: any}, schema?: Schema): object {
continue;
}
remappedObject[newKey] = mappedKeys.length > 1 ? deepCopy(obj[key]) : obj[key];
const keySchema: Schema & ObjectSchemaProperty | undefined = schema.properties[newKey];
const keySchema: (Schema & ObjectSchemaProperty) | undefined = schema.properties[newKey];
const currentValue = remappedObject[newKey];
if (Array.isArray(currentValue) && isArray(keySchema) && isObject(keySchema.items)) {
remappedObject[newKey] = currentValue.map(val => mapKeys(val, keySchema.items));
Expand Down Expand Up @@ -367,7 +367,7 @@ function unmapKeys(obj: {[key: string]: any}, schema?: Schema): object {
continue;
}
remappedObject[newKey] = deepCopy(obj[key]);
const keySchema: Schema & ObjectSchemaProperty | undefined = schema.properties[key];
const keySchema: (Schema & ObjectSchemaProperty) | undefined = schema.properties[key];
const currentValue = remappedObject[newKey];
if (Array.isArray(currentValue) && isArray(keySchema) && isObject(keySchema.items)) {
remappedObject[newKey] = currentValue.map(val => unmapKeys(val, keySchema.items));
Expand All @@ -380,8 +380,8 @@ function unmapKeys(obj: {[key: string]: any}, schema?: Schema): object {

export function untransformBody(body: any, schema: Schema | undefined): any {
if (isArray(schema) && isObject(schema.items)) {
const objectBody = body as Record<string, any>;
const mappedObjs = unmapKeys(objectBody, schema.items);
const objects = body as Array<Record<string, any>>;
const mappedObjs = objects.map(obj => unmapKeys(obj, schema.items));
return mappedObjs;
}

Expand Down
10 changes: 7 additions & 3 deletions test/compile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {compilePackBundle} from '../testing/compile';
import {executeFormulaOrSyncWithVM} from '../testing/execution';
import {newMockSyncExecutionContext} from '../testing/mocks';
import path from 'path';
import sinon from 'sinon';
import {translateErrorStackFromVM} from '../runtime/common/source_map';

describe('compile', () => {
Expand Down Expand Up @@ -79,9 +80,12 @@ describe('compile', () => {
});
assert.equal(response, 'okay');
assert.isTrue(
executionContext.temporaryBlobStorage.storeBlob.calledWithMatch((buffer: any) => {
return Buffer.isBuffer(buffer);
}, 'text/html'),
executionContext.temporaryBlobStorage.storeBlob.calledWithMatch(
sinon.match((buffer: any) => {
return Buffer.isBuffer(buffer);
}),
'text/html',
),
);
});
});
8 changes: 4 additions & 4 deletions test/execution_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Execution', () => {
describe('fetcher mocks', () => {
it('fetch calls are mocked', async () => {
const context = newMockExecutionContext();
context.fetcher.fetch.returns(newJsonFetchResponse({result: 'hello'}));
context.fetcher.fetch.resolves(newJsonFetchResponse({result: 'hello'}));
const result = await executeFormulaFromPackDef(fakePack, 'Lookup', ['foo'], context);
assert.equal(result, 'hello');

Expand Down Expand Up @@ -177,8 +177,8 @@ describe('Execution', () => {
});

const context = newMockExecutionContext();
context.temporaryBlobStorage.storeBlob.returns('blob-url-1');
context.temporaryBlobStorage.storeUrl.returns('blob-url-2');
context.temporaryBlobStorage.storeBlob.resolves('blob-url-1');
context.temporaryBlobStorage.storeUrl.resolves('blob-url-2');
const result = await executeFormulaFromPackDef(fakeBlobPack, 'Blobs', [], context);
assert.equal(result, 'blob-url-1,blob-url-2');

Expand Down Expand Up @@ -391,7 +391,7 @@ describe('Execution', () => {
it('executes getConnectionName formula', async () => {
assertCondition(fakePackWithMetadata.defaultAuthentication?.type === AuthenticationType.HeaderBearerToken);
const context = newMockExecutionContext();
context.fetcher.fetch.returns(newJsonFetchResponse({username: 'some-user'}));
context.fetcher.fetch.resolves(newJsonFetchResponse({username: 'some-user'}));

const result = await executeMetadataFormula(
fakePackWithMetadata.defaultAuthentication.getConnectionName!,
Expand Down
68 changes: 44 additions & 24 deletions test/handler_templates_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('handler templates', () => {
});

it('array schema', () => {
const inputBody = {Foo: 'bar', Baz: 2};
const inputBody = [{Foo: 'bar', Baz: 2}];
const body = untransformBody(inputBody, {
type: ValueType.Array,
items: {
Expand All @@ -118,10 +118,12 @@ describe('handler templates', () => {
},
},
});
assert.deepEqual(body, {
foo: 'bar',
baz: 2,
});
assert.deepEqual(body, [
{
foo: 'bar',
baz: 2,
},
]);
});

it('object schema', () => {
Expand All @@ -146,20 +148,26 @@ describe('handler templates', () => {
properties: {
Foo: {type: ValueType.String, fromKey: 'foo'},
Baz: {type: ValueType.Number, fromKey: 'baz'},
Biz: {type: ValueType.Array, fromKey: 'biz', items: {
type: ValueType.Object,
properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
Biz: {
type: ValueType.Array,
fromKey: 'biz',
items: {
type: ValueType.Object,
properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
},
},
}},
},
},
});
assert.deepEqual(body, {
foo: 'bar',
baz: 2,
biz: [{
buzz: false,
}],
biz: [
{
buzz: false,
},
],
});
});

Expand All @@ -170,9 +178,13 @@ describe('handler templates', () => {
properties: {
Foo: {type: ValueType.String, fromKey: 'foo'},
Baz: {type: ValueType.Number, fromKey: 'baz'},
Biz: {type: ValueType.Object, fromKey: 'biz', properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
}},
Biz: {
type: ValueType.Object,
fromKey: 'biz',
properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
},
},
},
});
assert.deepEqual(body, {
Expand Down Expand Up @@ -226,12 +238,16 @@ describe('handler templates', () => {
properties: {
Foo: {type: ValueType.String, fromKey: 'foo'},
Baz: {type: ValueType.Number, fromKey: 'baz'},
Biz: {type: ValueType.Array, fromKey: 'biz', items: {
type: ValueType.Object,
properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
Biz: {
type: ValueType.Array,
fromKey: 'biz',
items: {
type: ValueType.Object,
properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
},
},
}},
},
},
});
assert.deepEqual(body, ['foo', 'baz', 'biz']);
Expand All @@ -244,9 +260,13 @@ describe('handler templates', () => {
properties: {
Foo: {type: ValueType.String, fromKey: 'foo'},
Baz: {type: ValueType.Number, fromKey: 'baz'},
Biz: {type: ValueType.Object, fromKey: 'biz', properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
}},
Biz: {
type: ValueType.Object,
fromKey: 'biz',
properties: {
Buzz: {type: ValueType.Boolean, fromKey: 'buzz'},
},
},
},
});
assert.deepEqual(body, ['foo', 'baz', 'biz']);
Expand Down
Loading

0 comments on commit 8422fa6

Please sign in to comment.