SHIMODA Hiroshi
null+****@clear*****
Mon Aug 13 18:35:03 JST 2012
SHIMODA Hiroshi 2012-08-13 18:35:03 +0900 (Mon, 13 Aug 2012) New Revision: ca5c70d0d23599917f0f29a1fb7cbb3dd39cd653 https://github.com/groonga/gcs/commit/ca5c70d0d23599917f0f29a1fb7cbb3dd39cd653 Log: Support --option option by gcs-configure-fields Modified files: bin/gcs-configure-fields test/gcs-commands.test.js Modified: bin/gcs-configure-fields (+39 -7) =================================================================== --- bin/gcs-configure-fields 2012-08-13 18:05:18 +0900 (5f5ba03) +++ bin/gcs-configure-fields 2012-08-13 18:35:03 +0900 (d7f8463) @@ -12,6 +12,14 @@ commandLine 'The type of the field that you are configuring or deleting: ' + 'text, literal, uint. Required.', String) + .option('--option <option>', + 'Configures an option for the field specified by the --name and ' + + '--type options. Valid values: search, nosearch, facet, nofacet, ' + + 'result, noresult. Text and literal fields cannot have both the ' + + 'facet and result options enabled. By default, text and uint ' + + 'fields are always searchable and uint fields are always ' + + 'facet-enabled.', + String) .option('-d, --domain-name <domain name>', 'The name of the domain that you are configuring. Required.', String) @@ -37,16 +45,40 @@ if (commandLine.options.delete) { field.deleteSync(); console.log('Updated 1 Index Field:'); } else { - if (!commandLine.options.type) { - console.log('You must specify the field type.'); + if (!field.exists()) { + if (!commandLine.options.type) { + console.log('You must specify the field type.'); + return process.exit(1); + } + field.type = commandLine.options.type; + } else if (!commandLine.options.option) { + console.log('You must specify the configuring option.'); return process.exit(1); } - if (field.exists()) { - console.log('You must specify not-existing field name.'); - return process.exit(1); + + if (commandLine.options.option) { + try { + switch (commandLine.options.option) { + case 'search': field.searchEnabled = true; break; + case 'nosearch': field.searchEnabled = false; break; + case 'facet': field.facetEnabled = true; break; + case 'nofacet': field.facetEnabled = false; break; + case 'result': field.resultEnabled = true; break; + case 'noresult': field.resultEnabled = false; break; + default: + thro new Error('invalid field option ' + commandLine.options.option); + } + } catch(error) { + console.log(error.message); + return process.exit(1); + } } - field.type = commandLine.options.type; - field.createSync(); + + if (!field.exists()) + field.createSync(); + else + field.saveOptionsSync(); + console.log('Updated 1 Index Field:'); console.log('%s %s %s (%s)', field.name, field.state, field.type, field.options); } Modified: test/gcs-commands.test.js (+138 -10) =================================================================== --- test/gcs-commands.test.js 2012-08-13 18:05:18 +0900 (176951b) +++ test/gcs-commands.test.js 2012-08-13 18:35:03 +0900 (1333041) @@ -268,6 +268,15 @@ suite('gcs-configure-fields', function() { setup(commonSetup); teardown(commonTeardown); + function assertSuccess(result, name, type, options) { + assert.deepEqual({ code: result.code, + message: result.output.stdout }, + { code: 0, + message: 'Updated 1 Index Field:\n' + + name + ' Active ' + type + ' (' + options + ')\n' }, + result.output.stderr); + } + function testCreateField(done, name, type, options) { new Domain('companies', context).createSync(); utils @@ -280,12 +289,7 @@ suite('gcs-configure-fields', function() { '--type', type, '--database-path', temporaryDatabase.path) .next(function(result) { - assert.deepEqual({ code: result.code, - message: result.output.stdout }, - { code: 0, - message: 'Updated 1 Index Field:\n' + - name + ' Active ' + type + ' (' + options + ')\n' }, - result.output.stderr); + assertSuccess(result, name, type, options); context.reopen(); var domain = new Domain('companies', context); @@ -313,8 +317,7 @@ suite('gcs-configure-fields', function() { function testDeleteField(done, name, type) { var domain = new Domain('companies', context); domain.createSync(); - var field = domain.getIndexField(name); - field.type = type + var field = domain.getIndexField(name).setType(type); field.createSync(); utils .run('gcs-configure-fields', @@ -354,8 +357,7 @@ suite('gcs-configure-fields', function() { function testRecreateField(done, name, type) { var domain = new Domain('companies', context); domain.createSync(); - var field = domain.getIndexField(name); - field.type = type + var field = domain.getIndexField(name).setType(type); field.createSync(); utils .run('gcs-configure-fields', @@ -440,6 +442,132 @@ suite('gcs-configure-fields', function() { done(e); }); }); + + function assertOptionNotConfigurable(result, option, type) { + if (option.indexOf('search') > -1) + option = 'searchable option'; + else if (option.indexOf('facet') > -1) + option = 'facet option'; + else if (option.indexOf('result') > -1) + option = 'returnable option'; + assert.deepEqual({ code: result.code, + message: result.output.stdout }, + { code: 1, + message: option + ' cannot be configured for the type ' + type + '.\n' }, + result.output.stderr); + } + + function assertOptionConfigured(result, name, type, options) { + assertSuccess(result, name, type, options); + context.reopen(); + var field = new Domain('companies', context).getIndexField(name); + assert.equal(field.options, options); + } + + function testConfigureFieldOptions(type, results, done) { + var name = 'test'; + var domain = new Domain('companies', context); + domain.createSync(); + domain.getIndexField(name).setType(type).createSync(); + utils + .run('gcs-configure-fields', + '--name', name, + '--option', 'search', + '--database-path', temporaryDatabase.path) + .next(function(result) { + if (results.search == 'error') + assertOptionNotConfigurable(result, 'search', type); + else + assertOptionConfigured(result, name, type, results.search); + }) + .run('gcs-configure-fields', + '--name', name, + '--option', 'nosearch', + '--database-path', temporaryDatabase.path) + .next(function(result) { + if (results.nosearch == 'error') + assertOptionNotConfigurable(result, 'nosearch', type); + else + assertOptionConfigured(result, name, type, results.nosearch); + }) + .run('gcs-configure-fields', + '--name', name, + '--option', 'result', + '--database-path', temporaryDatabase.path) + .next(function(result) { + if (results.result == 'error') + assertOptionNotConfigurable(result, 'result', type); + else + assertOptionConfigured(result, name, type, results.result); + }) + .run('gcs-configure-fields', + '--name', name, + '--option', 'noresult', + '--database-path', temporaryDatabase.path) + .next(function(result) { + if (results.noresult == 'error') + assertOptionNotConfigurable(result, 'noresult', type); + else + assertOptionConfigured(result, name, type, results.noresult); + }) + .run('gcs-configure-fields', + '--name', name, + '--option', 'facet', + '--database-path', temporaryDatabase.path) + .next(function(result) { + if (results.facet == 'error') + assertOptionNotConfigurable(result, 'facet', type); + else + assertOptionConfigured(result, name, type, results.facet); + }) + .run('gcs-configure-fields', + '--name', name, + '--option', 'nofacet', + '--database-path', temporaryDatabase.path) + .next(function(result) { + if (results.nofacet == 'error') + assertOptionNotConfigurable(result, 'nofacet', type); + else + assertOptionConfigured(result, name, type, results.nofacet); + done(); + }) + .error(function(e) { + done(e); + }); + } + + test('change option of text field', function() { + testConfigureFieldOptions('text', { + search: 'error', + nosearch: 'error', + facet: 'Search Facet', + nofacet: 'Search', + result: 'Search Result', + noresult: 'Search' + }, done); + }); + + test('change option of uint field', function() { + testConfigureFieldOptions('uint', { + search: 'error', + nosearch: 'error', + facet: 'error', + nofacet: 'error', + result: 'error', + noresult: 'error' + }, done); + }); + + test('change option of literal field', function() { + testConfigureFieldOptions('literal', { + search: 'Search', + nosearch: '', + facet: 'Facet', + nofacet: '', + result: 'Result', + noresult: '' + }, done); + }); }); suite('gcs-configure-text-options', function() { -------------- next part -------------- HTML����������������������������...Download