tracker-0.16.2/tests/libtracker-data/tracker-sparql-blank-test.c

No issues found

  1 /*
  2  * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <string.h>
 23 
 24 #include <glib.h>
 25 #include <gio/gio.h>
 26 
 27 #include <libtracker-data/tracker-data-manager.h>
 28 #include <libtracker-data/tracker-data-query.h>
 29 #include <libtracker-data/tracker-data-update.h>
 30 #include <libtracker-data/tracker-data.h>
 31 #include <libtracker-data/tracker-sparql-query.h>
 32 
 33 static void
 34 test_blank (void)
 35 {
 36 	GError *error;
 37 	GVariant *updates;
 38 	GVariantIter iter;
 39 	GVariant *rows;
 40 	guint len = 0;
 41 	gchar *solutions[3][3];
 42 
 43 	error = NULL;
 44 
 45 	tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 46 
 47 	/* initialization */
 48 	tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
 49 	                           NULL,
 50 	                           NULL,
 51 	                           FALSE,
 52 	                           FALSE,
 53 	                           100,
 54 	                           100,
 55 	                           NULL,
 56 	                           NULL,
 57 	                           NULL,
 58 	                           &error);
 59 
 60 	g_assert_no_error (error);
 61 
 62 	/* perform update in transaction */
 63 
 64 	updates = tracker_data_update_sparql_blank ("INSERT { _:foo a rdfs:Resource } "
 65 	                                            "INSERT { _:foo a rdfs:Resource . _:bar a rdfs:Resource } ",
 66 	                                            &error);
 67 	g_assert_no_error (error);
 68 	g_assert (updates != NULL);
 69 
 70 	g_variant_iter_init (&iter, updates);
 71 	while ((rows = g_variant_iter_next_value (&iter))) {
 72 		GVariantIter sub_iter;
 73 		GVariant *sub_value;
 74 
 75 		g_variant_iter_init (&sub_iter, rows);
 76 
 77 		while ((sub_value = g_variant_iter_next_value (&sub_iter))) {
 78 			gchar *a = NULL, *b = NULL;
 79 			GVariantIter sub_sub_iter;
 80 			GVariant *sub_sub_value;
 81 
 82 			g_variant_iter_init (&sub_sub_iter, sub_value);
 83 
 84 			while ((sub_sub_value = g_variant_iter_next_value (&sub_sub_iter))) {
 85 				g_variant_get (sub_sub_value, "{ss}", &a, &b);
 86 				solutions[len][0] = a;
 87 				solutions[len][1] = b;
 88 				len++;
 89 				g_assert_cmpint (len, <=, 3);
 90 				g_variant_unref (sub_sub_value);
 91 			}
 92 			g_variant_unref (sub_value);
 93 		}
 94 		g_variant_unref (rows);
 95 	}
 96 
 97 	g_assert_cmpint (len, ==, 3);
 98 
 99 	g_assert_cmpstr (solutions[0][0], ==, "foo");
100 	g_assert (solutions[0][1] != NULL);
101 
102 	g_assert_cmpstr (solutions[1][0], ==, "foo");
103 	g_assert (solutions[1][1] != NULL);
104 
105 	g_assert_cmpstr (solutions[2][0], ==, "bar");
106 	g_assert (solutions[2][1] != NULL);
107 
108 	/* cleanup */
109 
110 	g_free (solutions[0][0]);
111 	g_free (solutions[0][1]);
112 	g_free (solutions[1][0]);
113 	g_free (solutions[1][1]);
114 	g_free (solutions[2][0]);
115 	g_free (solutions[2][1]);
116 
117 	g_variant_unref (updates);
118 
119 	tracker_data_manager_shutdown ();
120 }
121 
122 int
123 main (int argc, char **argv)
124 {
125 	gint result;
126 	gchar *current_dir;
127 
128 	g_test_init (&argc, &argv, NULL);
129 
130 	current_dir = g_get_current_dir ();
131 
132 	g_setenv ("XDG_DATA_HOME", current_dir, TRUE);
133 	g_setenv ("XDG_CACHE_HOME", current_dir, TRUE);
134 	g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
135 
136 	g_test_add_func ("/libtracker-data/sparql-blank", test_blank);
137 
138 	/* run tests */
139 
140 	result = g_test_run ();
141 
142 	/* clean up */
143 	g_print ("Removing temporary data\n");
144 	g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
145 
146 	return result;
147 }