Skip to content

Commit

Permalink
fix: use getItem for latest audit (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 authored Dec 5, 2023
1 parent 68ff507 commit 6e4a87e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,12 @@ export const getLatestAuditForSite = async (
siteId,
auditType,
) => {
const latestAudit = await dynamoClient.query({
TableName: config.tableNameLatestAudits,
KeyConditionExpression: 'siteId = :siteId AND auditType = :auditType',
ExpressionAttributeValues: {
':siteId': siteId,
':auditType': `${auditType}`,
},
Limit: 1,
const latestAudit = await dynamoClient.getItem(config.tableNameLatestAudits, {
siteId,
auditType,
});

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('Audit Access Pattern Tests', () => {
beforeEach(() => {
mockDynamoClient = {
query: sinon.stub().returns(Promise.resolve([])),
getItem: sinon.stub().resolves(),
removeItem: sinon.stub().resolves(),
};
mockLog = {
Expand All @@ -89,16 +90,16 @@ describe('Audit Access Pattern Tests', () => {
expect(mockDynamoClient.query.called).to.be.true;
});

it('calls getLatestAudits and return an array', async () => {
it('calls getLatestAudits and returns an array', async () => {
const result = await exportedFunctions.getLatestAudits('auditType', true);
expect(result).to.be.an('array');
expect(mockDynamoClient.query.called).to.be.true;
});

it('calls getLatestAuditForSite and return an array', async () => {
it('calls getLatestAuditForSite and returns null', async () => {
const result = await exportedFunctions.getLatestAuditForSite('siteId', 'auditType');
expect(result).to.be.null;
expect(mockDynamoClient.query.called).to.be.true;
expect(mockDynamoClient.getItem.called).to.be.true;
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('Site Access Pattern Tests', () => {
baseURL: 'https://example.com',
}];

const mockLatestAuditData = [{
const mockLatestAuditData = {
siteId: 'site1',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
Expand All @@ -190,22 +190,22 @@ describe('Site Access Pattern Tests', () => {
},
},
fullAuditRef: 'https://example.com',
}];
};

mockDynamoClient.query.onFirstCall().resolves(mockSiteData);
mockDynamoClient.query.onSecondCall().resolves(mockLatestAuditData);
mockDynamoClient.getItem.onFirstCall().resolves(mockLatestAuditData);

const result = await exportedFunctions.getSiteByBaseURLWithAuditInfo('https://example.com', 'lhs-mobile', true);
const audits = result.getAudits();
expect(audits).to.be.an('array').with.lengthOf(1);

const audit = audits[0];
expect(audit.getId()).to.be.a('string').that.is.not.empty;
expect(audit.getSiteId()).to.equal(mockLatestAuditData[0].siteId);
expect(audit.getAuditType()).to.equal(mockLatestAuditData[0].auditType);
expect(audit.getAuditedAt()).to.equal(mockLatestAuditData[0].auditedAt);
expect(audit.getAuditResult()).to.deep.equal(mockLatestAuditData[0].auditResult);
expect(audit.getFullAuditRef()).to.equal(mockLatestAuditData[0].fullAuditRef);
expect(audit.getSiteId()).to.equal(mockLatestAuditData.siteId);
expect(audit.getAuditType()).to.equal(mockLatestAuditData.auditType);
expect(audit.getAuditedAt()).to.equal(mockLatestAuditData.auditedAt);
expect(audit.getAuditResult()).to.deep.equal(mockLatestAuditData.auditResult);
expect(audit.getFullAuditRef()).to.equal(mockLatestAuditData.fullAuditRef);
});

it('calls getSiteByBaseURLWithAuditInfo and assigns all audits when latestOnly is false', async () => {
Expand Down

0 comments on commit 6e4a87e

Please sign in to comment.