-
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.
feat: partially write tests and fix get site command
- Loading branch information
1 parent
56f314b
commit 06dc51f
Showing
4 changed files
with
186 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 dynamoDBWrapper from '../src/db-wrapper.js'; | ||
import DB from '../src/db.js'; | ||
|
||
describe('DB Wrapper Tests', () => { | ||
let mockFunc; | ||
let mockRequest; | ||
let mockContext; | ||
|
||
beforeEach(() => { | ||
mockFunc = async () => {}; | ||
mockRequest = {}; | ||
mockContext = { | ||
attributes: {}, | ||
runtime: { | ||
region: 'test-region', | ||
}, | ||
log: { | ||
info: () => {}, | ||
error: () => {}, | ||
}, | ||
}; | ||
}); | ||
|
||
it('should create db if not present in context', async () => { | ||
await dynamoDBWrapper(mockFunc)(mockRequest, mockContext); | ||
assert(mockContext.db instanceof DB, 'context.db was not correctly instantiated.'); | ||
}); | ||
|
||
it('should not re-initialize db if already present in context', async () => { | ||
const mockDB = new DB(mockContext); | ||
mockContext.db = mockDB; | ||
|
||
await dynamoDBWrapper(mockFunc)(mockRequest, mockContext); | ||
|
||
assert.strictEqual(mockContext.db, mockDB, 'context.db was re-initialized.'); | ||
}); | ||
}); |
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,93 @@ | ||
/* | ||
* 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 nock from 'nock'; | ||
import assert from 'assert'; | ||
import esmock from 'esmock'; | ||
import DB from '../src/db.js'; | ||
import DBDocClientMock from './dynamo-db-doc-client-mock.js'; | ||
|
||
describe('DB Tests', () => { | ||
const region = 'test-region'; | ||
|
||
let mockContext; | ||
let logInfo = ''; | ||
let logError = ''; | ||
let testSite = { domain: 'www.testdomain.com', path: '/testpath' }; | ||
|
||
beforeEach(() => { | ||
mockContext = { | ||
runtime: { region }, | ||
log: { | ||
info: (message) => { | ||
logInfo = message; | ||
}, | ||
error: (message) => { | ||
logError = message; | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
it('should initialize with provided context', () => { | ||
const db = new DB(mockContext); | ||
assert.strictEqual(db.log, mockContext.log); | ||
}); | ||
|
||
it('should save the record to dynamodb and log success', async () => { | ||
const DBDocMock = await esmock('../src/db.js', { | ||
'@aws-sdk/lib-dynamodb': { | ||
DynamoDBDocumentClient: DBDocClientMock, | ||
}, | ||
}); | ||
const db = new DBDocMock(mockContext); | ||
await db.saveAuditIndex(testSite, { | ||
result: { | ||
mobile: { | ||
categories: { | ||
performance: { score: 0.90 }, | ||
seo: { score: 0.90 }, | ||
'best-practices': { score: 0.90 }, | ||
accessibility: { score: 0.90 }, | ||
}, | ||
}, | ||
desktop: { | ||
categories: { | ||
performance: { score: 0.90 }, | ||
seo: { score: 0.90 }, | ||
'best-practices': { score: 0.90 }, | ||
accessibility: { score: 0.90 }, | ||
}, | ||
}, | ||
}, | ||
}); | ||
assert.strictEqual(logInfo, 'Audit for domain www.testdomain.com saved successfully'); | ||
}); | ||
|
||
it('should get the site from dynamodb', async () => { | ||
const DBDocMock = await esmock('../src/db.js', { | ||
'@aws-sdk/lib-dynamodb': { | ||
DynamoDBDocumentClient: DBDocClientMock, | ||
}, | ||
}); | ||
const db = new DBDocMock(mockContext); | ||
const site = await db.getSite(testSite.domain, testSite.path); | ||
assert.strictEqual(logInfo, `Item retrieved successfully: ${testSite}`); | ||
assert.deepStrictEqual(site, testSite); | ||
}); | ||
}); |
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,37 @@ | ||
/* | ||
* 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 { GetCommand, PutCommand } from '@aws-sdk/lib-dynamodb'; | ||
|
||
export default class DBDocClientMock { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
|
||
static from() { | ||
return new DBDocClientMock(); | ||
} | ||
|
||
send(command) { | ||
if (command instanceof PutCommand) { | ||
return Promise.resolve({ }); | ||
} else if (command instanceof GetCommand) { | ||
return Promise.resolve({ | ||
Item: { | ||
domain: 'www.testdomain.com', | ||
path: '/testpath', | ||
}, | ||
}); | ||
} else { | ||
return Promise.reject(new Error('Unknown command in mock')); | ||
} | ||
} | ||
} |