[tracker/wip/carlosg/remote-namespace-manager: 4/4] tests: Add tests for TrackerNamespaceManager API
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/carlosg/remote-namespace-manager: 4/4] tests: Add tests for TrackerNamespaceManager API
- Date: Sun, 1 May 2022 18:49:34 +0000 (UTC)
commit c73bb659da0086ebf46fdf4b538532c81e627e19
Author: Carlos Garnacho <carlosg gnome org>
Date: Sun Apr 17 17:13:59 2022 +0200
tests: Add tests for TrackerNamespaceManager API
Ensure all namespaces that can be obtained through public API
(the deprecated default, but also for each type of SPARQL connection)
contains everything that is expected.
tests/libtracker-sparql/meson.build | 11 +
tests/libtracker-sparql/tracker-namespaces-test.c | 265 ++++++++++++++++++++++
2 files changed, 276 insertions(+)
---
diff --git a/tests/libtracker-sparql/meson.build b/tests/libtracker-sparql/meson.build
index 9f6733f9e..311a5c969 100644
--- a/tests/libtracker-sparql/meson.build
+++ b/tests/libtracker-sparql/meson.build
@@ -65,3 +65,14 @@ tests += {
'exe': tracker_serialize_test,
'suite': ['sparql'],
}
+
+tracker_namespaces_test = executable('tracker-namespaces-test',
+ 'tracker-namespaces-test.c',
+ dependencies: [tracker_common_dep, tracker_sparql_dep],
+ c_args: libtracker_sparql_test_c_args + test_c_args)
+
+tests += {
+ 'name': 'namespaces',
+ 'exe': tracker_namespaces_test,
+ 'suite': ['sparql'],
+}
diff --git a/tests/libtracker-sparql/tracker-namespaces-test.c
b/tests/libtracker-sparql/tracker-namespaces-test.c
new file mode 100644
index 000000000..d37c82734
--- /dev/null
+++ b/tests/libtracker-sparql/tracker-namespaces-test.c
@@ -0,0 +1,265 @@
+#include <libtracker-sparql/tracker-sparql.h>
+
+typedef struct {
+ const char *prefix;
+ const char *expanded;
+} NamespaceSpec;
+
+static NamespaceSpec nepomuk[] = {
+ { "dc", TRACKER_PREFIX_DC },
+ { "mfo", TRACKER_PREFIX_MFO },
+ { "nao", TRACKER_PREFIX_NAO },
+ { "nco", TRACKER_PREFIX_NCO },
+ { "nfo", TRACKER_PREFIX_NFO },
+ { "nie", TRACKER_PREFIX_NIE },
+ { "nmm", TRACKER_PREFIX_NMM },
+ { "nrl", TRACKER_PREFIX_NRL },
+ { "osinfo", TRACKER_PREFIX_OSINFO },
+ { "rdf", TRACKER_PREFIX_RDF },
+ { "rdfs", TRACKER_PREFIX_RDFS },
+ { "slo", TRACKER_PREFIX_SLO },
+ { "tracker", TRACKER_PREFIX_TRACKER },
+ { "xsd", TRACKER_PREFIX_XSD },
+ { "fts", "http://tracker.api.gnome.org/ontology/v3/fts#" },
+ { NULL, NULL },
+};
+
+enum {
+ NAMESPACE_DEFAULT,
+ NAMESPACE_DIRECT,
+ NAMESPACE_DBUS,
+ NAMESPACE_HTTP,
+ N_NAMESPACES
+};
+
+char *names[] = {
+ "default",
+ "direct",
+ "dbus",
+ "http",
+};
+
+G_STATIC_ASSERT (G_N_ELEMENTS (names) == N_NAMESPACES);
+
+static GDBusConnection *dbus_conn = NULL;
+static TrackerSparqlConnection *dbus = NULL;
+static TrackerSparqlConnection *direct = NULL;
+static TrackerSparqlConnection *remote = NULL;
+static gboolean started = FALSE;
+
+static TrackerNamespaceManager *
+get_namespace (int namespace)
+{
+ TrackerNamespaceManager *manager;
+
+ switch (namespace) {
+ case NAMESPACE_DEFAULT:
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ manager = tracker_namespace_manager_get_default ();
+ G_GNUC_END_IGNORE_DEPRECATIONS
+ break;
+ case NAMESPACE_DIRECT:
+ manager = tracker_sparql_connection_get_namespace_manager (direct);
+ break;
+ case NAMESPACE_DBUS:
+ manager = tracker_sparql_connection_get_namespace_manager (dbus);
+ break;
+ case NAMESPACE_HTTP:
+ manager = tracker_sparql_connection_get_namespace_manager (remote);
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ return manager;
+}
+
+TrackerSparqlConnection *
+create_local_connection (void)
+{
+ TrackerSparqlConnection *conn;
+ GFile *ontology;
+ GError *error = NULL;
+
+ ontology = g_file_new_for_path (TEST_ONTOLOGIES_DIR);
+
+ conn = tracker_sparql_connection_new (0, NULL, ontology, NULL, &error);
+ g_object_unref (ontology);
+ g_assert_no_error (error);
+
+ return conn;
+}
+
+static gpointer
+thread_func (gpointer user_data)
+{
+ TrackerEndpointDBus *endpoint;
+ TrackerEndpointHttp *endpoint_http;
+ GMainContext *context;
+ GMainLoop *main_loop;
+
+ context = g_main_context_new ();
+ g_main_context_push_thread_default (context);
+
+ main_loop = g_main_loop_new (context, FALSE);
+
+ endpoint = tracker_endpoint_dbus_new (direct, dbus_conn, NULL, NULL, NULL);
+ if (!endpoint)
+ return NULL;
+
+ endpoint_http = tracker_endpoint_http_new (direct, 54323, NULL, NULL, NULL);
+ if (!endpoint_http)
+ return NULL;
+
+ started = TRUE;
+ g_main_loop_run (main_loop);
+
+ return NULL;
+}
+
+static void
+create_connections (void)
+{
+ GThread *thread;
+ GError *error = NULL;
+ const char *bus_name;
+
+ direct = create_local_connection ();
+
+ dbus_conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+ g_assert_no_error (error);
+
+ thread = g_thread_new (NULL, thread_func, NULL);
+
+ while (!started)
+ g_usleep (100);
+
+ bus_name = g_dbus_connection_get_unique_name (dbus_conn);
+ dbus = tracker_sparql_connection_bus_new (bus_name,
+ NULL, dbus_conn,
+ &error);
+ g_assert_no_error (error);
+
+ remote = tracker_sparql_connection_remote_new ("http://127.0.0.1:54323/sparql");
+ g_thread_unref (thread);
+}
+
+static void
+namespace_check_foreach (gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ NamespaceSpec *spec = user_data;
+ gboolean found = FALSE;
+ int i;
+
+ for (i = 0; spec[i].prefix; i++) {
+ if (g_strcmp0 (key, spec[i].prefix) == 0 &&
+ g_strcmp0 (value, spec[i].expanded) == 0) {
+ found = TRUE;
+ break;
+ }
+ }
+
+ if (!found) {
+ g_error ("prefix '%s' expanding to '%s' not found in spec",
+ (const char *) key, (const char *) value);
+ }
+}
+
+static void
+assert_spec_in_namespace (TrackerNamespaceManager *manager,
+ NamespaceSpec spec[])
+{
+ int i;
+
+ for (i = 0; spec[i].prefix != NULL; i++) {
+ const char *expanded;
+
+ if (!tracker_namespace_manager_has_prefix (manager, spec[i].prefix)) {
+ g_error ("prefix '%s' expanding to '%s' not found in namespaces",
+ spec[i].prefix, spec[i].expanded);
+ }
+
+ expanded = tracker_namespace_manager_lookup_prefix (manager, spec[i].prefix);
+ g_assert_cmpstr (expanded, ==, spec[i].expanded);
+ }
+}
+
+static void
+assert_namespace_matches_spec (TrackerNamespaceManager *manager,
+ NamespaceSpec spec[])
+{
+ tracker_namespace_manager_foreach (manager,
+ namespace_check_foreach,
+ spec);
+
+ /* Also test the other way around, that all elements in spec
+ * exist in the namespace manager.
+ */
+ assert_spec_in_namespace (manager, spec);
+}
+
+static void
+test_check_nepomuk (TrackerNamespaceManager *manager)
+{
+ assert_namespace_matches_spec (manager, nepomuk);
+}
+
+static void
+test_expand_compress (TrackerNamespaceManager *manager)
+{
+ char *compressed, *expanded;
+
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ manager = tracker_namespace_manager_get_default ();
+ G_GNUC_END_IGNORE_DEPRECATIONS
+
+ expanded = tracker_namespace_manager_expand_uri (manager, "rdfs:Resource");
+ compressed = tracker_namespace_manager_compress_uri (manager, expanded);
+ g_assert_cmpstr (compressed, ==, "rdfs:Resource");
+ g_assert_cmpstr (expanded, ==, "http://www.w3.org/2000/01/rdf-schema#Resource");
+ g_free (compressed);
+ g_free (expanded);
+}
+
+static void
+add_test (const gchar *prefix,
+ const gchar *domain,
+ TrackerNamespaceManager *manager,
+ GTestDataFunc func)
+{
+ char *name;
+
+ name = g_strconcat (prefix, "/", domain, NULL);
+ g_test_add_data_func (name, manager, func);
+ g_free (name);
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ int i;
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_message ("Testing Tracker namespaces manager");
+
+ create_connections ();
+
+ for (i = NAMESPACE_DEFAULT; i < N_NAMESPACES; i++) {
+ TrackerNamespaceManager *namespace = get_namespace (i);
+
+ add_test ("/libtracker-sparql/tracker-namespaces/check",
+ names[i],
+ namespace,
+ (GTestDataFunc) test_check_nepomuk);
+ add_test ("/libtracker-sparql/tracker-namespaces/expand-compress",
+ names[i],
+ namespace,
+ (GTestDataFunc) test_expand_compress);
+ }
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]