This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move last two title helpers from dist-api (#170)
Add getSenderId and getTsCode title functions from distribution-api
- Loading branch information
Showing
5 changed files
with
220 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getSenderId, productMapping } from "../../lib/titles.js"; | ||
|
||
const allPrintProducts = productMapping.filter((p) => p.title && p.tsCode); | ||
|
||
describe("get complaints sender id", () => { | ||
describe("titles with sender id", () => { | ||
allPrintProducts | ||
.filter((p) => Boolean(p.complaintSenderId)) | ||
.forEach(({ namespace, title }) => { | ||
it(`${namespace}/${title}`, () => { | ||
getSenderId(namespace, title).should.eql(productMapping.find((pm) => pm.title === title).complaintSenderId); | ||
}); | ||
}); | ||
}); | ||
describe("titles without sender id", () => { | ||
allPrintProducts | ||
.filter((p) => !p.complaintSenderId) | ||
.forEach(({ namespace, title }) => { | ||
it(`${namespace}/${title}`, () => { | ||
try { | ||
getSenderId(namespace, title); | ||
} catch (error) { | ||
error.message.should.eql("No senderId for report complaint was found. Update product-mapping config"); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { getTsCode, productMapping } from "../../lib/titles.js"; | ||
|
||
const titleTsCodeMapping = [ | ||
{ | ||
tsCode: "0550", | ||
title: "expressen", | ||
text: "Expressen", | ||
singleTitle: false, | ||
}, | ||
{ | ||
tsCode: "0760", | ||
title: "gt", | ||
text: "GT", | ||
singleTitle: false, | ||
}, | ||
{ | ||
tsCode: "1160", | ||
title: "kvp", | ||
text: "Kvällsposten", | ||
singleTitle: false, | ||
}, | ||
{ | ||
tsCode: "0440", | ||
title: "dn", | ||
text: "DN", | ||
singleTitle: false, | ||
}, | ||
{ | ||
tsCode: "0435", | ||
title: "di", | ||
text: "DI", | ||
singleTitle: true, | ||
}, | ||
{ | ||
tsCode: "0990", | ||
title: "ht", | ||
text: "Hudiksvalls Tidning", | ||
singleTitle: false, | ||
}, | ||
{ | ||
tsCode: "0990", | ||
title: "hudiksvalsstidning", | ||
expectedTitle: "ht", | ||
text: "Hudiksvalls Tidning (wrong spelling)", | ||
singleTitle: false, | ||
}, | ||
]; | ||
|
||
describe("get tsCode test", () => { | ||
describe("get tsCode by title", () => { | ||
for (const s of titleTsCodeMapping) { | ||
describe(s.text, () => { | ||
it(`should find tsCode ${s.tsCode} based on title ${s.title}`, () => { | ||
const tsCode = getTsCode(s.title); | ||
tsCode.should.eql(s.tsCode); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
describe("get title by tsCode", () => { | ||
for (const s of titleTsCodeMapping) { | ||
describe(s.text, () => { | ||
it(`should find title ${s.title} based on tsCode ${s.tsCode}`, () => { | ||
const { title } = productMapping.find((pm) => pm.tsCode === s.tsCode); | ||
title.should.eql(s.expectedTitle || s.title); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
describe("get tsCode for invalid title", () => { | ||
it("should not find a tsCode for some-title", () => { | ||
const tsCode = getTsCode("some-title"); | ||
Boolean(tsCode).should.eql(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { productMapping, productConfig } from "../../lib/titles.js"; | ||
|
||
describe("sanity check productMapping", () => { | ||
describe("check for duplicate productCode/type combinations", () => { | ||
it("should not find any duplicate productCode/type combinations", () => { | ||
const allProductCodeTypeCombos = productMapping | ||
.filter((pm) => pm.productCode) | ||
.map((pm) => pm.type + pm.productCode); | ||
const thereAreDuplicates = hasDuplicates(allProductCodeTypeCombos); | ||
thereAreDuplicates.should.eql(false); | ||
}); | ||
}); | ||
|
||
describe("check for duplicate titles", () => { | ||
it("should not find any duplicate titles", () => { | ||
const allTitles = productMapping.filter((pm) => pm.title).map((pm) => pm.title); | ||
const thereAreDuplicates = hasDuplicates(allTitles); | ||
thereAreDuplicates.should.eql(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("sanity check productConfig", () => { | ||
const flattenedProductConfig = []; | ||
Object.keys(productConfig).map((namespace) => { | ||
flattenedProductConfig.push(...productConfig[namespace]); | ||
}); | ||
|
||
describe("check for duplicate tsCodes", () => { | ||
it("should not find any duplicate tsCodes", () => { | ||
const allTsCodes = flattenedProductConfig.filter((pm) => pm.tsCode).map((pm) => pm.tsCode); | ||
const thereAreDuplicates = hasDuplicates(allTsCodes); | ||
if (thereAreDuplicates) reportDuplicates(flattenedProductConfig, "tsCode"); | ||
thereAreDuplicates.should.eql(false); | ||
}); | ||
}); | ||
|
||
describe("check for duplicate titles", () => { | ||
it("should not find any duplicate titles", () => { | ||
const allTitles = flattenedProductConfig.filter((pm) => pm.title).map((pm) => pm.title); | ||
const thereAreDuplicates = hasDuplicates(allTitles); | ||
if (thereAreDuplicates) reportDuplicates(flattenedProductConfig, "title"); | ||
thereAreDuplicates.should.eql(false); | ||
}); | ||
}); | ||
|
||
describe("check for duplicate short names", () => { | ||
it("should not find any duplicate short names", () => { | ||
const allShortNames = flattenedProductConfig.filter((pm) => pm.shortName).map((pm) => pm.shortName); | ||
const thereAreDuplicates = hasDuplicates(allShortNames); | ||
if (thereAreDuplicates) reportDuplicates(flattenedProductConfig, "shortName"); | ||
thereAreDuplicates.should.eql(false); | ||
}); | ||
}); | ||
|
||
describe("check for duplicate subdirectories", () => { | ||
it("should not find any duplicate subdirectories", () => { | ||
const allSubDirectories = flattenedProductConfig.filter((pm) => pm.subDirectory).map((pm) => pm.subDirectory); | ||
const thereAreDuplicates = hasDuplicates(allSubDirectories); | ||
if (thereAreDuplicates) reportDuplicates(flattenedProductConfig, "subDirectory"); | ||
thereAreDuplicates.should.eql(false); | ||
}); | ||
}); | ||
}); | ||
|
||
function hasDuplicates(arr) { | ||
return new Set(arr).size !== arr.length; | ||
} | ||
|
||
function reportDuplicates(arr, property) { | ||
const propertyMap = {}; | ||
arr | ||
.filter((o) => o[property]) | ||
.map((o) => { | ||
const thisProperty = o[property]; | ||
if (!propertyMap[thisProperty]) propertyMap[thisProperty] = []; | ||
propertyMap[thisProperty].push(o.title || o.shortName || o.productName); | ||
}); | ||
const foundMultiples = []; | ||
Object.keys(propertyMap).forEach((p) => { | ||
if (propertyMap[p].length > 1) { | ||
const multiple = {}; | ||
multiple[p] = propertyMap[p]; | ||
foundMultiples.push(multiple); | ||
} | ||
}); | ||
if (foundMultiples.length) console.log(foundMultiples); // eslint-disable-line no-console | ||
} |