-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f31f451
commit 7cc3b38
Showing
6 changed files
with
289 additions
and
150 deletions.
There are no files selected for viewing
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
73 changes: 73 additions & 0 deletions
73
packages/spacecat-shared-dynamo/test/modules/getItem.test.js
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,73 @@ | ||
/* | ||
* 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 { expect } from 'chai'; | ||
import { createClient } from '../../src/index.js'; | ||
|
||
describe('getItem', () => { | ||
let dynamoDbClient; | ||
let mockDocClient; | ||
|
||
beforeEach(() => { | ||
mockDocClient = { | ||
get: async () => ({ Item: {} }), | ||
}; | ||
|
||
dynamoDbClient = createClient(console, undefined, mockDocClient); | ||
}); | ||
|
||
it('gets an item from the database', async () => { | ||
const key = { partitionKey: 'testPartitionKey' }; | ||
const result = await dynamoDbClient.getItem('TestTable', key); | ||
expect(result).to.be.an('object'); | ||
}); | ||
|
||
it('gets an item from the database with sort key', async () => { | ||
const key = { partitionKey: 'testPartitionKey', sortKey: 'testSortKey' }; | ||
const result = await dynamoDbClient.getItem('TestTable', key); | ||
expect(result).to.be.an('object'); | ||
}); | ||
|
||
it('throws an error for getItem with invalid tableName', async () => { | ||
const key = { partitionKey: 'testPartitionKey' }; | ||
try { | ||
await dynamoDbClient.getItem('', key); | ||
expect.fail('getItem did not throw with empty tableName'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Invalid tableName: must be a non-empty string.'); | ||
} | ||
}); | ||
|
||
it('throws an error for getItem with invalid key', async () => { | ||
try { | ||
await dynamoDbClient.getItem('TestTable', null); | ||
expect.fail('getItem did not throw with invalid key'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Invalid key: must be an object with a partitionKey.'); | ||
} | ||
}); | ||
|
||
it('handles errors in getItem', async () => { | ||
mockDocClient.get = async () => { | ||
throw new Error('Get failed'); | ||
}; | ||
|
||
try { | ||
await dynamoDbClient.getItem('TestTable', { partitionKey: 'testPartitionKey' }); | ||
expect.fail('getItem did not throw as expected'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Get failed'); | ||
} | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
packages/spacecat-shared-dynamo/test/modules/putItem.test.js
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,56 @@ | ||
/* | ||
* 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 { expect } from 'chai'; | ||
import { createClient } from '../../src/index.js'; | ||
|
||
describe('putItem', () => { | ||
let dynamoDbClient; | ||
let mockDocClient; | ||
|
||
beforeEach(() => { | ||
mockDocClient = { | ||
put: async () => ({}), | ||
}; | ||
|
||
dynamoDbClient = createClient(console, undefined, mockDocClient); | ||
}); | ||
|
||
it('puts an item into the database', async () => { | ||
const result = await dynamoDbClient.putItem('TestTable', { someKey: 'someValue' }); | ||
expect(result).to.deep.equal({ message: 'Item inserted/updated successfully.' }); | ||
}); | ||
|
||
it('throws an error for putItem with invalid tableName', async () => { | ||
try { | ||
await dynamoDbClient.putItem('', { someKey: 'someValue' }); | ||
expect.fail('putItem did not throw with empty tableName'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Invalid tableName: must be a non-empty string.'); | ||
} | ||
}); | ||
|
||
it('handles errors in putItem', async () => { | ||
mockDocClient.put = async () => { | ||
throw new Error('Put failed'); | ||
}; | ||
|
||
try { | ||
await dynamoDbClient.putItem('TestTable', { someKey: 'someValue' }); | ||
expect.fail('putItem did not throw as expected'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Put failed'); | ||
} | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
packages/spacecat-shared-dynamo/test/modules/query.test.js
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,60 @@ | ||
/* | ||
* 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 { expect } from 'chai'; | ||
import { createClient } from '../../src/index.js'; | ||
|
||
describe('query', () => { | ||
let dynamoDbClient; | ||
let mockDocClient; | ||
|
||
beforeEach(() => { | ||
mockDocClient = { | ||
query: async (params) => { | ||
// Check if LastEvaluatedKey is provided and simulate pagination | ||
if (params.ExclusiveStartKey === 'key2') { | ||
return { Items: ['item3'], LastEvaluatedKey: undefined }; | ||
} else { | ||
return { Items: ['item1', 'item2'], LastEvaluatedKey: 'key2' }; | ||
} | ||
}, | ||
}; | ||
|
||
dynamoDbClient = createClient(console, undefined, mockDocClient); | ||
}); | ||
|
||
it('queries items from the database', async () => { | ||
const result = await dynamoDbClient.query({ TableName: 'TestTable' }); | ||
expect(result).to.be.an('array'); | ||
}); | ||
|
||
it('queries items from the database with pagination', async () => { | ||
const result = await dynamoDbClient.query({ TableName: 'TestTable' }); | ||
expect(result).to.have.lengthOf(3); | ||
expect(result).to.deep.equal(['item1', 'item2', 'item3']); | ||
}); | ||
|
||
it('handles errors in query', async () => { | ||
mockDocClient.query = async () => { | ||
throw new Error('Query failed'); | ||
}; | ||
|
||
try { | ||
await dynamoDbClient.query({ TableName: 'TestTable' }); | ||
expect.fail('queryDb did not throw as expected'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Query failed'); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.