Skip to content
This repository was archived by the owner on Feb 25, 2022. It is now read-only.

Commit ea8c6ff

Browse files
authored
Merge pull request #54 from adobe/excel-bug
test(excel): add unit test for excel
2 parents dd793de + 0c4c256 commit ea8c6ff

File tree

8 files changed

+888
-299
lines changed

8 files changed

+888
-299
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ tmp
77
logs
88
.DS_Store
99
test-results.xml
10+
.env

package-lock.json

Lines changed: 767 additions & 279 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"devDependencies": {
4444
"@adobe/eslint-config-helix": "1.1.0",
4545
"@adobe/helix-ops": "1.11.2",
46+
"@adobe/helix-testutils": "^0.3.1",
4647
"@adobe/openwhisk-action-builder": "2.10.1",
4748
"@semantic-release/changelog": "5.0.1",
4849
"@semantic-release/exec": "5.0.0",
@@ -53,6 +54,7 @@
5354
"codecov": "3.6.5",
5455
"commitizen": "4.1.2",
5556
"cz-conventional-changelog": "3.2.0",
57+
"dotenv": "8.2.0",
5658
"eslint": "7.0.0",
5759
"eslint-plugin-header": "3.0.0",
5860
"eslint-plugin-import": "2.20.2",

src/embed.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ function hasParams(list, params) {
2323
}
2424

2525
function embed(url, params, log) {
26-
const matching = matchers
27-
.filter((candidate) => hasParams(candidate.required, params))
28-
.find((candidate) => candidate.pattern(url));
26+
const candidates = matchers
27+
.filter((candidate) => hasParams(candidate.required, params));
28+
29+
const matching = candidates.find((candidate) => candidate.pattern(url));
2930

3031
if (!url || !matching) {
3132
log.warn(`No matcher found for URL ${url}`);

src/matchers/excel.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,52 @@ async function extract(url, params, log = console) {
3535

3636
const tablesuri = `${worksheetsuri}${worksheetname}/tables/`;
3737
const tables = await client.get(tablesuri);
38-
const tablename = tables.value[0].name;
38+
const body = await (async () => {
39+
if (!tables.value.length) {
40+
log.info(`worksheet ${worksheetname} has no tables: ${tablesuri}, getting range instead`);
3941

40-
const columnsuri = `${tablesuri}${tablename}/columns/`;
41-
const columns = await client.get(columnsuri);
42+
const rangeuri = `${worksheetsuri}${worksheetname}/usedRange`;
43+
const range = await client.get(rangeuri);
4244

43-
const columnnames = columns.value.map(({ name }) => name);
4445

45-
const rowvalues = columns.value[0].values
46-
.map((_, rownum) => columnnames.reduce((row, name, colnum) => {
47-
const [value] = columns.value[colnum].values[rownum];
48-
// eslint-disable-next-line no-param-reassign
49-
row[name] = value;
50-
return row;
51-
}, {}));
46+
const rows = range.values;
47+
const columnames = rows.shift();
5248

53-
// discard the first row
54-
rowvalues.shift();
49+
const rowvalues = rows.map((row) => columnames.reduce((obj, name, index) => {
50+
// eslint-disable-next-line no-param-reassign
51+
obj[name] = row[index];
52+
return obj;
53+
}, {}));
54+
55+
return rowvalues;
56+
}
57+
const tablename = tables.value[0].name;
58+
59+
const columnsuri = `${tablesuri}${tablename}/columns/`;
60+
const columns = await client.get(columnsuri);
61+
62+
const columnnames = columns.value.map(({ name }) => name);
63+
const rowvalues = columns.value[0].values
64+
.map((_, rownum) => columnnames.reduce((row, name, colnum) => {
65+
const [value] = columns.value[colnum].values[rownum];
66+
// eslint-disable-next-line no-param-reassign
67+
row[name] = value;
68+
return row;
69+
}, {}));
70+
71+
// discard the first row
72+
rowvalues.shift();
73+
74+
return rowvalues;
75+
})();
5576

5677
return {
5778
statusCode: 200,
5879
headers: {
5980
'Content-Type': 'application/json',
6081
'Cache-Control': 'max-age=600',
6182
},
62-
body: rowvalues,
83+
body,
6384
};
6485
} catch (e) {
6586
log.error(e.message);
@@ -76,7 +97,7 @@ async function extract(url, params, log = console) {
7697

7798

7899
module.exports = {
79-
required: ['share', 'AZURE_WORD2MD_CLIENT_ID', 'AZURE_HELIX_USER', 'AZURE_HELIX_PASSWORD'],
100+
required: ['AZURE_WORD2MD_CLIENT_ID', 'AZURE_HELIX_USER', 'AZURE_HELIX_PASSWORD'],
80101
pattern: (url) => /^https:\/\/.*\.sharepoint\.com\//.test(url),
81102
extract,
82103
};

test/excel.integration.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
/* eslint-env mocha */
13+
14+
const assert = require('assert');
15+
const { condit } = require('@adobe/helix-testutils');
16+
const { main } = require('../src/index');
17+
18+
require('dotenv').config();
19+
20+
describe('Excel Integration Test', () => {
21+
condit('Retrieves Excel Spreadsheet without tables', condit.hasenv('AZURE_WORD2MD_CLIENT_ID', 'AZURE_HELIX_USER', 'AZURE_HELIX_PASSWORD'), async () => {
22+
const result = await main({
23+
__ow_logger: console,
24+
__ow_path: '/https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx',
25+
share: 'Edoi88tLKLpDsKzSfL-pcJYB2lIo7UKooYWnjm3w2WRrsA',
26+
27+
e: 'tD623x',
28+
AZURE_WORD2MD_CLIENT_ID: process.env.AZURE_WORD2MD_CLIENT_ID,
29+
AZURE_HELIX_USER: process.env.AZURE_HELIX_USER,
30+
AZURE_HELIX_PASSWORD: process.env.AZURE_HELIX_PASSWORD,
31+
});
32+
assert.equal(result.statusCode, 200);
33+
assert.equal(result.body.length, 3);
34+
}).timeout(10000);
35+
36+
condit('Retrieves Excel Spreadsheet with tables', condit.hasenv('AZURE_WORD2MD_CLIENT_ID', 'AZURE_HELIX_USER', 'AZURE_HELIX_PASSWORD'), async () => {
37+
const result = await main({
38+
__ow_logger: console,
39+
__ow_path: '/https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx',
40+
share: 'Edz_l4D0BghJjLkIfyZCB7sBLaBhySyT5An7fPHVS6CFuA',
41+
42+
e: 'e5ziwf',
43+
AZURE_WORD2MD_CLIENT_ID: process.env.AZURE_WORD2MD_CLIENT_ID,
44+
AZURE_HELIX_USER: process.env.AZURE_HELIX_USER,
45+
AZURE_HELIX_PASSWORD: process.env.AZURE_HELIX_PASSWORD,
46+
});
47+
assert.equal(result.statusCode, 200);
48+
assert.equal(result.body.length, 20);
49+
}).timeout(10000);
50+
});

test/excel.test.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/post-deploy.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,46 @@ describe('Post-Deploy Tests', () => {
4545
});
4646
}).timeout(10000);
4747

48-
it('Excel Embed', async () => {
48+
it('Excel Embed (without tables)', async () => {
4949
console.log('Trying', 'https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx?share=Edoi88tLKLpDsKzSfL-pcJYB2lIo7UKooYWnjm3w2WRrsA&email=helix%40adobe.com&e=tD623x');
5050

5151
await chai
5252
.request('https://adobeioruntime.net/')
5353
.get(`${getbaseurl()}/https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx?share=Edoi88tLKLpDsKzSfL-pcJYB2lIo7UKooYWnjm3w2WRrsA&email=helix%40adobe.com&e=tD623x`)
5454
.then((response) => {
5555
// console.log(response.body);
56+
expect(response).to.have.status(200);
5657
expect(response).to.be.json;
5758
expect(response.body).to.be.an('array').that.deep.includes({
5859
project: 'Helix',
5960
created: 2018,
6061
});
62+
}).catch((e) => {
63+
throw e;
64+
});
65+
}).timeout(10000);
66+
67+
it('Excel Embed (with tables)', async () => {
68+
console.log('Trying', 'https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx?share=Edz_l4D0BghJjLkIfyZCB7sBLaBhySyT5An7fPHVS6CFuA&email=helix%40adobe.com&e=e5ziwf');
69+
70+
await chai
71+
.request('https://adobeioruntime.net/')
72+
.get(`${getbaseurl()}/https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx?share=Edz_l4D0BghJjLkIfyZCB7sBLaBhySyT5An7fPHVS6CFuA&email=helix%40adobe.com&e=e5ziwf`)
73+
.then((response) => {
74+
// console.log(response.body);
6175
expect(response).to.have.status(200);
76+
expect(response).to.be.json;
77+
expect(response.body).to.be.an('array').that.deep.includes({
78+
Column1: 'Klapptüren',
79+
Gesamtkosten: 28976,
80+
Hersteller: 'Hyundai',
81+
Kofferraum: 304,
82+
Modell: 'Trajet',
83+
Preis: 23000,
84+
Preis2: 1.1,
85+
Verbrauch: 7.2,
86+
'Verbrauch pro Jahr': 5976,
87+
});
6288
}).catch((e) => {
6389
throw e;
6490
});

0 commit comments

Comments
 (0)