-
Notifications
You must be signed in to change notification settings - Fork 12
Add unit tests and enable comprehensive coverage #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
czirker
wants to merge
1
commit into
master
Choose a base branch
from
feature/TASK-003-add-unit-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,174 @@ | ||
| import { expect, assert } from "chai"; | ||
| import sinon from "sinon"; | ||
| import { DynamodbFetcher } from "../lib/DynamodbFetcher"; | ||
|
|
||
| describe('lib/DynamodbFetcher.ts', function () { | ||
| let fetcher: DynamodbFetcher<any, any>; | ||
|
|
||
| beforeEach(function () { | ||
| fetcher = new DynamodbFetcher('test-table', (item) => ({ id: item.id })); | ||
| }); | ||
|
|
||
| describe("constructor", function () { | ||
| it('should set tableName', function () { | ||
| expect(fetcher.tableName).to.equal('test-table'); | ||
| }); | ||
|
|
||
| it('should set getDdbKey function', function () { | ||
| const item = { id: '123', name: 'test' }; | ||
| const key = fetcher.getDdbKey(item); | ||
| expect(key).to.deep.equal({ id: '123' }); | ||
| }); | ||
|
|
||
| it('should initialize batch counter to 0', function () { | ||
| expect(fetcher.batch).to.equal(0); | ||
| }); | ||
|
|
||
| it('should set batchOptions if provided', function () { | ||
| const options = { maxRecords: 100, parallelLimit: 10 }; | ||
| const fetcherWithOptions = new DynamodbFetcher('table', (i) => i, options as any); | ||
| expect(fetcherWithOptions.batchOptions).to.not.be.undefined; | ||
| }); | ||
|
|
||
| it('should create ddb client', function () { | ||
| expect(fetcher.ddb).to.not.be.undefined; | ||
| }); | ||
| }); | ||
|
|
||
| describe("keyToString", function () { | ||
| it('should convert single key to string', function () { | ||
| const key = { id: '123' }; | ||
| const result = fetcher.keyToString(key); | ||
| expect(result).to.equal('id:123'); | ||
| }); | ||
|
|
||
| it('should convert multiple keys to string sorted alphabetically', function () { | ||
| const key = { z: '3', a: '1', m: '2' }; | ||
| const result = fetcher.keyToString(key); | ||
| expect(result).to.equal('a:1/m:2/z:3'); | ||
| }); | ||
|
|
||
| it('should handle numeric values', function () { | ||
| const key = { id: 123 }; | ||
| const result = fetcher.keyToString(key); | ||
| expect(result).to.equal('id:123'); | ||
| }); | ||
|
|
||
| it('should handle composite keys', function () { | ||
| const key = { pk: 'user#123', sk: 'profile' }; | ||
| const result = fetcher.keyToString(key); | ||
| expect(result).to.equal('pk:user#123/sk:profile'); | ||
| }); | ||
|
|
||
| it('should handle empty object', function () { | ||
| const key = {}; | ||
| const result = fetcher.keyToString(key); | ||
| expect(result).to.equal(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe("join", function () { | ||
| let batchGetStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(function () { | ||
| batchGetStub = sinon.stub(fetcher.ddb, 'batchGet'); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| batchGetStub.restore(); | ||
| }); | ||
|
|
||
| it('should return events with joinedData when DDB returns data', async function () { | ||
| batchGetStub.resolves({ | ||
| Responses: { | ||
| 'test-table': [ | ||
| { id: '1', name: 'Item 1' }, | ||
| { id: '2', name: 'Item 2' } | ||
| ] | ||
| } | ||
| }); | ||
|
|
||
| const events = [ | ||
| { id: '1', value: 'a' }, | ||
| { id: '2', value: 'b' } | ||
| ]; | ||
|
|
||
| const result = await fetcher.join(events); | ||
|
|
||
| expect(result).to.have.length(2); | ||
| expect((result[0] as any).joinedData).to.deep.equal({ id: '1', name: 'Item 1' }); | ||
| expect((result[1] as any).joinedData).to.deep.equal({ id: '2', name: 'Item 2' }); | ||
| }); | ||
|
|
||
| it('should handle empty events array', async function () { | ||
| const result = await fetcher.join([]); | ||
| expect(result).to.deep.equal([]); | ||
| expect(batchGetStub.called).to.be.false; | ||
| }); | ||
|
|
||
| it('should handle events with null keys', async function () { | ||
| const nullKeyFetcher = new DynamodbFetcher('test-table', (item) => item.id ? { id: item.id } : null); | ||
| const batchGetStubNull = sinon.stub(nullKeyFetcher.ddb, 'batchGet'); | ||
| batchGetStubNull.resolves({ | ||
| Responses: { | ||
| 'test-table': [{ id: '1', name: 'Item 1' }] | ||
| } | ||
| }); | ||
|
|
||
| const events = [ | ||
| { id: '1', value: 'a' }, | ||
| { value: 'b' } // no id, key will be null | ||
| ]; | ||
|
|
||
| const result = await nullKeyFetcher.join(events); | ||
|
|
||
| expect(result).to.have.length(2); | ||
| expect((result[0] as any).joinedData).to.not.be.undefined; | ||
| expect((result[1] as any).joinedData).to.be.undefined; | ||
|
|
||
| batchGetStubNull.restore(); | ||
| }); | ||
|
|
||
| it('should deduplicate keys', async function () { | ||
| batchGetStub.resolves({ | ||
| Responses: { | ||
| 'test-table': [{ id: '1', name: 'Item 1' }] | ||
| } | ||
| }); | ||
|
|
||
| const events = [ | ||
| { id: '1', value: 'a' }, | ||
| { id: '1', value: 'b' } // same id | ||
| ]; | ||
|
|
||
| await fetcher.join(events); | ||
|
|
||
| // Should only have one key in the batch | ||
| const batchParams = batchGetStub.firstCall.args[0]; | ||
| expect(batchParams.RequestItems['test-table'].Keys).to.have.length(1); | ||
| }); | ||
|
|
||
| it('should increment batch counter', async function () { | ||
| batchGetStub.resolves({ Responses: { 'test-table': [] } }); | ||
|
|
||
| expect(fetcher.batch).to.equal(0); | ||
| await fetcher.join([{ id: '1' }]); | ||
| expect(fetcher.batch).to.equal(1); | ||
| await fetcher.join([{ id: '2' }]); | ||
| expect(fetcher.batch).to.equal(2); | ||
| }); | ||
|
|
||
| it('should handle DDB returning undefined joinedData', async function () { | ||
| batchGetStub.resolves({ | ||
| Responses: { | ||
| 'test-table': [] // no matching items | ||
| } | ||
| }); | ||
|
|
||
| const events = [{ id: '1', value: 'a' }]; | ||
| const result = await fetcher.join(events); | ||
|
|
||
| expect((result[0] as any).joinedData).to.be.undefined; | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,145 @@ | ||
| import { expect, assert } from "chai"; | ||
| import { copy, error, date } from "../lib/aws-util"; | ||
|
|
||
| describe('lib/aws-util.ts', function () { | ||
|
|
||
| describe("copy", function () { | ||
| it('should return null for null input', function () { | ||
| assert.equal(copy(null), null); | ||
| }); | ||
|
|
||
| it('should return undefined for undefined input', function () { | ||
| assert.equal(copy(undefined), undefined); | ||
| }); | ||
|
|
||
| it('should create a shallow copy of an object', function () { | ||
| const original = { a: 1, b: 2 }; | ||
| const copied = copy(original); | ||
| expect(copied).to.deep.equal(original); | ||
| expect(copied).to.not.equal(original); | ||
| }); | ||
|
|
||
| it('should copy all properties', function () { | ||
| const original = { name: 'test', value: 42, flag: true }; | ||
| const copied = copy(original); | ||
| expect(copied.name).to.equal('test'); | ||
| expect(copied.value).to.equal(42); | ||
| expect(copied.flag).to.equal(true); | ||
| }); | ||
|
|
||
| it('should handle empty object', function () { | ||
| const original = {}; | ||
| const copied = copy(original); | ||
| expect(copied).to.deep.equal({}); | ||
| expect(copied).to.not.equal(original); | ||
| }); | ||
| }); | ||
|
|
||
| describe("error", function () { | ||
| it('should set message from string options', function () { | ||
| const err: any = new Error(); | ||
| const result = error(err, 'test message'); | ||
| expect(result.message).to.equal('test message'); | ||
| }); | ||
|
|
||
| it('should set message from object options', function () { | ||
| const err: any = new Error(); | ||
| const result = error(err, { message: 'object message' }); | ||
| expect(result.message).to.equal('object message'); | ||
| }); | ||
|
|
||
| it('should set code from options', function () { | ||
| const err: any = new Error(); | ||
| const result = error(err, { code: 'TEST_CODE' }); | ||
| expect(result.code).to.equal('TEST_CODE'); | ||
| }); | ||
|
|
||
| it('should set name from options.name', function () { | ||
| const err: any = new Error(); | ||
| const result = error(err, { name: 'CustomError' }); | ||
| expect(result.name).to.equal('CustomError'); | ||
| }); | ||
|
|
||
| it('should use code as name if name not provided', function () { | ||
| const err: any = new Error(); | ||
| const result = error(err, { code: 'ERROR_CODE' }); | ||
| expect(result.name).to.equal('ERROR_CODE'); | ||
| }); | ||
|
|
||
| it('should set stack from options', function () { | ||
| const err: any = new Error(); | ||
| const customStack = 'custom stack trace'; | ||
| const result = error(err, { stack: customStack }); | ||
| expect(result.stack).to.equal(customStack); | ||
| }); | ||
|
|
||
| it('should set time to current date', function () { | ||
| const err: any = new Error(); | ||
| const before = new Date(); | ||
| const result = error(err, 'test'); | ||
| const after = new Date(); | ||
| expect(result.time.getTime()).to.be.at.least(before.getTime()); | ||
| expect(result.time.getTime()).to.be.at.most(after.getTime()); | ||
| }); | ||
|
|
||
| it('should preserve original error when overwriting message', function () { | ||
| const err: any = new Error('original message'); | ||
| const result = error(err, 'new message'); | ||
| expect(result.originalError).to.not.be.undefined; | ||
| expect(result.originalError.message).to.equal('original message'); | ||
| }); | ||
|
|
||
| it('should preserve original error with object options containing message', function () { | ||
| const err: any = new Error('original message'); | ||
| const result = error(err, { message: 'new message' }); | ||
| expect(result.originalError).to.not.be.undefined; | ||
| expect(result.originalError.message).to.equal('original message'); | ||
| }); | ||
|
|
||
| it('should handle null options', function () { | ||
| const err: any = new Error('test'); | ||
| const result = error(err, null); | ||
| expect(result.message).to.equal('test'); | ||
| }); | ||
|
|
||
| it('should handle error with empty message', function () { | ||
| const err: any = new Error(); | ||
| err.message = ''; | ||
| const result = error(err, { code: 'TEST' }); | ||
| expect(result.message).to.equal(null); | ||
| }); | ||
|
|
||
| it('should default name to Error when no options provided', function () { | ||
| const err: any = {}; | ||
| const result = error(err, null); | ||
| expect(result.name).to.equal('Error'); | ||
| }); | ||
|
|
||
| it('should update err with all options properties', function () { | ||
| const err: any = new Error(); | ||
| const result = error(err, { | ||
| message: 'test', | ||
| code: 'CODE', | ||
| customProp: 'custom' | ||
| }); | ||
| expect(result.customProp).to.equal('custom'); | ||
| }); | ||
| }); | ||
|
|
||
| describe("date", function () { | ||
| describe("getDate", function () { | ||
| it('should return a Date object', function () { | ||
| const result = date.getDate(); | ||
| expect(result).to.be.instanceOf(Date); | ||
| }); | ||
|
|
||
| it('should return current time', function () { | ||
| const before = Date.now(); | ||
| const result = date.getDate(); | ||
| const after = Date.now(); | ||
| expect(result.getTime()).to.be.at.least(before); | ||
| expect(result.getTime()).to.be.at.most(after); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing TypeScript test file exclusion pattern
Low Severity
The exclude pattern only includes
**/*.utest.jsbut not**/*.utest.ts. Withall: trueenabled and TypeScript extensions configured, nyc now processes.tssource files directly. If a.utest.tsfile were created in thelib/directory (matching the include patternlib/**/*.ts), it wouldn't be excluded since**/*.utest.jsonly matches JavaScript test files. This could incorrectly inflate coverage metrics.