Skip to content

Commit

Permalink
feat: Add --all flag for search to return all results (#336)
Browse files Browse the repository at this point in the history
Closes: SDK-2213
  • Loading branch information
lukaszsocha2 authored Jun 2, 2022
1 parent 14b6d1d commit 23ea0a5
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ function parseMetadataValue(value) {
class SearchCommand extends BoxCommand {
async run() {
const { flags, args } = this.parse(SearchCommand);

if (flags.all && flags.limit) {
throw new BoxCLIError('--all and --limit flags cannot be used together.');
}

let options = {
limit: RESULTS_LIMIT,
};

if (flags.scope) {
options.scope = flags.scope;
}
Expand Down Expand Up @@ -153,7 +159,7 @@ class SearchCommand extends BoxCommand {
let limitedResults = [];
for await (let result of { [Symbol.asyncIterator]: () => results }) {
let numResults = limitedResults.push(result);
if (numResults >= itemsLimit) {
if (!flags.all && numResults >= itemsLimit) {
break;
}
}
Expand Down Expand Up @@ -288,6 +294,9 @@ SearchCommand.flags = {
limit: flags.integer({
description: 'Defines the maximum number of items to return. Default value is 100.',
}),
all: flags.boolean({
description: 'Returns all search results.',
}),
'include-recent-shared-links': flags.boolean({
description: 'Returns shared links that the user recently accessed'
}),
Expand Down
71 changes: 68 additions & 3 deletions test/commands/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const leche = require('leche');

describe('Search', () => {

describe('search', () => {
let query = 'Test',
let query = 'Test',
fixture = getFixture('search/get_search_query_page_1'),
fixture2 = getFixture('search/get_search_query_page_2'),
jsonOutput = getFixture('output/search_json.txt');
jsonOutput = getFixture('output/search_json.txt'),
jsonOutputLimitedTo5 = getFixture('output/search_json_limit_5.txt');

describe('search', () => {

test
.nock(TEST_API_ROOT, api => api
Expand Down Expand Up @@ -314,5 +316,68 @@ describe('Search', () => {
assert.equal(ctx.stdout, jsonOutput);
});
});

test
.nock(TEST_API_ROOT, api => api
.get('/2.0/search')
.query({
query,
limit: 100,
})
.reply(200, fixture)
.get('/2.0/search')
.query({
query,
limit: 100,
offset: 5
})
.reply(200, fixture2)
)
.stdout()
.command([
'search',
query,
'--json',
'--all',
'--token=test'
])
.it('should return all results when --all flag provided', ctx => {
assert.equal(ctx.stdout, jsonOutput);
});

test
.nock(TEST_API_ROOT, api => api
.get('/2.0/search')
.query({
query,
limit: 100,
})
.reply(200, fixture)
)
.stdout()
.command([
'search',
query,
'--json',
'--limit=5',
'--token=test'
])
.it('should return limited results when --limit flag provided', ctx => {
assert.equal(ctx.stdout, jsonOutputLimitedTo5);
});
});
describe('fails', () => {
test
.stderr()
.command([
'search',
query,
'--limit=80',
'--all',
'--token=test'
])
.it('when both --all and --limit flag provided', ctx => {
assert.include(ctx.stderr, '--all and --limit flags cannot be used together.');
});
});
});
Loading

0 comments on commit 23ea0a5

Please sign in to comment.