SHIMODA Hiroshi
null+****@clear*****
Mon Aug 13 11:43:23 JST 2012
SHIMODA Hiroshi 2012-08-13 11:43:23 +0900 (Mon, 13 Aug 2012) New Revision: cc67fa3e1e084aac2f5368d8ae0f0b141ea9fd69 https://github.com/groonga/gcs/commit/cc67fa3e1e084aac2f5368d8ae0f0b141ea9fd69 Log: Add Domain#dump() and Domain#load() Modified files: lib/database/domain.js test/database-domain.test.js Modified: lib/database/domain.js (+41 -0) =================================================================== --- lib/database/domain.js 2012-08-12 16:40:02 +0900 (6cb6bbd) +++ lib/database/domain.js 2012-08-13 11:43:23 +0900 (6f5bcf8) @@ -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) + }); } }; Modified: test/database-domain.test.js (+83 -0) =================================================================== --- test/database-domain.test.js 2012-08-12 16:40:02 +0900 (3baca25) +++ test/database-domain.test.js 2012-08-13 11:43:23 +0900 (ccbcc19) @@ -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); + }); + }); }); }); -------------- next part -------------- HTML����������������������������...Download