From 11956500fbcc7ed996205d4be3dadbb89e9cec57 Mon Sep 17 00:00:00 2001 From: Milan Crha Date: Wed, 22 Feb 2017 11:41:14 +0100 Subject: Add ECalCache getters test + some other minor changes --- tests/libedata-cal/CMakeLists.txt | 3 +- tests/libedata-cal/components/event-4.ics | 1 - tests/libedata-cal/components/event-7.ics | 1 + tests/libedata-cal/test-cal-cache-getters.c | 247 ++++++++++++++++++++++++++++ tests/libedata-cal/test-cal-cache-search.c | 4 +- 5 files changed, 252 insertions(+), 4 deletions(-) create mode 100644 tests/libedata-cal/test-cal-cache-getters.c (limited to 'tests') diff --git a/tests/libedata-cal/CMakeLists.txt b/tests/libedata-cal/CMakeLists.txt index 4f0b24edf..558012e82 100644 --- a/tests/libedata-cal/CMakeLists.txt +++ b/tests/libedata-cal/CMakeLists.txt @@ -78,8 +78,9 @@ set(extra_defines) set(TESTS test-cal-backend-sexp test-intervaltree - test-cal-cache-offline + test-cal-cache-getters test-cal-cache-intervals + test-cal-cache-offline test-cal-cache-search ) diff --git a/tests/libedata-cal/components/event-4.ics b/tests/libedata-cal/components/event-4.ics index eb409ff44..2fde42505 100644 --- a/tests/libedata-cal/components/event-4.ics +++ b/tests/libedata-cal/components/event-4.ics @@ -9,5 +9,4 @@ DTEND:20170102T180000Z SUMMARY:After-party clean up LOCATION:All around CLASS:PUBLIC -CATEGORIES:Holiday,Work END:VEVENT diff --git a/tests/libedata-cal/components/event-7.ics b/tests/libedata-cal/components/event-7.ics index f1c052311..769b7ac24 100644 --- a/tests/libedata-cal/components/event-7.ics +++ b/tests/libedata-cal/components/event-7.ics @@ -10,4 +10,5 @@ ATTACH:file:///usr/share/icons/hicolor/48x48/apps/evolution.png CLASS:PUBLIC CREATED:20170221T125054Z LAST-MODIFIED:20170221T125054Z +CATEGORIES:Holiday,Work END:VEVENT diff --git a/tests/libedata-cal/test-cal-cache-getters.c b/tests/libedata-cal/test-cal-cache-getters.c new file mode 100644 index 000000000..4c054133a --- /dev/null +++ b/tests/libedata-cal/test-cal-cache-getters.c @@ -0,0 +1,247 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com) + * + * This library is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +#include "test-cal-cache-utils.h" + +static ECalComponentId * +extract_id_from_component (ECalComponent *component) +{ + ECalComponentId *id; + + g_assert (component != NULL); + + id = e_cal_component_get_id (component); + g_assert (id != NULL); + g_assert (id->uid != NULL); + + return id; +} + +static ECalComponentId * +extract_id_from_string (const gchar *icalstring) +{ + ECalComponent *component; + ECalComponentId *id; + + g_assert (icalstring != NULL); + + component = e_cal_component_new_from_string (icalstring); + g_assert (component != NULL); + + id = extract_id_from_component (component); + + g_object_unref (component); + + return id; +} + +static void +test_get_one (ECalCache *cal_cache, + const gchar *uid, + const gchar *rid, + gboolean expect_failure) +{ + ECalComponent *component = NULL; + ECalComponentId *id; + gchar *icalstring = NULL; + gboolean success; + GError *error = NULL; + + success = e_cal_cache_get_component (cal_cache, uid, rid, &component, NULL, &error); + if (expect_failure) { + g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND); + g_assert (!success); + g_assert (!component); + + g_clear_error (&error); + } else { + g_assert_no_error (error); + g_assert (success); + g_assert_nonnull (component); + + id = extract_id_from_component (component); + + g_assert_cmpstr (id->uid, ==, uid); + g_assert_cmpstr (id->rid, ==, rid && *rid ? rid : NULL); + + e_cal_component_free_id (id); + g_object_unref (component); + } + + success = e_cal_cache_get_component_as_string (cal_cache, uid, rid, &icalstring, NULL, &error); + if (expect_failure) { + g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND); + g_assert (!success); + g_assert (!icalstring); + + g_clear_error (&error); + } else { + g_assert_no_error (error); + g_assert (success); + g_assert_nonnull (icalstring); + + id = extract_id_from_string (icalstring); + + g_assert_cmpstr (id->uid, ==, uid); + g_assert_cmpstr (id->rid, ==, rid && *rid ? rid : NULL); + + e_cal_component_free_id (id); + g_free (icalstring); + } +} + +static void +test_getters_one (TCUFixture *fixture, + gconstpointer user_data) +{ + test_get_one (fixture->cal_cache, "unexistent-event", NULL, TRUE); + test_get_one (fixture->cal_cache, "unexistent-event", "", TRUE); + test_get_one (fixture->cal_cache, "event-2", NULL, FALSE); + test_get_one (fixture->cal_cache, "event-2", "", FALSE); + test_get_one (fixture->cal_cache, "event-5", NULL, FALSE); + test_get_one (fixture->cal_cache, "event-5", "", FALSE); + test_get_one (fixture->cal_cache, "event-5", "20131231T000000Z", TRUE); + test_get_one (fixture->cal_cache, "event-6", NULL, FALSE); + test_get_one (fixture->cal_cache, "event-6", "", FALSE); + test_get_one (fixture->cal_cache, "event-6", "20170225T134900", FALSE); +} + +/* NULL-terminated list of pairs , what to expect */ +static void +test_get_all (ECalCache *cal_cache, + const gchar *uid, + ...) +{ + ECalComponentId *id; + GSList *items, *link; + va_list va; + const gchar *tmp; + GHashTable *expects; + gboolean success; + GError *error = NULL; + + expects = g_hash_table_new_full ((GHashFunc) e_cal_component_id_hash, (GEqualFunc) e_cal_component_id_equal, + (GDestroyNotify) e_cal_component_free_id, NULL); + + va_start (va, uid); + tmp = va_arg (va, const gchar *); + while (tmp) { + const gchar *rid = va_arg (va, const gchar *); + id = e_cal_component_id_new (tmp, rid); + + g_hash_table_insert (expects, id, NULL); + + tmp = va_arg (va, const gchar *); + } + va_end (va); + + items = NULL; + + success = e_cal_cache_get_components_by_uid (cal_cache, uid, &items, NULL, &error); + if (!g_hash_table_size (expects)) { + g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND); + g_assert (!success); + g_assert (!items); + + g_clear_error (&error); + } else { + g_assert_no_error (error); + g_assert (success); + g_assert_nonnull (items); + + g_assert_cmpint (g_hash_table_size (expects), ==, g_slist_length (items)); + + for (link = items; link; link = g_slist_next (link)) { + id = extract_id_from_component (link->data); + + g_assert_cmpstr (id->uid, ==, uid); + g_assert (g_hash_table_contains (expects, id)); + + e_cal_component_free_id (id); + } + + g_slist_free_full (items, g_object_unref); + } + + items = NULL; + + success = e_cal_cache_get_components_by_uid_as_string (cal_cache, uid, &items, NULL, &error); + if (!g_hash_table_size (expects)) { + g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND); + g_assert (!success); + g_assert (!items); + + g_clear_error (&error); + } else { + g_assert_no_error (error); + g_assert (success); + g_assert_nonnull (items); + + g_assert_cmpint (g_hash_table_size (expects), ==, g_slist_length (items)); + + for (link = items; link; link = g_slist_next (link)) { + id = extract_id_from_string (link->data); + + g_assert_cmpstr (id->uid, ==, uid); + g_assert (g_hash_table_contains (expects, id)); + + e_cal_component_free_id (id); + } + + g_slist_free_full (items, g_free); + } + + g_hash_table_destroy (expects); +} + +static void +test_getters_all (TCUFixture *fixture, + gconstpointer user_data) +{ + test_get_all (fixture->cal_cache, "unexistent-event", NULL); + test_get_all (fixture->cal_cache, "unexistent-event", NULL); + test_get_all (fixture->cal_cache, "event-2", "event-2", NULL, NULL); + test_get_all (fixture->cal_cache, "event-5", "event-5", NULL, NULL); + test_get_all (fixture->cal_cache, "event-6", "event-6", NULL, "event-6", "20170225T134900", NULL); +} + +gint +main (gint argc, + gchar **argv) +{ + TCUClosure closure_events = { TCU_LOAD_COMPONENT_SET_EVENTS }; + +#if !GLIB_CHECK_VERSION (2, 35, 1) + g_type_init (); +#endif + g_test_init (&argc, &argv, NULL); + + /* Ensure that the client and server get the same locale */ + g_assert (g_setenv ("LC_ALL", "en_US.UTF-8", TRUE)); + setlocale (LC_ALL, ""); + + g_test_add ("/ECalCache/Getters/One", TCUFixture, &closure_events, + tcu_fixture_setup, test_getters_one, tcu_fixture_teardown); + g_test_add ("/ECalCache/Getters/All", TCUFixture, &closure_events, + tcu_fixture_setup, test_getters_all, tcu_fixture_teardown); + + return g_test_run (); +} diff --git a/tests/libedata-cal/test-cal-cache-search.c b/tests/libedata-cal/test-cal-cache-search.c index 754aec49e..7373fa409 100644 --- a/tests/libedata-cal/test-cal-cache-search.c +++ b/tests/libedata-cal/test-cal-cache-search.c @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2016 Red Hat, Inc. (www.redhat.com) + * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com) * * This library is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by @@ -356,7 +356,7 @@ test_search_has_categories (TCUFixture *fixture, gconstpointer user_data) { test_search (fixture, "(has-categories? #f)", "!event-2"); - test_search (fixture, "(has-categories? \"Holiday\")", "event-4"); + test_search (fixture, "(has-categories? \"Holiday\")", "event-7"); test_search (fixture, "(has-categories? \"Hard\" \"Work\")", "event-2"); test_search (fixture, "(has-categories? \"Hard\" \"Work\")", "!event-4"); } -- cgit v1.2.1