tracker-0.16.2/tests/libtracker-extract/tracker-guarantee-test.c

No issues found

  1 /*
  2  * Copyright (C) 2011, 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 Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2.1 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  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser 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 #include "config.h"
 20 #include <glib.h>
 21 #include <libtracker-sparql/tracker-sparql.h>
 22 #include <libtracker-extract/tracker-guarantee.h>
 23 
 24 typedef struct {
 25         gchar *file_uri;
 26         gchar *extracted_title;
 27         gchar *expected_title;
 28 } TestCase;
 29 
 30 TestCase test_cases_title [] = {
 31         { "file:///a/b/a_video_with_metadata.avi", "extracted title", "extracted title" },
 32 
 33 #ifdef GUARANTEE_METADATA 
 34         { "file:///a/b/a_video_with_no_metadata.avi", NULL, "a video with no metadata" },
 35         { "file:///a/b/a_video_with_no_metadata.avi", "", "a video with no metadata" },
 36         { "file:///a/b/a.video.with.no.metadata.avi", NULL, "a.video.with.no.metadata" },
 37         { "file:///a/b/a video without extension", NULL, "a video without extension" },
 38         { "file:///a/b/.hidden_file", NULL, "hidden file" },
 39 #endif
 40 
 41         { NULL, NULL, NULL}
 42 };
 43 
 44 /*
 45  * @uri of the file that is being processed
 46  * @value is the title returned by the extractor
 47  * @expected can be either the title of the extractor (if not NULL or empty) or calculated from the filename
 48  */
 49 static void
 50 internal_test_title (const gchar *uri,
 51                      const gchar *value,
 52                      const gchar *expected)
 53 {
 54         TrackerSparqlBuilder *builder;
 55         gchar                *sparql;
 56         gchar                *title_guaranteed;
 57 
 58         builder = tracker_sparql_builder_new_update ();
 59         tracker_sparql_builder_insert_open (builder, "test");
 60         tracker_sparql_builder_subject_iri (builder, "test://resource");
 61         g_assert (tracker_guarantee_title_from_file (builder, 
 62                                                      "nie:title",
 63                                                      value,
 64                                                      uri,
 65                                                      &title_guaranteed));
 66         tracker_sparql_builder_insert_close (builder);
 67 
 68         sparql = g_strdup_printf ("INSERT INTO <test> {\n<test://resource> nie:title \"%s\" .\n}\n",
 69                                   expected);
 70         g_assert_cmpstr (sparql,
 71                          ==,
 72                          tracker_sparql_builder_get_result (builder));
 73 
 74         g_assert_cmpstr (title_guaranteed,
 75                          ==,
 76                          expected);
 77 
 78         g_object_unref (builder);
 79         g_free (sparql);
 80 }
 81 
 82 static void
 83 internal_test_date (const gchar *uri,
 84                     const gchar *value)
 85 {
 86         TrackerSparqlBuilder *builder;
 87 
 88         builder = tracker_sparql_builder_new_update ();
 89         tracker_sparql_builder_insert_open (builder, "test");
 90         tracker_sparql_builder_subject_iri (builder, "test://resource");
 91         g_assert (tracker_guarantee_date_from_file_mtime (builder, 
 92                                                           "test:mtime",
 93                                                           value,
 94                                                           uri));
 95         tracker_sparql_builder_insert_close (builder);
 96         /* mtime can change in the file so we just check that the property is in the output */
 97         g_assert  (g_strstr_len (tracker_sparql_builder_get_result (builder), -1, "test:mtime"));
 98 
 99         g_object_unref (builder);
100 }
101 
102 static void
103 test_guarantee_title (void)
104 {
105         int i;
106 
107         for (i = 0; test_cases_title[i].file_uri != NULL; i++) {
108                 internal_test_title (test_cases_title[i].file_uri,
109                                      test_cases_title[i].extracted_title,
110                                      test_cases_title[i].expected_title);
111         }
112 
113 #ifdef GUARANTEE_METADATA        
114         g_print ("%d test cases (guarantee metadata enabled) ", i);
115 #else
116         g_print ("%d test cases (guarantee metadata disabled) ", i);
117 #endif
118 }
119 
120 static void
121 test_guarantee_date (void)
122 {
123 #ifdef GUARANTEE_METADATA
124         GFile *f;
125         gchar *uri;
126 #endif
127 
128         internal_test_date ("file:///does/not/matter/here", "2011-10-10T12:13:14Z0300");
129 
130 #ifdef GUARANTEE_METADATA        
131         f = g_file_new_for_path (TOP_SRCDIR "/tests/libtracker-extract/guarantee-mtime-test.txt");
132         uri = g_file_get_uri (f);
133         
134         internal_test_date (uri, NULL);
135         internal_test_date (uri, "");
136 
137         g_free (uri);
138         g_object_unref (f);
139 #endif
140 }
141 
142 
143 int
144 main (int argc, char** argv)
145 {
146         g_test_init (&argc, &argv, NULL);
147 
148         g_test_add_func ("/libtracker-extract/guarantee/title",
149                          test_guarantee_title);
150         g_test_add_func ("/libtracker-extract/guarantee/date",
151                          test_guarantee_date);
152 
153         return g_test_run ();
154 }