Skip to content

Commit

Permalink
add missing tests (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-adams authored Oct 10, 2024
1 parent b4d42df commit 9cf016d
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getS3Client, sendPutObjectToS3Bucket } from 'src/lib/aws'
import { db, getPrismaClient } from 'src/lib/db'
import { logger } from 'src/lib/logger'

enum Severity {
export enum Severity {
Error = 'ERR',
Warning = 'WARN',
Info = 'INFO',
Expand Down
77 changes: 67 additions & 10 deletions api/src/services/uploads/uploads.scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {

import type { ScenarioData } from '@redwoodjs/testing/api'

import { Severity } from 'src/functions/processValidationJson/processValidationJson'

export const standard = defineScenario<
| Prisma.OrganizationCreateArgs
| Prisma.AgencyCreateArgs
Expand Down Expand Up @@ -154,13 +156,13 @@ export const standard = defineScenario<
create: [
{
passed: true,
results: '{error:false}',
results: { errors: [] },
createdAt: '2024-01-26T15:11:27.000Z',
initiatedById: scenario.user.one.id,
},
{
passed: true,
results: '{error:false}',
results: { errors: [] },
createdAt: '2024-01-27T10:32:00.000Z',
initiatedById: scenario.user.one.id,
},
Expand All @@ -181,13 +183,13 @@ export const standard = defineScenario<
create: [
{
passed: true,
results: '{error:false}',
results: { errors: [] },
initiatedById: scenario.user.two.id,
createdAt: '2024-01-29T18:13:25.000Z',
},
{
passed: true,
results: '{error:false}',
results: { errors: [] },
initiatedById: scenario.user.two.id,
createdAt: '2024-01-29T17:10:22.000Z',
},
Expand All @@ -208,7 +210,7 @@ export const standard = defineScenario<
create: [
{
passed: true,
results: '{error:false}',
results: { errors: [] },
initiatedById: scenario.user.two.id,
createdAt: '2024-01-29T18:13:25.000Z',
},
Expand All @@ -227,9 +229,26 @@ export const standard = defineScenario<
expenditureCategoryId: scenario.expenditureCategory.two.id,
validations: {
create: [
{
passed: false,
isManual: true,
results: {
errors: [
{
message: `Manually invalidated by User...`,
tab: 'N/A',
row: 'N/A',
col: 'N/A',
severity: Severity.Error,
},
],
},
initiatedById: scenario.user.two.id,
createdAt: '2024-01-30T18:13:25.000Z',
},
{
passed: true,
results: '{error:false}',
results: { errors: [] },
initiatedById: scenario.user.two.id,
createdAt: '2024-01-29T18:13:25.000Z',
},
Expand All @@ -239,6 +258,36 @@ export const standard = defineScenario<
updatedAt: '2024-01-21T18:10:17.000Z',
},
}),
five: (scenario) => ({
data: {
filename: 'InvalidUpload.xls',
uploadedById: scenario.user.four.id,
agencyId: scenario.agency.two.id,
reportingPeriodId: scenario.reportingPeriod.one.id,
expenditureCategoryId: scenario.expenditureCategory.two.id,
validations: {
create: [
{
passed: false,
results: {
errors: [
{
message: `Value is required for Project__c`,
tab: 'Project',
row: 13,
col: 'G',
severity: Severity.Error,
},
],
},
initiatedById: scenario.user.two.id,
},
],
},
createdAt: '2024-01-21T18:10:17.000Z',
updatedAt: '2024-01-21T18:10:17.000Z',
},
}),
},
})

Expand Down Expand Up @@ -442,13 +491,17 @@ export const uploadCheck = defineScenario<
create: [
{
passed: true,
results: '{error:false}',
results: {
errors: [],
},
createdAt: '2024-01-26T15:11:27.000Z',
initiatedById: scenario.user.one.id,
},
{
passed: true,
results: '{error:false}',
results: {
errors: [],
},
createdAt: '2024-01-27T10:32:00.000Z',
initiatedById: scenario.user.one.id,
},
Expand All @@ -469,13 +522,17 @@ export const uploadCheck = defineScenario<
create: [
{
passed: true,
results: '{error:false}',
results: {
errors: [],
},
createdAt: '2024-01-26T15:11:27.000Z',
initiatedById: scenario.user.one.id,
},
{
passed: true,
results: '{error:false}',
results: {
errors: [],
},
createdAt: '2024-01-27T10:32:00.000Z',
initiatedById: scenario.user.one.id,
},
Expand Down
113 changes: 106 additions & 7 deletions api/src/services/uploads/uploads.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Upload } from '@prisma/client'
import { Upload } from '@prisma/client'

import {
deleteUploadFile,
Expand All @@ -10,6 +10,7 @@ import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'

import { updateOrganization } from '../organizations/organizations'
import { createUploadValidation } from '../uploadValidations/uploadValidations'

import {
uploads,
Expand All @@ -20,6 +21,7 @@ import {
downloadUploadFile,
Upload as UploadRelationResolver,
getUploadsByExpenditureCategory,
getValidUploadsInCurrentPeriod,
sendTreasuryReport,
SubrecipientLambdaPayload,
ProjectLambdaPayload,
Expand Down Expand Up @@ -102,18 +104,23 @@ describe('uploads', () => {
'returns all uploads in their agency for organization staff',
async (scenario: StandardScenario) => {
mockCurrentUser(scenario.user.three)
const result = await uploads()
const result: Upload[] = await uploads()

const uploadsBelongToOrg = await uploadsBelongToOrganization(
result,
scenario.organization.two.id
)
const uploadsBelongToAgency = result.every(async (upload) => {
return upload.agencyId === scenario.user.three.agencyId
})
expect(uploadsBelongToOrg).toBe(true)

expect(result.length).toEqual(2)
const uploadsBelongToAgency = result.every(
(upload) => upload.agencyId === scenario.user.three.agencyId
)
expect(uploadsBelongToAgency).toEqual(true)
expect(uploadsBelongToOrg).toBe(true)

const expectedUploads = Object.values(scenario.upload).filter(
(upload) => upload.agencyId === scenario.user.three.agencyId
)
expect(result.length).toEqual(expectedUploads.length)
}
)

Expand Down Expand Up @@ -271,6 +278,98 @@ describeScenario<StandardScenario>(
})
}
)
describe('getValidUploadsInCurrentPeriod', () => {
scenario(
'When an upload was first validated and subsequently invalidated then it should be ignored.',
async (scenario: StandardScenario) => {
mockCurrentUser(scenario.user.three)
const uploads = await getValidUploadsInCurrentPeriod(
scenario.organization.two,
scenario.reportingPeriod.one
)

expect(uploads).toHaveLength(1)
expect(uploads.map((upload) => upload.id)).not.toContain(
scenario.upload.four.id
)
}
)

scenario(
'When an upload was first invalid and subsequently became valid then it should be included.',
async (scenario: StandardScenario) => {
mockCurrentUser(scenario.user.three)
await createUploadValidation({
input: {
uploadId: scenario.upload.four.id,
passed: true,
results: { errors: [] },
initiatedById: scenario.user.three.id,
},
})

const uploads = await getValidUploadsInCurrentPeriod(
scenario.organization.two,
scenario.reportingPeriod.one
)

expect(uploads).toHaveLength(2)
expect(uploads.map((upload) => upload.id)).toContain(
scenario.upload.four.id
)
}
)

scenario(
'When an upload has multiple valid UploadValidation records it should be included.',
async (scenario: StandardScenario) => {
mockCurrentUser(scenario.user.three)

// Find one with multiple passed validations
const upload = scenario.upload.one

const uploads = await getValidUploadsInCurrentPeriod(
scenario.organization.one,
upload.reportingPeriodId
)

expect(uploads.map((upload) => upload.id)).toContain(upload.id)
}
)

scenario(
'When an upload only has a single valid UploadValidation record it should be included.',
async (scenario: StandardScenario) => {
mockCurrentUser(scenario.user.three)

// Find one with single valid validation
const upload = scenario.upload.three

const uploads = await getValidUploadsInCurrentPeriod(
scenario.organization.two,
upload.reportingPeriodId
)

expect(uploads.map((upload) => upload.id)).toContain(upload.id)
}
)
scenario(
'When an upload only has a single invalid UploadValidation record it should be ignored.',
async (scenario: StandardScenario) => {
mockCurrentUser(scenario.user.three)

// Find one with single invalid validation
const upload = scenario.upload.five

const uploads = await getValidUploadsInCurrentPeriod(
scenario.organization.two,
upload.reportingPeriodId
)

expect(uploads.map((upload) => upload.id)).not.toContain(upload.id)
}
)
})

describe('treasury report', () => {
beforeEach(() => {
Expand Down
14 changes: 6 additions & 8 deletions api/src/services/uploads/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,12 @@ export const getValidUploadsInCurrentPeriod = async (
})

/* Step 2: Filter out uploads whose latest validation is not passed */
const validUploadsInPeriod = uploadsInPeriod
.filter((upload) => {
const latestValidation = upload.validations.reduce((latest, current) =>
current.createdAt > latest.createdAt ? current : latest
)
return latestValidation.passed
})
.map((upload) => upload)
const validUploadsInPeriod = uploadsInPeriod.filter((upload) => {
const latestValidation = upload.validations.reduce((latest, current) =>
current.createdAt > latest.createdAt ? current : latest
)
return latestValidation.passed
})

return validUploadsInPeriod
}
Expand Down

0 comments on commit 9cf016d

Please sign in to comment.