[tepl] MetadataStore: start writing unit tests and fix a bug
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] MetadataStore: start writing unit tests and fix a bug
- Date: Mon, 30 Mar 2020 21:41:33 +0000 (UTC)
commit 7e6eaf626527535717f2472e8f53fe151859d420
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon Mar 30 15:28:03 2020 +0200
MetadataStore: start writing unit tests and fix a bug
tepl/tepl-metadata-store.c | 29 ++--
testsuite/meson.build | 3 +-
testsuite/test-metadata-store.c | 182 +++++++++++++++++++--
testsuite/test-metadata-store/empty.xml | 0
.../test-metadata-store/metadata-tag-only.xml | 2 +
5 files changed, 190 insertions(+), 26 deletions(-)
---
diff --git a/tepl/tepl-metadata-store.c b/tepl/tepl-metadata-store.c
index 3804d43..0a78fc1 100644
--- a/tepl/tepl-metadata-store.c
+++ b/tepl/tepl-metadata-store.c
@@ -980,7 +980,6 @@ tepl_metadata_store_save (TeplMetadataStore *store,
GError **error)
{
GBytes *bytes;
- GError *my_error = NULL;
gboolean ok;
g_return_val_if_fail (TEPL_IS_METADATA_STORE (store), FALSE);
@@ -993,17 +992,25 @@ tepl_metadata_store_save (TeplMetadataStore *store,
return TRUE;
}
- g_file_make_directory_with_parents (store->priv->xml_file,
- cancellable,
- &my_error);
- if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+ if (g_file_has_parent (store->priv->xml_file, NULL))
{
- g_clear_error (&my_error);
- }
- if (my_error != NULL)
- {
- g_propagate_error (error, my_error);
- return FALSE;
+ GFile *parent_dir;
+ GError *my_error = NULL;
+
+ parent_dir = g_file_get_parent (store->priv->xml_file);
+ g_file_make_directory_with_parents (parent_dir, cancellable, &my_error);
+ g_object_unref (parent_dir);
+
+ if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+ {
+ g_clear_error (&my_error);
+ }
+ if (my_error != NULL)
+ {
+ g_propagate_error (error, my_error);
+ return FALSE;
+ }
+
}
resize_hash_table_according_to_max_number_of_locations (store);
diff --git a/testsuite/meson.build b/testsuite/meson.build
index 32befa8..b978ee0 100644
--- a/testsuite/meson.build
+++ b/testsuite/meson.build
@@ -18,7 +18,8 @@ foreach test_name : unit_tests
test_exe = executable(
test_name,
test_name + '.c',
- dependencies: TEPL_STATIC_DEP
+ dependencies: TEPL_STATIC_DEP,
+ c_args: '-DUNIT_TESTS_SOURCE_DIR="@0@"'.format(meson.current_source_dir())
)
test(test_name, test_exe)
endforeach
diff --git a/testsuite/test-metadata-store.c b/testsuite/test-metadata-store.c
index 6b4879e..1a894fe 100644
--- a/testsuite/test-metadata-store.c
+++ b/testsuite/test-metadata-store.c
@@ -19,49 +19,203 @@
#include <tepl/tepl.h>
-static void
-metadata_store_load_cb (GObject *source_object,
- GAsyncResult *result,
- gpointer user_data)
+static GFile *
+get_store_file_for_test_data_filename (const gchar *test_data_filename,
+ gboolean should_exist)
+{
+ GFile *store_file;
+
+ store_file = g_file_new_build_filename (UNIT_TESTS_SOURCE_DIR,
+ "test-metadata-store",
+ test_data_filename,
+ NULL);
+ g_assert_cmpint (should_exist, ==, g_file_query_exists (store_file, NULL));
+
+ return store_file;
+}
+
+static gchar *
+get_file_content (GFile *file)
{
- TeplMetadataStore *store = TEPL_METADATA_STORE (source_object);
GError *error = NULL;
+ gchar *file_content = NULL;
- tepl_metadata_store_load_finish (store, result, &error);
+ g_file_load_contents (file, NULL, &file_content, NULL, NULL, &error);
g_assert_no_error (error);
+ g_assert_true (file_content != NULL);
+ return file_content;
+}
+
+static void
+check_equal_file_content (GFile *file1,
+ GFile *file2)
+{
+ gchar *file1_content = get_file_content (file1);
+ gchar *file2_content = get_file_content (file2);
+
+ g_assert_true (g_str_equal (file1_content, file2_content));
+
+ g_free (file1_content);
+ g_free (file2_content);
+}
+
+static void
+load_store_file_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TeplMetadataStore *store = TEPL_METADATA_STORE (source_object);
+ GError **error = user_data;
+
+ tepl_metadata_store_load_finish (store, result, error);
gtk_main_quit ();
}
static void
-test_basic (void)
+load_store_file (GFile *store_file,
+ GError **error)
{
TeplMetadataStore *store;
- GFile *store_file;
store = tepl_metadata_store_get_singleton ();
-
- store_file = g_file_new_for_path ("gcsvedit-metadata.xml");
tepl_metadata_store_set_store_file (store, store_file);
- g_object_unref (store_file);
tepl_metadata_store_load_async (store,
G_PRIORITY_DEFAULT,
NULL,
- metadata_store_load_cb,
- NULL);
+ load_store_file_cb,
+ error);
gtk_main ();
+}
+
+static GFile *
+save_store (GError **error)
+{
+ TeplMetadataStore *store = tepl_metadata_store_get_singleton ();
+ GFile *tmp_file;
+ GError *my_error = NULL;
+
+ tmp_file = g_file_new_build_filename (g_get_tmp_dir (),
+ "tepl-metadata-store-test.xml",
+ NULL);
+ g_file_delete (tmp_file, NULL, &my_error);
+ if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+ {
+ g_clear_error (&my_error);
+ }
+ g_assert_no_error (my_error);
+ tepl_metadata_store_set_store_file (store, tmp_file);
+ tepl_metadata_store_save (store, NULL, error);
+
+ return tmp_file;
+}
+
+/* To force saving the store file. */
+static void
+mark_metadata_store_as_modified (void)
+{
+ TeplMetadataStore *store;
+ GFile *location;
+ GFileInfo *metadata;
+
+ store = tepl_metadata_store_get_singleton ();
+ location = g_file_new_for_path ("foo");
+ metadata = g_file_info_new ();
+
+ g_assert_true (tepl_metadata_store_get_metadata_for_location (store, location) == NULL);
+ tepl_metadata_store_set_metadata_for_location (store, location, metadata);
+ tepl_metadata_store_set_metadata_for_location (store, location, NULL);
+
+ g_object_unref (location);
+ g_object_unref (metadata);
+}
+
+static void
+test_load_non_existing_store_file (void)
+{
+ GFile *store_file;
+ GError *error = NULL;
+
+ store_file = get_store_file_for_test_data_filename ("does_not_exist.xml", FALSE);
+ load_store_file (store_file, &error);
+ g_assert_no_error (error);
+
+ g_object_unref (store_file);
_tepl_metadata_store_unref_singleton ();
}
+static void
+test_load_empty_store_file (void)
+{
+ GFile *store_file;
+ GError *error = NULL;
+
+ store_file = get_store_file_for_test_data_filename ("empty.xml", TRUE);
+ load_store_file (store_file, &error);
+ g_assert_true (error != NULL && error->domain == G_MARKUP_ERROR);
+ g_clear_error (&error);
+
+ g_object_unref (store_file);
+ _tepl_metadata_store_unref_singleton ();
+}
+
+static void
+test_empty_store_impl (void)
+{
+ GFile *tmp_file;
+ GFile *store_file;
+ GError *error = NULL;
+
+ /* Empty store, and not modified. */
+
+ tmp_file = save_store (&error);
+ g_assert_no_error (error);
+ g_assert_true (!g_file_query_exists (tmp_file, NULL));
+ g_clear_object (&tmp_file);
+
+ /* Empty store, modified. */
+
+ mark_metadata_store_as_modified ();
+
+ tmp_file = save_store (&error);
+ g_assert_no_error (error);
+ g_assert_true (g_file_query_exists (tmp_file, NULL));
+
+ store_file = get_store_file_for_test_data_filename ("metadata-tag-only.xml", TRUE);
+ check_equal_file_content (store_file, tmp_file);
+
+ g_object_unref (tmp_file);
+ g_object_unref (store_file);
+ _tepl_metadata_store_unref_singleton ();
+}
+
+static void
+test_empty_store (void)
+{
+ GFile *store_file;
+ GError *error = NULL;
+
+ /* Without loading */
+ test_empty_store_impl ();
+
+ /* With loading */
+ store_file = get_store_file_for_test_data_filename ("metadata-tag-only.xml", TRUE);
+ load_store_file (store_file, &error);
+ g_assert_no_error (error);
+ test_empty_store_impl ();
+}
+
int
main (int argc,
char **argv)
{
gtk_test_init (&argc, &argv);
- g_test_add_func ("/metadata-store/basic", test_basic);
+ g_test_add_func ("/metadata_store/load_non_existing_store_file", test_load_non_existing_store_file);
+ g_test_add_func ("/metadata_store/load_empty_store_file", test_load_empty_store_file);
+ g_test_add_func ("/metadata_store/empty_store", test_empty_store);
return g_test_run ();
}
diff --git a/testsuite/test-metadata-store/empty.xml b/testsuite/test-metadata-store/empty.xml
new file mode 100644
index 0000000..e69de29
diff --git a/testsuite/test-metadata-store/metadata-tag-only.xml
b/testsuite/test-metadata-store/metadata-tag-only.xml
new file mode 100644
index 0000000..26c2f62
--- /dev/null
+++ b/testsuite/test-metadata-store/metadata-tag-only.xml
@@ -0,0 +1,2 @@
+<metadata>
+</metadata>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]