Skip to content

Commit

Permalink
feat(frontend): use spreadsheet for elastic tests (#575)
Browse files Browse the repository at this point in the history
* feat(frontend): copy to clipboard document slug

* feat(api): use spreadsheet data for elastic test script

* feat(api): add eleastic tests script option param

* test(api): update tests

* test(frontend): add test for clipboard hoc

* doc(api): update readme

* fix: rename routeBySource in clipboardHoc
  • Loading branch information
lionelB authored Mar 8, 2019
1 parent 4b8ad48 commit d343e98
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 208 deletions.
21 changes: 21 additions & 0 deletions packages/code-du-travail-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,24 @@ docker-compose up -d
```shell
http://localhost:1337/api/v1/search?q=incapacité%20travail
```

## Tester les requêtes.

Un script permet de tester la non régréssion du moteur de recherche lorsqu'on modifie la configuration d'Elasticsearch ou le contenu des requetes que l'on fait.

Pour cela on se base sur un [fichier en ligne](https://docs.google.com/spreadsheets/d/e/2PACX-1vTBTINraToLx_LgkQtfr0BjGVm3VZ332vYsZsZ0Cc6TB3p5ukaPQ95yOod5Pzcr1R1amf26DJrm5b-Y/pubhtml?gid=0&single=true) qui regroupe l'ensemble de tests.

Chaque document est identifié par un clé contenant `/${source}/${slug}`. Afin de récupérer cette information simplement, un click sur la loupe du champ de recherche vous permet d'avoir cette information directement dans le presse-papier. Il ne reste plus qu'à copier l'information dans la colone des documents attendus pour compléter le tests.
![](https://user-images.githubusercontent.com/160320/53883257-c7621f00-4018-11e9-9ea9-c269fb66fd68.png)

Le script génére aussi un rapport succint ainsi qu'un rapport détaillé et met à jour un fichier de résultats.
Pour lancer le script, il est nécessaire d'avoir une instance en local d'elasticsearch ainsi que de l'api.

```
node /packages/code-du-travail-api/scripts/elastic.js
```

Le script accepte les paramètres suivants:

- `-v, --verbose` affiche le rapport détaillé pour chaque tests
- `-u, --update` met à jour le fichier de resultats
Original file line number Diff line number Diff line change
@@ -1,98 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`computeLineScore should return an object with prevScore, score, diffScore properties 1`] = `
exports[`computeLineScore should return an object with prevScore, score, diffScore properties for ["a"] 1`] = `
Object {
"Actual_Rank1": 1,
"Actual_Rank2": "",
"Actual_Rank3": "",
"Actual_Rank4": "",
"Actual_Rank5": "",
"Expected_1": "a.url",
"Expected_2": "",
"Expected_3": "",
"Expected_4": "",
"Expected_5": "",
"countResults": "1/1",
"diffScore": 1,
"found": "1/1",
"prevScore": 0,
"query": "query",
"score": 1,
"url_0": "a.url",
"url_1": "b.url",
"url_2": "c.url",
"url_3": "d.url",
"url_4": "e.url",
}
`;

exports[`computeLineScore should return an object with prevScore, score, diffScore properties 2`] = `
exports[`computeLineScore should return an object with prevScore, score, diffScore properties for ["a,b,c,d,e"] 1`] = `
Object {
"Actual_Rank1": 1,
"Actual_Rank2": 2,
"Actual_Rank3": 3,
"Actual_Rank4": 4,
"Actual_Rank5": 5,
"Expected_1": "a.url",
"Expected_2": "b.url",
"Expected_3": "c.url",
"Expected_4": "d.url",
"Expected_5": "e.url",
"countResults": "5/5",
"diffScore": 1,
"found": "5/5",
"prevScore": 0,
"query": "query",
"score": 1,
"url_0": "a.url",
"url_1": "b.url",
"url_2": "c.url",
"url_3": "d.url",
"url_4": "e.url",
}
`;

exports[`computeLineScore should return an object with prevScore, score, diffScore properties 3`] = `
exports[`computeLineScore should return an object with prevScore, score, diffScore properties for ["e,d,c,b,a"] 1`] = `
Object {
"Actual_Rank1": "",
"Actual_Rank2": "",
"Actual_Rank3": "",
"Actual_Rank4": "",
"Actual_Rank5": "",
"Expected_1": "o.url",
"Expected_2": "z.url",
"Expected_3": "x.url",
"Expected_4": "y.url",
"Expected_5": "u.url",
"countResults": "0/5",
"diffScore": 0,
"diffScore": 0.76,
"found": "5/5",
"prevScore": 0,
"score": 0,
"url_0": "a.url",
"url_1": "b.url",
"url_2": "c.url",
"url_3": "d.url",
"url_4": "e.url",
"query": "query",
"score": 0.76,
}
`;

exports[`computeLineScore should return an object with prevScore, score, diffScore properties 4`] = `
exports[`computeLineScore should return an object with prevScore, score, diffScore properties for ["o,z,x,y,u"] 1`] = `
Object {
"Actual_Rank1": 5,
"Actual_Rank2": 4,
"Actual_Rank3": 3,
"Actual_Rank4": 2,
"Actual_Rank5": 1,
"Expected_1": "e.url",
"Expected_2": "d.url",
"Expected_3": "c.url",
"Expected_4": "b.url",
"Expected_5": "a.url",
"countResults": "5/5",
"diffScore": 0.76,
"diffScore": 0,
"found": "0/5",
"prevScore": 0,
"score": 0.76,
"url_0": "a.url",
"url_1": "b.url",
"url_2": "c.url",
"url_3": "d.url",
"url_4": "e.url",
"query": "query",
"score": 0,
}
`;

Expand Down
94 changes: 52 additions & 42 deletions packages/code-du-travail-api/lib/__tests__/lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,52 @@ test.each([
});

test.each([
{
Expected_1: "a.url",
Expected_2: "",
Expected_3: "",
Expected_4: "",
Expected_5: ""
},
{
Expected_1: "a.url",
Expected_2: "b.url",
Expected_3: "c.url",
Expected_4: "d.url",
Expected_5: "e.url"
},
{
Expected_1: "o.url",
Expected_2: "z.url",
Expected_3: "x.url",
Expected_4: "y.url",
Expected_5: "u.url"
},
{
Expected_1: "e.url",
Expected_2: "d.url",
Expected_3: "c.url",
Expected_4: "b.url",
Expected_5: "a.url"
}
[["a"], ["/source/slug-a", "", "", "", ""]],
[
["a,b,c,d,e"],
[
"/source/slug-a",
"/source/slug-b",
"/source/slug-c",
"/source/slug-d",
"/source/slug-e"
]
],
[
["o,z,x,y,u"],
[
"/source/slug-o",
"/source/slug-z",
"/source/slug-x",
"/source/slug-y",
"/source/slug-u"
]
],
[
["e,d,c,b,a"],
[
"/source/slug-e",
"/source/slug-d",
"/source/slug-c",
"/source/slug-b",
"/source/slug-a"
]
]
])(
"computeLineScore should return an object with prevScore, score, diffScore properties",
line => {
"computeLineScore should return an object with prevScore, score, diffScore properties for %p",
(label, expectedResults) => {
const hits = [
{ _source: { url: "a.url" } },
{ _source: { url: "b.url" } },
{ _source: { url: "c.url" } },
{ _source: { url: "d.url" } },
{ _source: { url: "e.url" } }
{ _source: { source: "source", slug: "slug-a" } },
{ _source: { source: "source", slug: "slug-b" } },
{ _source: { source: "source", slug: "slug-c" } },
{ _source: { source: "source", slug: "slug-d" } },
{ _source: { source: "source", slug: "slug-e" } }
];
expect(computeLineScore(line, hits)).toMatchSnapshot();
const query = "query";
const previousResults = {};
expect(
computeLineScore({ query, previousResults, expectedResults, hits })
).toMatchSnapshot();
}
);

Expand All @@ -85,22 +92,25 @@ test("printResultsAbstracts should output absctract", () => {
test("printResultsDetails should output absctract", () => {
const results = [
{
Request: "Fromage",
query: "Fromage",
prevScore: 0.1,
score: 0.2,
diffScore: 0.1
diffScore: 0.1,
found: "1/5"
},
{
Request: "Entrées",
query: "Entrées",
prevScore: 0.5,
score: 0.2,
diffScore: 0.3
diffScore: 0.3,
found: "1/5"
},
{
Request: "Legumes",
query: "Legumes",
prevScore: 0,
score: 1,
diffScore: 1
diffScore: 1,
found: "1/1"
}
];
expect(printResultsDetails(results)).toMatchSnapshot();
Expand Down
53 changes: 23 additions & 30 deletions packages/code-du-travail-api/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const range = require("get-range");

function sum(a, b) {
return a + b;
}
Expand All @@ -21,39 +19,34 @@ function computeScore(expectedResultsUrl, actualResulstUrl) {
);
}

function computeLineScore(line, hits) {
function computeLineScore({
query,
previousResultsData = {},
expectedResults,
hits
}) {
const resultsUrl = hits.map(
result =>
result._source.url || `${result._source.source}/${result._source.slug}`
result => `/${result._source.source}/${result._source.slug}`
);
const expectedValues = [...range(1, 6)].map(i => line[`Expected_${i}`]);

const resultsRank = expectedValues.reduce((state, url, i) => {
const resultsRank = expectedResults.map(url => {
const position = resultsUrl.indexOf(url);
state[`Actual_Rank${i + 1}`] = position > -1 ? position + 1 : "";
return state;
}, {});

const foundResults = Object.values(resultsRank).filter(Boolean);
const foundExpected = expectedValues.filter(Boolean);
const countResults = `${foundResults.length}/${foundExpected.length}`;
return position > -1 ? position + 1 : "";
});

const urlsObj = resultsUrl.reduce((state, url, index) => {
state[`url_${index}`] = url;
return state;
}, {});
const numFound = resultsRank.filter(Boolean).length;
const numExpected = expectedResults.filter(Boolean).length;
const found = `${numFound}/${numExpected}`;

const score = computeScore(expectedValues, resultsUrl);
const prevScore = parseFloat(line.score, 10) || 0;
const score = computeScore(expectedResults, resultsUrl);
const prevScore = parseFloat(previousResultsData.score, 10) || 0;
const diffScore = score - prevScore;
return {
...line,
query,
prevScore,
score,
diffScore,
countResults,
...resultsRank,
...urlsObj
found
};
}

Expand All @@ -77,14 +70,14 @@ function printResultsDetails(results) {
next > prev
? ":chart_with_upwards_trend:"
: next < prev
? ":chart_with_downwards_trend:"
: ":heavy_minus_sign:";
? ":chart_with_downwards_trend:"
: ":heavy_minus_sign:";
const output = results
.map(
({ Request, prevScore, score, diffScore }) =>
`| ${Request} | \` ${percent(prevScore)} <${percent(
score
)}> (${percent(diffScore)})\` | ${emoji(prevScore, score)} |`
({ query, prevScore, score, diffScore }) =>
`| ${query} | \` ${percent(prevScore)} <${percent(score)}> (${percent(
diffScore
)})\` | ${emoji(prevScore, score)} |`
)
.join("\n");
return `| file | Δ | |
Expand Down
3 changes: 2 additions & 1 deletion packages/code-du-travail-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@cdt/data...courrier-type": "^1.8.0",
"@koa/cors": "^2.2.1",
"commander": "^2.19.0",
"dotenv": "^6.0.0",
"elastic-apm-node": "^1.11.0",
"elasticsearch": "^15.0.0",
Expand All @@ -45,7 +46,7 @@
"axios": "^0.18.0",
"csvtojson": "^2.0.8",
"eslint": "^5.13.0",
"get-range": "^1.0.0",
"google-spreadsheets": "^2.0.0",
"json2csv": "^4.3.3",
"nodemon": "^1.18.7",
"p-limit": "^2.1.0",
Expand Down
Loading

0 comments on commit d343e98

Please sign in to comment.