2006-11-29 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / tests / test-rhythmdb-query-model.c
blobbaa2a14475154915cdbb81b4762dbff47a63a2a2
2 #include <check.h>
3 #include <gtk/gtk.h>
4 #include "test-utils.h"
5 #include "rhythmdb-query-model.h"
7 #include "rb-debug.h"
8 #include "rb-file-helpers.h"
9 #include "rb-util.h"
11 START_TEST (test_rhythmdb_db_queries)
13 RhythmDBEntry *entry = NULL;
14 RhythmDBQuery *query;
15 GValue val = {0,};
17 start_test_case ();
19 entry = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///whee.ogg");
20 fail_unless (entry != NULL, "failed to create entry");
22 g_value_init (&val, G_TYPE_STRING);
23 g_value_set_static_string (&val, "Rock");
24 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_GENRE, &val);
25 g_value_unset (&val);
27 g_value_init (&val, G_TYPE_STRING);
28 g_value_set_static_string (&val, "Nine Inch Nails");
29 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ARTIST, &val);
30 g_value_unset (&val);
32 g_value_init (&val, G_TYPE_STRING);
33 g_value_set_static_string (&val, "Pretty Hate Machine");
34 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ALBUM, &val);
35 g_value_unset (&val);
37 g_value_init (&val, G_TYPE_STRING);
38 g_value_set_static_string (&val, "Sin");
39 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_TITLE, &val);
40 g_value_unset (&val);
42 rhythmdb_commit (db);
44 /* basic queries and conjunctions */
45 query = rhythmdb_query_parse (db,
46 RHYTHMDB_QUERY_PROP_EQUALS, RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
47 RHYTHMDB_QUERY_PROP_EQUALS, RHYTHMDB_PROP_TITLE, "Sin",
48 RHYTHMDB_QUERY_END);
49 fail_unless (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
50 rhythmdb_query_free (query);
52 end_step ();
54 query = rhythmdb_query_parse (db,
55 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_ARTIST, "Nine Inch",
56 RHYTHMDB_QUERY_END);
57 fail_unless (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
58 rhythmdb_query_free (query);
60 end_step ();
62 query = rhythmdb_query_parse (db,
63 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_ALBUM, "Load",
64 RHYTHMDB_QUERY_END);
65 fail_if (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
66 rhythmdb_query_free (query);
68 end_step ();
70 query = rhythmdb_query_parse (db,
71 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_SEARCH_MATCH, "Pretty Nine",
72 RHYTHMDB_QUERY_END);
73 rhythmdb_query_preprocess (db, query);
74 fail_unless (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
75 rhythmdb_query_free (query);
77 end_step ();
79 /* disjunctions */
80 query = rhythmdb_query_parse (db,
81 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_TITLE, "Sin",
82 RHYTHMDB_QUERY_DISJUNCTION,
83 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_TITLE, "Son",
84 RHYTHMDB_QUERY_END);
85 fail_unless (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
86 rhythmdb_query_free (query);
88 end_step ();
90 query = rhythmdb_query_parse (db,
91 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_TITLE, "Sun",
92 RHYTHMDB_QUERY_DISJUNCTION,
93 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_TITLE, "Sin",
94 RHYTHMDB_QUERY_END);
95 fail_unless (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
96 rhythmdb_query_free (query);
98 end_step ();
100 query = rhythmdb_query_parse (db,
101 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_TITLE, "Sun",
102 RHYTHMDB_QUERY_DISJUNCTION,
103 RHYTHMDB_QUERY_PROP_LIKE, RHYTHMDB_PROP_TITLE, "Son",
104 RHYTHMDB_QUERY_END);
105 fail_if (rhythmdb_evaluate_query (db, query, entry), "query evaluated incorrectly");
106 rhythmdb_query_free (query);
108 /* TODO: subqueries */
110 rhythmdb_entry_delete (db, entry);
112 end_test_case ();
114 END_TEST
116 /* this tests that chained query models, where the base shows hidden entries
117 * forwards visibility changes correctly. This is basically what static playlists do */
118 START_TEST (test_hidden_chain_filter)
120 RhythmDBQueryModel *base_model;
121 RhythmDBQueryModel *filter_model;
122 RhythmDBQuery *query;
123 RhythmDBEntry *entry;
124 GtkTreeIter iter;
125 GValue val = {0,};
127 start_test_case ();
129 /* setup */
130 base_model = rhythmdb_query_model_new_empty (db);
131 g_object_set (base_model, "show-hidden", TRUE, NULL);
133 filter_model = rhythmdb_query_model_new_empty (db);
134 g_object_set (filter_model, "base-model", base_model, NULL);
135 query = g_ptr_array_new ();
136 g_object_set (filter_model, "query", query, NULL);
137 rhythmdb_query_free (query);
139 entry = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///whee.ogg");
140 rhythmdb_commit (db);
142 g_value_init (&val, G_TYPE_BOOLEAN);
145 /* add entry to base, should be in both */
146 rhythmdb_query_model_add_entry (base_model, entry, -1);
147 fail_unless (rhythmdb_query_model_entry_to_iter (base_model, entry, &iter));
148 fail_unless (rhythmdb_query_model_entry_to_iter (filter_model, entry, &iter));
150 end_step ();
152 /* hide entry, should be in base and not filtered */
153 g_value_set_boolean (&val, TRUE);
154 set_waiting_signal (G_OBJECT (db), "entry-changed");
155 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_HIDDEN, &val);
156 rhythmdb_commit (db);
157 wait_for_signal ();
159 fail_unless (rhythmdb_query_model_entry_to_iter (base_model, entry, &iter));
160 fail_if (rhythmdb_query_model_entry_to_iter (filter_model, entry, &iter));
162 end_step ();
164 /* show entry again, should be in both */
165 g_value_set_boolean (&val, FALSE);
166 set_waiting_signal (G_OBJECT (db), "entry-changed");
167 rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_HIDDEN, &val);
168 rhythmdb_commit (db);
169 wait_for_signal ();
171 fail_unless (rhythmdb_query_model_entry_to_iter (base_model, entry, &iter));
172 fail_unless (rhythmdb_query_model_entry_to_iter (filter_model, entry, &iter));
174 end_step ();
176 /* tidy up */
177 rhythmdb_entry_delete (db, entry);
178 g_object_unref (base_model);
179 g_object_unref (filter_model);
180 g_value_unset (&val);
182 end_test_case ();
184 END_TEST
186 static Suite *
187 rhythmdb_query_model_suite (void)
189 Suite *s = suite_create ("rhythmdb-query-model");
190 TCase *tc_chain = tcase_create ("rhythmdb-query-model-core");
191 TCase *tc_bugs = tcase_create ("rhythmdb-query-model-bugs");
193 suite_add_tcase (s, tc_chain);
194 tcase_add_checked_fixture (tc_chain, test_rhythmdb_setup, test_rhythmdb_shutdown);
195 suite_add_tcase (s, tc_bugs);
196 tcase_add_checked_fixture (tc_bugs, test_rhythmdb_setup, test_rhythmdb_shutdown);
198 /* test core functionality */
199 tcase_add_test (tc_chain, test_rhythmdb_db_queries);
201 /* tests for breakable bug fixes */
202 tcase_add_test (tc_bugs, test_hidden_chain_filter);
204 return s;
208 main (int argc, char **argv)
210 int ret;
211 SRunner *sr;
212 Suite *s;
214 /* init stuff */
215 rb_profile_start ("rhythmdb-query-model test suite");
217 g_thread_init (NULL);
218 rb_threads_init ();
219 gtk_set_locale ();
220 gtk_init (&argc, &argv);
221 gnome_vfs_init ();
222 rb_debug_init (TRUE);
223 rb_refstring_system_init ();
224 rb_file_helpers_init ();
227 GDK_THREADS_ENTER ();
229 /* setup tests */
230 s = rhythmdb_query_model_suite ();
231 sr = srunner_create (s);
232 srunner_run_all (sr, CK_NORMAL);
233 ret = srunner_ntests_failed (sr);
234 srunner_free (sr);
237 rb_file_helpers_shutdown ();
238 rb_refstring_system_shutdown ();
239 gnome_vfs_shutdown ();
241 rb_profile_end ("rhythmdb-query-model test suite");
242 return ret;