[tracker/binary-log-2: 7/23] libtracker-db: Add tests for journal writer and reader



commit 47bac92fedc3c4b785dd708110cc21f9f32de456
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Jan 4 14:31:47 2010 +0000

    libtracker-db: Add tests for journal writer and reader

 tests/libtracker-db/.gitignore           |    2 +
 tests/libtracker-db/Makefile.am          |   24 ++-
 tests/libtracker-db/tracker-db-journal.c |  359 ++++++++++++++++++++++++++++++
 3 files changed, 382 insertions(+), 3 deletions(-)
---
diff --git a/tests/libtracker-db/.gitignore b/tests/libtracker-db/.gitignore
index 5bfa131..707332b 100644
--- a/tests/libtracker-db/.gitignore
+++ b/tests/libtracker-db/.gitignore
@@ -1 +1,3 @@
 tracker-index-writer
+tracker-db-journal
+tracker-store.journal
diff --git a/tests/libtracker-db/Makefile.am b/tests/libtracker-db/Makefile.am
index 8397c17..022453a 100644
--- a/tests/libtracker-db/Makefile.am
+++ b/tests/libtracker-db/Makefile.am
@@ -17,6 +17,8 @@ noinst_PROGRAMS = $(TEST_PROGS)
 #
 
 INCLUDES = 								\
+	-DTOP_SRCDIR=\"$(top_srcdir)\"					\
+	-DTOP_BUILDDIR=\"$(top_builddir)\"				\
 	-DG_LOG_DOMAIN=\"Tracker\"					\
 	-DTRACKER_COMPILATION						\
 	-I$(top_srcdir)/src						\
@@ -29,11 +31,27 @@ INCLUDES = 								\
 	$(DBUS_CFLAGS)							\
 	$(SQLITE3_CFLAGS)
 
-# TEST_PROGS +=								\
-# 	tracker-db-dbus							\
+TEST_PROGS +=								\
+	tracker-db-journal
+
 # 	tracker-db-manager-unattach					\
 # 	tracker-db-manager-attach					\
-# 	tracker-db-manager-custom					\
+# 	tracker-db-manager-custom					
+
+
+tracker_db_journal_SOURCES = 						\
+	tracker-db-journal.c
+
+tracker_db_journal_LDADD =						\
+	$(top_builddir)/src/libtracker-db/libtracker-db.la 		\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la 	\
+	$(top_builddir)/tests/common/libtracker-testcommon.la 		\
+	$(SQLITE3_LIBS)							\
+	$(GMODULE_LIBS)							\
+	$(GTHREAD_LIBS)							\
+	$(GLIB2_LIBS)							\
+	-lz
+
 #
 # tracker_db_manager_attach_SOURCES = 					\
 # 	tracker-db-manager-test-attach.c 				\
diff --git a/tests/libtracker-db/tracker-db-journal.c b/tests/libtracker-db/tracker-db-journal.c
new file mode 100644
index 0000000..516f0a6
--- /dev/null
+++ b/tests/libtracker-db/tracker-db-journal.c
@@ -0,0 +1,359 @@
+/*
+ * Copyright (C) 2008, Nokia (urho konttori nokia com)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include <glib/gstdio.h>
+
+#include <libtracker-common/tracker-crc32.h>
+
+#include <libtracker-db/tracker-db-journal.h>
+
+static void
+test_init_and_shutdown (void)
+{
+	gboolean result;
+
+	/* check double init/shutdown */
+	result = tracker_db_journal_init (NULL);
+	g_assert (result == TRUE);
+
+	result = tracker_db_journal_shutdown ();
+	g_assert (result == TRUE);
+
+	result = tracker_db_journal_init (NULL);
+	g_assert (result == TRUE);
+
+	result = tracker_db_journal_shutdown ();
+	g_assert (result == TRUE);
+}
+
+static void
+test_write_functions (void)
+{
+	gchar *path;
+	const gchar *filename;
+	gsize initial_size, actual_size;
+	gboolean result;
+
+	path = g_build_filename (TOP_SRCDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
+	g_unlink (path);
+
+	tracker_db_journal_init (path);
+
+        filename = tracker_db_journal_get_filename ();
+	g_assert (filename != NULL);
+	g_assert_cmpstr (filename, ==, path);
+
+	/* Size is 8 due to header */
+	actual_size = tracker_db_journal_get_size ();
+	g_assert_cmpint (actual_size, ==, 8);
+
+	/* Check with rollback, nothing is added */
+	initial_size = tracker_db_journal_get_size ();
+	result = tracker_db_journal_start_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (10, "http://resource";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (11, "http://predicate";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_delete_statement (10, 11, "test");
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_rollback_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+	actual_size = tracker_db_journal_get_size ();
+	g_assert_cmpint (initial_size, ==, actual_size);
+
+	/* Check with commit, somethign is added */
+	result = tracker_db_journal_start_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (12, "http://resource";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (13, "http://predicate";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (14, "http://resource";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_delete_statement_id (12, 13, 14);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_commit_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+	actual_size = tracker_db_journal_get_size ();
+	g_assert_cmpint (initial_size, !=, actual_size);
+
+	/* Test insert statement */
+	result = tracker_db_journal_start_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (15, "http://resource";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (16, "http://predicate";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_insert_statement (15, 16, "test");
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_commit_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+
+	/* Test insert id */
+	result = tracker_db_journal_start_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (17, "http://resource";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (18, "http://predicate";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_resource (19, "http://resource";);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_append_insert_statement_id (17, 18, 19);
+	g_assert_cmpint (result, ==, TRUE);
+	result = tracker_db_journal_commit_transaction ();
+	g_assert_cmpint (result, ==, TRUE);
+
+	/* Test fsync */
+	result = tracker_db_journal_fsync ();
+	g_assert_cmpint (result, ==, TRUE);
+
+	tracker_db_journal_shutdown ();
+
+	g_free (path);
+}
+
+static void
+test_read_functions (void)
+{
+	GError *error = NULL;
+	gchar *path;
+	gboolean result;
+	TrackerDBJournalEntryType type;
+	guint32 id, s_id, p_id, o_id;
+	const gchar *uri, *str;
+
+	path = g_build_filename (TOP_SRCDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
+
+	/* NOTE: we don't unlink here so we can use the data from the write tests */
+
+	/* Create an iterator */
+	result = tracker_db_journal_reader_init (path);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START);
+
+	/* First transaction */
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 12);
+	g_assert_cmpstr (uri, ==, "http://resource";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 13);
+	g_assert_cmpstr (uri, ==, "http://predicate";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 14);
+	g_assert_cmpstr (uri, ==, "http://resource";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_DELETE_STATEMENT_ID);
+
+	result = tracker_db_journal_reader_get_statement_id (&s_id, &p_id, &o_id);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (s_id, ==, 12);
+	g_assert_cmpint (p_id, ==, 13);
+	g_assert_cmpint (o_id, ==, 14);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
+
+	/* Second transaction */
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 15);
+	g_assert_cmpstr (uri, ==, "http://resource";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 16);
+	g_assert_cmpstr (uri, ==, "http://predicate";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_INSERT_STATEMENT);
+
+	result = tracker_db_journal_reader_get_statement (&s_id, &p_id, &str);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (s_id, ==, 15);
+	g_assert_cmpint (p_id, ==, 16);
+	g_assert_cmpstr (str, ==, "test");
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
+
+	/* Third transaction */
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 17);
+	g_assert_cmpstr (uri, ==, "http://resource";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 18);
+	g_assert_cmpstr (uri, ==, "http://predicate";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
+
+	result = tracker_db_journal_reader_get_resource (&id, &uri);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (id, ==, 19);
+	g_assert_cmpstr (uri, ==, "http://resource";);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_INSERT_STATEMENT_ID);
+
+	result = tracker_db_journal_reader_get_statement_id (&s_id, &p_id, &o_id);
+	g_assert_cmpint (result, ==, TRUE);
+	g_assert_cmpint (s_id, ==, 17);
+	g_assert_cmpint (p_id, ==, 18);
+	g_assert_cmpint (o_id, ==, 19);
+
+	result = tracker_db_journal_reader_next (&error);
+	g_assert_no_error (error);
+	g_assert_cmpint (result, ==, TRUE);
+
+	type = tracker_db_journal_reader_get_type ();
+	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
+
+	/* Shutdown */
+	result = tracker_db_journal_reader_shutdown ();
+	g_assert_cmpint (result, ==, TRUE);
+
+	g_free (path);
+}
+
+int
+main (int argc, char **argv) 
+{
+	int result;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+
+	g_test_add_func ("/libtracker-db/tracker-db-journal/init-and-shutdown",
+	                 test_init_and_shutdown);
+	g_test_add_func ("/libtracker-db/tracker-db-journal/write-functions",
+	                 test_write_functions);
+	g_test_add_func ("/libtracker-db/tracker-db-journal/read-functions",
+	                 test_read_functions);
+
+	result = g_test_run ();
+
+	return result;
+}



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