Skip to content

Commit

Permalink
Added support for importing parent property.
Browse files Browse the repository at this point in the history
  • Loading branch information
mansoor.sajjad committed Jul 27, 2022
1 parent 6af5516 commit 006a502
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/streams/documentStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ function processRecord(record, next_uid, stats) {

const pelias_document = new peliasModel.Document( source, layer, model_id );

const parent = getCaseInsensitiveAsJSON( `parent_json`, record );
if (parent) {
const parentFields = Object.keys(parent);
parentFields.forEach(parentField => {
const parentElement = parent[parentField];
pelias_document.addParent(parentField, parentElement.name, parentElement.id, parentElement.abbr, parentElement.source)
})
}

getMultiLangNames(record).forEach(([lang, value]) => {
const names = getNames(value, lang === 'default' ? '' : `_${lang}`);
if (names && names.length > 0) {
Expand Down
77 changes: 77 additions & 0 deletions test/streams/documentStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,80 @@ tape( 'documentStream rejects invalid popularity', function(test) {
test.end();
});
});

tape('documentStream accepts parent value if present', function (test) {
const input = {
LAT: 5,
LON: 6,
popularity: '500',
parent_json: {
country: { id: "NOR", name: "NO" }
}
};
const stats = { badRecordCount: 0 };
const documentStream = DocumentStream.create('prefix', stats);

test_stream([input], documentStream, function (err, actual) {
test.equal(actual.length, 1, 'the document should be pushed');
test.equal(stats.badRecordCount, 0, 'bad record count unchanged');
test.end();
});
});

tape('documentStream accepts multiple parent values if present', function (test) {
const input = {
LAT: 5,
LON: 6,
popularity: '500',
parent_json: {
country: { id: "NOR", name: "Norway" },
locality: { id: "0301", name: "Oslo", abbr: "osl", source: "nationalRegistry" }
}
};
const stats = { badRecordCount: 0 };
const documentStream = DocumentStream.create('prefix', stats);

test_stream([input], documentStream, function (err, actual) {
test.equal(actual.length, 1, 'the document should be pushed');
test.equal(stats.badRecordCount, 0, 'bad record count unchanged');
test.end();
});
});

tape('documentStream rejects parent without name property', function (test) {
const input = {
LAT: 5,
LON: 6,
popularity: '500',
parent_json: {
country: { id: "NOR" },
}
};
const stats = { badRecordCount: 0 };
const documentStream = DocumentStream.create('prefix', stats);

test_stream([input], documentStream, function (err, actual) {
test.equal(actual.length, 0, 'the document should be rejected');
test.equal(stats.badRecordCount, 1, 'bad record count unchanged');
test.end();
});
});

tape('documentStream rejects parent without id property', function (test) {
const input = {
LAT: 5,
LON: 6,
popularity: '500',
parent_json: {
country: { name: "NOR" },
}
};
const stats = { badRecordCount: 0 };
const documentStream = DocumentStream.create('prefix', stats);

test_stream([input], documentStream, function (err, actual) {
test.equal(actual.length, 0, 'the document should be rejected');
test.equal(stats.badRecordCount, 1, 'bad record count unchanged');
test.end();
});
});

0 comments on commit 006a502

Please sign in to comment.