Navigation Menu

Skip to content

Commit

Permalink
load: support no records case
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 25, 2014
1 parent 976dc55 commit a0d6462
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/adapter/api/groonga.js
Expand Up @@ -5,6 +5,22 @@ var statusCodes = {
SUCCESS: 0
};

function createResponse(statusCode, startTimeInMilliseconds, body) {
var elapsedTimeInMilliseconds = Date.now() - startTimeInMilliseconds;
var header = [
statusCode,
startTimeInMilliseconds / 1000,
elapsedTimeInMilliseconds / 1000,
];
return [header, body];
};

function sendResponse(response, statusCode, startTimeInMilliseconds, body) {
var groongaResponse =
createResponse(statusCode, startTimeInMilliseconds, body);
response.jsonp(groongaResponse);
};

module.exports = {
'groonga': new command.HTTPRequestResponse({
path: '/d/:commandName',
Expand Down Expand Up @@ -46,15 +62,10 @@ module.exports = {
}
}
if (isEnd && nRecords == nResponses) {
var statusCode = statusCodes.SUCCESS;
var elapsedTimeInMilliseconds =
Date.now() - startTimeInMilliseconds;
var header = [
statusCode,
startTimeInMilliseconds / 1000,
elapsedTimeInMilliseconds / 1000,
];
response.jsonp([header, [nAdded]]);
sendResponse(response,
statusCodes.SUCCESS,
startTimeInMilliseconds,
[nAdded]);
}
});
};
Expand All @@ -63,6 +74,13 @@ module.exports = {
});
request.once('end', function() {
isEnd = true;

if (nRecords == 0) {
sendResponse(response,
statusCodes.SUCCESS,
startTimeInMilliseconds,
[0]);
}
});
}
})
Expand Down
15 changes: 15 additions & 0 deletions test/adapter/api/groonga.test.js
Expand Up @@ -51,6 +51,21 @@ suite('adapter/api: Groonga', function() {

suite('success', function() {
suite('key only', function() {
test('zero', function(done) {
pushSuccessResponse();
var body = [
]
utils.post('/d/load?table=Users', JSON.stringify(body))
.next(function(response) {
try {
assert.deepEqual([0], JSON.parse(response.body)[1]);
done();
} catch (error) {
done(error);
}
});
});

test('one', function(done) {
pushSuccessResponse();
var body = [
Expand Down

0 comments on commit a0d6462

Please sign in to comment.