[tracker] tests/libtracker-miner: Mock connection to inject results in the code



commit a5b007f74cf02118095008ccf3a7d1db0c62f093
Author: Ivan Frade <ivan frade gmail com>
Date:   Tue Nov 1 15:46:55 2011 +0200

    tests/libtracker-miner: Mock connection to inject results in the code
    
    Implementation of the Tracker.Sparql.Connection interface that allows
    to set predefined results on it.

 tests/libtracker-miner/Makefile.am                 |   14 +++-
 .../libtracker-miner/tracker-connection-mock.vala  |   99 ++++++++++++++++++++
 2 files changed, 112 insertions(+), 1 deletions(-)
---
diff --git a/tests/libtracker-miner/Makefile.am b/tests/libtracker-miner/Makefile.am
index b85fd01..8fb3af2 100644
--- a/tests/libtracker-miner/Makefile.am
+++ b/tests/libtracker-miner/Makefile.am
@@ -47,10 +47,22 @@ LDADD =                                                \
 libtracker_miner_tests_la_VALAFLAGS =                  \
 	--header tracker-miner-mock.h                  \
 	--pkg glib-2.0                                 \
+	--pkg gio-2.0                                  \
+	--pkg gio-unix-2.0                             \
+	--pkg posix				       \
+	$(top_srcdir)/src/libtracker-sparql/tracker-sparql-$(TRACKER_API_VERSION).vapi \
 	$(BUILD_VALAFLAGS)
 
 libtracker_miner_tests_la_SOURCES =                    \
-	tracker-miner-mock.vala
+	tracker-miner-mock.vala			       \
+	tracker-connection-mock.vala
+
+libtracker_miner_tests_la_LIBADD = 		       \
+	$(top_builddir)/src/libtracker-data/libtracker-data.la \
+	$(top_builddir)/src/libtracker-common/libtracker-common.la \
+	$(top_builddir)/src/libtracker-sparql-backend/libtracker-sparql- TRACKER_API_VERSION@.la \
+	$(BUILD_LIBS)
+
 
 tracker_crawler_test_SOURCES =                         \
 	tracker-crawler-test.c
diff --git a/tests/libtracker-miner/tracker-connection-mock.vala b/tests/libtracker-miner/tracker-connection-mock.vala
new file mode 100644
index 0000000..d60275c
--- /dev/null
+++ b/tests/libtracker-miner/tracker-connection-mock.vala
@@ -0,0 +1,99 @@
+using GLib;
+using Tracker;
+
+
+public class TrackerMockResults : Tracker.Sparql.Cursor {
+	int rows;
+	int current_row = -1;
+	string[,] results;
+	string[] var_names;
+	Sparql.ValueType[] types;
+	int cols;
+
+	public TrackerMockResults (owned string[,] results, int rows, int cols, string[] var_names, Sparql.ValueType[] types) {
+		this.rows = rows;
+		this.cols = cols;
+		this.results = (owned) results;
+		this.types = types;
+		this.var_names = var_names;
+	}
+
+	public override int n_columns { get { return cols; } }
+
+	public override Sparql.ValueType get_value_type (int column)
+	requires (current_row >= 0) {
+		return this.types[column];
+	}
+
+	public override unowned string? get_variable_name (int column)
+	requires (current_row >= 0) {
+		return this.var_names[column];
+	}
+
+	public override unowned string? get_string (int column, out long length = null)
+	requires (current_row >= 0) {
+		unowned string str;
+
+		str = results[current_row, column];
+
+		if (&length != null) {
+			length = str.length;
+		}
+
+		return str;
+	}
+
+	public override bool next (Cancellable? cancellable = null) throws GLib.Error {
+		if (current_row >= rows - 1) {
+			return false;
+		}
+		current_row++;
+		return true;
+	}
+
+	public override async bool next_async (Cancellable? cancellable = null) throws GLib.Error {
+		/* This cursor isn't blocking, it's fine to just call next here */
+		return next (cancellable);
+	}
+
+	public override void rewind () {
+		current_row = 0;
+	}
+}
+
+
+
+
+public class TrackerMockConnection : Sparql.Connection {
+
+    TrackerMockResults results = null;
+    TrackerMockResults hardcoded = new TrackerMockResults ({{"11", "12"}, {"21", "22"}}, 2, 2,
+                                                           {"artist", "album"}, 
+                                                           {Sparql.ValueType.STRING, Sparql.ValueType.STRING});
+
+	public override Sparql.Cursor query (string sparql, 
+                                  Cancellable? cancellable = null) 
+    throws Sparql.Error, IOError, DBusError {
+        if (this.results != null) {
+            return results;
+        } else {
+            return hardcoded;
+        }
+    }
+
+
+	public async override Sparql.Cursor query_async (string sparql, Cancellable? cancellable = null) 
+    throws Sparql.Error, IOError, DBusError {
+        if (this.results != null) {
+            return results;
+        } else {
+            return hardcoded;
+        }
+    }
+
+
+    public void set_results (TrackerMockResults results) {
+        this.results = results;
+    }
+
+}
\ No newline at end of file



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