YUKI Hiroshi
null+****@clear*****
Mon Dec 10 17:15:59 JST 2012
YUKI Hiroshi 2012-12-10 17:15:59 +0900 (Mon, 10 Dec 2012) New Revision: 0eb80b48a3759597c0d10168bca03989ff902fb6 https://github.com/groonga/gcs/commit/0eb80b48a3759597c0d10168bca03989ff902fb6 Log: Add tests for batch/xml Added files: test/batch-xml.test.js Modified files: lib/batch/index.js lib/batch/xml.js Modified: lib/batch/index.js (+1 -0) =================================================================== --- lib/batch/index.js 2012-12-10 17:02:22 +0900 (01d068e) +++ lib/batch/index.js 2012-12-10 17:15:59 +0900 (cdabfee) @@ -1 +1,2 @@ exports.Processor = require('./processor').Processor; +exports.xml = require('./xml'); Modified: lib/batch/xml.js (+5 -1) =================================================================== --- lib/batch/xml.js 2012-12-10 17:02:22 +0900 (b0238ed) +++ lib/batch/xml.js 2012-12-10 17:15:59 +0900 (894f9a2) @@ -17,6 +17,8 @@ function _toJSON(xml) { return json; } +var UINT_MATCHER = /^(?:0|[1-9][0-9]*)$/; + function toJSON(xml) { var batches = []; var json = _toJSON(xml); @@ -34,7 +36,9 @@ function toJSON(xml) { var fields = item.field; if (!Array.isArray(fields)) fields = [fields]; fields.forEach(function(field) { - batch.fields[field['@'].name] = field['#']; + var value = field['#']; + if (UINT_MATCHER.test(value)) value = parseInt(value); + batch.fields[field['@'].name] = value; }); batches.push(batch); }); Added: test/batch-xml.test.js (+42 -0) 100644 =================================================================== --- /dev/null +++ test/batch-xml.test.js 2012-12-10 17:15:59 +0900 (51d767b) @@ -0,0 +1,42 @@ +var utils = require('./test-utils'); + +var assert = require('chai').assert; +var fs = require('fs'); +var path = require('path'); + +var xml = require('../lib/batch/xml'); +var fixtures = path.join(__dirname, 'fixture/companies'); + +function readJSONBatch(filePath) { + var jsonBatch = fs.readFileSync(filePath, 'UTF-8'); + jsonBatch = JSON.parse(jsonBatch); + return sortBatches(jsonBatch); +} + +function readXMLBatch(filePath) { + var xmlBatch = fs.readFileSync(filePath, 'UTF-8'); + xmlBatch = xml.toJSON(xmlBatch); + return sortBatches(xmlBatch); +} + +function sortBatches(batches) { + return batches.sort(function(a, b) { + return a.type - b.type || + a.id - b.id || + a.version - b.version; + }); +} + +suite('batch/xml', function() { + test('add, multiple items, single values', function() { + var jsonBatch = readJSONBatch(path.join(fixtures, 'add.sdf.json')); + var xmlBatch = readJSONBatch(path.join(fixtures, 'add.sdf.xml')); + assert.deepEqual(xmlBatch, jsonBatch); + }); + + test('delete, single item', function() { + var jsonBatch = readJSONBatch(path.join(fixtures, 'delete.sdf.json')); + var xmlBatch = readJSONBatch(path.join(fixtures, 'delete.sdf.xml')); + assert.deepEqual(xmlBatch, jsonBatch); + }); +}); -------------- next part -------------- HTML����������������������������... Download