Skip to content
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

fix: get audit no query #34

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,12 @@ export const getAuditForSite = async (
auditType,
auditedAt,
) => {
const audit = await dynamoClient.query({
TableName: config.tableNameAudits,
KeyConditionExpression: 'siteId = :siteId AND SK = :sk',
ExpressionAttributeValues: {
':siteId': siteId,
':sk': `${auditType}#${auditedAt}`,
},
Limit: 1,
const audit = await dynamoClient.getItem(config.tableNameAudits, {
siteId,
SK: `${auditType}#${auditedAt}`,
});

return audit.length > 0 ? AuditDto.fromDynamoItem(audit[0]) : null;
return audit ? AuditDto.fromDynamoItem(audit) : null;
};

/**
Expand Down Expand Up @@ -158,7 +153,7 @@ export const getLatestAuditForSite = async (
) => {
const latestAudit = await dynamoClient.query({
TableName: config.tableNameLatestAudits,
KeyConditionExpression: 'siteId = :siteId AND begins_with(auditType, :auditType)',
KeyConditionExpression: 'siteId = :siteId AND auditType = :auditType',
ExpressionAttributeValues: {
':siteId': siteId,
':auditType': `${auditType}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ describe('Audit Access Pattern Tests', () => {
beforeEach(() => {
mockDynamoClient = {
query: sinon.stub().returns(Promise.resolve([])),
getItem: sinon.stub().resolves(),
};
mockLog = { log: sinon.stub() };
exportedFunctions = auditFunctions(mockDynamoClient, TEST_DA_CONFIG, mockLog);
});

it('successfully retrieves an audit for a site', async () => {
const mockAuditData = [{
const mockAuditData = {
siteId: 'siteId',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
Expand All @@ -129,21 +130,21 @@ describe('Audit Access Pattern Tests', () => {
},
},
fullAuditRef: 'https://someurl.com',
}];
mockDynamoClient.query.returns(Promise.resolve(mockAuditData));
};
mockDynamoClient.getItem.resolves(mockAuditData);

const result = await exportedFunctions.getAuditForSite('siteId', 'auditType', 'auditedAt');
expect(result).to.not.be.null;
expect(result.getScores()).to.be.an('object');
expect(mockDynamoClient.query.calledOnce).to.be.true;
expect(mockDynamoClient.getItem.calledOnce).to.be.true;
});

it('returns null if no audit is found for a site', async () => {
mockDynamoClient.query.returns(Promise.resolve([]));
mockDynamoClient.getItem.resolves(undefined);

const result = await exportedFunctions.getAuditForSite('siteId', 'auditType', 'auditedAt');
expect(result).to.be.null;
expect(mockDynamoClient.query.calledOnce).to.be.true;
expect(mockDynamoClient.getItem.calledOnce).to.be.true;
});
});

Expand All @@ -170,6 +171,7 @@ describe('Audit Access Pattern Tests', () => {
beforeEach(() => {
mockDynamoClient = {
query: sinon.stub().returns(Promise.resolve([])),
getItem: sinon.stub().returns(Promise.resolve()),
putItem: sinon.stub().returns(Promise.resolve()),
removeItem: sinon.stub().returns(Promise.resolve()),
};
Expand All @@ -193,7 +195,7 @@ describe('Audit Access Pattern Tests', () => {
});

it('throws an error if audit already exists', async () => {
mockDynamoClient.query.returns(Promise.resolve([auditData]));
mockDynamoClient.getItem.resolves(auditData);

await expect(exportedFunctions.addAudit(auditData)).to.be.rejectedWith('Audit already exists');
});
Expand Down
Loading