Skip to content

Commit

Permalink
fix(model json converted to conflict rewind csv):
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed Aug 28, 2020
1 parent 466369a commit c6603d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion functions/import-conflict-convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function (ctx) {

convertConflictsToRewind.columnNames = columnNames
convertConflictsToRewind.lineToJson = lineToJson
convertConflictsToRewind.jsonToRewind = jsonToRewind

return convertConflictsToRewind
}
Expand Down Expand Up @@ -40,7 +41,6 @@ function columnNames (line) {
} // columnNames

function lineToJson (line, columnNames) {

const columns = csvParse(line)[0].map(s => s.trim())

const json = {}
Expand All @@ -50,3 +50,11 @@ function lineToJson (line, columnNames) {
})
return json
} // lineToJson

function jsonToRewind (json, model) {
const modelName = `${model.namespace}.${model.name}`
const keyString = model.primaryKey.map(k => json[k]).join('_')
const oldValue = JSON.stringify(json)

return `${modelName},${keyString},'${oldValue}','{"action":"conflict"}'`
} // jsonToRewind
17 changes: 16 additions & 1 deletion test/conflicts-to-rewind-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const path = require('path')

describe('transform conflicts file to rewind audit entry', function () {
this.timeout(process.env.TIMEOUT || 5000)
let tymlyService, conflictConvertor
let tymlyService, conflictConvertor, gazetteerModel

before('set up tymly', async () => {
const tymlyServices = await tymly.boot(
Expand All @@ -28,6 +28,7 @@ describe('transform conflicts file to rewind audit entry', function () {
)

tymlyService = tymlyServices.tymly
gazetteerModel = tymlyService.blueprintComponents.models.wmfs_gazetteer
conflictConvertor = tymlyServices.functions.functions.ordnanceSurvey_importConflictConvertor
})

Expand Down Expand Up @@ -63,6 +64,20 @@ describe('transform conflicts file to rewind audit entry', function () {
})
})

it('json to rewind row', () => {
const json = {
uprn: '12345678',
counter: '1',
street_name_1: '1 Trouser Street'
}

const rewind = conflictConvertor.func.jsonToRewind(json, gazetteerModel)

expect(rewind).to.eql(
'wmfs.gazetteer,12345678_1,\'{"uprn":"12345678","counter":"1","street_name_1":"1 Trouser Street"}\',\'{"action":"conflict"}\''
)
})

after('shutdown tymly', async () => {
await tymlyService.shutdown()
})
Expand Down

0 comments on commit c6603d1

Please sign in to comment.