Skip to content

Commit

Permalink
fix: Don't need to pick up primary key from csv header
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed Aug 28, 2020
1 parent c6603d1 commit 93f52eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
24 changes: 4 additions & 20 deletions functions/import-conflict-convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,10 @@ module.exports = function (ctx) {

function columnNames (line) {
const PK_COLUMN_PREFIX = '.'
const columnNames = {
all: [],
pk: []
}
const parsed = csvParse(line.toString())[0].map(s => s.trim().toLowerCase())

for (const name of parsed) {
if (name[0] === PK_COLUMN_PREFIX) {
const trimmed = name.substring(1)
columnNames.all.push(trimmed)
columnNames.pk.push(trimmed)
} else {
columnNames.all.push(name)
}
} // for ...

// if no primary key found, assume first column is pk
if (columnNames.pk.length === 0) {
columnNames.pk.push(columnNames.all[0])
}
const parsed = csvParse(line.toString())[0].map(s => s.trim().toLowerCase())

const columnNames = parsed.map(n => n.startsWith(PK_COLUMN_PREFIX) ? n.substring(1) : n)
return columnNames
} // columnNames

Expand All @@ -45,7 +28,8 @@ function lineToJson (line, columnNames) {

const json = {}
columns.forEach((value, i) => {
const name = columnNames.all[i]
if (!value) return
const name = columnNames[i]
json[name] = value
})
return json
Expand Down
20 changes: 13 additions & 7 deletions test/conflicts-to-rewind-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ const expect = chai.expect

const tymly = require('@wmfs/tymly')
const path = require('path')
const fs = require('fs')
const rimraf = require('rimraf')

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

const conflictsDir = path.join(__dirname, './fixtures/rewind/property/sync/conflicts')
const gazetteerCsv = path.join(conflictsDir, 'gazetteer.csv')
const rewindDir = path.join(conflictsDir, 'inserts')

before('set up tymly', async () => {
const tymlyServices = await tymly.boot(
{
Expand All @@ -32,6 +38,10 @@ describe('transform conflicts file to rewind audit entry', function () {
conflictConvertor = tymlyServices.functions.functions.ordnanceSurvey_importConflictConvertor
})

before('remove output dir', () => {
rimraf.sync(rewindDir)
})

it('convert gazetteer conflicts', () => {
expect(conflictConvertor).to.exist()
})
Expand All @@ -41,16 +51,12 @@ describe('transform conflicts file to rewind audit entry', function () {

const columnNames = conflictConvertor.func.columnNames(headerLine)

expect(columnNames.pk).to.eql(['uprn', 'counter'])
expect(columnNames.all).to.eql(['uprn', 'counter', 'lpi_key', 'street_name_1', 'area_name_1', 'post_town', 'postcode'])
expect(columnNames).to.eql(['uprn', 'counter', 'lpi_key', 'street_name_1', 'area_name_1', 'post_town', 'postcode'])
})

it('line to json', () => {
const columnNames = {
pk: ['uprn', 'counter'],
all: ['uprn', 'counter', 'lpi_key', 'street_name_1', 'area_name_1', 'post_town', 'postcode']
}
const line = '12345678,1,abcdef,1 Trouser Street,Pantaloon Alley,Legville,LG1 2PR'
const columnNames = ['uprn', 'counter', 'lpi_key', 'street_name_1', 'street_name_2', 'area_name_1', 'post_town', 'postcode']
const line = '12345678,1,abcdef,1 Trouser Street,,Pantaloon Alley,Legville,LG1 2PR'

const json = conflictConvertor.func.lineToJson(line, columnNames)
expect(json).to.eql({
Expand Down

0 comments on commit 93f52eb

Please sign in to comment.