Skip to content

Commit

Permalink
Print all columns in CSV output for heterogeneous collection (#164)
Browse files Browse the repository at this point in the history
* csv changes

* simpler code

* added test case
  • Loading branch information
sujaygarlanka authored Aug 27, 2019
1 parent a2bf217 commit a8dda02
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/box-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 21 additions & 1 deletion test/commands/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});

});

});
58 changes: 58 additions & 0 deletions test/fixtures/folders/get_folders_id_items.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
6 changes: 6 additions & 0 deletions test/fixtures/output/bulk_items_output_csv.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a8dda02

Please sign in to comment.