Kouhei Sutou
null+****@clear*****
Tue Mar 25 11:09:02 JST 2014
Kouhei Sutou 2014-03-25 11:09:02 +0900 (Tue, 25 Mar 2014) New Revision: 914472083ae58cd79e3314a41831d0449f70f9bf https://github.com/droonga/express-droonga/commit/914472083ae58cd79e3314a41831d0449f70f9bf Message: /tables/:tableName: Support group_by TODO: * Add validation * Split tests to other file Modified files: lib/adapter/api/rest-request-builder.js test/rest-request-builder.test.js Modified: lib/adapter/api/rest-request-builder.js (+51 -2) =================================================================== --- lib/adapter/api/rest-request-builder.js 2014-03-25 10:02:45 +0900 (d81f8f5) +++ lib/adapter/api/rest-request-builder.js 2014-03-25 11:09:02 +0900 (6a20080) @@ -17,7 +17,7 @@ function getIntegerValue(params, key) { return parseInt(value); } -function buildQuery(tableName, queryParams) { +function buildSearchQuery(tableName, queryParams) { var query = { source: tableName, output: { @@ -71,6 +71,48 @@ function buildQuery(tableName, queryParams) { return query; } +function buildGroupByQuery(sourceQueryName, params) { + var query = { + source: sourceQueryName, + groupBy: { + }, + output: { + elements: [ + 'count', + 'records' + ] + } + }; + + if ('key' in params) + query.groupBy.key = params.key; + if ('max_n_sub_records' in params) + query.groupBy.maxNSubRecords = getIntegerValue(params, 'max_n_sub_records'); + + if ('attributes' in params) { + var requestAttributes = params.attributes; + var keys = Object.keys(requestAttributes); + var attributes = []; + keys.forEach(function(key) { + var requestAttribute = requestAttributes[key]; + var attribute = { + label: key + }; + attribute.source = requestAttribute.source; + if ('attributes' in requestAttribute) + attribute.attributes = requestAttribute.attributes; + attributes.push(attribute); + }); + query.output.attributes = attributes; + } + + if ('limit' in params) { + query.output.limit = getIntegerValue(params, 'limit'); + } + + return query; +} + function searchRequestBuilder(request) { var params = request.params; if (!params.tableName) @@ -86,7 +128,14 @@ function searchRequestBuilder(request) { } catch(error) { queryName = params.tableName.toLowerCase(); } - queries[queryName] = buildQuery(params.tableName, queryParams); + queries[queryName] = buildSearchQuery(params.tableName, queryParams); + if ('group_by' in queryParams) { + var group_by_queries = queryParams.group_by; + var keys = Object.keys(group_by_queries); + keys.forEach(function(key) { + queries[key] = buildGroupByQuery(queryName, group_by_queries[key]); + }); + } var message = {}; message.queries = queries; Modified: test/rest-request-builder.test.js (+110 -0) =================================================================== --- test/rest-request-builder.test.js 2014-03-25 10:02:45 +0900 (a750dba) +++ test/rest-request-builder.test.js 2014-03-25 11:09:02 +0900 (466d03e) @@ -147,6 +147,116 @@ suite('building message from REST adapter request', function() { testFailFor({ query: { timeout: '-0.1' } }, 'invalid integer', baseRequest); testFailFor({ query: { timeout: 'foobar' } }, 'invalid integer', baseRequest); }); + + // TODO: split test file + suite('group_by', function() { + var request; + + setup(function () { + request = { + params: { + tableName: 'Memos' + }, + query: { + group_by: { + } + } + }; + }); + + function buildGroupByQuery(query) { + var name = 'group'; + request.query.group_by[name] = query; + return builders.search(request).queries[name]; + }; + + test('source', function() { + var query = { + key: '_key' + }; + assert.equalJSON(buildGroupByQuery(query).source, + 'memos'); + }); + + test('key', function() { + var query = { + key: '_key' + }; + var expected = { + key: '_key' + }; + assert.equalJSON(buildGroupByQuery(query).groupBy, + expected); + }); + + test('max_n_sub_records', function() { + var query = { + key: '_key', + max_n_sub_records: 10 + }; + var expected = { + key: '_key', + maxNSubRecords: 10 + }; + assert.equalJSON(buildGroupByQuery(query).groupBy, + expected); + }); + + test('limit', function() { + var query = { + key: '_key', + limit: 10 + }; + assert.equalJSON(buildGroupByQuery(query).output.limit, + 10); + }); + + suite('attributes', function() { + test('source', function() { + var query = { + key: '_key', + attributes: { + titleLabel: { + source: 'title' + } + } + }; + var expected = [ + { + label: 'titleLabel', + source: 'title' + } + ]; + assert.equalJSON(buildGroupByQuery(query).output.attributes, + expected); + }); + + test('attributes', function() { + var query = { + key: '_key', + attributes: { + sub_records: { + source: '_subrecs', + attributes: [ + 'title' + ] + } + } + }; + var expected = [ + { + label: 'sub_records', + source: '_subrecs', + attributes: [ + 'title' + ] + } + ]; + assert.equalJSON(buildGroupByQuery(query).output.attributes, + expected); + }); + }); + }); }); }); -------------- next part -------------- HTML����������������������������... Télécharger