Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
test_tag_selection_metadata.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
22
23#include "testdb.h"
24
25#include "common/image.h" // UNKNOWN_IMAGE
26#include "metadata/tags.h" // DT_TF_CATEGORY
27
28#ifdef _WIN32
29#include "win/main_wrapper.h"
30#endif
31
32
34{
35 (void)state;
36 const int32_t film = testdb_make_film("/testdb/sel");
37 const int32_t a = testdb_make_image(film, "a.raw");
38 const int32_t b = testdb_make_image(film, "b.raw");
39 const int32_t c = testdb_make_image(film, "c.raw");
40 assert_true(a > 0 && b > a && c > b);
41
42 // nothing selected: the sentinel, not a garbage id
45
46 // selection order must not matter: the answer is the LOWEST id, which the unordered
47 // scan this replaced produced only because imgid is the table's INTEGER PRIMARY KEY
51 assert_int_equal(dt_selection_repository_get_lowest_id(), a);
52
53 // get_all comes back ascending, whatever order the ids went in
55 assert_int_equal(g_list_length(all), 3);
56 assert_int_equal(GPOINTER_TO_INT(all->data), a);
57 assert_int_equal(GPOINTER_TO_INT(all->next->data), b);
58 assert_int_equal(GPOINTER_TO_INT(all->next->next->data), c);
59 g_list_free(all);
60
62 assert_int_equal(dt_selection_repository_get_lowest_id(), b);
64}
65
66static void test_tag_agreement(void **state)
67{
68 (void)state;
69 const int32_t film = testdb_make_film("/testdb/tags");
70 const int32_t a = testdb_make_image(film, "a.raw");
71 const int32_t b = testdb_make_image(film, "b.raw");
72 assert_true(a > 0 && b > 0);
73 GList *both = g_list_append(g_list_append(NULL, GINT_TO_POINTER(a)), GINT_TO_POINTER(b));
74
75 const guint shared = dt_tag_repository_insert("alpha");
76 const guint only_a = dt_tag_repository_insert("beta");
77 const guint category = dt_tag_repository_insert("group|c");
78 assert_true(shared > 0 && only_a > 0 && category > 0);
80
81 // a tag on only one image breaks tag agreement; the shared category keeps its own
82 assert_true(dt_tag_repository_attach(shared, a));
83 assert_true(dt_tag_repository_attach(shared, b));
84 assert_true(dt_tag_repository_attach(only_a, a));
85 assert_true(dt_tag_repository_attach(category, a));
86 assert_true(dt_tag_repository_attach(category, b));
87
88 gboolean same_tags = FALSE, same_categories = FALSE;
89 dt_tag_repository_get_agreement(both, &same_tags, &same_categories);
90 assert_false(same_tags);
91 assert_true(same_categories);
92
93 // complete the pair and both agree
94 assert_true(dt_tag_repository_attach(only_a, b));
95 dt_tag_repository_get_agreement(both, &same_tags, &same_categories);
96 assert_true(same_tags);
97 assert_true(same_categories);
98
99 // the darktable| namespace is bookkeeping, not a keyword: it must not break agreement
100 const guint internal = dt_tag_repository_insert("darktable|style|x");
101 assert_true(dt_tag_repository_attach(internal, a));
102 dt_tag_repository_get_agreement(both, &same_tags, &same_categories);
103 assert_true(same_tags);
104 assert_true(same_categories);
105
106 // the empty set agrees vacuously
107 dt_tag_repository_get_agreement(NULL, &same_tags, &same_categories);
108 assert_true(same_tags);
109 assert_true(same_categories);
110
111 g_list_free(both);
112}
113
120
121static void _collect_meta(void *user_data, const int keyid, const char *value, const uint32_t count)
122{
123 _meta_collect_t *c = (_meta_collect_t *)user_data;
124 c->rows++;
125 if(keyid == 0 && !g_strcmp0(value, "same-creator")) c->shared_key_count = (int)count;
126 if(keyid == 2) c->split_key_rows++;
127}
128
130{
131 (void)state;
132 const int32_t film = testdb_make_film("/testdb/meta");
133 const int32_t a = testdb_make_image(film, "a.raw");
134 const int32_t b = testdb_make_image(film, "b.raw");
135 assert_true(a > 0 && b > 0);
136
137 const dt_metadata_row_t rows[] = {
138 { .imgid = a, .keyid = 0, .value = "same-creator" },
139 { .imgid = b, .keyid = 0, .value = "same-creator" },
140 { .imgid = a, .keyid = 2, .value = "title-a" },
141 { .imgid = b, .keyid = 2, .value = "title-b" },
142 };
144
148
149 _meta_collect_t c = { 0 };
151 assert_int_equal(c.rows, 3); // one shared value + two split values
152 assert_int_equal(c.shared_key_count, 2); // both selected images carry it -> they agree
153 assert_int_equal(c.split_key_rows, 2); // two values at count 1 -> they disagree
154
156}
157
158int main(int argc, char *argv[])
159{
160 const struct CMUnitTest tests[] = {
161 cmocka_unit_test(test_selection_lowest_id),
162 cmocka_unit_test(test_tag_agreement),
163 cmocka_unit_test(test_metadata_foreach_selected),
164 };
165 return cmocka_run_group_tests(tests, testdb_setup, testdb_teardown);
166}
#define FALSE
Definition ashift_lsd.c:158
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
dev roi_request value
#define UNKNOWN_IMAGE
Definition image.h:78
void dt_metadata_repository_foreach_selected(dt_metadata_repository_selected_cb cb, void *user_data)
Every distinct (key, value) pair across main.selected_images, ordered by value.
void dt_metadata_repository_add(const dt_metadata_row_t *rows, const size_t count)
Insert count rows as a single multi-VALUES statement.
int main()
Definition prova.c:47
void dt_selection_repository_select(const int32_t imgid)
Add imgid to the selection. Selecting an already-selected image does nothing.
void dt_selection_repository_clear(void)
Empty the selection.
void dt_selection_repository_deselect(const int32_t imgid)
Remove imgid from the selection.
GList * dt_selection_repository_get_all(void)
Every selected image id, GINT_TO_POINTER, ASCENDING. Free with g_list_free().
int32_t dt_selection_repository_get_lowest_id(void)
The lowest selected image id, or UNKNOWN_IMAGE when nothing is selected.
const float uint32_t state[4]
gboolean dt_tag_repository_attach(const guint tagid, const int32_t imgid)
Attach tagid to imgid, at the end of the tag order.
void dt_tag_repository_get_agreement(GList *imgids, gboolean *same_tags, gboolean *same_categories)
Do all of imgids carry exactly the same tags? The same categories?
void dt_tag_repository_set_flags(const guint tagid, const gint flags)
Replace the flags word of tagid.
guint dt_tag_repository_insert(const char *name)
Insert a tag named name and return its new id, or 0 on failure.
@ DT_TF_CATEGORY
Definition tags.h:64
static void _collect_meta(void *user_data, const int keyid, const char *value, const uint32_t count)
static void test_selection_lowest_id(void **state)
static void test_metadata_foreach_selected(void **state)
static void test_tag_agreement(void **state)
Shared fixture for the src/database repository tests.
static int testdb_setup(void **state)
Definition testdb.h:55
static int32_t testdb_make_film(const char *folder)
Definition testdb.h:85
static int testdb_teardown(void **state)
Definition testdb.h:66
static int32_t testdb_make_image(const int32_t film_id, const char *filename)
Definition testdb.h:92