Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
test_image_repository.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4 Copyright (C) 2026 Guillaume Stutin.
5
6 Ansel is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Ansel is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
18*/
19
27
28#include "testdb.h"
29#include "common/image.h"
30#include "system/mem_alloc.h"
31
32#include <glib/gstdio.h>
33#include <sqlite3.h>
34
35#ifdef _WIN32
36#include "win/main_wrapper.h"
37#endif
38
39// an arbitrary flag bit with no side meaning in these tests
40#define TEST_FLAG 2048
41
43{
44 (void)state;
45 const int32_t film = testdb_make_film("/testdb/filmroll");
46 assert_true(film > 0);
47 const int32_t a = testdb_make_image(film, "a.raw");
48 const int32_t b = testdb_make_image(film, "b.raw");
49 const int32_t c = testdb_make_image(film, "c.raw");
50 assert_true(a > 0 && b > 0 && c > 0);
51
52 GList *two = g_list_append(g_list_append(NULL, GINT_TO_POINTER(a)), GINT_TO_POINTER(b));
53 GList *all = g_list_append(g_list_copy(two), GINT_TO_POINTER(c));
54
55 // set the flag on TWO images at once -- the case the old IN (?) binding silently dropped
57
59 assert_int_equal(g_list_length(flagged), 2);
60 assert_int_equal(GPOINTER_TO_INT(flagged->data), a);
61 assert_int_equal(GPOINTER_TO_INT(flagged->next->data), b);
62 g_list_free(flagged);
63
64 // a single-element list keeps working (the only case the old binding handled)
65 GList *one = g_list_append(NULL, GINT_TO_POINTER(a));
67 assert_int_equal(g_list_length(flagged), 1);
68 assert_int_equal(GPOINTER_TO_INT(flagged->data), a);
69 g_list_free(flagged);
70
71 // an empty set asks about nothing
73
74 g_list_free(one);
75 g_list_free(two);
76 g_list_free(all);
77}
78
79/* dt_image_repository_set_flags_masked() writes the mask's bits and nothing else.
80 *
81 * The XMP crawler reads an image's whole `flags` word, then lists a directory -- a
82 * filesystem round-trip -- before deciding what to write back. Anything the user changes in
83 * that window (a star rating, a colour label) shares the word, so the crawl must write only
84 * the two bits it owns. This pins that. */
86{
87 (void)state;
88 const int32_t film = testdb_make_film("/testdb/masked");
89 assert_true(film > 0);
90 const int32_t img = testdb_make_image(film, "masked.raw");
91 assert_true(img > 0);
92
93 // two bits the write will own, and one standing in for the rating the user sets meanwhile
94 const int owned_a = 1 << 4;
95 const int owned_b = 1 << 5;
96 const int mask = owned_a | owned_b;
97
98 GList *one = g_list_append(NULL, GINT_TO_POINTER(img));
99
100 // start with owned_a set and owned_b clear, plus the unrelated bit
101 assert_true(dt_image_repository_set_flag_among(one, owned_a | TEST_FLAG));
102
103 // the crawl's write: clear owned_a, set owned_b, say nothing about the rest
104 assert_true(dt_image_repository_set_flags_masked(img, mask, owned_b));
105
106 GList *got = dt_image_repository_get_ids_with_flag_among(one, owned_b);
107 assert_int_equal(g_list_length(got), 1);
108 g_list_free(got);
109
110 assert_null(dt_image_repository_get_ids_with_flag_among(one, owned_a));
111
112 // the bit outside the mask survived -- this is the whole point
114 assert_int_equal(g_list_length(got), 1);
115 g_list_free(got);
116
117 // an empty mask writes nothing and says so, rather than clearing the word
118 assert_false(dt_image_repository_set_flags_masked(img, 0, 0));
120 assert_int_equal(g_list_length(got), 1);
121 g_list_free(got);
122
123 g_list_free(one);
124}
125
126static void test_full_paths(void **state)
127{
128 (void)state;
129 const int32_t film = testdb_make_film("/testdb/paths");
130 const int32_t a = testdb_make_image(film, "a.raw");
131 const int32_t b = testdb_make_image(film, "b.raw");
132 assert_true(a > 0 && b > 0);
133
134 GList *ids = g_list_append(g_list_append(NULL, GINT_TO_POINTER(a)), GINT_TO_POINTER(b));
135 GList *paths = dt_image_repository_get_full_paths(ids);
136 assert_int_equal(g_list_length(paths), 2);
137 // the repository joins folder and file with the platform's separator, whatever the folder uses
138 gchar *expected_a = g_strconcat("/testdb/paths", G_DIR_SEPARATOR_S, "a.raw", NULL);
139 gchar *expected_b = g_strconcat("/testdb/paths", G_DIR_SEPARATOR_S, "b.raw", NULL);
140 assert_string_equal((const char *)paths->data, expected_a);
141 assert_string_equal((const char *)paths->next->data, expected_b);
142 dt_free(expected_a);
143 dt_free(expected_b);
144 g_list_free_full(paths, g_free);
145 g_list_free(ids);
146}
147
148static void test_id_range(void **state)
149{
150 (void)state;
151 const int32_t film = testdb_make_film("/testdb/range");
152 const int32_t a = testdb_make_image(film, "a.raw");
153 const int32_t b = testdb_make_image(film, "b.raw");
154 const int32_t c = testdb_make_image(film, "c.raw");
155 assert_true(a > 0 && c > a);
156
157 assert_int_equal(dt_image_repository_count_in_id_range(a, c), 3);
158 assert_int_equal(dt_image_repository_count_in_id_range(b, b), 1);
159 assert_int_equal(dt_image_repository_count_in_id_range(c + 1, c + 100), 0);
160}
161
163{
164 (void)state;
165 const int32_t film = testdb_make_film("/testdb/ts");
166 const int32_t a = testdb_make_image(film, "a.raw");
167 assert_true(a > 0);
168
169 // 2100-01-01: overflows a 32-bit time_t. The caller this setter replaced bound the
170 // timestamp with sqlite3_bind_int(), which truncates in 2038.
171 const int64_t year2100 = 4102444800LL;
172 assert_true(dt_image_repository_set_write_timestamp(a, year2100));
173 assert_true(dt_image_repository_get_write_timestamp(a) == year2100);
174}
175
176static void test_group_member_rows(void **state)
177{
178 (void)state;
179 const int32_t film = testdb_make_film("/testdb/group");
180 const int32_t a = testdb_make_image(film, "a.raw");
181 const int32_t b = testdb_make_image(film, "b.raw");
182 assert_true(a > 0 && b > 0);
183 assert_true(dt_image_repository_set_group(a, a));
184 assert_true(dt_image_repository_set_group(b, a));
185
187 assert_int_equal(g_list_length(members), 2);
188 const dt_image_group_member_t *first = (const dt_image_group_member_t *)members->data;
189 const dt_image_group_member_t *second = (const dt_image_group_member_t *)members->next->data;
190 assert_int_equal(first->imgid, a);
191 assert_string_equal(first->filename, "a.raw");
192 assert_int_equal(second->imgid, b);
193 assert_string_equal(second->filename, "b.raw");
194 g_list_free_full(members, dt_image_group_member_free);
195}
196
198{
199 (void)state;
200 const int32_t film = testdb_make_film("/testdb/distinct");
201 const int32_t a = testdb_make_image(film, "a.raw");
202 const int32_t b = testdb_make_image(film, "b.raw");
203 assert_true(a > 0 && b > 0);
204
205 GList *ids = g_list_append(g_list_append(NULL, GINT_TO_POINTER(a)), GINT_TO_POINTER(b));
207 assert_non_null(counts);
208
209 assert_int_equal(counts[DT_IMAGE_FIELD_FILM_ROLL], 1); // same film -> agree
210 assert_int_equal(counts[DT_IMAGE_FIELD_FILENAME], 2); // different names -> disagree
211 assert_int_equal(counts[DT_IMAGE_FIELD_IMGID], 2); // ids differ by definition
212 assert_int_equal(counts[DT_IMAGE_FIELD_VERSION], 1); // both imported at version 0
213
214 free(counts);
215 g_list_free(ids);
216
217 // the empty set answers nothing rather than something
219}
220
221static gboolean _count_two_rows(const int32_t imgid, const int64_t write_timestamp,
222 const int version, const char *image_path, const int flags,
223 void *user_data)
224{
225 (void)imgid;
226 (void)write_timestamp;
227 (void)version;
228 (void)image_path;
229 (void)flags;
230 int *seen = (int *)user_data;
231 (*seen)++;
232 return *seen < 2; // refuse the second row
233}
234
235/* The walk ends at the first row the callback refuses. That is how the XMP crawler stops when
236 * Ansel quits; a walk that went on regardless would hold the quit up until the last image. */
238{
239 (void)state;
240 const int32_t film = testdb_make_film("/testdb/walk");
241 assert_true(film > 0);
242 assert_true(testdb_make_image(film, "w0.raw") > 0);
243 assert_true(testdb_make_image(film, "w1.raw") > 0);
244 assert_true(testdb_make_image(film, "w2.raw") > 0);
245
246 int seen = 0;
248 assert_int_equal(seen, 2);
249}
250
251/* A row that is not there -- what the lighttable reads just after a removal -- marks the struct
252 * invalid. The struct starts out looking loaded, as dt_image_cache_get_reload() hands it over
253 * (in place, with no dt_image_init()): starting from UNKNOWN_IMAGE, the test would pass without
254 * the write. */
256{
257 const int32_t film = testdb_make_film("/testdb/absentfilm");
258 assert_true(film > 0);
259 const int32_t id = testdb_make_image(film, "present.raw");
260 assert_true(id > 0);
261
262 dt_image_t img;
263 dt_image_init(&img);
264 img.id = id;
265
266 assert_false(dt_image_repository_load(id + 10000, &img));
267 assert_int_equal(img.id, UNKNOWN_IMAGE);
268}
269
270/* The test below needs a library on disk: in memory, every connection has a database of its own,
271 * and no other connection can lock it. */
272static gchar *_dir = NULL;
273static gchar *_library = NULL;
274
275static int _file_setup(void **state __attribute__((unused)))
276{
277 _dir = g_dir_make_tmp("ansel_test_image_repository_XXXXXX", NULL);
278 if(IS_NULL_PTR(_dir)) return -1;
279 _library = g_build_filename(_dir, "library.db", NULL);
280 const dt_database_params_t params = { .alternative = _library,
281 .library = NULL,
282 .load_data = FALSE,
283 .has_gui = FALSE,
284 .verbose = FALSE };
285 return dt_database_open(&params) == DT_DATABASE_OPEN_OK ? 0 : -1;
286}
287
288static int _file_teardown(void **state)
289{
291 /* The library, and the empty backup marker dt_database_open() leaves beside it. The marker is
292 * read-only, which Windows will not delete. */
293 GDir *d = g_dir_open(_dir, 0, NULL);
294 if(!IS_NULL_PTR(d))
295 {
296 const char *name = NULL;
297 while((name = g_dir_read_name(d)))
298 {
299 gchar *path = g_build_filename(_dir, name, NULL);
300 g_chmod(path, 0600);
301 g_remove(path);
302 dt_free(path);
303 }
304 g_dir_close(d);
305 }
306 g_rmdir(_dir);
308 dt_free(_dir);
309 return 0;
310}
311
312/* A read that fails leaves the struct as it was: the image may well exist, and invalidating an
313 * entry that dt_image_cache_get_reload() reloads in place would hide it for as long as it stays
314 * cached. The failure is the one another program causes: a second connection, standing in for
315 * it, holds the library's EXCLUSIVE lock, and the read gets SQLITE_BUSY. */
317{
318 const int32_t film = testdb_make_film("/testdb/lockedfilm");
319 assert_true(film > 0);
320 const int32_t id = testdb_make_image(film, "locked.raw");
321 assert_true(id > 0);
322
323 sqlite3 *other = NULL;
324 assert_int_equal(sqlite3_open(_library, &other), SQLITE_OK);
325 assert_int_equal(sqlite3_exec(other, "BEGIN EXCLUSIVE", NULL, NULL, NULL), SQLITE_OK);
326
327 dt_image_t img;
328 dt_image_init(&img);
329 img.id = id;
330 img.width = 4242;
331 const gboolean found = dt_image_repository_load(id, &img);
332
333 // released before asserting, so that a failure does not leave the file locked for the teardown
334 sqlite3_exec(other, "ROLLBACK", NULL, NULL, NULL);
335 sqlite3_close(other);
336
337 assert_false(found);
338 assert_int_equal(img.id, id);
339 assert_int_equal(img.width, 4242);
340}
341
342int main(int argc, char *argv[])
343{
344 const struct CMUnitTest tests[] = {
345 cmocka_unit_test(test_flag_among_multi_image),
347 cmocka_unit_test(test_full_paths),
348 cmocka_unit_test(test_id_range),
349 cmocka_unit_test(test_write_timestamp_is_64bit),
350 cmocka_unit_test(test_group_member_rows),
351 cmocka_unit_test(test_count_distinct_fields),
354 };
355 const struct CMUnitTest on_disk[] = {
357 };
358 const int failed = cmocka_run_group_tests(tests, testdb_setup, testdb_teardown);
359 return failed + cmocka_run_group_tests(on_disk, _file_setup, _file_teardown);
360}
#define FALSE
Definition ashift_lsd.c:158
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
void dt_image_init(dt_image_t *img)
dt_database_open_result_t dt_database_open(const dt_database_params_t *params)
Definition database.c:3640
@ DT_DATABASE_OPEN_OK
Definition database.h:78
#define UNKNOWN_IMAGE
Definition image.h:78
int dt_image_repository_count_in_id_range(const int32_t min_imgid, const int32_t max_imgid)
How many images have an id in [min_imgid, max_imgid]. -1 when unreadable.
gboolean dt_image_repository_set_flags_masked(const int32_t imgid, const int mask, const int value)
Write only mask's bits of imgid's flags, taking them from value.
GList * dt_image_repository_get_ids_with_flag_among(GList *imgids, const int flag)
Of the images in imgids, those whose flags carry flag, in row order.
int64_t dt_image_repository_get_write_timestamp(const int32_t imgid)
main.images.write_timestamp for imgid, or 0.
gboolean dt_image_repository_set_group(const int32_t imgid, const int32_t group_id)
Set imgid's group_id.
gboolean dt_image_repository_set_write_timestamp(const int32_t imgid, const int64_t timestamp)
Set write_timestamp of imgid to timestamp (seconds since the epoch). Bound as a 64-bit integer; the c...
void dt_image_group_member_free(gpointer data)
Free one dt_image_group_member_t. Suits g_list_free_full().
gboolean dt_image_repository_load(const int32_t imgid, dt_image_t *img)
Fill img from the main.images row for imgid.
GList * dt_image_repository_get_group_member_rows(const int32_t group_id)
Every member of group_id, in row order, named. Free with g_list_free_full(list, dt_image_group_member...
void dt_image_repository_foreach_with_path(dt_image_repository_path_row_cb cb, void *user_data)
Walk every image in the library, film roll by film roll, filename within each.
gboolean dt_image_repository_set_flag_among(GList *imgids, const int flag)
Set flag on every image in imgids.
GList * dt_image_repository_get_full_paths(GList *imgids)
"<folder>/<filename>" for every image in imgids, distinct, in row order. Caller owns the strings.
int * dt_image_repository_count_distinct_fields(GList *imgids)
How many DISTINCT values each dt_image_field_t takes over imgids.
@ DT_IMAGE_FIELD_VERSION
@ DT_IMAGE_FIELD_FILENAME
@ DT_IMAGE_FIELD_FILM_ROLL
@ DT_IMAGE_FIELD_IMGID
static const dt_iop_drawlayer_params_t * params
Definition layers.h:80
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
dt_mipmap_buffer_dsc_flags flags
Definition mipmap_cache.c:4
g_free(manifest_path)
const char * name
Definition pdf.h:90
int main()
Definition prova.c:47
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
const float uint32_t state[4]
One member of a group, with what a tooltip needs to name it.
int32_t width
Definition image.h:406
int32_t id
Definition image.h:410
static char * _dir
static gchar * _library
static void test_flag_among_multi_image(void **state)
static int _file_teardown(void **state)
static void test_load_marks_an_absent_image_invalid(void **state __attribute__((unused)))
static void test_foreach_with_path_stops_when_asked(void **state)
static void test_write_timestamp_is_64bit(void **state)
static void test_set_flags_masked_leaves_other_bits(void **state)
static void test_group_member_rows(void **state)
static int _file_setup(void **state __attribute__((unused)))
static gboolean _count_two_rows(const int32_t imgid, const int64_t write_timestamp, const int version, const char *image_path, const int flags, void *user_data)
static void test_count_distinct_fields(void **state)
static void test_full_paths(void **state)
static void test_load_failure_leaves_the_image_untouched(void **state __attribute__((unused)))
static void test_id_range(void **state)
#define TEST_FLAG
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