Navigation Menu

Skip to content

Commit

Permalink
Add tests for configurations related to tables
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 13, 2012
1 parent 0ca9284 commit f66f007
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
12 changes: 10 additions & 2 deletions lib/database/domain.js
Expand Up @@ -403,6 +403,9 @@ Domain.prototype = {
},

setConfiguration: function(key, value) {
if (!this.context)
throw new Error('no context');

this.context.commandSync('load', {
table: this.configurationsTableName,
values: JSON.stringify([{
Expand All @@ -412,12 +415,14 @@ Domain.prototype = {
});
},
getConfiguration: function(key) {
if (!this.context)
throw new Error('no context');

var options = {
table: this.configurationsTableName,
limit: -1,
offset: 0,
query: '_key=' + key,
output_columns: '_key,value'
filter: '_key == "' + key.replace(/"/g, '\\"') + '"'
};
var values = this.context.commandSync('select', options);
values = nroonga.formatSelectResult(values);
Expand All @@ -428,6 +433,9 @@ Domain.prototype = {
return JSON.parse(value);
},
deleteConfiguration: function(key) {
if (!this.context)
throw new Error('no context');

this.context.commandSync('delete', {
table: this.configurationsTableName,
key: key
Expand Down
2 changes: 1 addition & 1 deletion lib/database/index-field.js
Expand Up @@ -270,7 +270,7 @@ IndexField.prototype = {
},
deleteSync: function() {
// backup information for re-creation
this._type = type;
this._type = this.type;
this._facetEnabled = this.facetEnabled;
this._resultEnabled = this.resultEnabled;
this._searchEnabled = this.searchEnabled;
Expand Down
95 changes: 95 additions & 0 deletions test/database-domain.test.js
Expand Up @@ -518,5 +518,100 @@ suite('database', function() {
assert.deepEqual(actualDump.slice(-2), expectedDump);
});
});

suite('configuration operations', function() {
var temporaryDatabase;
var context;
var domain;

setup(function() {
temporaryDatabase = utils.createTemporaryDatabase();
context = temporaryDatabase.get();
domain = new Domain('companies', context);
domain.createSync();
});

teardown(function() {
domain = undefined;
temporaryDatabase.teardown();
temporaryDatabase = undefined;
});

test('setConfiguration', function() {
domain.setConfiguration('key1_string', 'abc');
domain.setConfiguration('key2_number', 123);
domain.setConfiguration('key3_hash', { value: true });

var dumpExpected =
'table_create ' + domain.tableName + ' ' +
'TABLE_HASH_KEY ShortText\n' +
'table_create ' + domain.configurationsTableName + ' ' +
'TABLE_HASH_KEY ShortText\n' +
'column_create ' + domain.configurationsTableName + ' ' +
'value COLUMN_SCALAR ShortText\n' +
'table_create ' + domain.termsTableName + ' ' +
'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
'--default_tokenizer TokenBigram\n' +
'load --table ' + domain.configurationsTableName + '\n' +
'[\n' +
'["_key","value"],\n' +
'["key1_string","\\"abc\\""],\n' +
'["key2_number","123"],\n' +
'["key3_hash","{\\"value\\":true}"]\n' +
']';
var dumpActual = context.commandSync('dump', {
tables: domain.configurationsTableName
});
assert.equal(dumpExpected, dumpActual);
});

test('getConfiguration', function() {
var expectedValues = {
string: 'abc',
number: 123,
hash: { value: true }
};
domain.setConfiguration('key1_string', expectedValues.string);
domain.setConfiguration('key2_number', expectedValues.number);
domain.setConfiguration('key3_hash', expectedValues.hash);

var actualValues = {
string: domain.getConfiguration('key1_string'),
number: domain.getConfiguration('key2_number'),
hash: domain.getConfiguration('key3_hash'),
};
assert.deepEqual(actualValues, expectedValues);
});

test('getConfiguration (undefined configuration)', function() {
assert.deepEqual(undefined, domain.getConfiguration('unknown'));
});

test('deleteConfiguration', function() {
domain.setConfiguration('key1_string', 'abc');
domain.setConfiguration('key2_number', 123);
domain.deleteConfiguration('key2_number');

var dumpExpected =
'table_create ' + domain.tableName + ' ' +
'TABLE_HASH_KEY ShortText\n' +
'table_create ' + domain.configurationsTableName + ' ' +
'TABLE_HASH_KEY ShortText\n' +
'column_create ' + domain.configurationsTableName + ' ' +
'value COLUMN_SCALAR ShortText\n' +
'table_create ' + domain.termsTableName + ' ' +
'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
'--default_tokenizer TokenBigram\n' +
'load --table ' + domain.configurationsTableName + '\n' +
'[\n' +
'["_key","value"],\n' +
'["key1_string","\\"abc\\""]\n' +
']';
var dumpActual = context.commandSync('dump', {
tables: domain.configurationsTableName
});
assert.equal(dumpExpected, dumpActual);
});
});
});
});
14 changes: 11 additions & 3 deletions test/database-index-field.test.js
Expand Up @@ -7,15 +7,23 @@ var IndexField = require('../lib/database/index-field').IndexField;

suite('database', function() {
suite('IndexField', function() {
var temporaryDatabase;
var context;
var domain;

setup(function() {
domain = new Domain('testdomain');
temporaryDatabase = utils.createTemporaryDatabase();
context = temporaryDatabase.get();
domain = new Domain('testdomain', context);
domain.id = Domain.DEFAULT_ID;
domain.createSync();
});

teardown(function() {
domain = undefined;
context = undefined;
temporaryDatabase.teardown();
temporaryDatabase = undefined;
});

test('lower case', function() {
Expand Down Expand Up @@ -108,7 +116,7 @@ suite('database', function() {
resultEnabled: false,
searchEnabled: true,
state: 'Active',
options: 'Search Facet Result'
options: 'Search'
});
});

Expand Down Expand Up @@ -144,7 +152,7 @@ suite('database', function() {
resultEnabled: false,
searchEnabled: false,
state: 'Active',
options: 'Search Facet Result'
options: ''
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/fixture/companies/ddl-custom-id.grn
Expand Up @@ -2,6 +2,7 @@ table_create companies_id0123_index_product TABLE_HASH_KEY ShortText
table_create companies_id0123_index_age TABLE_HASH_KEY UInt32
table_create companies_id0123_index_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram
table_create companies_id0123_configurations TABLE_HASH_KEY ShortText
column_create companies_id0123_configurations value COLUMN_SCALAR ShortText
table_create companies_id0123 TABLE_HASH_KEY ShortText
column_create companies_id0123 address COLUMN_SCALAR ShortText
column_create companies_id0123 age COLUMN_SCALAR UInt32
Expand All @@ -15,4 +16,3 @@ column_create companies_id0123_index_BigramTerms companies_description COLUMN_IN
column_create companies_id0123_index_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION companies_id0123 address
column_create companies_id0123_index_age companies_age COLUMN_INDEX|WITH_POSITION companies_id0123 age
column_create companies_id0123_index_product companies_product COLUMN_INDEX|WITH_POSITION companies_id0123 product
column_create companies_id0123_configurations value COLUMN_SCALAR ShortText
2 changes: 1 addition & 1 deletion test/fixture/companies/ddl.grn
Expand Up @@ -2,6 +2,7 @@ table_create companies_00000000000000000000000000_index_product TABLE_HASH_KEY S
table_create companies_00000000000000000000000000_index_age TABLE_HASH_KEY UInt32
table_create companies_00000000000000000000000000_index_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram
table_create companies_00000000000000000000000000_configurations TABLE_HASH_KEY ShortText
column_create companies_00000000000000000000000000_configurations value COLUMN_SCALAR ShortText
table_create companies_00000000000000000000000000 TABLE_HASH_KEY ShortText
column_create companies_00000000000000000000000000 address COLUMN_SCALAR ShortText
column_create companies_00000000000000000000000000 age COLUMN_SCALAR UInt32
Expand All @@ -15,4 +16,3 @@ column_create companies_00000000000000000000000000_index_BigramTerms companies_d
column_create companies_00000000000000000000000000_index_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 address
column_create companies_00000000000000000000000000_index_age companies_age COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 age
column_create companies_00000000000000000000000000_index_product companies_product COLUMN_INDEX|WITH_POSITION companies_00000000000000000000000000 product
column_create companies_00000000000000000000000000_configurations value COLUMN_SCALAR ShortText

0 comments on commit f66f007

Please sign in to comment.