From a8dda0226373a2c05edf20a7571b60d1145e3c8f Mon Sep 17 00:00:00 2001 From: Sujay Garlanka Date: Tue, 27 Aug 2019 14:18:11 -0700 Subject: [PATCH] Print all columns in CSV output for heterogeneous collection (#164) * csv changes * simpler code * added test case --- package-lock.json | 4 +- src/box-command.js | 7 ++- test/commands/bulk.test.js | 22 ++++++- .../folders/get_folders_id_items.json | 58 +++++++++++++++++++ .../fixtures/output/bulk_items_output_csv.txt | 6 ++ 5 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/folders/get_folders_id_items.json create mode 100644 test/fixtures/output/bulk_items_output_csv.txt diff --git a/package-lock.json b/package-lock.json index 3eb91bed..e6878b5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3737,7 +3737,6 @@ "resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz", "integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=", "dev": true, - "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -4801,8 +4800,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=", - "dev": true, - "optional": true + "dev": true }, "lru-cache": { "version": "4.1.3", diff --git a/src/box-command.js b/src/box-command.js index d9377913..9807536d 100644 --- a/src/box-command.js +++ b/src/box-command.js @@ -860,7 +860,12 @@ class BoxCommand extends Command { objectArray = [objectArray]; DEBUG.output('Creating tabular output from single object'); } - let keyPaths = this.getNestedKeys(objectArray[0]); + + let keyPaths = []; + for (let object of objectArray) { + keyPaths = _.union(keyPaths, this.getNestedKeys(object)); + } + DEBUG.output('Found %d keys for tabular output', keyPaths.length); formattedData.push(keyPaths); for (let object of objectArray) { diff --git a/test/commands/bulk.test.js b/test/commands/bulk.test.js index 823323eb..cd439069 100644 --- a/test/commands/bulk.test.js +++ b/test/commands/bulk.test.js @@ -808,9 +808,11 @@ describe('Bulk', () => { let inputFilePath = path.join(__dirname, '../fixtures/bulk/bulk_files_tasks_list_input.json'), fixture = getFixture('files/get_files_id_tasks_page_1'), fixture2 = getFixture('files/get_files_id_tasks_page_2'), + fixture3 = getFixture('folders/get_folders_id_items'), jsonCollectionOutput = getFixture('output/bulk_collection_output_json.txt'), tableCollectionOutput = getFixture('output/bulk_collection_output_table.txt'), - csvCollectionOutput = getFixture('output/bulk_collection_output_csv.txt'); + csvCollectionOutput = getFixture('output/bulk_collection_output_csv.txt'), + csvItemsOutput = getFixture('output/bulk_items_output_csv.txt'); test .nock(TEST_API_ROOT, api => api @@ -919,6 +921,24 @@ describe('Bulk', () => { assert.equal(ctx.stdout, csvCollectionOutput); }); + test + .nock(TEST_API_ROOT, api => api + .get('/2.0/folders/0/items') + .query({ usemarker: true }) + .reply(200, fixture3) + ) + .stdout() + .stderr({print: true}) + .command([ + 'folders:items', + '0', + '--csv', + '--token=test' + ]) + .it('should output flattened CSV with union of all fields present in each item when each command run returns an array (CSV Output)', ctx => { + assert.equal(ctx.stdout, csvItemsOutput); + }); + }); }); diff --git a/test/fixtures/folders/get_folders_id_items.json b/test/fixtures/folders/get_folders_id_items.json new file mode 100644 index 00000000..3d2aa740 --- /dev/null +++ b/test/fixtures/folders/get_folders_id_items.json @@ -0,0 +1,58 @@ +{ + "total_count": 5, + "entries": [ + { + "type": "folder", + "id": "1234567", + "sequence_id": "10", + "etag": "10", + "name": "Box" + }, + { + "type": "folder", + "id": "22585432", + "sequence_id": "9", + "etag": "9", + "name": "Human Resources" + }, + { + "type": "folder", + "id": "24432981", + "sequence_id": "8", + "etag": "8", + "name": "Engineering" + }, + { + "type": "file", + "id": "124000", + "file_version": { + "type": "file_version", + "id": "431000", + "sha1": "b7e432rew9bc2850a5780a86a25a8574d1b473f" + }, + "sequence_id": "79", + "etag": "79", + "sha1": "b7e9fsdfe329bc2850a5780a86a25a8574d1b473f", + "name": "Draymond.boxnote" + }, + { + "type": "web_link", + "id": "13413421543", + "sequence_id": "77", + "etag": "77", + "name": "77 Crunchy Dragon Rolls", + "url": "https://cloud.app.box.com/s/432fesr32fsdfw" + } + ], + "next_marker": null, + "order": [ + { + "by": "type", + "direction": "ASC" + }, + { + "by": "name", + "direction": "ASC" + } + ] +} \ No newline at end of file diff --git a/test/fixtures/output/bulk_items_output_csv.txt b/test/fixtures/output/bulk_items_output_csv.txt new file mode 100644 index 00000000..02257592 --- /dev/null +++ b/test/fixtures/output/bulk_items_output_csv.txt @@ -0,0 +1,6 @@ +type,id,sequence_id,etag,name,file_version.type,file_version.id,file_version.sha1,sha1,url +folder,1234567,10,10,Box,,,,, +folder,22585432,9,9,Human Resources,,,,, +folder,24432981,8,8,Engineering,,,,, +file,124000,79,79,Draymond.boxnote,file_version,431000,b7e432rew9bc2850a5780a86a25a8574d1b473f,b7e9fsdfe329bc2850a5780a86a25a8574d1b473f, +web_link,13413421543,77,77,77 Crunchy Dragon Rolls,,,,,https://cloud.app.box.com/s/432fesr32fsdfw