[tracker: 20/21] add test for the parsing erros that printed while parsing the ontology




commit cc504f7bc519b31c1dbd1a31e8552ddf15114012
Author: Abanoub Ghadban <abanoub gdb gmail com>
Date:   Sat Aug 21 12:57:10 2021 +0200

    add test for the parsing erros that printed while parsing the ontology
    
    The parsing errors that printed should be stored in a file that has the extension .errors.out.
    The test works by inspecting the error messages that printed using g_printerr() and compare them with the 
expected error messages that stored in *.errors.out file.

 .../ontology-error/parsing-errors-001.errors.out   |  13 +++
 .../ontology-error/parsing-errors-001.ontology     |  57 ++++++++++
 .../ontology-error/parsing-errors-001.out          |   1 +
 .../libtracker-data/tracker-ontology-error-test.c  | 118 ++++++++++++++++++++-
 4 files changed, 188 insertions(+), 1 deletion(-)
---
diff --git a/tests/libtracker-data/ontology-error/parsing-errors-001.errors.out 
b/tests/libtracker-data/ontology-error/parsing-errors-001.errors.out
new file mode 100644
index 000000000..4069d8a42
--- /dev/null
+++ b/tests/libtracker-data/ontology-error/parsing-errors-001.errors.out
@@ -0,0 +1,13 @@
+22:29: Unknown class http://www.w3.org/2001/XMLSchema#datetime
+~
+32:47: Unknown property http://example.org/ns#not_exist
+~
+42:23: Unknown class http://example.org/ns#a
+~
+48:41: Property http://example.org/ns#a_n_cardinality has multiple values while trying to add it as 
nrl:domainIndex in http://example.org/ns#C, this isn't supported
+~
+49:27: Unknown class http://example.org/ns#a
+~
+55:43: Unknown property http://example.org/ns#a_superprop_n
+~
+56:32: Unknown class http://example.org/ns#not_exist
diff --git a/tests/libtracker-data/ontology-error/parsing-errors-001.ontology 
b/tests/libtracker-data/ontology-error/parsing-errors-001.ontology
new file mode 100644
index 000000000..b8fa7b9a8
--- /dev/null
+++ b/tests/libtracker-data/ontology-error/parsing-errors-001.ontology
@@ -0,0 +1,57 @@
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix test: <http://example.org/ns#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+@prefix nrl: <http://tracker.api.gnome.org/ontology/v3/nrl#> .
+
+test: a nrl:Namespace, nrl:Ontology ;
+        nrl:lastModified "2010-03-23T11:00:04Z" ;
+        nrl:prefix "test" .
+
+test:A a rdfs:Class .
+
+test:a_string a rdf:Property ;
+    rdfs:comment "Property to test the conversions from string" ;
+    rdfs:domain test:A ;
+    rdfs:range  xsd:string ;
+    nrl:maxCardinality 1 .
+
+test:a_datetime a rdf:Property ;
+    rdfs:comment "Property to test the changes string/date" ;
+    rdfs:domain test:A ;
+    rdfs:range  xsd:datetime ; # Should be dateTime
+    nrl:maxCardinality 1 .
+
+test:a_int a rdf:Property ;
+    rdfs:comment "Property to test the changes string/int" ;
+    rdfs:domain test:A ;
+    rdfs:range  xsd:integer ;
+    nrl:maxCardinality 1 .
+
+# The property is not defined
+test:not_exist a nrl:InverseFunctionalProperty .
+
+test:a_n_cardinality a rdf:Property ;
+    rdfs:comment "Property to test the cardinality changes" ;
+    rdfs:domain test:A ;
+    rdfs:range xsd:string .
+# Max cardinality N because is not specified
+
+test:a_superprop a rdf:Property ;
+    rdfs:comment "To use as superproperty and test the superproperty changes in the subprop." ;
+    rdfs:domain test:a ;  # not defined class
+    rdfs:range xsd:string ;
+    nrl:maxCardinality 1 .
+
+# Can't make n cardinality property as a domain index
+test:C a rdfs:Class ;
+    nrl:domainIndex test:a_n_cardinality ;
+    rdfs:subClassOf test:a . # not defined class
+
+test:prop1 a rdf:property . # Should be rdf:Property
+
+test:c_subprop_n a rdf:Property ;
+     rdfs:comment "To test changes in the superproperty" ;
+     rdfs:subPropertyOf test:a_superprop_n ; # not defined property
+     rdfs:domain test:not_exist ; # not defined class
+     rdfs:range xsd:string .
diff --git a/tests/libtracker-data/ontology-error/parsing-errors-001.out 
b/tests/libtracker-data/ontology-error/parsing-errors-001.out
new file mode 100644
index 000000000..9501aa35a
--- /dev/null
+++ b/tests/libtracker-data/ontology-error/parsing-errors-001.out
@@ -0,0 +1 @@
+The ontology files contain 7 parsing error\(s\)
\ No newline at end of file
diff --git a/tests/libtracker-data/tracker-ontology-error-test.c 
b/tests/libtracker-data/tracker-ontology-error-test.c
index 6d93852ed..e3e11c4d2 100644
--- a/tests/libtracker-data/tracker-ontology-error-test.c
+++ b/tests/libtracker-data/tracker-ontology-error-test.c
@@ -40,9 +40,111 @@ const TestInfo tests[] = {
        { "ontology-error/unknown-prefix-002" },
        { "ontology-error/unknown-prefix-003" },
        { "ontology-error/incomplete-property-001" },
+       { "ontology-error/parsing-errors-001" },
        { NULL }
 };
 
+GString *recorded_error_msgs;
+GPrintFunc old_printerr_handler;
+
+static void
+printerr_handler (const gchar *string)
+{
+       gchar *error_msg = g_strdup (string);
+       g_strstrip (error_msg);
+
+       if (recorded_error_msgs->len)
+               g_string_append (recorded_error_msgs, "~\n");
+
+       g_string_append_printf (recorded_error_msgs, "%s\n", error_msg);
+       g_free (error_msg);
+}
+
+static void
+record_printed_errors ()
+{
+       recorded_error_msgs = g_string_new ("");
+       old_printerr_handler = g_set_printerr_handler (printerr_handler);
+}
+
+static void
+stop_error_recording (gchar **printed_error_msgs)
+{
+       g_set_printerr_handler (old_printerr_handler);
+
+       *printed_error_msgs = recorded_error_msgs->str;
+       g_string_free (recorded_error_msgs, FALSE);
+}
+
+static gchar*
+load_error_msgs (gchar *errors_path, gchar *ontology_path)
+{
+       GError *error = NULL;
+       gchar *raw_errors = NULL, *error_msg;
+       GString *prefixed_errors = NULL;
+       gchar *ret;
+       GFile *ontology_file = g_file_new_for_path (ontology_path);
+       gchar *ontology_uri = g_file_get_uri (ontology_file);
+
+       g_file_get_contents (errors_path, &raw_errors, NULL, &error);
+       g_assert_no_error (error);
+
+       error_msg = strtok (raw_errors, "~");
+       while (error_msg) {
+               if (!prefixed_errors) {
+                       prefixed_errors = g_string_new ("");
+               } else {
+                       g_string_append (prefixed_errors, "~\n");
+               }
+
+               g_strstrip (error_msg);
+               g_string_append_printf (prefixed_errors, "parsing-error: %s:%s\n",
+                                       ontology_uri, error_msg);
+               error_msg = strtok (NULL, "~");
+       }
+
+       ret = prefixed_errors->str;
+
+       g_string_free (prefixed_errors, FALSE);
+       g_free (raw_errors);
+       g_object_unref (ontology_file);
+       g_free (ontology_uri);
+       return ret;
+}
+
+static void
+assert_same_output (gchar *output1, gchar* output2)
+{
+       GError *error = NULL;
+       gchar *quoted_output1, *quoted_output2;
+       gchar *command_line;
+       gchar *quoted_command_line;
+       gchar *shell;
+       gchar *diff;
+
+       if (strcmp (output1, output2) == 0)
+               return;
+
+       /* print result difference */
+       quoted_output1 = g_shell_quote (output1);
+       quoted_output2 = g_shell_quote (output2);
+
+       command_line = g_strdup_printf ("diff -u <(echo %s) <(echo %s)", quoted_output1, quoted_output2);
+       quoted_command_line = g_shell_quote (command_line);
+       shell = g_strdup_printf ("bash -c %s", quoted_command_line);
+       g_spawn_command_line_sync (shell, &diff, NULL, NULL, &error);
+       g_assert_no_error (error);
+
+       g_error ("%s", diff);
+
+       g_free (quoted_output1);
+       g_free (quoted_output2);
+       g_free (command_line);
+       g_free (quoted_command_line);
+       g_free (shell);
+       g_free (diff);
+}
+
 static void
 ontology_error_helper (GFile *ontology_location, char *error_path)
 {
@@ -105,8 +207,11 @@ test_ontology_error (void)
                gchar *source_ontology_filename = g_strconcat (tests[i].test_name, ".ontology", NULL);
                gchar *source_ontology_path = g_build_path (G_DIR_SEPARATOR_S, prefix, 
source_ontology_filename, NULL);
                gchar *error_filename = g_strconcat (tests[i].test_name, ".out", NULL);
+               gchar *errors_filename = g_strconcat (tests[i].test_name, ".errors.out", NULL);
                gchar *error_path = g_build_path (G_DIR_SEPARATOR_S, prefix, error_filename, NULL);
+               gchar *errors_path = g_build_path (G_DIR_SEPARATOR_S, prefix, errors_filename, NULL);
                gchar *from, *to;
+               gchar *expected_error_msgs = "", *printed_error_msgs;
 
                source_ontology_file = g_file_new_for_path (source_ontology_path);
 
@@ -114,7 +219,6 @@ test_ontology_error (void)
                to = g_file_get_path (test_ontology_file);
                g_debug ("copy %s to %s", from, to);
                g_free (from);
-               g_free (to);
 
                // Copy the ontology to the temporary ontologies directory
                g_file_copy (source_ontology_file, test_ontology_file, G_FILE_COPY_OVERWRITE, NULL, NULL, 
NULL, &error);
@@ -122,12 +226,24 @@ test_ontology_error (void)
                g_assert_no_error (error);
                g_assert_cmpint (g_chmod (test_ontology_path, 0666), ==, 0);
 
+               /* The error messages are prefixed with the ontology path which contain the error
+                * So, it needs the ontology path to prefix the expected error messages with it */
+               if (g_file_test (errors_path, G_FILE_TEST_EXISTS))
+                       expected_error_msgs = load_error_msgs (errors_path, to);
+
+               record_printed_errors ();
                ontology_error_helper (test_schemas, error_path);
+               stop_error_recording (&printed_error_msgs);
 
+               assert_same_output (expected_error_msgs, printed_error_msgs);
+
+               g_free (to);
                g_free (source_ontology_filename);
                g_free (source_ontology_path);
                g_free (error_filename);
+               g_free (errors_filename);
                g_free (error_path);
+               g_free (errors_path);
                g_object_unref (source_ontology_file);
        }
 


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