Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix options parameter in metadata query method #878

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/managers/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,11 @@ class Metadata {
* @param {string} ancestorFolderId - The folder_id to which to restrain the query
* @param {Object} [options] - Optional parameters
* @param {string} [options.query] - The logical expression of the query
* @param {Object} [options.query_parameters] - Required if query present. The arguments for the query
* @param {Object} [options.query_params] - Required if query present. The arguments for the query
* @param {Object} [options.order_by] - The field_key(s) to order on and the corresponding direction(s)
* @param {Array} [options.fields] - An array of fields to return
* @param {int} [options.limit=100] - The number of results to return for a single request
* @param {string} [options.marker] - Pagination marker
* @param {Function} [callback] - Passed a collection of items and their associated metadata
* @returns {Promise<void>} Promise resolving to a collection of items and their associated metadata
*/
Expand All @@ -402,9 +404,11 @@ class Metadata {
ancestorFolderId: string,
options?: {
query?: string;
query_parameters?: Record<string, any>;
order_by: Record<string, any>;
query_params?: Record<string, any>;
order_by?: Record<string, any>;
fields?: string[];
limit?: number;
marker?: string;
},
callback?: Function
) {
Expand Down
70 changes: 70 additions & 0 deletions tests/integration_test/__tests__/metadata.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const {getAppClient} = require('../context');
const {createBoxTestFolder} = require('../objects/box-test-folder');
const {createBoxTestFile} = require('../objects/box-test-file');
const utils = require('../lib/utils');
const path = require('path');
const context = {};

beforeAll(async() => {
context.appClient = await getAppClient();
let folder = await createBoxTestFolder(context.appClient);
context.folder = folder;
});

afterAll(async() => {
await context.folder.dispose();
context.folder = null;
});

test('test metadata search', async() => {
const templateKey = `template_${utils.randomName()}`;
const fields = [
{
type: 'float',
key: 'testFloatValue',
displayName: 'testFloatValue',
},
];

const metadataTemplate = await context.appClient.metadata.createTemplate(templateKey, fields, {
scope: 'enterprise',
templateKey,
});
try {
expect(metadataTemplate.id).toBeDefined();
expect(metadataTemplate.templateKey).toBe(templateKey);
expect(metadataTemplate.displayName).toBe(templateKey);
const file = await createBoxTestFile(context.appClient, path.join(__dirname, '../resources/blank.pdf'), 'blank_sign_1.pdf', context.folder.id);
try {
const metadata = await context.appClient.files.addMetadata(file.id, 'enterprise', templateKey, {testFloatValue: 150});
expect(metadata.$template).toBe(templateKey);
expect(metadata.testFloatValue).toBe(150);

const searchForm = `${metadataTemplate.scope}.${metadataTemplate.templateKey}`;
const ancestorFolderId = '0';
const queryOptions = {
query: 'testFloatValue >= :arg',
query_params: {arg: '100'},
limit: 1,
order_by: [
{
field_key: 'testFloatValue',
direction: 'asc',
},
],
};
const searchResults = await context.appClient.metadata.query(searchForm, ancestorFolderId, queryOptions);
// Sometimes, despite correctly sent metadata, the search does not return files because they probably haven't been properly indexed yet
expect(searchResults.entries.length).toBeGreaterThanOrEqual(0);
}
finally {
await file.dispose();
}
}
finally {
await context.appClient.metadata.deleteTemplate('enterprise', metadataTemplate.templateKey);
}
}, 120000);

31 changes: 31 additions & 0 deletions tests/lib/managers/metadata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,35 @@ describe('Metadata', function() {
.then(data => assert.equal(data, response));
});
});

describe('query()', function() {
it('should make POST request to search the API when called', function() {
var from = 'enterprise_987654321.someTemplate';
var ancestorFolderId = '0';
var options = {
query: 'value >= :amount',
query_params: {amount: '100'},
limit: 10,
marker: 'vwxyz',
order_by: [
{
"field_key": "value",
"direction": "asc"
}
]
};

var expectedParams = {
body: {
ancestor_folder_id: ancestorFolderId,
from: from,
...options
},
};

sandbox.stub(boxClientFake, 'wrapWithDefaultHandler').returnsArg(0);
sandbox.mock(boxClientFake).expects('post').withArgs('/metadata_queries/execute_read', expectedParams);
metadata.query(from, ancestorFolderId, options);
});
});
});
Loading