]> uap-core.de Git - note.git/commitdiff
fix note_store_delete_collection_async
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 20 Jan 2026 19:26:18 +0000 (20:26 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 20 Jan 2026 19:26:18 +0000 (20:26 +0100)
application/store.c
application/store.h
application/tests/test-store.c
application/tests/test-store.h
application/tests/testmain.c

index b7547b5f722e875a07998a61fde6114ecaf14584..e7f677379983c03778054bc6d6292aa64111e30d 100644 (file)
@@ -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;
index d625021f719f8ac769ee59f7a83f9071d59d8f52..d7757483db6845eb5198ef2091a47726cfb11008 100644 (file)
@@ -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);
 
index 3fccdc12fc4657edd01b7ec8835fad9412414750..c5486216b13cd3213794724e998fb1868c422f6d 100644 (file)
@@ -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);
+    }
+}
index 4635b009cee65c4ed546c128da175cc0515a0bec..cdaf80cbde7a8177bc6478822253fecb38c81647 100644 (file)
@@ -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
index 199939e79e580433c4330973b30b6a11dacaae8c..5e69a5dc0068082b0d23157e7b18c8f470bb5105 100644 (file)
@@ -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);