diff options
author | Christian Dywan <christian.dywan@canonical.com> | 2013-12-16 23:38:24 +0100 |
---|---|---|
committer | Christian Dywan <christian.dywan@canonical.com> | 2013-12-16 23:38:24 +0100 |
commit | 168b6d9a09e5ad73bfaa49d58a4ab0305dfe183b (patch) | |
tree | 26617ccda89b6b89dd6b6f16b056be49cb1560be | |
parent | a53522764b202b31c978ab63952c07dcdcd16c1e (diff) | |
download | midori-168b6d9a09e5ad73bfaa49d58a4ab0305dfe183b.tar.gz |
Implement clear method in HistoryDatabase and use in unit test
-rw-r--r-- | midori/midori-history.c | 32 | ||||
-rw-r--r-- | midori/midori-historydatabase.vala | 14 | ||||
-rw-r--r-- | tests/completion.vala | 25 |
3 files changed, 31 insertions, 40 deletions
diff --git a/midori/midori-history.c b/midori/midori-history.c index 11c18f07..bb717747 100644 --- a/midori/midori-history.c +++ b/midori/midori-history.c @@ -18,13 +18,16 @@ static void midori_history_clear_cb (KatzeArray* array, sqlite3* db) { - char* errmsg = NULL; - if (sqlite3_exec (db, "DELETE FROM history; DELETE FROM search", - NULL, NULL, &errmsg) != SQLITE_OK) + GError* error = NULL; + MidoriHistoryDatabase* database = midori_history_database_new (NULL, &error); + if (error == NULL) + midori_history_database_clear (database, 0, &error); + if (error != NULL) { - g_printerr (_("Failed to clear history: %s\n"), errmsg); - sqlite3_free (errmsg); + g_printerr (_("Failed to clear history: %s\n"), error->message); + g_error_free (error); } + g_object_unref (database); } KatzeArray* @@ -60,19 +63,16 @@ midori_history_on_quit (KatzeArray* array, MidoriWebSettings* settings) { gint max_history_age = katze_object_get_int (settings, "maximum-history-age"); - sqlite3* db = g_object_get_data (G_OBJECT (array), "db"); - char* errmsg = NULL; - gchar* sqlcmd = g_strdup_printf ( - "DELETE FROM history WHERE " - "(julianday(date('now')) - julianday(date(date,'unixepoch')))" - " >= %d", max_history_age); - if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK) + GError* error = NULL; + MidoriHistoryDatabase* database = midori_history_database_new (NULL, &error); + if (error == NULL) + midori_history_database_clear (database, max_history_age, &error); + if (error != NULL) { /* i18n: Couldn't remove items that are older than n days */ - g_printerr (_("Failed to remove old history items: %s\n"), errmsg); - sqlite3_free (errmsg); + g_printerr (_("Failed to remove old history items: %s\n"), error->message); + g_error_free (error); } - g_free (sqlcmd); - sqlite3_close (db); + g_object_unref (database); } diff --git a/midori/midori-historydatabase.vala b/midori/midori-historydatabase.vala index 8e1f3a42..5f93bee2 100644 --- a/midori/midori-historydatabase.vala +++ b/midori/midori-historydatabase.vala @@ -128,5 +128,19 @@ namespace Midori { ":day", typeof (int64), day); return statement.exec (); } + + public bool clear (int64 maximum_age=0) throws DatabaseError { + unowned string sqlcmd = """ + DELETE FROM history WHERE + (julianday(date('now')) - julianday(date(date,'unixepoch'))) + >= :maximum_age; + DELETE FROM search WHERE + (julianday(date('now')) - julianday(date(date,'unixepoch'))) + >= :maximum_age; + """; + var statement = prepare (sqlcmd, + ":maximum_age", typeof (int64), maximum_age); + return statement.exec (); + } } } diff --git a/tests/completion.vala b/tests/completion.vala index 8799a141..8abaf4db 100644 --- a/tests/completion.vala +++ b/tests/completion.vala @@ -87,25 +87,6 @@ void completion_autocompleter () { error ("Expected %d but got %d", 3, n); } -struct TestCaseCompletion { - public string prefix; - public string text; - public int expected_count; -} - -const TestCaseCompletion[] completions = { - { "history", "example", 1 } -}; - -async void complete_spec (Midori.Completion completion, TestCaseCompletion spec) { - assert (completion.can_complete (spec.text)); - var cancellable = new Cancellable (); - var suggestions = yield completion.complete (spec.text, null, cancellable); - if (spec.expected_count != suggestions.length ()) - error ("%u expected for %s/ %s but got %u", - spec.expected_count, spec.prefix, spec.text, suggestions.length ()); -} - async void complete_history (Midori.HistoryDatabase history) { try { history.insert ("http://example.com", "Ejemplo", 0, 0); @@ -133,15 +114,11 @@ void completion_history () { assert (bookmarks_database.db != null); history = new Midori.HistoryDatabase (app); assert (history.db != null); + history.clear (0); } catch (Midori.DatabaseError error) { assert_not_reached(); } - var completion = new Midori.HistoryCompletion (); - completion.prepare (app); - foreach (var spec in completions) - complete_spec.begin (completion, spec); - Midori.Test.grab_max_timeout (); var loop = MainContext.default (); complete_history.begin (history); |