udapted vi.po
[rhythmbox.git] / tests / test-rhythmdb-query.c
blob6d4adef478426338398d8e71b3d2ba500ef78dcc
1 /*
2 * arch-tag: Querying tests for the RhythmDB tree database
4 * Copyright (C) 2003 Colin Walters <walters@verbum.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <glib.h>
22 #include <gtk/gtk.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "rhythmdb-query-model.h"
26 #include "rhythmdb-tree.h"
27 #include "rb-debug.h"
28 #include "rb-thread-helpers.h"
30 static RhythmDBEntry *
31 create_entry (RhythmDB *db,
32 const char *location, const char *name, const char *album,
33 const char *artist, const char *genre)
35 RhythmDBEntry *entry;
36 GValue val = {0, };
38 entry = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, location);
39 g_assert (entry);
40 g_value_init (&val, G_TYPE_STRING);
41 g_value_set_static_string (&val, genre);
42 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_GENRE, &val);
43 g_value_unset (&val);
45 g_value_init (&val, G_TYPE_STRING);
46 g_value_set_static_string (&val, artist);
47 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ARTIST, &val);
48 g_value_unset (&val);
50 g_value_init (&val, G_TYPE_STRING);
51 g_value_set_static_string (&val, album);
52 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ALBUM, &val);
53 g_value_unset (&val);
55 g_value_init (&val, G_TYPE_STRING);
56 g_value_set_static_string (&val, name);
57 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_TITLE, &val);
58 g_value_unset (&val);
60 return entry;
63 #define DESTROY_MODELS() \
64 G_STMT_START { \
65 g_object_unref (G_OBJECT (main_model)); \
66 } G_STMT_END
68 static void
69 assert_entry_in_model (GtkTreeModel *model, RhythmDBEntry *entry)
71 GtkTreeIter iter;
72 RhythmDBEntry *tree_entry;
73 g_assert (gtk_tree_model_get_iter_first (model, &iter));
74 gtk_tree_model_get (model, &iter, 0, &tree_entry, -1);
75 while (tree_entry != entry) {
76 g_assert (gtk_tree_model_iter_next (model, &iter));
77 gtk_tree_model_get (model, &iter, 0, &tree_entry, -1);
81 /* static void */
82 /* assert_string_in_model (GtkTreeModel *model, const char *str) */
83 /* { */
84 /* GtkTreeIter iter; */
85 /* char *tree_str; */
86 /* g_assert (gtk_tree_model_get_iter_first (model, &iter)); */
87 /* gtk_tree_model_get (model, &iter, 0, &tree_str, -1); */
88 /* while (strcmp (tree_str, str)) { */
89 /* g_free (tree_str); */
90 /* g_assert (gtk_tree_model_iter_next (model, &iter)); */
91 /* gtk_tree_model_get (model, &iter, 0, &tree_str, -1); */
92 /* } */
93 /* g_free (tree_str); */
94 /* } */
96 #define VERIFY_MAIN_MODEL(ENTRY) assert_entry_in_model (main_model, ENTRY);
98 #define VERIFY_META_MODELS(ALBUM, ARTIST, GENRE)
99 /* #define VERIFY_META_MODELS(ALBUM, ARTIST, GENRE) \ */
100 /* G_STMT_START { \ */
101 /* assert_string_in_model (genre_model, GENRE); \ */
102 /* assert_string_in_model (artist_model, ARTIST); \ */
103 /* assert_string_in_model (album_model, ALBUM); \ */
104 /* } G_STMT_END */
106 static void
107 completed_cb (RhythmDBQueryModel *model, gboolean *complete)
109 rb_debug ("query complete");
110 *complete = TRUE;
113 static void
114 wait_for_model_completion (RhythmDBQueryModel *model)
116 gboolean complete = FALSE;
117 GTimeVal timeout;
118 g_signal_connect (G_OBJECT (model), "complete",
119 G_CALLBACK (completed_cb), &complete);
121 while (!complete) {
122 g_get_current_time (&timeout);
123 g_time_val_add (&timeout, G_USEC_PER_SEC);
125 rb_debug ("polling model for changes");
126 rhythmdb_query_model_sync (model, &timeout);
127 g_usleep (G_USEC_PER_SEC / 10.0);
131 typedef void (*query_func) (RhythmDB *db, GtkTreeModel *model, ...);
134 main (int argc, char **argv)
136 RhythmDB *db;
137 RhythmDBEntry *entry, *entry2, *entry3, *entry4;
138 GtkTreeModel *main_model;
139 GtkTreeIter iter;
140 guint testnum = 1;
141 query_func qfunc;
143 gtk_init (&argc, &argv);
144 g_thread_init (NULL);
145 gdk_threads_init ();
146 rb_thread_helpers_init ();
147 rb_debug_init (TRUE);
149 GDK_THREADS_ENTER ();
151 qfunc = rhythmdb_do_full_query;
153 begin:
155 db = rhythmdb_tree_new ("test");
158 * TEST 1: Entry creation with album
160 g_print ("Test %d\n", testnum);
161 rhythmdb_write_lock (db);
163 entry = create_entry (db, "file:///sin.mp3",
164 "Sin", "Pretty Hate Machine", "Nine Inch Nails", "Rock");
166 rhythmdb_write_unlock (db);
167 g_print ("Test %d\n", testnum);
169 testnum++;
171 * TEST 2: Do a query for all songs, verify our single song is in it
173 g_print ("Test %d\n", testnum);
174 rhythmdb_read_lock (db);
176 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
177 qfunc (db, main_model,
178 RHYTHMDB_QUERY_PROP_EQUALS,
179 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
180 RHYTHMDB_QUERY_END);
181 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
183 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
184 /* We should only have one entry. */
185 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
187 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
188 VERIFY_MAIN_MODEL (entry);
189 DESTROY_MODELS ();
191 rhythmdb_read_unlock (db);
192 g_print ("Test %d\n", testnum);
194 testnum++;
196 * TEST 3: Do a query for songs named "Sin"
198 g_print ("Test %d\n", testnum);
199 rhythmdb_read_lock (db);
201 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
202 rhythmdb_do_full_query (db, main_model,
203 RHYTHMDB_QUERY_PROP_EQUALS,
204 RHYTHMDB_PROP_TITLE, "Sin",
205 RHYTHMDB_QUERY_END);
206 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
208 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
209 /* We should only have one entry. */
210 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
212 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
213 VERIFY_MAIN_MODEL (entry);
214 DESTROY_MODELS ();
216 rhythmdb_read_unlock (db);
217 g_print ("Test %d\n", testnum);
219 testnum++;
221 * TEST 4: Do a query for songs named "Cow", should be empty
223 g_print ("Test %d\n", testnum);
224 rhythmdb_read_lock (db);
226 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
227 rhythmdb_do_full_query (db, main_model,
228 RHYTHMDB_QUERY_PROP_EQUALS,
229 RHYTHMDB_PROP_TITLE, "Cow",
230 RHYTHMDB_QUERY_END);
231 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
233 g_assert (!gtk_tree_model_get_iter_first (main_model, &iter));
235 DESTROY_MODELS ();
237 rhythmdb_read_unlock (db);
238 g_print ("Test %d\n", testnum);
240 testnum++;
242 * TEST 5
243 * Do a query for songs named "Cow" and "Sin", should be empty.
245 g_print ("Test %d\n", testnum);
246 rhythmdb_read_lock (db);
248 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
249 rhythmdb_do_full_query (db, main_model,
250 RHYTHMDB_QUERY_PROP_EQUALS,
251 RHYTHMDB_PROP_TITLE, "Cow",
252 RHYTHMDB_QUERY_PROP_EQUALS,
253 RHYTHMDB_PROP_TITLE, "Sin",
254 RHYTHMDB_QUERY_END);
255 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
257 g_assert (!gtk_tree_model_get_iter_first (main_model, &iter));
259 DESTROY_MODELS ();
260 rhythmdb_read_unlock (db);
261 g_print ("Test %d\n", testnum);
263 testnum++;
265 * TEST 6
266 * Do a query for songs named "Cow" or "Sin", should have our song.
268 g_print ("Test %d\n", testnum);
269 rhythmdb_read_lock (db);
271 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
272 rhythmdb_do_full_query (db, main_model,
273 RHYTHMDB_QUERY_PROP_EQUALS,
274 RHYTHMDB_PROP_TITLE, "Cow",
275 RHYTHMDB_QUERY_DISJUNCTION,
276 RHYTHMDB_QUERY_PROP_EQUALS,
277 RHYTHMDB_PROP_TITLE, "Sin",
278 RHYTHMDB_QUERY_END);
279 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
281 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
282 /* We should only have one entry. */
283 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
285 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
286 VERIFY_MAIN_MODEL (entry);
287 DESTROY_MODELS ();
289 rhythmdb_read_unlock (db);
290 g_print ("Test %d\n", testnum);
292 testnum++;
294 * TEST 7
295 * Do a query for songs with Genre "Rock", should have our song.
297 g_print ("Test %d\n", testnum);
298 rhythmdb_read_lock (db);
300 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
301 rhythmdb_do_full_query (db, main_model,
302 RHYTHMDB_QUERY_PROP_EQUALS,
303 RHYTHMDB_PROP_GENRE, "Rock",
304 RHYTHMDB_QUERY_END);
305 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
307 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
308 /* We should only have one entry. */
309 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
311 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
312 VERIFY_MAIN_MODEL (entry);
313 DESTROY_MODELS ();
315 rhythmdb_read_unlock (db);
316 g_print ("Test %d\n", testnum);
318 testnum++;
320 * TEST 8
321 * Do a query for songs with Genre "Nine Inch Nails",
322 * should be empty.
324 g_print ("Test %d\n", testnum);
325 rhythmdb_read_lock (db);
327 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
328 rhythmdb_do_full_query (db, main_model,
329 RHYTHMDB_QUERY_PROP_EQUALS,
330 RHYTHMDB_PROP_GENRE, "Nine Inch Nails",
331 RHYTHMDB_QUERY_END);
332 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
334 g_assert (!gtk_tree_model_get_iter_first (main_model, &iter));
336 DESTROY_MODELS ();
338 rhythmdb_read_unlock (db);
339 g_print ("Test %d\n", testnum);
341 testnum++;
343 * TEST 9
344 * Do a query for songs with album "Pretty Hate Machine",
345 * should have our song.
347 g_print ("Test %d\n", testnum);
348 rhythmdb_read_lock (db);
350 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
351 rhythmdb_do_full_query (db, main_model,
352 RHYTHMDB_QUERY_PROP_EQUALS,
353 RHYTHMDB_PROP_ALBUM, "Pretty Hate Machine",
354 RHYTHMDB_QUERY_END);
355 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
357 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
358 /* We should only have one entry. */
359 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
361 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
362 VERIFY_MAIN_MODEL (entry);
363 DESTROY_MODELS ();
365 rhythmdb_read_unlock (db);
366 g_print ("Test %d\n", testnum);
368 testnum++;
370 * TEST 10
371 * Do a query for songs with artist "Nine Inch Nails",
372 * should have our song.
374 g_print ("Test %d\n", testnum);
375 rhythmdb_read_lock (db);
377 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
378 rhythmdb_do_full_query (db, main_model,
379 RHYTHMDB_QUERY_PROP_EQUALS,
380 RHYTHMDB_PROP_ARTIST, "Nine Inch Nails",
381 RHYTHMDB_QUERY_END);
382 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
384 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
385 /* We should only have one entry. */
386 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
388 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
389 VERIFY_MAIN_MODEL (entry);
390 DESTROY_MODELS ();
392 rhythmdb_read_unlock (db);
393 g_print ("Test %d\n", testnum);
395 testnum++;
396 g_print ("Test %d\n", testnum);
397 rhythmdb_write_lock (db);
399 entry2 = create_entry (db, "file:///head like a hole.mp3",
400 "Head Like A Hole", "Pretty Hate Machine",
401 "Nine Inch Nails", "Rock");
403 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
404 rhythmdb_do_full_query (db, main_model,
405 RHYTHMDB_QUERY_PROP_EQUALS,
406 RHYTHMDB_PROP_ARTIST, "Nine Inch Nails",
407 RHYTHMDB_QUERY_END);
408 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
410 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
411 g_assert (gtk_tree_model_iter_next (main_model, &iter));
412 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
414 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
415 VERIFY_MAIN_MODEL (entry);
416 VERIFY_MAIN_MODEL (entry2);
417 DESTROY_MODELS ();
419 rhythmdb_write_unlock (db);
420 g_print ("Test %d\n", testnum);
422 testnum++;
423 g_print ("Test %d\n", testnum);
424 rhythmdb_write_lock (db);
426 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
427 rhythmdb_do_full_query (db, main_model,
428 RHYTHMDB_QUERY_PROP_EQUALS,
429 RHYTHMDB_PROP_ALBUM, "Pretty Hate Machine",
430 RHYTHMDB_QUERY_END);
431 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
433 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
434 g_assert (gtk_tree_model_iter_next (main_model, &iter));
435 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
437 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
438 VERIFY_MAIN_MODEL (entry);
439 VERIFY_MAIN_MODEL (entry2);
440 DESTROY_MODELS ();
442 rhythmdb_write_unlock (db);
443 g_print ("Test %d\n", testnum);
445 testnum++;
446 g_print ("Test %d\n", testnum);
447 rhythmdb_write_lock (db);
449 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
450 rhythmdb_do_full_query (db, main_model,
451 RHYTHMDB_QUERY_PROP_EQUALS,
452 RHYTHMDB_PROP_GENRE, "Rock",
453 RHYTHMDB_QUERY_END);
454 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
456 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
457 g_assert (gtk_tree_model_iter_next (main_model, &iter));
458 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
460 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
461 VERIFY_MAIN_MODEL (entry);
462 VERIFY_MAIN_MODEL (entry2);
463 DESTROY_MODELS ();
465 rhythmdb_write_unlock (db);
466 g_print ("Test %d\n", testnum);
468 g_print ("Test %d\n", testnum);
469 rhythmdb_write_lock (db);
471 entry3 = create_entry (db, "file:///angel.ogg",
472 "Angel", "Mezzanine", "Massive Attack", "Electronica");
474 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
475 rhythmdb_do_full_query (db, main_model,
476 RHYTHMDB_QUERY_PROP_EQUALS,
477 RHYTHMDB_PROP_GENRE, "Electronica",
478 RHYTHMDB_QUERY_END);
479 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
481 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
482 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
484 VERIFY_META_MODELS ("Mezzanine", "Massive Attack", "Electronica");
485 VERIFY_MAIN_MODEL (entry3);
486 DESTROY_MODELS ();
488 rhythmdb_write_unlock (db);
489 g_print ("Test %d\n", testnum);
491 testnum++;
492 g_print ("Test %d\n", testnum);
493 rhythmdb_write_lock (db);
495 entry4 = create_entry (db, "file:///killa bees.ogg",
496 "Killa Bees", "Armageddon", "Usual Suspects", "Drum N' Bass");
498 main_model = GTK_TREE_MODEL (rhythmdb_query_model_new_empty (db));
499 rhythmdb_do_full_query (db, main_model,
500 RHYTHMDB_QUERY_PROP_EQUALS,
501 RHYTHMDB_PROP_TITLE, "Angel",
502 RHYTHMDB_QUERY_DISJUNCTION,
503 RHYTHMDB_QUERY_PROP_EQUALS,
504 RHYTHMDB_PROP_TITLE, "Sin",
505 RHYTHMDB_QUERY_DISJUNCTION,
506 RHYTHMDB_QUERY_PROP_EQUALS,
507 RHYTHMDB_PROP_TITLE, "Head Like A Hole",
508 RHYTHMDB_QUERY_DISJUNCTION,
509 RHYTHMDB_QUERY_PROP_EQUALS,
510 RHYTHMDB_PROP_TITLE, "Killa Bees",
511 RHYTHMDB_QUERY_END);
512 wait_for_model_completion (RHYTHMDB_QUERY_MODEL (main_model));
514 g_assert (gtk_tree_model_get_iter_first (main_model, &iter));
515 g_assert (gtk_tree_model_iter_next (main_model, &iter));
516 g_assert (gtk_tree_model_iter_next (main_model, &iter));
517 g_assert (gtk_tree_model_iter_next (main_model, &iter));
518 g_assert (!gtk_tree_model_iter_next (main_model, &iter));
520 VERIFY_META_MODELS ("Mezzanine", "Massive Attack", "Electronica");
521 VERIFY_META_MODELS ("Armageddon", "Usual Suspects", "Drum N' Bass");
522 VERIFY_META_MODELS ("Pretty Hate Machine", "Nine Inch Nails", "Rock");
523 VERIFY_MAIN_MODEL (entry);
524 VERIFY_MAIN_MODEL (entry2);
525 VERIFY_MAIN_MODEL (entry3);
526 VERIFY_MAIN_MODEL (entry4);
527 DESTROY_MODELS ();
529 rhythmdb_write_unlock (db);
530 g_print ("Test %d\n", testnum);
532 testnum++;
534 if (qfunc == rhythmdb_do_full_query) {
535 rhythmdb_shutdown (db);
536 g_object_unref (G_OBJECT (db));
537 qfunc = rhythmdb_do_full_query_async;
538 goto begin;
542 * THE END
544 rhythmdb_shutdown (db);
545 g_object_unref (G_OBJECT (db));
546 GDK_THREADS_LEAVE ();
548 exit (0);