summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2023-01-16 00:41:07 +0100
committerCarlos Garnacho <carlosg@gnome.org>2023-01-17 11:29:27 +0100
commitc655107d799c5cfb324e68ebdccefa7edd083bb7 (patch)
treee9ff6b42b1321642b0d5e24bce3f90f964fd85ea /tests
parent62b2d320b2d569a8c3966b350b36211102da0d57 (diff)
downloadtracker-c655107d799c5cfb324e68ebdccefa7edd083bb7.tar.gz
libtracker-sparql: Increase test coverage of TrackerResource
Diffstat (limited to 'tests')
-rw-r--r--tests/libtracker-sparql/tracker-resource-test.c210
1 files changed, 208 insertions, 2 deletions
diff --git a/tests/libtracker-sparql/tracker-resource-test.c b/tests/libtracker-sparql/tracker-resource-test.c
index c08cd150a..d6e3f4125 100644
--- a/tests/libtracker-sparql/tracker-resource-test.c
+++ b/tests/libtracker-sparql/tracker-resource-test.c
@@ -21,7 +21,7 @@
#include <locale.h>
-#include <libtracker-sparql/tracker-resource.h>
+#include <libtracker-sparql/tracker-sparql.h>
static void
test_resource_get_empty (void)
@@ -159,8 +159,9 @@ test_resource_get_set_pointer_validation (void)
static void
test_resource_serialization (void)
{
- TrackerResource *res, *child, *child2, *copy;
+ TrackerResource *res, *child, *child2, *child3, *copy;
GVariant *variant, *variant2;
+ GValue value = G_VALUE_INIT;
/* Create sample data */
res = tracker_resource_new (NULL);
@@ -169,6 +170,13 @@ test_resource_serialization (void)
tracker_resource_set_boolean (res, "nfo:isBootable", TRUE);
tracker_resource_set_int (res, "nie:usageCounter", 4);
tracker_resource_set_int64 (res, "nie:contentSize", 42);
+ tracker_resource_set_uri (res, "dc:relation", "tracker:");
+
+ g_value_init (&value, G_TYPE_INT);
+ g_value_set_int (&value, 1);
+ tracker_resource_add_gvalue (res, "nie:usageCounter", &value);
+ g_value_unset (&value);
+
tracker_resource_add_uri (res, "rdf:type", "nfo:Audio");
tracker_resource_add_uri (res, "rdf:type", "nfo:Media");
@@ -180,6 +188,16 @@ test_resource_serialization (void)
tracker_resource_set_string (child2, "nie:title", "baz");
tracker_resource_set_take_relation (child, "nfo:belongsToContainer", child2);
+ child3 = tracker_resource_new ("_:a");
+ tracker_resource_add_string (child3, "nie:title", "blah");
+ tracker_resource_add_double (child3, "nfo:duration", 12.9);
+ tracker_resource_add_boolean (res, "nfo:isBootable", FALSE);
+ tracker_resource_add_int (res, "nie:usageCounter", 5);
+ tracker_resource_add_int64 (res, "nie:contentSize", 420);
+
+ tracker_resource_add_relation (child, "nie:interpretedAs", g_object_ref (child2));
+ tracker_resource_add_relation (child, "nie:interpretedAs", child3);
+
/* Ensure multiple serialize calls produce the same variant */
variant = tracker_resource_serialize (res);
g_assert_true (variant != NULL);
@@ -197,6 +215,7 @@ test_resource_serialization (void)
g_assert_cmpstr (tracker_resource_get_first_string (copy, "nie:title"), ==, "foo");
g_assert_cmpint (tracker_resource_get_first_int (copy, "nie:usageCounter"), ==, 4);
g_assert_cmpint (tracker_resource_get_first_int64 (copy, "nie:contentSize"), ==, 42);
+ g_assert_cmpstr (tracker_resource_get_first_uri (copy, "dc:relation"), ==, "tracker:");
child = tracker_resource_get_first_relation (copy, "nie:isStoredAs");
g_assert_true (child != NULL);
@@ -256,6 +275,189 @@ test_resource_iri_valid_chars (void)
g_object_unref (resource);
}
+static void
+test_resource_compare (void)
+{
+ TrackerResource *res1, *res2, *res3;
+
+ res1 = tracker_resource_new ("http://example.com/a");
+ res2 = tracker_resource_new ("http://example.com/b");
+ res3 = tracker_resource_new ("http://example.com/c");
+
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res1, tracker_resource_get_identifier (res1)), ==, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res1, tracker_resource_get_identifier (res2)), <, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res1, tracker_resource_get_identifier (res3)), <, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res2, tracker_resource_get_identifier (res1)), >, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res2, tracker_resource_get_identifier (res2)), ==, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res2, tracker_resource_get_identifier (res3)), <, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res3, tracker_resource_get_identifier (res1)), >, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res3, tracker_resource_get_identifier (res2)), >, 0);
+ g_assert_cmpint (tracker_resource_identifier_compare_func (res3, tracker_resource_get_identifier (res3)), ==, 0);
+
+ g_object_unref (res1);
+ g_object_unref (res2);
+ g_object_unref (res3);
+}
+
+static void
+deserialize_cb (GObject *source,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GError *error = NULL;
+
+ tracker_sparql_connection_deserialize_finish (TRACKER_SPARQL_CONNECTION (source),
+ res, &error);
+ g_assert_no_error (error);
+
+ g_main_loop_quit (user_data);
+}
+
+static void
+deserialize (TrackerSparqlConnection *connection,
+ const gchar *rdf,
+ TrackerRdfFormat rdf_format,
+ const gchar *default_graph)
+{
+ GMainLoop *loop;
+ GInputStream *stream;
+
+ stream = g_memory_input_stream_new_from_data (rdf, strlen (rdf), NULL);
+
+ loop = g_main_loop_new (NULL, FALSE);
+ tracker_sparql_connection_deserialize_async (connection,
+ 0,
+ rdf_format,
+ default_graph,
+ stream,
+ NULL,
+ deserialize_cb,
+ loop);
+
+ g_main_loop_run (loop);
+ g_main_loop_unref (loop);
+ g_object_unref (stream);
+}
+
+static void
+test_resource_load (void)
+{
+ TrackerResource *resource, *content, *artist, *album;
+ gchar *ttl, *trig, *json_ld, *sparql;
+ TrackerSparqlConnection *connection;
+ TrackerNamespaceManager *namespaces;
+ GDateTime *datetime;
+ GFile *nepomuk;
+ GError *error = NULL;
+ guint i, j;
+ gchar *graphs[] = {
+ "graph:direct",
+ "graph:sparql",
+ "graph:turtle",
+ "graph:trig",
+ "graph:json",
+ };
+
+ datetime = g_date_time_new_utc (2020, 01, 01, 01, 01, 01);
+
+ /* Create set of resources to insert */
+ resource = tracker_resource_new ("file://song.mp3");
+ tracker_resource_set_uri (resource, "rdf:type", "nfo:FileDataObject");
+ tracker_resource_set_string (resource, "nfo:fileName", "song.mp3");
+ tracker_resource_set_datetime (resource, "nfo:fileCreated", datetime);
+ tracker_resource_set_int (resource, "nie:byteSize", 123456);
+
+ content = tracker_resource_new ("inode:123");
+ tracker_resource_set_uri (content, "rdf:type", "nmm:MusicPiece");
+ tracker_resource_add_uri (content, "rdf:type", "nfo:Audio");
+ tracker_resource_add_uri (content, "rdf:type", "nfo:Media");
+ tracker_resource_set_string (content, "nie:title", "Song");
+ tracker_resource_set_string (content, "nfo:codec", "MP3");
+ tracker_resource_set_int (content, "nfo:duration", 123);
+ tracker_resource_set_int64 (content, "nfo:averageBitrate", 320000);
+ tracker_resource_set_double (content, "nfo:sampleRate", 999.99);
+ tracker_resource_set_boolean (content, "nmm:uPnPShared", FALSE);
+
+ artist = tracker_resource_new ("urn:artist");
+ tracker_resource_set_uri (artist, "rdf:type", "nmm:Artist");
+ tracker_resource_set_string (artist, "nmm:artistName", "Artist");
+
+ album = tracker_resource_new ("urn:album");
+ tracker_resource_set_uri (album, "rdf:type", "nmm:MusicAlbum");
+ tracker_resource_set_string (album, "nie:title", "Album");
+
+ tracker_resource_set_take_relation (resource, "nie:interpretedAs", content);
+ tracker_resource_set_relation (content, "nie:isStoredAs", resource);
+ tracker_resource_set_take_relation (content, "nmm:musicAlbum", album);
+ tracker_resource_add_take_relation (content, "nmm:artist", artist);
+
+ /* Create connection */
+ nepomuk = g_file_new_for_path (TEST_ONTOLOGIES_DIR);
+ connection = tracker_sparql_connection_new (0, NULL, nepomuk, NULL, &error);
+ g_assert_no_error (error);
+ namespaces = tracker_sparql_connection_get_namespace_manager (connection);
+
+ /* Get RDF/SPARQL output for the resource */
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ ttl = tracker_resource_print_turtle (resource, NULL);
+ trig = tracker_resource_print_rdf (resource, namespaces, TRACKER_RDF_FORMAT_TRIG, "graph:trig");
+ json_ld = tracker_resource_print_jsonld (resource, NULL);
+ sparql = tracker_resource_print_sparql_update (resource, NULL, "graph:sparql");
+ G_GNUC_END_IGNORE_DEPRECATIONS
+
+ /* Insert data in separate graphs for comparisons */
+ tracker_sparql_connection_update_resource (connection, "graph:direct", resource, NULL, &error);
+ g_assert_no_error (error);
+ tracker_sparql_connection_update (connection, sparql, NULL, &error);
+ g_assert_no_error (error);
+ deserialize (connection, ttl, TRACKER_RDF_FORMAT_TURTLE, "graph:turtle");
+ deserialize (connection, trig, TRACKER_RDF_FORMAT_TRIG, NULL);
+ deserialize (connection, json_ld, TRACKER_RDF_FORMAT_JSON_LD, "graph:json");
+
+ /* Diff every graph with every other */
+ for (i = 0; i < G_N_ELEMENTS (graphs); i++) {
+ for (j = 0; j < G_N_ELEMENTS (graphs); j++) {
+ TrackerSparqlCursor *cursor;
+ gchar *query;
+
+ if (i == j)
+ continue;
+
+ query = g_strdup_printf ("SELECT ?s ?p ?o {"
+ " GRAPH %s {"
+ " ?s ?p ?o ."
+ " FILTER (?p != nrl:added && ?p != nrl:modified)"
+ " } MINUS {"
+ " GRAPH %s {"
+ " ?s ?p ?o ."
+ " FILTER (?p != nrl:added && ?p != nrl:modified)"
+ " }"
+ " }"
+ "}",
+ graphs[i], graphs[j]);
+
+ cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
+ g_assert_no_error (error);
+ g_free (query);
+
+ /* Cursor should come up empty, all graphs have the same data */
+ g_assert_false (tracker_sparql_cursor_next (cursor, NULL, &error));
+ g_assert_no_error (error);
+
+ g_clear_object (&cursor);
+ }
+ }
+
+ g_free (ttl);
+ g_free (trig);
+ g_free (json_ld);
+ g_free (sparql);
+ g_date_time_unref (datetime);
+
+ tracker_sparql_connection_close (connection);
+ g_object_unref (connection);
+}
+
int
main (int argc,
char **argv)
@@ -278,6 +480,10 @@ main (int argc,
test_resource_serialization);
g_test_add_func ("/libtracker-sparql/tracker-resource/iri-valid-chars",
test_resource_iri_valid_chars);
+ g_test_add_func ("/libtracker-sparql/tracker-resource/compare",
+ test_resource_compare);
+ g_test_add_func ("/libtracker-sparql/tracker-resource/load",
+ test_resource_load);
return g_test_run ();
}