Navigation Menu

Skip to content

Commit

Permalink
Support --option option by gcs-configure-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 13, 2012
1 parent e752b78 commit ca5c70d
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 17 deletions.
46 changes: 39 additions & 7 deletions bin/gcs-configure-fields
Expand Up @@ -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)
Expand All @@ -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);
}
148 changes: 138 additions & 10 deletions test/gcs-commands.test.js
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit ca5c70d

Please sign in to comment.