Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Dec 17, 2024
1 parent 652355c commit 5dbb046
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 181 deletions.
49 changes: 5 additions & 44 deletions src/middleware/overview.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ const fetchEntityCounts = fetchOneFromAllDatasets({
result: 'entityCounts'
})

/**
* For the purpose of displaying single status label on (possibly) many issues,
* we want issues with 'worse' status to be weighted higher.
*/
const statusOrdering = new Map(['Live', 'Needs fixing', 'Error', 'Not submitted'].map((status, i) => [status, i]))

/**
* Calculates overall "health" of the datasets (not)provided by an organisation.
*
Expand Down Expand Up @@ -78,6 +72,11 @@ export const datasetSubmissionDeadlineCheck = (req, res, next) => {
const { resources } = req
const currentDate = new Date()

if (!resources) {
const error = new Error('datasetSubmissionDeadlineCheck requires resources')
next(error)
}

req.noticeFlags = requiredDatasets.map(dataset => {
const datasetResources = resources[dataset.dataset]
let resource = datasetResources?.find(resource => resource.dataset === dataset.dataset)
Expand Down Expand Up @@ -188,44 +187,6 @@ export const addNoticesToDatasets = (req, res, next) => {
next()
}

/**
* The overview data can contain multiple rows per dataset,
* and we want a collection of one item per dataset,
* because that's how we display it on the page.
*
* @param {object[]} lpaOverview
* @returns {object[]}
*/
export function aggregateOverviewData (req, res, next) {
const { lpaOverview } = req
if (!Array.isArray(lpaOverview)) {
throw new Error('lpaOverview should be an array')
}
const grouped = _.groupBy(lpaOverview, 'dataset')
const datasets = []
for (const [dataset, rows] of Object.entries(grouped)) {
let numIssues = 0
for (const row of rows) {
if (row.status !== 'Needs fixing') {
continue
}
if (row.issue_count) {
const numFields = (row.fields ?? '').split(',').length
if (row.issue_count >= row.entity_count) numIssues += numFields
else numIssues += row.issue_count
}
}
const info = {
dataset,
issue_count: numIssues,
endpoint: rows[0].endpoint,
error: rows[0].error,
status: _.maxBy(rows, row => statusOrdering.get(row.status)).status
}
datasets.push(info)
}
}

export function prepareDatasetObjects (req, res, next) {
const { issues, endpoints } = req

Expand Down
5 changes: 3 additions & 2 deletions test/unit/middleware/datasetOverview.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ describe('Dataset Overview Middleware', () => {
datasetSpecification: { fields: [{ field: 'field1' }, { field: 'field2' }] },
columnSummary: [{ mapping_field: 'field1', non_mapping_field: 'field3' }],
entityCount: { entity_count: 10 },
sources: [
resources: [
{ endpoint_url: 'endpoint1', documentation_url: 'doc-url1', status: '200', endpoint_entry_date: 'LU1', latest_log_entry_date: 'LA1', resource_start_date: '2023-01-01' },
{ endpoint_url: 'endpoint2', documentation_url: 'doc-url2', status: '404', exception: 'exception', endpoint_entry_date: 'LU2', latest_log_entry_date: 'LA2', resource_start_date: '2023-01-02' }
],
issues: [
entryIssueCounts: [
{
issue: 'Example issue 1',
issue_type: 'Example issue type 1',
Expand All @@ -38,6 +38,7 @@ describe('Dataset Overview Middleware', () => {
status: 'Error'
}
],
entityIssueCounts: [],
notice: undefined
}
prepareDatasetOverviewTemplateParams(reqWithResults, res, () => {})
Expand Down
3 changes: 2 additions & 1 deletion test/unit/middleware/dataview.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ describe('dataview.middleware.test.js', () => {
orgInfo: { name: 'Mock Org' },
dataset: { name: 'Mock Dataset' },
tableParams: { columns: ['foo'], fields: ['foo'] },
issues: [],
entityIssueCounts: [],
entryIssueCounts: [],
pagination: {},
entityCount: { count: 1 },
offset: 0,
Expand Down
178 changes: 44 additions & 134 deletions test/unit/middleware/overview.middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, vi, expect, beforeEach, afterEach } from 'vitest'
import { addNoticesToDatasets, aggregateOverviewData, datasetSubmissionDeadlineCheck, getOverview, prepareOverviewTemplateParams } from '../../../src/middleware/overview.middleware'
import { addNoticesToDatasets, datasetSubmissionDeadlineCheck, getOverview, prepareOverviewTemplateParams } from '../../../src/middleware/overview.middleware'
import { setupNunjucks } from '../../../src/serverSetup/nunjucks.js'
import jsdom from 'jsdom'

Expand All @@ -26,29 +26,29 @@ const reqTemplate = {
datasets: [
{
dataset: 'dataset1',
issue_count: 0,
endpoint: 'https://example.com',
issueCount: 0,
endpointCount: 1,
error: undefined,
status: 'Live'
},
{
dataset: 'dataset2',
issue_count: 0,
endpoint: null,
issueCount: 0,
endpointCount: 0,
error: undefined,
status: 'Needs fixing'
},
{
dataset: 'dataset3',
issue_count: 0,
endpoint: 'https://example.com',
issueCount: 0,
endpointCount: 1,
error: undefined,
status: 'Error'
},
{
dataset: 'dataset4',
issue_count: 0,
endpoint: null,
issueCount: 0,
endpointCount: 0,
error: 'There was a 404 error',
status: 'Error'
}
Expand Down Expand Up @@ -82,12 +82,12 @@ describe('overview.middleware', () => {
organisation: { name: 'Example LPA', organisation: 'LPA' },
datasets: {
statutory: expect.arrayContaining([
{ endpoint: 'https://example.com', status: 'Live', dataset: 'dataset1', error: undefined, issue_count: 0, project: 'open-digital-planning' },
{ endpoint: 'https://example.com', status: 'Error', dataset: 'dataset3', error: undefined, issue_count: 0, project: 'open-digital-planning' }
{ endpointCount: 1, status: 'Live', dataset: 'dataset1', error: undefined, issueCount: 0 },
{ endpointCount: 1, status: 'Error', dataset: 'dataset3', error: undefined, issueCount: 0 }
]),
other: expect.arrayContaining([
{ endpoint: null, status: 'Needs fixing', dataset: 'dataset2', error: undefined, issue_count: 0, project: 'open-digital-planning' },
{ endpoint: null, status: 'Error', dataset: 'dataset4', error: 'There was a 404 error', issue_count: 0, project: 'open-digital-planning' }
{ endpointCount: 0, status: 'Needs fixing', dataset: 'dataset2', error: undefined, issueCount: 0 },
{ endpointCount: 0, status: 'Error', dataset: 'dataset4', error: 'There was a 404 error', issueCount: 0 }
])
},
totalDatasets: 4,
Expand All @@ -102,101 +102,6 @@ describe('overview.middleware', () => {
expect(errorCardNodes[0].querySelector('.govuk-task-list__hint').textContent.trim()).toBe('There was an error accessing the data URL')
expect(errorCardNodes[1].querySelector('.govuk-task-list__hint').textContent.trim()).toBe('There was a 404 error')
})

it('should patch dataset status based on the provision_summary info', () => {
const req = structuredClone(reqTemplate)
console.assert(req.datasets[0].status === 'Live')
req.datasetErrorStatus = [{ dataset: 'dataset1' }]
const res = { render: vi.fn() }

prepareOverviewTemplateParams(req, res, () => {})

const ds1 = req.templateParams.datasets.statutory[0]
expect(ds1.status).toBe('Error')
expect(ds1.error).toBeUndefined()

const ds4 = req.templateParams.datasets.other[1]
expect(ds4.status).toBe('Error')
expect(ds4.error).toBe(req.datasets[3].error) // Error message should be left untouched
})
})

describe('aggregateOverviewData middleware', () => {
const req = { lpaOverview: [] }
const res = {}
const next = vi.fn()

beforeEach(() => {
vi.resetAllMocks()
})

it('should set req.datasets to just the required datasets when input is empty', async () => {
aggregateOverviewData(req, res, next)
expect(req.datasets).toEqual([
{ status: 'Not submitted', dataset: 'brownfield-land' }
])
})

it('should count issues correctly', async () => {
const exampleData = [
{ endpoint: 'https://example.com', status: 'Live', dataset: 'dataset1', entity_count: 11 },
{ endpoint: null, status: 'Error', dataset: 'dataset2', entity_count: 12, issue_count: 12 },
{ endpoint: 'https://example.com/3', status: 'Needs fixing', dataset: 'dataset3', entity_count: 5, issue_count: 5, fields: 'foo' },
{ endpoint: 'https://example.com/3', status: 'Needs fixing', dataset: 'dataset3', entity_count: 5, issue_count: 2, fields: 'bar' }
]

req.lpaOverview = exampleData

aggregateOverviewData(req, res, next)

expect(req.datasets).toEqual([
{ endpoint: 'https://example.com', status: 'Live', dataset: 'dataset1', error: undefined, issue_count: 0 },
{ endpoint: null, status: 'Error', dataset: 'dataset2', error: undefined, issue_count: 0 },
{ endpoint: 'https://example.com/3', status: 'Needs fixing', dataset: 'dataset3', error: undefined, issue_count: 3 },
{ dataset: 'brownfield-land', status: 'Not submitted' }
])
})

it('should ensure dataset issues get to the surface', async () => {
const exampleData = [
{ endpoint: 'https://example.com', status: 'Live', dataset: 'dataset1', entity_count: 11 },
{ endpoint: null, status: 'Error', dataset: 'dataset1', entity_count: 12, issue_count: 12 },
{ endpoint: 'https://example.com/2', status: 'Live', dataset: 'dataset2', entity_count: 5, issue_count: 5, fields: 'foo' },
{ endpoint: 'https://example.com/2', status: 'Needs fixing', dataset: 'dataset2', entity_count: 5, issue_count: 2, fields: 'bar' }
]

req.lpaOverview = exampleData

aggregateOverviewData(req, res, next)

expect(req.datasets[0].status).toBe('Error')
expect(req.datasets[1].status).toBe('Needs fixing')
})

it('should handle multiple fields', async () => {
const exampleData = [
{ endpoint: 'https://example.com/2', status: 'Needs fixing', dataset: 'dataset1', entity_count: 5, issue_count: 5, fields: 'foo,bar' },
{ endpoint: 'https://example.com/2', status: 'Needs fixing', dataset: 'dataset2', entity_count: 5, issue_count: 2, fields: 'baz,qux' }
]

req.lpaOverview = exampleData

aggregateOverviewData(req, res, next)

expect(req.datasets[0].status).toBe('Needs fixing')
expect(req.datasets[0].issue_count).toBe(2) // 2 columns affected
expect(req.datasets[1].status).toBe('Needs fixing')
expect(req.datasets[1].issue_count).toBe(2) // 2 rows affected (in the same two fields)
})

it('should\'t add a required dataset if it is already present', async () => {
const exampleData = [
{ endpoint: 'https://example.com/2', status: 'Needs fixing', dataset: 'brownfield-land', entity_count: 5, issue_count: 5, fields: 'foo,bar' }
]
req.lpaOverview = exampleData
aggregateOverviewData(req, res, next)
expect(req.datasets.length).toEqual(1)
})
})

describe('getOverview', () => {
Expand All @@ -209,12 +114,12 @@ describe('overview.middleware', () => {
organisation: { name: 'Example LPA', organisation: 'LPA' },
datasets: {
statutory: [
{ endpoint: 'https://example.com', status: 'Live', dataset: 'statutory1' }
{ endpointCount: 1, status: 'Live', dataset: 'statutory1' }
],
other: [
{ endpoint: 'https://example.com', status: 'Live', dataset: 'dataset1' },
{ endpoint: null, status: 'Needs fixing', dataset: 'dataset2' },
{ endpoint: 'https://example.com', status: 'Error', dataset: 'dataset3' }
{ endpointCount: 1, status: 'Live', dataset: 'dataset1' },
{ endpointCount: 1, status: 'Needs fixing', dataset: 'dataset2' },
{ endpointCount: 1, status: 'Error', dataset: 'dataset3' }
]
},
totalDatasets: 3,
Expand All @@ -232,7 +137,7 @@ describe('overview.middleware', () => {

describe('datasetSubmissionDeadlineCheck', () => {
const req = {
resourceLookup: []
resources: []
}
const res = {}
const next = vi.fn()
Expand All @@ -246,12 +151,14 @@ describe('overview.middleware', () => {
})

it('sets dueNotice flag if they are in the notice period and they haven\'t submitted this year', async () => {
req.resourceLookup = [
{
dataset: 'brownfield-land',
startDate: '1995-03-17T10:00:00.000z'
}
]
req.resources = {
'brownfield-land': [
{
dataset: 'brownfield-land',
startDate: '1995-03-17T10:00:00.000z'
}
]
}

vi.setSystemTime(new Date('1996-03-03T00:00:00.000Z'))

Expand All @@ -262,13 +169,14 @@ describe('overview.middleware', () => {
})

it('sets overdueNotice flag if they haven\'t submitted for last year', async () => {
req.resourceLookup = [
{
dataset: 'brownfield-land',
startDate: '1994-03-17T10:00:00.000z'
}
]

req.resources = {
'brownfield-land': [
{
dataset: 'brownfield-land',
startDate: '1994-03-17T10:00:00.000z'
}
]
}
vi.setSystemTime(new Date('1995-12-03T00:00:00.000Z'))

await datasetSubmissionDeadlineCheck(req, res, next)
Expand All @@ -278,12 +186,14 @@ describe('overview.middleware', () => {
})

it('doesn\'t set any flag if they have submitted this year and we are in the notice period', async () => {
req.resourceLookup = [
{
dataset: 'brownfield-land',
startDate: '1996-03-17T10:00:00.000z'
}
]
req.resources = {
'brownfield-land': [
{
dataset: 'brownfield-land',
startDate: '1996-03-17T10:00:00.000z'
}
]
}

vi.setSystemTime(new Date('1996-03-03T00:00:00.000Z'))

Expand All @@ -294,7 +204,7 @@ describe('overview.middleware', () => {
})

it('sets dueNotice flag if they haven\'t ever submitted and we are in the notice period', async () => {
req.resourceLookup = []
req.resources = []

vi.setSystemTime(new Date('1996-03-03T00:00:00.000Z'))

Expand All @@ -305,7 +215,7 @@ describe('overview.middleware', () => {
})

it('sets overdueNotice flag if they haven\'t ever submitted and we are not in the notice period', async () => {
req.resourceLookup = []
req.resources = []

vi.setSystemTime(new Date('1995-11-03T00:00:00.000Z'))

Expand Down

0 comments on commit 5dbb046

Please sign in to comment.