From: Olaf Wintermann Date: Tue, 20 Jan 2026 19:26:18 +0000 (+0100) Subject: fix note_store_delete_collection_async X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=61eb73691657c12dbaa7adae85bc04c7bb7ff2db;p=note.git fix note_store_delete_collection_async --- diff --git a/application/store.c b/application/store.c index b7547b5..e7f6773 100644 --- a/application/store.c +++ b/application/store.c @@ -82,7 +82,7 @@ #define SQL_RESOURCE_DELETE "delete from resources where resource_id = ? ;" -#define SQL_RESOURCE_DELETE_CHILDREN "delete from resource where parent_id = ? ;" +#define SQL_RESOURCE_DELETE_CHILDREN "delete from resources where parent_id = ? ;" #define SQL_RESOURCE_UPDATE_PARENT "update resources set parent_id = ? where parent_id = ? ;" @@ -519,7 +519,7 @@ Resource* note_store_get_notebook_by_id(NoteStore *store, int64_t resource_id) { return NULL; } -static int64_t resource_get_children(int64_t resource_id) { +int64_t note_store_count_children(int64_t resource_id) { DBUQuery *q = connection->createQuery(connection, NULL); dbuQuerySetSQL(q, SQL_RESOURCE_COUNT_CHILDREN); dbuQuerySetParamInt64(q, 1, resource_id); @@ -1211,15 +1211,17 @@ static int qthr_delete_collection(ExecJob *job) { DBUResult *result = dbuSqlExecParamInt64(connection, NULL, SQL_RESOURCE_DELETE_CHILDREN, job->id1); if(!result) { job->error = 3; + return 0; } int ok = dbuResultIsOk(result); dbuResultFree(result); if(!ok) { job->error = 4; + return 0; } } else { // only delete the resource when there are no children - int64_t nchildren = resource_get_children(job->id1); + int64_t nchildren = note_store_count_children(job->id1); if(nchildren > 0) { job->error = 1; return 0; diff --git a/application/store.h b/application/store.h index d625021..d775748 100644 --- a/application/store.h +++ b/application/store.h @@ -119,6 +119,8 @@ NoteStore* note_store_get(); Resource* note_store_get_notebook_by_id(NoteStore *store, int64_t resource_id); +int64_t note_store_count_children(int64_t resource_id); + CxList* note_store_get_notes(const CxAllocator *a, int64_t parent_collection_id); void note_store_get_notes_async(UiObject *obj, int64_t parent_resource_id, listresult_func resultcb, void *userdata); diff --git a/application/tests/test-store.c b/application/tests/test-store.c index 3fccdc1..c548621 100644 --- a/application/tests/test-store.c +++ b/application/tests/test-store.c @@ -529,3 +529,59 @@ CX_TEST(test_note_store_delete_empty_collection_async) { ui_close(obj); } } + +CX_TEST(test_note_store_delete_collection_async) { + CX_TEST_DO { + UiObject *obj = ui_dummy_object(); + + NoteStore *store = note_store_get(); + CX_TEST_ASSERT(store); + + // 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_collection_test2").ptr; + + int 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); + + int64_t res1_id = res1->resource_id; + int64_t res1_child_id = res1_child->resource_id; + + // test delete + int result = -1; + note_store_delete_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 was actually deleted + CX_TEST_ASSERT(!note_store_reload()); + store = note_store_get(); + CX_TEST_ASSERT(store); + + Resource *res1_deleted = note_store_get_notebook_by_id(store, res1_id); + CX_TEST_ASSERT(res1_deleted == NULL); + + int64_t nchildren = note_store_count_children(res1_id); + CX_TEST_ASSERT(nchildren == 0); + + // cleanup + ui_close(obj); + } +} diff --git a/application/tests/test-store.h b/application/tests/test-store.h index 4635b00..cdaf80c 100644 --- a/application/tests/test-store.h +++ b/application/tests/test-store.h @@ -47,6 +47,7 @@ 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); +CX_TEST(test_note_store_delete_collection_async); #ifdef __cplusplus diff --git a/application/tests/testmain.c b/application/tests/testmain.c index 199939e..5e69a5d 100644 --- a/application/tests/testmain.c +++ b/application/tests/testmain.c @@ -64,6 +64,7 @@ int main(int argc, char **argv) { 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_note_store_delete_collection_async); cx_test_register(suite, test_parse_markdown_para); cx_test_register(suite, test_parse_markdown_formatting_simple);