From: Olaf Wintermann Date: Mon, 19 Jan 2026 19:47:45 +0000 (+0100) Subject: add test_note_store_delete_empty_collection_async X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=ca887512041a5c19b50e433dcdc39c2621f20843;p=note.git add test_note_store_delete_empty_collection_async --- diff --git a/application/store.c b/application/store.c index c275a93..b7547b5 100644 --- a/application/store.c +++ b/application/store.c @@ -1245,16 +1245,16 @@ static int qthr_delete_collection(ExecJob *job) { void note_store_delete_empty_collection_async( UiObject *obj, - Resource *res, + int64_t resource_id, execresult_func resultcb, void *userdata) { ExecJob *job = malloc(sizeof(ExecJob)); - job->resource = res; + job->resource = NULL; job->resultcb = resultcb; job->userdata = userdata; job->error = 0; - job->id1 = res->resource_id; + job->id1 = resource_id; job->id2 = 0; job->flag = 0; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection, job, (ui_callback)uithr_execjob_finished, job); @@ -1262,16 +1262,16 @@ void note_store_delete_empty_collection_async( void note_store_delete_collection_async( UiObject *obj, - Resource *res, + int64_t resource_id, execresult_func resultcb, void *userdata) { ExecJob *job = malloc(sizeof(ExecJob)); - job->resource = res; + job->resource = NULL; job->resultcb = resultcb; job->userdata = userdata; job->error = 0; - job->id1 = res->resource_id; + job->id1 = resource_id; job->id2 = 0; job->flag = NOTESTORE_COLLECTION_DELETE_ALL; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection, job, (ui_callback)uithr_execjob_finished, job); @@ -1301,17 +1301,17 @@ static int qthr_delete_collection_and_move_children(ExecJob *job) { void note_store_delete_collection_and_move_children_async( UiObject *obj, - Resource *res, + int64_t resource_id, int64_t new_parent_id, execresult_func resultcb, void *userdata) { ExecJob *job = malloc(sizeof(ExecJob)); job->resultcb = resultcb; - job->resource = res; + job->resource = NULL; job->userdata = userdata; job->error = 0; - job->id1 = res->resource_id; + job->id1 = resource_id; job->id2 = new_parent_id; job->flag = 0; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection_and_move_children, job, (ui_callback)uithr_execjob_finished, job); diff --git a/application/store.h b/application/store.h index 4e00bd7..d625021 100644 --- a/application/store.h +++ b/application/store.h @@ -150,19 +150,19 @@ void note_store_collection_get_size_async( void note_store_delete_empty_collection_async( UiObject *obj, - Resource *res, + int64_t resource_id, execresult_func resultcb, void *userdata); void note_store_delete_collection_async( UiObject *obj, - Resource *res, + int64_t resource_id, execresult_func resultcb, void *userdata); void note_store_delete_collection_and_move_children_async( UiObject *obj, - Resource *res, + int64_t resource_id, int64_t new_parent_id, execresult_func resultcb, void *userdata); diff --git a/application/tests/test-store.c b/application/tests/test-store.c index 8df1184..3fccdc1 100644 --- a/application/tests/test-store.c +++ b/application/tests/test-store.c @@ -427,6 +427,8 @@ CX_TEST(test_note_store_save_notebook_async_with_move) { test_res2 = cxListAt(test_res1->children, 0); CX_TEST_ASSERT(!cx_strcmp(test_res2->nodename, "child_moved_to_group2")); + + ui_close(obj); } } @@ -447,3 +449,83 @@ CX_TEST(test_note_store_get_notebook_by_id) { } } +static void test_delete_result(UiEvent *event, int error, void *userdata) { + int *result = userdata; + *result = error; +} + +CX_TEST(test_note_store_delete_empty_collection_async) { + CX_TEST_DO { + UiObject *obj = ui_dummy_object(); + + NoteStore *store = note_store_get(); + CX_TEST_ASSERT(store); + + // create an empty collection + Resource *res0 = cxZalloc(store->mp->allocator, sizeof(Resource)); + res0->parent_id = store->root->resource_id; + res0->nodename = cx_strdup_a(store->mp->allocator, "delete_empty_test").ptr; + + int error = -1; + note_store_save_notebook_async(obj, res0, 0, test_save_notebook_result, &error); + ui_exec_buffered_mainthread_calls_wait(3); + + // create a collection with children + Resource *res1 = cxZalloc(store->mp->allocator, sizeof(Resource)); + res1->parent_id = store->root->resource_id; + res1->nodename = cx_strdup_a(store->mp->allocator, "delete_empty_test_fail").ptr; + + error = -1; + note_store_save_notebook_async(obj, res1, 0, test_save_notebook_result, &error); + ui_exec_buffered_mainthread_calls_wait(3); + + CX_TEST_ASSERT(error == 0); + CX_TEST_ASSERT(res1->resource_id != 0); + + Resource *res1_child = cxZalloc(store->mp->allocator, sizeof(Resource)); + res1_child->parent_id = res1->resource_id; + res1_child->nodename = cx_strdup_a(store->mp->allocator, "child1").ptr; + + error = -1; + note_store_save_notebook_async(obj, res1_child, 0, test_save_notebook_result, &error); + ui_exec_buffered_mainthread_calls_wait(3); + + CX_TEST_ASSERT(error == 0); + CX_TEST_ASSERT(res1->resource_id != 0); + + // test delete + int result = -1; + note_store_delete_empty_collection_async(obj, res0->resource_id, test_delete_result, &result); + ui_exec_buffered_mainthread_calls_wait(3); + + CX_TEST_ASSERT(result == 0); + + // check, if the resource was actually deleted + int64_t res0_id = res0->resource_id; + int64_t res1_id = res1->resource_id; + CX_TEST_ASSERT(!note_store_reload()); + store = note_store_get(); + CX_TEST_ASSERT(store); + + Resource *res0_deleted = note_store_get_notebook_by_id(store, res0_id); + CX_TEST_ASSERT(res0_deleted == NULL); + + // test delete-fail + result = -1; + note_store_delete_empty_collection_async(obj, res1_id, test_delete_result, &result); + ui_exec_buffered_mainthread_calls_wait(3); + + CX_TEST_ASSERT(result != 0); + + // check, if the resource still exists + CX_TEST_ASSERT(!note_store_reload()); + store = note_store_get(); + CX_TEST_ASSERT(store); + + Resource *res1_not_deleted = note_store_get_notebook_by_id(store, res1_id); + CX_TEST_ASSERT(res1_not_deleted != NULL); + + // cleanup + ui_close(obj); + } +} diff --git a/application/tests/test-store.h b/application/tests/test-store.h index 3644559..4635b00 100644 --- a/application/tests/test-store.h +++ b/application/tests/test-store.h @@ -46,6 +46,7 @@ CX_TEST(test_note_store_update_resource_async); CX_TEST(test_note_store_save_notebook_async); CX_TEST(test_note_store_save_notebook_async_with_move); CX_TEST(test_note_store_get_notebook_by_id); +CX_TEST(test_note_store_delete_empty_collection_async); #ifdef __cplusplus diff --git a/application/tests/testmain.c b/application/tests/testmain.c index 7b07644..199939e 100644 --- a/application/tests/testmain.c +++ b/application/tests/testmain.c @@ -63,6 +63,7 @@ int main(int argc, char **argv) { cx_test_register(suite, test_note_store_save_notebook_async); cx_test_register(suite, test_note_store_save_notebook_async_with_move); cx_test_register(suite, test_note_store_get_notebook_by_id); + cx_test_register(suite, test_note_store_delete_empty_collection_async); cx_test_register(suite, test_parse_markdown_para); cx_test_register(suite, test_parse_markdown_formatting_simple);