From: Olaf Wintermann Date: Mon, 26 Jan 2026 18:06:20 +0000 (+0100) Subject: add test_note_store_save_repository_async X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=HEAD;p=note.git add test_note_store_save_repository_async --- diff --git a/application/store.c b/application/store.c index f1cea5e..1d29baa 100644 --- a/application/store.c +++ b/application/store.c @@ -1393,9 +1393,14 @@ typedef struct SaveRepositoryJob { execresult_func resultcb; void *userdata; int error; + int isnew; } SaveRepositoryJob; static void uithr_save_repository_job_finished(UiEvent *event, SaveRepositoryJob *job) { + if(job->error == 0 && job->isnew) { + cxListAdd(current_store->repositories, job->repo); + } + job->resultcb(event, job->error, job->userdata); free(job); } @@ -1412,6 +1417,7 @@ static int qthr_store_save_repository(SaveRepositoryJob *job) { // new repository dbuQuerySetSQL(q, SQL_REPOSITORY_NEW); update_id = TRUE; + job->isnew = TRUE; } dbuQuerySetParamString(q, 1, cx_str(repo->name)); @@ -1458,6 +1464,15 @@ void note_store_save_repository_async(UiObject *obj, Repository *repository, exe ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_save_repository, job, (ui_callback)uithr_save_repository_job_finished, job); } +Repository* note_store_get_repository_by_id(int64_t repository_id) { + CxIterator i = cxListIterator(current_store->repositories); + cx_foreach(Repository *, repo, i) { + if(repo->repository_id == repository_id) { + return repo; + } + } + return NULL; +} static void uithr_countjob_finished(UiEvent *event, CountJob *job) { diff --git a/application/store.h b/application/store.h index 40dca82..b78e709 100644 --- a/application/store.h +++ b/application/store.h @@ -178,6 +178,8 @@ void note_store_notebook_swap_position_async( void note_store_save_repository_async(UiObject *obj, Repository *repository, execresult_func resultcb, void *userdata); +Repository* note_store_get_repository_by_id(int64_t repository_id); + void note_store_count_repository_usage_async(UiObject *obj, Repository *repository, countresult_func resultcb, void *userdata); #ifdef __cplusplus diff --git a/application/tests/test-store.c b/application/tests/test-store.c index 6d687b8..8d72d21 100644 --- a/application/tests/test-store.c +++ b/application/tests/test-store.c @@ -664,3 +664,61 @@ CX_TEST(test_note_store_delete_collection_async) { ui_close(obj); } } + +static void repository_saved(UiEvent *event, int error, void *userdata) { + int *ret = userdata; + *ret = error; +} + +CX_TEST(test_note_store_save_repository_async) { + CX_TEST_DO { + UiObject *obj = ui_dummy_object(); + NoteStore *store = note_store_get(); + const CxAllocator *a = store->mp->allocator; + + size_t numrepos = store->repositories ? cxListSize(store->repositories) : 0; + + // test creating a new repository + Repository *repo1 = cxZalloc(a, sizeof(Repository)); + repo1->name = cx_strdup_a(a, "TestNewRepo1").ptr; + + int error = -1; + note_store_save_repository_async(obj, repo1, repository_saved, &error); + ui_exec_buffered_mainthread_calls_wait(3); + + CX_TEST_ASSERT(error == 0); // created successfully + CX_TEST_ASSERT(repo1->repository_id != 0); // new id updated in the repo object + + size_t numrepos2 = store->repositories ? cxListSize(store->repositories) : 0; + CX_TEST_ASSERT(numrepos2 == numrepos + 1); // repo was added to the NoteStore repositories list + + // test updating a repository + repo1->name = cx_strdup_a(a, "TestNewRepo1_update").ptr; + repo1->local_path = cx_strdup_a(a, "$home/notes").ptr; + repo1->url = cx_strdup_a(a, "http://example.com/webdav/").ptr; + repo1->encryption = 1; + repo1->default_key = cx_strdup_a(a, "key1").ptr; + + error = -1; + note_store_save_repository_async(obj, repo1, repository_saved, &error); + ui_exec_buffered_mainthread_calls_wait(3); + + CX_TEST_ASSERT(error == 0); // created successfully + int64_t repository_id = repo1->repository_id; + + CX_TEST_ASSERT(!note_store_reload()); + store = note_store_get(); + CX_TEST_ASSERT(store); + + Repository *repo2 = note_store_get_repository_by_id(repository_id); + CX_TEST_ASSERT(repo2); + CX_TEST_ASSERT(!cx_strcmp(repo2->name, "TestNewRepo1_update")); + CX_TEST_ASSERT(!cx_strcmp(repo2->local_path, "$home/notes")); + CX_TEST_ASSERT(!cx_strcmp(repo2->url, "http://example.com/webdav/")); + CX_TEST_ASSERT(!cx_strcmp(repo2->default_key, "key1")); + CX_TEST_ASSERT(repo2->encryption == 1); + + // cleanup + ui_close(obj); + } +} diff --git a/application/tests/test-store.h b/application/tests/test-store.h index 086f3e9..2a9099c 100644 --- a/application/tests/test-store.h +++ b/application/tests/test-store.h @@ -49,6 +49,7 @@ 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); +CX_TEST(test_note_store_save_repository_async); #ifdef __cplusplus diff --git a/application/tests/testmain.c b/application/tests/testmain.c index 2df046d..58280ce 100644 --- a/application/tests/testmain.c +++ b/application/tests/testmain.c @@ -66,6 +66,7 @@ int main(int argc, char **argv) { 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_note_store_save_repository_async); cx_test_register(suite, test_parse_markdown_para); cx_test_register(suite, test_parse_markdown_formatting_simple);