Skip to content

Commit

Permalink
Merge pull request #222 from digital-land/feat/stepByStep/correctData
Browse files Browse the repository at this point in the history
Feat/step by step/correct data
  • Loading branch information
GeorgeGoodall authored Aug 6, 2024
2 parents 370c902 + 7057822 commit d407173
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
53 changes: 19 additions & 34 deletions src/controllers/OrganisationsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,27 @@ const organisationsController = {
},

async getGetStarted (req, res, next) {
const params = {
organisation: {
organisation: 'local-authority:ADU',
name: 'Adur District Council',
dataset: 'local-authority'
},
dataset: {
attribution: 'historic-england',
collection: 'historic-england',
consideration: 'world-heritage-sites',
dataset: 'world-heritage-site-buffer-zone',
description: '',
end_date: '',
entry_date: '',
github_discussion: '',
key_field: '',
entity_minimum: 16110000,
entity_maximum: 16129999,
licence: 'ogl3',
name: 'World heritage site buffer zone',
paint_options: '{ "colour": "#EB1EE5", "opacity": 0.2 }',
plural: 'World heritage site buffer zones',
phase: 'beta',
prefix: '',
realm: 'dataset',
replacement_dataset: '',
start_date: '',
text: 'A [World Heritage Site](/dataset/world-heritage-site) may have a [buffer zone](https://whc.unesco.org/en/series/25/) with implications for planning.',
typology: 'geography',
version: '1.0',
wikidata: 'Q9259',
wikipedia: 'World_Heritage_Site'
try {
// get the organisation name
const lpa = req.params.lpa
const organisationResult = await datasette.runQuery(`SELECT name FROM organisation WHERE organisation = '${lpa}'`)
const organisation = organisationResult.formattedData[0]

// get the dataset name
const datasetId = req.params.dataset
const datasetResult = await datasette.runQuery(`SELECT name FROM dataset WHERE dataset = '${datasetId}'`)
const dataset = datasetResult.formattedData[0]

const params = {
organisation,
dataset
}
}

res.render('organisations/get-started.html', params)
res.render('organisations/get-started.html', params)
} catch (err) {
logger.error(err)
next(err)
}
}

}
Expand Down
53 changes: 50 additions & 3 deletions test/unit/organisationsController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ vi.mock('../../src/utils/utils.js', () => {
dataSubjects: {}
}
})
vi.mock('../../src/services/datasette.js')

vi.mock('../../src/services/datasette.js', () => ({
default: {
runQuery: vi.fn()
}
}))

describe('OrganisationsController.js', () => {
beforeEach(() => {
Expand Down Expand Up @@ -140,8 +145,50 @@ describe('OrganisationsController.js', () => {
})

describe('get-started', () => {
it.todo('should render the get-started template with the correct params')
it('should render the get-started template with the correct params', async () => {
const req = { params: { lpa: 'example-lpa', dataset: 'example-dataset' } }
const res = { render: vi.fn() }
const next = vi.fn()

datasette.runQuery.mockImplementation((query) => {
if (query.includes('example-lpa')) {
return {
formattedData: [
{ name: 'Example LPA' }
]
}
} else if (query.includes('example-dataset')) {
return {
formattedData: [
{ name: 'Example Dataset' }
]
}
}
})

await organisationsController.getGetStarted(req, res, next)

expect(res.render).toHaveBeenCalledTimes(1)
expect(res.render).toHaveBeenCalledWith('organisations/get-started.html', {
organisation: { name: 'Example LPA' },
dataset: { name: 'Example Dataset' }
})
})

it('should catch and pass errors to the next function', async () => {
const req = { params: { lpa: 'example-lpa', dataset: 'example-dataset' } }
const res = { render: vi.fn() }
const next = vi.fn()

it.todo('should catch and pass errors to the next function')
// Mock the datasette.runQuery method to throw an error
datasette.runQuery.mockImplementation(() => {
throw new Error('example error')
})

await organisationsController.getGetStarted(req, res, next)

expect(next).toHaveBeenCalledTimes(1)
expect(next).toHaveBeenCalledWith(expect.any(Error))
})
})
})

0 comments on commit d407173

Please sign in to comment.