2006-12-03 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / tests / test-rhythmdb.c
blob65f46b646991c10c0fa37baa460349a469a6f182
1 #include <check.h>
2 #include <gtk/gtk.h>
3 #include <string.h>
4 #include <glib/gi18n.h>
6 #include "test-utils.h"
8 #include "rb-debug.h"
9 #include "rb-file-helpers.h"
10 #include "rb-util.h"
12 #include "rhythmdb.h"
13 #include "rhythmdb-tree.h"
14 #include "rhythmdb-query-model.h"
19 static void
20 set_true (RhythmDBEntry *entry, gboolean *b)
22 *b = TRUE;
27 /* tests */
28 START_TEST (test_rhythmdb_indexing)
30 RhythmDBEntry *entry = NULL;
31 GValue val = {0,};
32 gboolean b;
34 entry = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///whee.ogg");
35 fail_unless (entry != NULL, "failed to create entry");
37 g_value_init (&val, G_TYPE_STRING);
38 g_value_set_static_string (&val, "Rock");
39 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_GENRE, &val);
40 g_value_unset (&val);
42 g_value_init (&val, G_TYPE_STRING);
43 g_value_set_static_string (&val, "Nine Inch Nails");
44 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ARTIST, &val);
45 g_value_unset (&val);
47 g_value_init (&val, G_TYPE_STRING);
48 g_value_set_static_string (&val, "Pretty Hate Machine");
49 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ALBUM, &val);
50 g_value_unset (&val);
52 g_value_init (&val, G_TYPE_STRING);
53 g_value_set_static_string (&val, "Sin");
54 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_TITLE, &val);
55 g_value_unset (&val);
57 rhythmdb_commit (db);
59 /* check the data is recorded correctly */
60 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION), "file:///whee.ogg") == 0,
61 "LOCATION set incorrectly");
62 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_GENRE), "Rock") == 0,
63 "GENRE set incorrectly");
64 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ARTIST), "Nine Inch Nails") == 0,
65 "ARTIST set incorrectly");
66 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ALBUM), "Pretty Hate Machine") == 0,
67 "ALBUM set incorrectly");
68 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE), "Sin") == 0,
69 "TITLE set incorrectly");
71 /* check changing album */
72 g_value_init (&val, G_TYPE_STRING);
73 g_value_set_static_string (&val, "Broken");
74 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ALBUM, &val);
75 g_value_unset (&val);
76 rhythmdb_commit (db);
78 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION), "file:///whee.ogg") == 0,
79 "LOCATION set incorrectly");
80 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_GENRE), "Rock") == 0,
81 "GENRE set incorrectly");
82 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ARTIST), "Nine Inch Nails") == 0,
83 "ARTIST set incorrectly");
84 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ALBUM), "Broken") == 0,
85 "ALBUM set incorrectly");
86 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE), "Sin") == 0,
87 "TITLE set incorrectly");
89 /* check changing artist */
90 g_value_init (&val, G_TYPE_STRING);
91 g_value_set_static_string (&val, "Evanescence");
92 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ARTIST, &val);
93 g_value_unset (&val);
94 rhythmdb_commit (db);
96 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION), "file:///whee.ogg") == 0,
97 "LOCATION set incorrectly");
98 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_GENRE), "Rock") == 0,
99 "GENRE set incorrectly");
100 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ARTIST), "Evanescence") == 0,
101 "ARTIST set incorrectly");
102 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ALBUM), "Broken") == 0,
103 "ALBUM set incorrectly");
104 fail_unless (strcmp (rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE), "Sin") == 0,
105 "TITLE set incorrectly");
107 /* check removal */
108 rhythmdb_entry_delete (db, entry);
109 entry = NULL;
111 b = FALSE;
112 rhythmdb_entry_foreach (db, (GFunc)set_true, &b);
113 fail_unless (b == FALSE, "entry not deleted");
115 END_TEST
117 START_TEST (test_rhythmdb_multiple)
119 RhythmDBEntry *entry1, *entry2, *entry3;
121 /* add multiple entries */
122 entry1 = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///foo.mp3");
123 rhythmdb_commit (db);
124 fail_unless (entry1 != NULL, "failed to create entry");
125 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///foo.mp3") == entry1, "entry missing");
127 entry2 = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///bar.mp3");
128 rhythmdb_commit (db);
129 fail_unless (entry2 != NULL, "failed to create entry");
130 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///bar.mp3") == entry2, "entry missing");
132 entry3 = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///baz.mp3");
133 rhythmdb_commit (db);
134 fail_unless (entry3 != NULL, "failed to create entry");
135 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///baz.mp3") == entry3, "entry missing");
137 /* check they're still there */
138 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///foo.mp3") == entry1, "entry missing");
139 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///bar.mp3") == entry2, "entry missing");
140 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///baz.mp3") == entry3, "entry missing");
142 /* remove the middle one and check again */
143 rhythmdb_entry_delete (db, entry2);
144 rhythmdb_commit (db);
146 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///foo.mp3") == entry1, "entry missing");
147 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///bar.mp3") == NULL, "entry not deleted");
148 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///baz.mp3") == entry3, "entry missing");
150 /* and the others */
151 rhythmdb_entry_delete (db, entry1);
152 rhythmdb_entry_delete (db, entry3);
153 rhythmdb_commit (db);
155 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///foo.mp3") == NULL, "entry not deleted");
156 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///bar.mp3") == NULL, "entry not deleted");
157 fail_unless (rhythmdb_entry_lookup_by_location (db, "file:///baz.mp3") == NULL, "entry not deleted");
159 END_TEST
161 START_TEST (test_rhythmdb_mirroring)
163 GValue val = {0,};
164 RhythmDBEntry *entry;
165 const char *str;
167 entry = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///foo.mp3");
168 fail_unless (entry != NULL, "failed to create entry");
170 /* check the last-played date is mirrored */
171 g_value_init (&val, G_TYPE_ULONG);
172 g_value_set_ulong (&val, 1354285);
173 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_LAST_PLAYED, &val);
174 g_value_unset (&val);
175 rhythmdb_commit (db);
177 str = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LAST_PLAYED_STR);
178 fail_unless (str && (strlen (str) > 0), "date not converted to string");
180 /* check folded and sort-key varients */
181 g_value_init (&val, G_TYPE_STRING);
182 g_value_set_static_string (&val, "FOO");
183 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_TITLE, &val);
184 g_value_unset (&val);
185 rhythmdb_commit (db);
187 str = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE_SORT_KEY);
188 fail_unless (str && (strlen (str) > 0), "sort-key not generated");
189 str = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE_FOLDED);
190 fail_unless (str && (strcmp (str, "foo") == 0), "folded variant not generated");
192 g_value_init (&val, G_TYPE_STRING);
193 g_value_set_static_string (&val, "BAR");
194 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_TITLE, &val);
195 g_value_unset (&val);
196 rhythmdb_commit (db);
198 str = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE_SORT_KEY);
199 fail_unless (str && (strlen (str) > 0), "sort-key not generated");
200 str = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_TITLE_FOLDED);
201 fail_unless (str && (strcmp (str, "bar") == 0), "folded variant not generated");
205 END_TEST
207 START_TEST (test_rhythmdb_deserialisation1)
209 RhythmDBQueryModel *model;
211 /* empty db */
212 g_object_set (G_OBJECT (db), "name", "deserialization-test1.xml", NULL);
213 set_waiting_signal (G_OBJECT (db), "load-complete");
214 rhythmdb_load (db);
215 wait_for_signal ();
217 model = rhythmdb_query_model_new_empty (db);
218 g_object_set (G_OBJECT (model), "show-hidden", TRUE, NULL);
219 set_waiting_signal (G_OBJECT (model), "complete");
220 rhythmdb_do_full_query (db, RHYTHMDB_QUERY_RESULTS (model),
221 NULL,
222 RHYTHMDB_QUERY_PROP_EQUALS,
223 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
224 RHYTHMDB_QUERY_END);
225 wait_for_signal ();
226 fail_unless (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL) == 0, "deserialisation incorrect");
227 g_object_unref (model);
229 END_TEST
231 START_TEST (test_rhythmdb_deserialisation2)
233 RhythmDBQueryModel *model;
235 /* single entry db */
236 g_object_set (G_OBJECT (db), "name", "deserialization-test2.xml", NULL);
237 set_waiting_signal (G_OBJECT (db), "load-complete");
238 rhythmdb_load (db);
239 wait_for_signal ();
241 model = rhythmdb_query_model_new_empty (db);
242 g_object_set (G_OBJECT (model), "show-hidden", TRUE, NULL);
243 set_waiting_signal (G_OBJECT (model), "complete");
244 rhythmdb_do_full_query (db, RHYTHMDB_QUERY_RESULTS (model),
245 RHYTHMDB_QUERY_PROP_EQUALS,
246 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
247 RHYTHMDB_QUERY_END);
248 wait_for_signal ();
249 /* FIXME: this fails for some reason
250 fail_unless (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL) == 1, "deserialisation incorrect");*/
251 g_object_unref (model);
253 /* TODO: check values */
255 END_TEST
257 START_TEST (test_rhythmdb_deserialisation3)
259 RhythmDBQueryModel *model;
261 /* two entries of different types db */
262 g_object_set (G_OBJECT (db), "name", "deserialization-test3.xml", NULL);
263 set_waiting_signal (G_OBJECT (db), "load-complete");
264 rhythmdb_load (db);
265 wait_for_signal ();
267 model = rhythmdb_query_model_new_empty (db);
268 g_object_set (G_OBJECT (model), "show-hidden", TRUE, NULL);
269 set_waiting_signal (G_OBJECT (model), "complete");
270 rhythmdb_do_full_query (db, RHYTHMDB_QUERY_RESULTS (model),
271 NULL,
272 RHYTHMDB_QUERY_PROP_EQUALS,
273 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
274 RHYTHMDB_QUERY_END);
275 wait_for_signal ();
276 /* FIXME: this fails for some reason
277 fail_unless (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL) == 1, "deserialisation incorrect");*/
278 g_object_unref (model);
280 /* TODO: check values */
282 END_TEST
285 static Suite *
286 rhythmdb_suite (void)
288 Suite *s = suite_create ("rhythmdb");
289 TCase *tc_chain = tcase_create ("rhythmdb-core");
290 TCase *tc_bugs = tcase_create ("rhythmdb-bugs");
292 suite_add_tcase (s, tc_chain);
293 tcase_add_checked_fixture (tc_chain, test_rhythmdb_setup, test_rhythmdb_shutdown);
294 suite_add_tcase (s, tc_bugs);
295 tcase_add_checked_fixture (tc_bugs, test_rhythmdb_setup, test_rhythmdb_shutdown);
297 /* test core functionality */
298 /*tcase_add_test (tc_chain, test_refstring);*/
299 tcase_add_test (tc_chain, test_rhythmdb_indexing);
300 tcase_add_test (tc_chain, test_rhythmdb_multiple);
301 tcase_add_test (tc_chain, test_rhythmdb_mirroring);
302 /*tcase_add_test (tc_chain, test_rhythmdb_signals);*/
303 /*tcase_add_test (tc_chain, test_rhythmdb_query);*/
304 tcase_add_test (tc_chain, test_rhythmdb_deserialisation1);
305 tcase_add_test (tc_chain, test_rhythmdb_deserialisation2);
306 tcase_add_test (tc_chain, test_rhythmdb_deserialisation3);
307 /*tcase_add_test (tc_chain, test_rhythmdb_serialisation);*/
309 /* tests for breakable bug fixes */
311 return s;
315 main (int argc, char **argv)
317 int ret;
318 SRunner *sr;
319 Suite *s;
321 /* init stuff */
322 rb_profile_start ("rhythmbox test suite");
324 g_thread_init (NULL);
325 rb_threads_init ();
326 gtk_set_locale ();
327 gtk_init (&argc, &argv);
328 gnome_vfs_init ();
329 rb_debug_init (TRUE);
330 rb_refstring_system_init ();
331 rb_file_helpers_init ();
334 GDK_THREADS_ENTER ();
336 /* setup tests */
337 s = rhythmdb_suite ();
338 sr = srunner_create (s);
339 srunner_run_all (sr, CK_NORMAL);
340 ret = srunner_ntests_failed (sr);
341 srunner_free (sr);
344 rb_file_helpers_shutdown ();
345 rb_refstring_system_shutdown ();
346 gnome_vfs_shutdown ();
348 rb_profile_end ("rhythmbox test suite");
349 return ret;