ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject [2/3] incubator-ignite git commit: #ignite-961: rewrite query-tests.
Date Tue, 14 Jul 2015 11:46:00 GMT
#ignite-961: rewrite query-tests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/fee4ed9e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/fee4ed9e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/fee4ed9e

Branch: refs/heads/ignite-961-promise
Commit: fee4ed9ea9210da2b87c87049ccedb7ae1d397ad
Parents: 88bb97d
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Tue Jul 14 14:19:26 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Tue Jul 14 14:19:26 2015 +0300

----------------------------------------------------------------------
 modules/nodejs/src/main/js/cache.js             |  46 +++--
 .../ignite/internal/NodeJsSqlQuerySelfTest.java |  10 --
 modules/nodejs/src/test/js/test-query.js        | 173 +++++++------------
 3 files changed, 99 insertions(+), 130 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/fee4ed9e/modules/nodejs/src/main/js/cache.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/main/js/cache.js b/modules/nodejs/src/main/js/cache.js
index a84b993..f1396e3 100644
--- a/modules/nodejs/src/main/js/cache.js
+++ b/modules/nodejs/src/main/js/cache.js
@@ -268,29 +268,46 @@ Cache.prototype.size = function(callback) {
  * @param {SqlQuery|SqlFieldsQuery} qry Query
  */
 Cache.prototype.query = function(qry) {
-    if (qry.returnType() == null) {
-        qry.end("No type for sql query.");
+    var cmd;
 
-        return;
-    }
+    if (qry.type() === "Sql") {
+        if (qry.returnType() == null) {
+            return new Promise(function(resolve, reject) {
+                reject("No type for sql query.");
+            });
+        }
 
-    var command = this._createQueryCommand("qryexecute", qry).addParam("type", qry.returnType()).
-        setPostData(JSON.stringify({"arg" : qry.arguments()}));
+        cmd = this._sqlQuery(qry);
+    }
+    else {
+        cmd = this._sqlFieldsQuery(qry);
+    }
 
     var server = this._server;
+    var cache = this;
 
     return new Promise(function(resolve, reject) {
-        server.runCommand(command, function(err, res){
+        server.runCommand(cmd, function(err, res){
             if(err != null) {
                 reject(err);
             }
             else {
-                resolve(new Cursor(qry, res));
+                resolve(new Cursor(qry, res, cache));
             }
         });
     });
 }
 
+Cache.prototype._sqlFieldsQuery = function(qry) {
+    return this._createQueryCommand("qryfieldsexecute", qry).
+        setPostData(JSON.stringify({"arg" : qry.arguments()}));
+}
+
+Cache.prototype._sqlQuery = function(qry) {
+    return this._createQueryCommand("qryexecute", qry).addParam("type", qry.returnType()).
+        setPostData(JSON.stringify({"arg" : qry.arguments()}));
+}
+
 Cache.prototype.__createPromise = function(cmd) {
     var server = this._server;
 
@@ -320,9 +337,10 @@ Cache.prototype._createQueryCommand = function(name, qry) {
     return command.addParam("psz", qry.pageSize());
 }
 
-Cursor = function(qry, res) {
+Cursor = function(qry, res, cache) {
     this._qry = qry;
     this._res = res;
+    this._cache = cache;
 }
 
 Cursor.prototype.next = function() {
@@ -330,10 +348,12 @@ Cursor.prototype.next = function() {
         throw "All pages are returned.";
     }
 
-    var cmd = this._createCommand("qryfetch").addParam("qryId", _res.queryId).
-        addParam("psz", _qry.pageSize());
+    var cmd = this._cache._createCommand("qryfetch").addParam("qryId", this._res.queryId).
+        addParam("psz", this._qry.pageSize());
 
-    var server = this._server;
+    var server = this._cache._server;
+    var qry = this._qry;
+    var cache = this._cache;
 
     return new Promise(function(resolve, reject) {
        server.runCommand(cmd, function(err, res) {
@@ -341,7 +361,7 @@ Cursor.prototype.next = function() {
                reject(err);
            }
            else {
-               resolve(new Cursor(qry, res));
+               resolve(new Cursor(qry, res, cache));
            }
        });
     });

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/fee4ed9e/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
index e83a32e..9024b93 100644
--- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
+++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsSqlQuerySelfTest.java
@@ -70,16 +70,6 @@ public class NodeJsSqlQuerySelfTest extends NodeJsAbstractTest {
         runJsScript("testSqlQueryWithParams");
     }
 
-
-    /**
-     * @throws Exception If failed.
-     */
-    public void testSqlPromiseQuery() throws Exception {
-        initCache();
-
-        runJsScript("testSqlPromiseQuery");
-    }
-
     /**
      * Init cache.
      */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/fee4ed9e/modules/nodejs/src/test/js/test-query.js
----------------------------------------------------------------------
diff --git a/modules/nodejs/src/test/js/test-query.js b/modules/nodejs/src/test/js/test-query.js
index fd01945..cbdb643 100644
--- a/modules/nodejs/src/test/js/test-query.js
+++ b/modules/nodejs/src/test/js/test-query.js
@@ -24,131 +24,84 @@ var SqlQuery = Ignite.SqlQuery;
 var SqlFieldsQuery = Ignite.SqlFieldsQuery;
 
 testSqlQuery = function() {
-    function sqlQuery(ignite, error) {
-        assert(error == null, "error on sql query [err=" + error + "]");
+    TestUtils.startIgniteNode().then(function(ignite) {
+        ignite.cache("mycache").put("key0", "val0").then(function() {
+            var qry = new SqlQuery("select * from String");
 
-        var qry = new SqlQuery("select * from String");
+            qry.setReturnType("String");
 
-        qry.setReturnType("String");
+            var fullRes = [];
 
-        var fullRes = [];
-
-        qry.on("page", function(res) {
-            fullRes = fullRes.concat(res);
-        });
-
-        qry.on("end", function(err) {
-            assert(err === null, "Error on query [err=" + err + "].");
+            function onQuery(cursor) {
+                var page = cursor.page();
 
-            assert(fullRes.length === 1, "Result length is not correct" +
-                "[expected=1, val = " + fullRes.length + "]");
+                fullRes = fullRes.concat(page);
 
-            assert(fullRes[0]["key"] === "key0", "Result value for key is not correct "+
-                "[expected=key0, real=" + fullRes[0]["key"] + "]");
+                if (cursor.isFinished()) {
+                    assert(fullRes.length === 1, "Result length is not correct" +
+                        "[expected=1, val = " + fullRes.length + "]");
 
-            assert(fullRes[0]["value"] === "val0", "Result value for key is not correct "+
-                "[expected=val0, real=" + fullRes[0]["value"] + "]");
+                    assert(fullRes[0]["key"] === "key0", "Result value for key is not correct
"+
+                        "[expected=key0, real=" + fullRes[0]["key"] + "]");
 
-            TestUtils.testDone();
-        });
+                    assert(fullRes[0]["value"] === "val0", "Result value for key is not correct
"+
+                        "[expected=val0, real=" + fullRes[0]["value"] + "]");
 
-        ignite.cache("mycache").query(qry);
-    }
+                    TestUtils.testDone();
 
-    function put(error, ignite) {
-        assert(error == null, "error on put [err=" + error + "]");
+                    return;
+                }
 
-        ignite.cache("mycache").put("key0", "val0", sqlQuery.bind(null, ignite))
-    }
+                cursor.next().then(onQuery);
+            }
 
-    TestUtils.startIgniteNode(put);
+            ignite.cache("mycache").query(qry).then(onQuery);
+        }).catch(function(err) {
+            assert(err === null, err);
+        })
+    }).catch(function(err) {
+        assert(err === null, err);
+    });
 }
 
-testSqlPromiseQuery = function() {
-    function sqlQuery(ignite, error) {
-        assert(error == null, "error on sql query [err=" + error + "]");
-
-        var qry = new SqlQuery("select * from String");
-        qry.setReturnType("String");
+testSqlFieldsQuery = function() {
+    TestUtils.startIgniteNode().then(function(ignite) {
+        var qry = new SqlFieldsQuery("select concat(firstName, ' ', lastName) from Person");
 
         var fullRes = [];
 
         function onQuery(cursor) {
-            console.log("RESULT PAGE=" + cursor.page())
-           fullRes = fullRes.concat(cursor.page());
+            var page = cursor.page();
 
-            if (cursor.isFinished()) {
-                console.log("Result=" + JSON.stringify(fullRes));
+            fullRes = fullRes.concat(page);
 
-                assert(fullRes.length === 1, "Result length is not correct" +
+            if (cursor.isFinished()) {
+                assert(fullRes.length === 4, "Result length is not correct" +
                     "[expected=1, val = " + fullRes.length + "]");
 
-                assert(fullRes[0]["key"] === "key0", "Result value for key is not correct
"+
-                    "[expected=key0, real=" + fullRes[0]["key"] + "]");
+                fullRes.sort();
+
+                assert(fullRes[0].indexOf("Jane Doe") > -1,
+                    "Result does not contain Jane Doe [res=" + fullRes[0] + "]");
 
-                assert(fullRes[0]["value"] === "val0", "Result value for key is not correct
"+
-                    "[expected=val0, real=" + fullRes[0]["value"] + "]");
+                console.log("Result: " + JSON.stringify(fullRes));
 
                 TestUtils.testDone();
 
                 return;
             }
 
-            var nextPromise = cursor.next();
-
-            nextPromise.then(onQuery);
-
+            cursor.next().then(onQuery);
         }
 
-        ignite.cachePromise("mycache").query(qry).then(onQuery);
-    }
-
-    function put(error, ignite) {
-        assert(error == null, "error on put [err=" + error + "]");
-
-        ignite.cache("mycache").put("key0", "val0", sqlQuery.bind(null, ignite))
-    }
-
-    TestUtils.startIgniteNode(put);
-}
-
-testSqlFieldsQuery = function() {
-    function sqlFieldsQuery(error, ignite) {
-        assert(error == null, "error on sqlfields query [err=" + error + "]");
-
-        var qry = new SqlFieldsQuery("select concat(firstName, ' ', lastName) from Person");
-
-        var fullRes = [];
-
-        qry.on("page", function(res) {
-            console.log("PAGE:" + res);
-            fullRes = fullRes.concat(res);
-        });
-
-        qry.on("end", function(err) {
-            assert(err === null, "Error on query [err=" + err + "].");
-
-            assert(fullRes.length === 4, "Result length is not correct" +
-                "[expected=1, val = " + fullRes.length + "]");
-
-            fullRes.sort();
-
-            assert(fullRes[0].indexOf("Jane Doe") > -1,
-                "Result does not contain Jane Doe [res=" + fullRes[0] + "]");
-
-            TestUtils.testDone();
-        });
-
-        ignite.cache("person").query(qry);
-    }
-
-    TestUtils.startIgniteNode(sqlFieldsQuery.bind(null));
+        ignite.cache("person").query(qry).then(onQuery);
+    }).catch(function(err) {
+        assert(err === null, err);
+    });
 }
 
 testSqlQueryWithParams = function() {
-    function sqlQueryWithParams(error, ignite) {
-        assert(error == null, "error on sql query [err=" + error + "]");
-
+    TestUtils.startIgniteNode().then(function(ignite) {
         var qry = new SqlQuery("salary > ? and salary <= ?");
 
         qry.setReturnType("Person");
@@ -157,25 +110,31 @@ testSqlQueryWithParams = function() {
 
         var fullRes = [];
 
-        qry.on("page", function(res) {
-            fullRes = fullRes.concat(res);
-        });
+        function onQuery(cursor) {
+            var page = cursor.page();
+
+            fullRes = fullRes.concat(page);
+
+            if (cursor.isFinished()) {
+                assert(fullRes.length === 2, "Result length is not correct" +
+                    "[expected=1, val = " + fullRes.length + "]");
 
-        qry.on("end", function(err) {
-            assert(err === null, "Error on query [err=" + err + "].");
+                assert(((fullRes[0]["value"]["firstName"].indexOf("Jane") > -1) ||
+                    (fullRes[0]["value"]["firstName"].indexOf("John") > -1)),
+                    "Result does not contain Jane and John [res=" + fullRes[0]["value"]["firstName"]
+ "]");
 
-            assert(fullRes.length === 2, "Result length is not correct" +
-                "[expected=1, val = " + fullRes.length + "]");
+                console.log("Result: " + JSON.stringify(fullRes));
 
-            assert(((fullRes[0]["value"]["firstName"].indexOf("Jane") > -1) ||
-                (fullRes[0]["value"]["firstName"].indexOf("John") > -1)),
-                "Result does not contain Jane and John [res=" + fullRes[0]["value"]["firstName"]
+ "]");
+                TestUtils.testDone();
 
-            TestUtils.testDone();
-        });
+                return;
+            }
 
-        ignite.cache("person").query(qry);
-    }
+            cursor.next().then(onQuery);
+        }
 
-    TestUtils.startIgniteNode(sqlQueryWithParams.bind(null));
+        ignite.cache("person").query(qry).then(onQuery);
+    }).catch(function(err) {
+        assert(err === null, err);
+    });
 }
\ No newline at end of file


Mime
View raw message