-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adding unit tests for sqs client
- Loading branch information
Showing
5 changed files
with
123 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
import { SendMessageCommand } from '@aws-sdk/client-sqs'; | ||
|
||
export default class SqsClientMock { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
send(command) { | ||
if (command instanceof SendMessageCommand) { | ||
if (JSON.parse(command.input.MessageBody).message.includes('error')) { | ||
return Promise.reject(new Error('SQSClient.send encountered an error')); | ||
} | ||
return Promise.resolve({ MessageId: 'testMessageId' }); | ||
} else { | ||
return Promise.reject(new Error('Unknown command in mock')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
|
||
import assert from 'assert'; | ||
import nock from 'nock'; | ||
import esmock from 'esmock'; | ||
import SqsClientMock from './sqs-client-mock.js'; | ||
import SQSQueue from '../src/sqs-queue.js'; | ||
|
||
describe('SQSQueue Tests', () => { | ||
const region = 'test-region'; | ||
const queueUrl = 'https://queue-url'; | ||
const messageId = 'testMessageId'; | ||
|
||
let mockContext; | ||
let logInfo = ''; | ||
let logError = ''; | ||
|
||
beforeEach(() => { | ||
mockContext = { | ||
attributes: { | ||
queueUrl, | ||
}, | ||
region, | ||
log: { | ||
info: (message) => { | ||
logInfo = message; | ||
}, | ||
error: (message) => { | ||
logError = message; | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
it('should initialize with provided context', () => { | ||
const queue = new SQSQueue(mockContext); | ||
assert.strictEqual(queue.queueUrl, mockContext.attributes.queueUrl); | ||
assert.strictEqual(queue.log, mockContext.log); | ||
assert.strictEqual(logInfo, `Creating SQS client in region ${region}`); | ||
}); | ||
|
||
it('should send a message to the queue and log success', async () => { | ||
const SQSQueueMock = await esmock('../src/sqs-queue.js', { | ||
'@aws-sdk/client-sqs': { | ||
SQSClient: SqsClientMock, | ||
}, | ||
}); | ||
const queue = new SQSQueueMock(mockContext); | ||
await queue.sendAuditResult('test message'); | ||
assert.strictEqual(logInfo, `Success, message sent. MessageID: ${messageId}`); | ||
}); | ||
|
||
it('should throw an error, when sending a message to the queue fails', async () => { | ||
const SQSQueueMock = await esmock('../src/sqs-queue.js', { | ||
'@aws-sdk/client-sqs': { | ||
SQSClient: SqsClientMock, | ||
}, | ||
}); | ||
const queue = new SQSQueueMock(mockContext); | ||
try { | ||
await queue.sendAuditResult('error test message'); | ||
assert.fail('Expected SQLClient to throw an error'); | ||
} catch (error) { | ||
assert.strictEqual(logError, 'Error: SQSClient.send encountered an error'); | ||
} | ||
}); | ||
}); |