Skip to content

Commit

Permalink
test: Update tests to use updated Bundling class
Browse files Browse the repository at this point in the history
  • Loading branch information
setu4993 committed Dec 18, 2021
1 parent aa9a95e commit 91baabe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { Architecture, Code, Runtime } from '@aws-cdk/aws-lambda';
import { DockerImage, FileSystem } from '@aws-cdk/core';
import { stageDependencies, bundle } from '../lib/bundling';
import { stageDependencies, Bundling } from '../lib/bundling';

jest.spyOn(Code, 'fromAsset');
jest.spyOn(DockerImage, 'fromBuild');
Expand All @@ -26,7 +26,7 @@ beforeEach(() => {

test('Bundling a function without dependencies', () => {
const entry = path.join(__dirname, 'lambda-handler-nodeps');
bundle({
Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
architecture: Architecture.X86_64,
Expand All @@ -53,7 +53,7 @@ test('Bundling a function without dependencies', () => {

test('Bundling a function with requirements.txt installed', () => {
const entry = path.join(__dirname, 'lambda-handler');
bundle({
Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
architecture: Architecture.X86_64,
Expand All @@ -73,7 +73,7 @@ test('Bundling a function with requirements.txt installed', () => {

test('Bundling Python 2.7 with requirements.txt installed', () => {
const entry = path.join(__dirname, 'lambda-handler');
bundle({
Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_2_7,
architecture: Architecture.X86_64,
Expand All @@ -94,7 +94,7 @@ test('Bundling Python 2.7 with requirements.txt installed', () => {
test('Bundling a layer with dependencies', () => {
const entry = path.join(__dirname, 'lambda-handler');

bundle({
Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
Expand All @@ -114,7 +114,7 @@ test('Bundling a layer with dependencies', () => {
test('Bundling a python code layer', () => {
const entry = path.join(__dirname, 'lambda-handler-nodeps');

bundle({
Bundling.bundle({
entry: path.join(entry, '.'),
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
Expand Down
60 changes: 31 additions & 29 deletions packages/@aws-cdk/aws-lambda-python/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ import { Template } from '@aws-cdk/assertions';
import { Code, Runtime } from '@aws-cdk/aws-lambda';
import { AssetHashType, AssetOptions, Stack } from '@aws-cdk/core';
import { PythonFunction } from '../lib';
import { bundle } from '../lib/bundling';
import { Bundling } from '../lib/bundling';

jest.mock('../lib/bundling', () => {
return {
bundle: jest.fn().mockImplementation((options: AssetOptions): Code => {
const mockObjectKey = (() => {
const hashType = options.assetHashType ?? (options.assetHash ? 'custom' : 'source');
switch (hashType) {
case 'source': return 'SOURCE_MOCK';
case 'output': return 'OUTPUT_MOCK';
case 'custom': {
if (!options.assetHash) { throw new Error('no custom hash'); }
return options.assetHash;
Bundling: {
bundle: jest.fn().mockImplementation((options: AssetOptions): Code => {
const mockObjectKey = (() => {
const hashType = options.assetHashType ?? (options.assetHash ? 'custom' : 'source');
switch (hashType) {
case 'source': return 'SOURCE_MOCK';
case 'output': return 'OUTPUT_MOCK';
case 'custom': {
if (!options.assetHash) { throw new Error('no custom hash'); }
return options.assetHash;
}
}
}

throw new Error('unexpected asset hash type');
})();

return {
isInline: false,
bind: () => ({
s3Location: {
bucketName: 'mock-bucket-name',
objectKey: mockObjectKey,
},
}),
bindToResource: () => { return; },
};
}),
hasDependencies: jest.fn().mockReturnValue(false),

throw new Error('unexpected asset hash type');
})();

return {
isInline: false,
bind: () => ({
s3Location: {
bucketName: 'mock-bucket-name',
objectKey: mockObjectKey,
},
}),
bindToResource: () => { return; },
};
}),
hasDependencies: jest.fn().mockReturnValue(false),
},
};
});

Expand All @@ -47,7 +49,7 @@ test('PythonFunction with defaults', () => {
entry: 'test/lambda-handler',
});

expect(bundle).toHaveBeenCalledWith(expect.objectContaining({
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/aws-lambda-python\/test\/lambda-handler$/),
outputPathSuffix: '.',
}));
Expand All @@ -64,7 +66,7 @@ test('PythonFunction with index in a subdirectory', () => {
handler: 'custom_handler',
});

expect(bundle).toHaveBeenCalledWith(expect.objectContaining({
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/aws-lambda-python\/test\/lambda-handler-sub$/),
outputPathSuffix: '.',
}));
Expand Down
28 changes: 15 additions & 13 deletions packages/@aws-cdk/aws-lambda-python/test/layer.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import * as path from 'path';
import { Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { stageDependencies, bundle } from '../lib/bundling';
import { stageDependencies, Bundling } from '../lib/bundling';
import { PythonLayerVersion } from '../lib/layer';

jest.mock('../lib/bundling', () => {
return {
bundle: jest.fn().mockReturnValue({
bind: () => {
return {
s3Location: {
bucketName: 'bucket',
objectKey: 'key',
},
};
},
bindToResource: () => { return; },
}),
Bundling: {
bundle: jest.fn().mockReturnValue({
bind: () => {
return {
s3Location: {
bucketName: 'bucket',
objectKey: 'key',
},
};
},
bindToResource: () => { return; },
}),
},
stageDependencies: jest.fn().mockReturnValue(true),
};
});
Expand All @@ -37,7 +39,7 @@ test('Bundling a layer from files', () => {
entry,
});

expect(bundle).toHaveBeenCalledWith(expect.objectContaining({
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry,
outputPathSuffix: 'python',
}));
Expand Down

0 comments on commit 91baabe

Please sign in to comment.