[the-board/tracker: 9/14] [model] Initial implementation of TrackerClient with unit tests



commit 51bf1d1b71a1b4e3817b5243b48ba0caf72092e4
Author: Lucas Rocha <lucasr lucasr org>
Date:   Wed May 11 23:02:39 2011 +0100

    [model] Initial implementation of TrackerClient with unit tests

 src/js/model/trackerClient.js |   34 +++++++++----
 test/js/testTrackerClient.js  |  113 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 10 deletions(-)
---
diff --git a/src/js/model/trackerClient.js b/src/js/model/trackerClient.js
index 880dadd..c0009f8 100644
--- a/src/js/model/trackerClient.js
+++ b/src/js/model/trackerClient.js
@@ -85,32 +85,45 @@ TrackerClient.prototype = {
             throw new Error("Tracker is not ready for queries");
         }
 
-        this._connection.query_async(query, null,
+        if (!query) {
+            throw new Error("Cannot execute empty query");
+        }
+
+        this._connection.query_async(query,
+                                     GLib.PRIORITY_DEFAULT,
+                                     null,
                                      Lang.bind(this,
                                                this._onQueryAsync,
                                                onReturn),
                                      null);
     },
 
-    update : function(query, onReturn) {
+    update : function(update, onReturn) {
         if (this._state != State.READY) {
             throw new Error("Tracker is not ready for updates");
         }
 
-        this._connection.update_async(query, null,
+        if (!update) {
+            throw new Error("Cannot execute empty update");
+        }
+
+        this._connection.update_async(update,
+                                      GLib.PRIORITY_DEFAULT,
+                                      null /* cancellable */,
                                       Lang.bind(this,
                                                 this._onUpdateAsync,
                                                 onReturn),
-                                     null);
+                                      null);
     },
 
-    _onQueryAsync : function(source, result, data, onReturn) {
+    _onQueryAsync : function(source, result, onReturn) {
         let cursor = null;
 
         try {
-            cursor = Tracker.SparqlConnection.query_finish(result);
+            cursor = this._connection.query_finish(result);
+            log("Tracker: successfull query in Tracker");
         } catch(e) {
-            log("Tracker: failed to run query in Tracker");
+            log("Tracker: failed to run query in Tracker " + e);
         }
 
         if (onReturn) {
@@ -118,13 +131,14 @@ TrackerClient.prototype = {
         }
     },
 
-    _onUpdateAsync : function(source, result, data, onReturn) {
+    _onUpdateAsync : function(source, result, onReturn) {
         let success = true;
 
         try {
-            Tracker.SparqlConnection.update_finish(result);
+            this._connection.update_finish(result);
+            log("Tracker: successfull update in Tracker");
         } catch(e) {
-            log("Tracker: failed to run update in Tracker");
+            log("Tracker: failed to run update in Tracker " + e);
             success = false;
         }
 
diff --git a/test/js/testTrackerClient.js b/test/js/testTrackerClient.js
new file mode 100644
index 0000000..3dc8eba
--- /dev/null
+++ b/test/js/testTrackerClient.js
@@ -0,0 +1,113 @@
+const GLib = imports.gi.GLib;
+const Mainloop = imports.mainloop;
+const TrackerClient = imports.model.trackerClient;
+
+const _SOME_VALID_QUERY  = "SELECT ?r WHERE { ?r nie:title 'abc' }";
+const _SOME_VALID_UPDATE = "INSERT { <abc> a nie:InformationElement ; nie:title 'abc' }";
+
+function _waitUntilReady(tracker) {
+    let onTrackerStateChanged = function() {
+        if (tracker.state == TrackerClient.State.READY) {
+            Mainloop.quit("tracker-client");
+        }
+    };
+
+    if (tracker.state != TrackerClient.State.READY) {
+        tracker.connect("state-changed",
+                        onTrackerStateChanged);
+
+        Mainloop.run("tracker-client");
+    }
+}
+
+function _runUpdate(tracker, update) {
+    let success = false;
+
+    let onResult = function(aSuccess) {
+        success = aSuccess;
+        Mainloop.quit("tracker-client");
+    };
+
+    tracker.update(update, onResult);
+
+    Mainloop.run("tracker-client");
+
+    return success;
+}
+
+function _runQuery(tracker, query) {
+    let cursor = null;
+
+    let onResult = function(aCursor) {
+        cursor = aCursor;
+        Mainloop.quit("tracker-client");
+    };
+
+    tracker.query(query, onResult);
+
+    Mainloop.run("tracker-client");
+
+    return cursor;
+}
+
+function testInitialState() {
+    let tracker = new TrackerClient.TrackerClient();
+    assertEquals(TrackerClient.State.CONNECTING, tracker.state);
+}
+
+function testEmptyUpdate() {
+    let tracker = new TrackerClient.TrackerClient();
+    assertRaises(function() { tracker.update(); });;
+    assertRaises(function() { tracker.update(""); });;
+}
+
+function testEmptyQuery() {
+    let tracker = new TrackerClient.TrackerClient();
+    assertRaises(function() { tracker.query(); });;
+    assertRaises(function() { tracker.query(""); });;
+}
+
+function testQueryBeforeReadyState() {
+    let tracker = new TrackerClient.TrackerClient();
+    assertRaises(function() { tracker.query(_SOME_VALID_UPDATE); });;
+}
+
+function testUpdateBeforeReadyState() {
+    let tracker = new TrackerClient.TrackerClient();
+    assertRaises(function() { tracker.query(_SOME_VALID_QUERY); });;
+}
+
+function testReadyState() {
+    let tracker = new TrackerClient.TrackerClient();
+
+    _waitUntilReady(tracker);
+
+    assertEquals(TrackerClient.State.READY, tracker.state);
+}
+
+function testQuery() {
+    let tracker = new TrackerClient.TrackerClient();
+
+    _waitUntilReady(tracker);
+
+    let update =
+        "INSERT { <abc> a nie:InformationElement ; nie:title 'abc' }";
+
+    let updateSuccess = _runUpdate(tracker, update);
+    assertTrue(updateSuccess);
+
+    let query =
+        "SELECT ?r WHERE { ?r nie:title 'abc' }";
+
+    let cursor = _runQuery(tracker, query);
+
+    let n = 0;
+    while (cursor.next(null)) { n++; }
+
+    assertEquals(1, n);
+}
+
+function testUpdate() {
+}
+
+gjstestRun();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]