Skip to content

Commit

Permalink
fix: removeSite, add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Dec 1, 2023
1 parent 5c24998 commit 5d0b00f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,12 @@ export const addAudit = async (dynamoClient, log, auditData) => {
return audit;
};

async function removeAudits(dynamoClient, audits) {
async function removeAudits(dynamoClient, audits, latest = false) {
const tableName = latest ? TABLE_NAME_LATEST_AUDITS : TABLE_NAME_AUDITS;
// TODO: use batch-remove (needs dynamo client update)
const removeAuditPromises = audits.map((audit) => dynamoClient.removeItem({
TableName: TABLE_NAME_AUDITS,
Key: {
siteId: audit.getSiteId(),
auditedAt: audit.getAuditedAt(),
},
const removeAuditPromises = audits.map((audit) => dynamoClient.removeItem(tableName, {
siteId: audit.getSiteId(),
SK: `${audit.getAuditType()}#${audit.getAuditedAt()}`,
}));

await Promise.all(removeAuditPromises);
Expand All @@ -216,7 +214,7 @@ export const removeAuditsForSite = async (dynamoClient, log, siteId) => {
const latestAudits = await getLatestAuditsForSite(dynamoClient, log, siteId);

await removeAudits(dynamoClient, audits);
await removeAudits(dynamoClient, latestAudits);
await removeAudits(dynamoClient, latestAudits, true);
} catch (error) {
log.error(`Error removing audits for site ${siteId}: ${error.message}`);
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export const removeSite = async (dynamoClient, log, siteId) => {
try {
await removeAuditsForSite(dynamoClient, log, siteId);

await dynamoClient.removeItem(TABLE_NAME_SITES, { siteId });
await dynamoClient.removeItem(TABLE_NAME_SITES, { id: siteId });
} catch (error) {
log.error(`Error removing site: ${error.message}`);
throw error;
Expand Down
16 changes: 16 additions & 0 deletions packages/spacecat-shared-data-access/test-it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,20 @@ describe('DynamoDB Integration Test', async () => {
// Try to add the same audit again
await expect(dataAccess.addAudit(auditData)).to.be.rejectedWith('Audit already exists');
});

it('successfully removes a site and its related audits', async () => {
const siteToRemove = await dataAccess.getSiteByBaseURL('https://example1.com');
const siteId = siteToRemove.getId();

await expect(dataAccess.removeSite(siteId)).to.eventually.be.fulfilled;

const siteAfterRemoval = await dataAccess.getSiteByBaseURL('https://example1.com');
expect(siteAfterRemoval).to.be.null;

const auditsAfterRemoval = await dataAccess.getAuditsForSite(siteId);
expect(auditsAfterRemoval).to.be.an('array').that.is.empty;

const latestAuditAfterRemoval = await dataAccess.getLatestAuditForSite(siteId, AUDIT_TYPE_LHS);
expect(latestAuditAfterRemoval).to.be.null;
});
});

0 comments on commit 5d0b00f

Please sign in to comment.