Navigation Menu

Skip to content

Commit

Permalink
Add Domain#dump() and Domain#load()
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 13, 2012
1 parent c044abc commit cc67fa3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/database/domain.js
Expand Up @@ -319,6 +319,47 @@ Domain.prototype = {

exists: function() {
return !!this.getIdFromDatabase();
},

dump: function() {
var dump = this.context.commandSync('dump', {
tables: this.tableName
});

var tableContents = dump.split('load --table ' + this.tableName)[1];
tableContents = JSON.parse(tableContents);

var columnNames = tableContents[0];
columnNames = columnNames.map(function(columnName) {
if (columnName == '_key')
return 'id';
else
return columnName;
});

var values = tableContents.slice(1).map(function(record) {
var formattedRecord = {};
record.forEach(function(columnValue, columnIndex) {
var column = columnNames[columnIndex];
formattedRecord[column] = columnValue;
});
return formattedRecord;
});
return values;
},

load: function(values) {
values = values.map(function(record) {
if ('id' in record && !('_key' in record)) {
record['_key'] = record['id'];
delete record['id'];
}
return record;
});
this.context.commandSync('load', {
table: this.tableName,
values: JSON.stringify(values)
});
}
};

Expand Down
83 changes: 83 additions & 0 deletions test/database-domain.test.js
Expand Up @@ -398,5 +398,88 @@ suite('database', function() {
]);
});
});

suite('dump and load', function() {
var temporaryDatabase;
var context;
var domain;

setup(function() {
temporaryDatabase = utils.createTemporaryDatabase();
context = temporaryDatabase.get();
utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl.grn');
utils.loadDumpFile(context, __dirname + '/fixture/companies/data.grn');
domain = new Domain('companies', context);
});

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

test('dump', function() {
var actualDump = domain.dump();
assert.isTrue(Array.isArray(actualDump), actualDump);
assert.equal(actualDump.length, 10, actualDump);

var expectedDump = [
{ id: 'id1',
address: 'Shibuya, Tokyo, Japan',
age: 1,
description: '',
email_address: 'info@razil.jp',
name: 'Brazil',
product: 'groonga' },
{ id: 'id2',
address: 'Sapporo, Hokkaido, Japan',
age: 2,
description: '',
email_address: 'info@enishi-tech.com',
name: 'Enishi Tech Inc.',
product: 'groonga' },
{ id: 'id3',
address: 'Hongo, Tokyo, Japan',
age: 3,
description: '',
email_address: 'info@clear-code.com',
name: 'ClearCode Inc.',
product: 'groonga' }
];
assert.deepEqual(actualDump.slice(0, 3), expectedDump);
});

test('load', function() {
var values = [
{ id: 'id10',
description: 'updated',
product: 'spd13' },
{ id: 'id11',
description: 'new',
name: 'Nergal Heavy Industries',
product: 'nadesico' }
];
domain.load(values);

var actualDump = domain.dump();
var expectedDump = [
{ id: 'id10',
address: 'New York, United States',
age: 10,
description: 'updated',
email_address: '',
name: 'U.S. Robots and Mechanical Men',
product: 'spd13' },
{ id: 'id11',
address: '',
age: 0,
description: 'new',
email_address: '',
name: 'Nergal Heavy Industries',
product: 'nadesico' }
];
assert.deepEqual(actualDump.slice(-2), expectedDump);
});
});
});
});

0 comments on commit cc67fa3

Please sign in to comment.