[Groonga-commit] droonga/express-droonga at a0d6462 [master] load: support no records case

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Apr 25 13:02:56 JST 2014


Kouhei Sutou	2014-04-25 13:02:56 +0900 (Fri, 25 Apr 2014)

  New Revision: a0d6462466258f3fd1d17360c4a846e7eb5ce31a
  https://github.com/droonga/express-droonga/commit/a0d6462466258f3fd1d17360c4a846e7eb5ce31a

  Message:
    load: support no records case

  Modified files:
    lib/adapter/api/groonga.js
    test/adapter/api/groonga.test.js

  Modified: lib/adapter/api/groonga.js (+27 -9)
===================================================================
--- lib/adapter/api/groonga.js    2014-04-25 12:55:48 +0900 (48a5ac9)
+++ lib/adapter/api/groonga.js    2014-04-25 13:02:56 +0900 (cb99aea)
@@ -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',
@@ -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]);
           }
         });
       };
@@ -63,6 +74,13 @@ module.exports = {
       });
       request.once('end', function() {
         isEnd = true;
+
+        if (nRecords == 0) {
+          sendResponse(response,
+                       statusCodes.SUCCESS,
+                       startTimeInMilliseconds,
+                       [0]);
+        }
       });
     }
   })

  Modified: test/adapter/api/groonga.test.js (+15 -0)
===================================================================
--- test/adapter/api/groonga.test.js    2014-04-25 12:55:48 +0900 (cbc515d)
+++ test/adapter/api/groonga.test.js    2014-04-25 13:02:56 +0900 (832f677)
@@ -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 = [
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index