comparison tests/test-database.c @ 0:f41e1b48510d

misc: initial import
author David Demelier <markand@malikania.fr>
date Wed, 25 Nov 2020 21:13:03 +0100
parents
children 468236e1ef94
comparison
equal deleted inserted replaced
-1:000000000000 0:f41e1b48510d
1 /*
2 * test-database.c -- test database functions
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <stdio.h>
20 #include <unistd.h>
21
22 #define GREATEST_USE_ABBREVS 0
23 #include <greatest.h>
24
25 #include "database.h"
26 #include "image.h"
27 #include "util.h"
28
29 #define TEST_DATABASE "test.db"
30
31 static void
32 setup(void *data)
33 {
34 remove(TEST_DATABASE);
35
36 if (!database_open(TEST_DATABASE))
37 die("abort: could not open database");
38
39 (void)data;
40 }
41
42 static void
43 finish(void *data)
44 {
45 database_finish();
46
47 (void)data;
48 }
49
50 GREATEST_TEST
51 recents_empty(void)
52 {
53 struct image images[10];
54 size_t max = 10;
55
56 if (!database_recents(images, &max))
57 GREATEST_FAIL();
58
59 GREATEST_ASSERT_EQ(max, 0);
60 GREATEST_PASS();
61 }
62
63 GREATEST_TEST
64 recents_one(void)
65 {
66 struct image images[10];
67 size_t max = 10;
68 struct image one = {
69 .title = estrdup("test 1"),
70 .author = estrdup("unit test"),
71 .data = estrdup("PNG..."),
72 .datasz = 6,
73 .filename = estrdup("image.png"),
74 .duration = IMAGE_DURATION_HOUR,
75 .visible = true
76 };
77
78 if (!database_insert(&one))
79 GREATEST_FAIL();
80 if (!database_recents(images, &max))
81 GREATEST_FAIL();
82
83 GREATEST_ASSERT_EQ(max, 1);
84 GREATEST_ASSERT(images[0].id);
85 GREATEST_ASSERT_STR_EQ(images[0].title, "test 1");
86 GREATEST_ASSERT_STR_EQ(images[0].author, "unit test");
87 GREATEST_ASSERT_MEM_EQ(images[0].data, "PNG...", 6);
88 GREATEST_ASSERT_EQ(images[0].datasz, 6);
89 GREATEST_ASSERT_STR_EQ(images[0].filename, "image.png");
90 GREATEST_ASSERT_EQ(images[0].duration, IMAGE_DURATION_HOUR);
91 GREATEST_ASSERT(images[0].visible);
92 GREATEST_PASS();
93 }
94
95 GREATEST_TEST
96 recents_hidden(void)
97 {
98 struct image images[10];
99 size_t max = 10;
100 struct image one = {
101 .title = estrdup("test 1"),
102 .author = estrdup("unit test"),
103 .data = estrdup("PNG..."),
104 .datasz = 6,
105 .filename = estrdup("image.png"),
106 .duration = IMAGE_DURATION_HOUR,
107 .visible = false
108 };
109
110 if (!database_insert(&one))
111 GREATEST_FAIL();
112 if (!database_recents(images, &max))
113 GREATEST_FAIL();
114
115 GREATEST_ASSERT_EQ(max, 0);
116 GREATEST_PASS();
117 }
118
119 GREATEST_TEST
120 recents_many(void)
121 {
122 static const int expected[] = { 2, 1, 0 };
123 struct image images[3];
124 size_t max = 3;
125 struct image image = {
126 .duration = IMAGE_DURATION_HOUR,
127 .visible = true
128 };
129
130 for (int i = 0; i < 3; ++i) {
131 image.title = estrdup(bprintf("test %d", i));
132 image.author = estrdup(bprintf("unit test %d", i));
133 image.data = estrdup(bprintf("PNG... %d", i));
134 image.datasz = 8;
135 image.filename = estrdup(bprintf("%d.png", i));
136
137 if (!database_insert(&image))
138 GREATEST_FAIL();
139
140 /* Sleep a little bit to avoid same timestamp. */
141 sleep(2);
142 };
143
144 if (!database_recents(images, &max))
145 GREATEST_FAIL();
146
147 GREATEST_ASSERT_EQ(max, 3U);
148
149 for (int i = 0; i < 3; ++i) {
150 /* Selected in most recents first. */
151 GREATEST_ASSERT(images[i].id);
152 GREATEST_ASSERT_STR_EQ(images[i].title,
153 bprintf("test %d", expected[i]));
154 GREATEST_ASSERT_STR_EQ(images[i].author,
155 bprintf("unit test %d", expected[i]));
156 GREATEST_ASSERT_MEM_EQ(images[i].data,
157 bprintf("PNG... %d", expected[i]), 6);
158 GREATEST_ASSERT_EQ(images[i].datasz, 8);
159 GREATEST_ASSERT_STR_EQ(images[i].filename,
160 bprintf("%i.png", expected[i]));
161 GREATEST_ASSERT_EQ(images[i].duration, IMAGE_DURATION_HOUR);
162 GREATEST_ASSERT(images[i].visible);
163 };
164
165 GREATEST_PASS();
166 }
167
168 GREATEST_TEST
169 recents_limits(void)
170 {
171 static const int expected[] = { 19, 18, 17 };
172 struct image images[3];
173 size_t max = 3;
174 struct image image = {
175 .duration = IMAGE_DURATION_HOUR,
176 .visible = true
177 };
178
179 for (int i = 0; i < 20; ++i) {
180 image.title = estrdup(bprintf("test %d", i));
181 image.author = estrdup(bprintf("unit test %d", i));
182 image.data = estrdup(bprintf("PNG... %d", i));
183 image.datasz = 8;
184 image.filename = estrdup(bprintf("%d.png", i));
185
186 if (!database_insert(&image))
187 GREATEST_FAIL();
188
189 /* Sleep a little bit to avoid same timestamp. */
190 sleep(2);
191 };
192
193 if (!database_recents(images, &max))
194 GREATEST_FAIL();
195
196 GREATEST_ASSERT_EQ(max, 3U);
197
198 for (int i = 0; i < 3; ++i) {
199 /* Selected in most recents first. */
200 GREATEST_ASSERT(images[i].id);
201 GREATEST_ASSERT_STR_EQ(images[i].title,
202 bprintf("test %d", expected[i]));
203 GREATEST_ASSERT_STR_EQ(images[i].author,
204 bprintf("unit test %d", expected[i]));
205 GREATEST_ASSERT_MEM_EQ(images[i].data,
206 bprintf("PNG... %d", expected[i]), 6);
207 GREATEST_ASSERT_EQ(images[i].datasz, 8);
208 GREATEST_ASSERT_STR_EQ(images[i].filename,
209 bprintf("%i.png", expected[i]));
210 GREATEST_ASSERT_EQ(images[i].duration, IMAGE_DURATION_HOUR);
211 GREATEST_ASSERT(images[i].visible);
212 };
213
214 GREATEST_PASS();
215 }
216
217 GREATEST_SUITE(recents)
218 {
219 GREATEST_SET_SETUP_CB(setup, NULL);
220 GREATEST_SET_TEARDOWN_CB(finish, NULL);
221 GREATEST_RUN_TEST(recents_empty);
222 GREATEST_RUN_TEST(recents_one);
223 GREATEST_RUN_TEST(recents_hidden);
224 GREATEST_RUN_TEST(recents_many);
225 GREATEST_RUN_TEST(recents_limits);
226 }
227
228 GREATEST_TEST
229 get_basic(void)
230 {
231 struct image original = {
232 .title = estrdup("test 1"),
233 .author = estrdup("unit test"),
234 .data = estrdup("PNG..."),
235 .datasz = 6,
236 .filename = estrdup("image.png"),
237 .duration = IMAGE_DURATION_HOUR,
238 .visible = false
239 };
240 struct image new = {0};
241
242 if (!database_insert(&original))
243 GREATEST_FAIL();
244 if (!database_get(&new, original.id))
245 GREATEST_FAIL();
246
247 GREATEST_ASSERT_STR_EQ(new.id, original.id);
248 GREATEST_ASSERT_STR_EQ(new.title, original.title);
249 GREATEST_ASSERT_STR_EQ(new.author, original.author);
250 GREATEST_ASSERT_MEM_EQ(new.data, original.data, 6);
251 GREATEST_ASSERT_EQ(new.datasz, original.datasz);
252 GREATEST_ASSERT_STR_EQ(new.filename, original.filename);
253 GREATEST_ASSERT_EQ(new.visible, original.visible);
254 GREATEST_PASS();
255 }
256
257 GREATEST_TEST
258 get_nonexistent(void)
259 {
260 struct image new = {0};
261
262 GREATEST_ASSERT(!database_get(&new, "unknown"));
263 GREATEST_ASSERT(!new.id);
264 GREATEST_ASSERT(!new.title);
265 GREATEST_ASSERT(!new.author);
266 GREATEST_ASSERT(!new.data);
267 GREATEST_ASSERT(!new.datasz);
268 GREATEST_ASSERT(!new.filename);
269 GREATEST_ASSERT(!new.timestamp);
270 GREATEST_ASSERT(!new.visible);
271 GREATEST_PASS();
272 }
273
274 GREATEST_SUITE(get)
275 {
276 GREATEST_SET_SETUP_CB(setup, NULL);
277 GREATEST_SET_TEARDOWN_CB(finish, NULL);
278 GREATEST_RUN_TEST(get_basic);
279 GREATEST_RUN_TEST(get_nonexistent);
280 }
281
282 GREATEST_TEST
283 search_basic(void)
284 {
285 struct image searched[3] = { 0 };
286 struct image originals[] = {
287 {
288 .title = estrdup("Super Mario"),
289 .author = estrdup("Mario"),
290 .data = estrdup("PNG mario"),
291 .datasz = 9,
292 .filename = "mario.png",
293 .duration = IMAGE_DURATION_HOUR,
294 .visible = true
295 },
296 {
297 .title = estrdup("Super Luigi"),
298 .author = estrdup("Luigi"),
299 .data = estrdup("PNG luigi"),
300 .datasz = 9,
301 .filename = "luigi.png",
302 .duration = IMAGE_DURATION_HOUR,
303 .visible = true
304 },
305 };
306 size_t max = 3;
307
308 for (int i = 0; i < 3; ++i)
309 if (!database_insert(&originals[i]))
310 GREATEST_FAIL();
311
312 /*
313 * Search:
314 *
315 * title = <any>
316 * author = Mario,
317 */
318 if (!database_search(searched, &max, NULL, "Mario"))
319 GREATEST_FAIL();
320
321 GREATEST_ASSERT_EQ(max, 1);
322 GREATEST_ASSERT(searched[0].id);
323 GREATEST_ASSERT_STR_EQ(searched[0].title, "Super Mario");
324 GREATEST_ASSERT_STR_EQ(searched[0].author, "Mario");
325 GREATEST_ASSERT_MEM_EQ(searched[0].data, "PNG mario", 9);
326 GREATEST_ASSERT_EQ(searched[0].datasz, 9);
327 GREATEST_ASSERT_STR_EQ(searched[0].filename, "mario.png");
328 GREATEST_ASSERT_EQ(searched[0].duration, IMAGE_DURATION_HOUR);
329 GREATEST_ASSERT(searched[0].visible);
330 GREATEST_PASS();
331 }
332
333 GREATEST_TEST
334 search_notfound(void)
335 {
336 struct image searched = {0};
337 struct image original = {
338 .title = estrdup("Super Mario"),
339 .author = estrdup("Mario"),
340 .data = estrdup("PNG mario"),
341 .datasz = 9,
342 .filename = "mario.png",
343 .duration = IMAGE_DURATION_HOUR,
344 .visible = true
345 };
346 size_t max = 1;
347
348 if (!database_insert(&original))
349 GREATEST_FAIL();
350
351 /*
352 * Search:
353 *
354 * title = <any>
355 * author = jean,
356 */
357 if (!database_search(&searched, &max, NULL, "jean"))
358 GREATEST_FAIL();
359
360 GREATEST_ASSERT_EQ(max, 0);
361 GREATEST_ASSERT(!searched.id);
362 GREATEST_ASSERT(!searched.title);
363 GREATEST_ASSERT(!searched.author);
364 GREATEST_ASSERT(!searched.data);
365 GREATEST_ASSERT(!searched.datasz);
366 GREATEST_ASSERT(!searched.filename);
367 GREATEST_ASSERT(!searched.timestamp);
368 GREATEST_ASSERT(!searched.visible);
369 GREATEST_PASS();
370 }
371
372 GREATEST_TEST
373 search_private(void)
374 {
375 struct image searched = {0};
376 struct image original = {
377 .title = estrdup("Super Mario"),
378 .author = estrdup("Mario"),
379 .data = estrdup("PNG mario"),
380 .datasz = 9,
381 .filename = "mario.png",
382 .duration = IMAGE_DURATION_HOUR,
383 .visible = false
384 };
385 size_t max = 1;
386
387 if (!database_insert(&original))
388 GREATEST_FAIL();
389
390 /*
391 * Search:
392 *
393 * title = <any>
394 * author = <any>
395 */
396 if (!database_search(&searched, &max, NULL, NULL))
397 GREATEST_FAIL();
398
399 GREATEST_ASSERT_EQ(max, 0);
400 GREATEST_ASSERT(!searched.id);
401 GREATEST_ASSERT(!searched.title);
402 GREATEST_ASSERT(!searched.author);
403 GREATEST_ASSERT(!searched.data);
404 GREATEST_ASSERT(!searched.datasz);
405 GREATEST_ASSERT(!searched.filename);
406 GREATEST_ASSERT(!searched.timestamp);
407 GREATEST_ASSERT(!searched.visible);
408 GREATEST_PASS();
409 }
410
411 GREATEST_SUITE(search)
412 {
413 GREATEST_SET_SETUP_CB(setup, NULL);
414 GREATEST_SET_TEARDOWN_CB(finish, NULL);
415 GREATEST_RUN_TEST(search_basic);
416 GREATEST_RUN_TEST(search_notfound);
417 GREATEST_RUN_TEST(search_private);
418 }
419
420 GREATEST_TEST
421 clear_run(void)
422 {
423 struct image searched = { 0 };
424 struct image originals[] = {
425 /* Will be deleted */
426 {
427 .title = estrdup("Super Mario"),
428 .author = estrdup("Mario"),
429 .data = estrdup("PNG mario"),
430 .datasz = 9,
431 .filename = "mario.png",
432 .duration = IMAGE_DURATION_HOUR,
433 .visible = true,
434 .duration = 1
435 },
436 /* Will be deleted */
437 {
438 .title = estrdup("Super Luigi"),
439 .author = estrdup("Luigi"),
440 .data = estrdup("PNG luigi"),
441 .datasz = 9,
442 .filename = "mario.png",
443 .duration = IMAGE_DURATION_HOUR,
444 .visible = true,
445 .duration = 1
446 },
447 /* Will be kept */
448 {
449 .title = estrdup("Bowser"),
450 .author = estrdup("Bowser"),
451 .data = estrdup("PNG bowser"),
452 .datasz = 10,
453 .filename = "bowser.png",
454 .duration = IMAGE_DURATION_HOUR,
455 .visible = true
456 },
457 };
458 size_t max = 1;
459
460 for (int i = 0; i < 3; ++i)
461 if (!database_insert(&originals[i]))
462 GREATEST_FAIL();
463
464 /* Sleep 2 seconds to exceed the lifetime of Mario and Luigi images. */
465 sleep(2);
466 database_clear();
467
468 /*
469 * Search:
470 *
471 * title = <any>
472 * author = <any>
473 */
474 if (!database_search(&searched, &max, NULL, NULL))
475 GREATEST_FAIL();
476
477 GREATEST_ASSERT_EQ(max, 1);
478 GREATEST_ASSERT(searched.id);
479 GREATEST_ASSERT_STR_EQ(searched.title, "Bowser");
480 GREATEST_ASSERT_STR_EQ(searched.author, "Bowser");
481 GREATEST_ASSERT_MEM_EQ(searched.data, "PNG bowser", 10);
482 GREATEST_ASSERT_EQ(searched.datasz, 10);
483 GREATEST_ASSERT_STR_EQ(searched.filename, "bowser.png");
484 GREATEST_ASSERT_EQ(searched.duration, IMAGE_DURATION_HOUR);
485 GREATEST_ASSERT(searched.visible);
486 GREATEST_PASS();
487 }
488
489 GREATEST_SUITE(clear)
490 {
491 GREATEST_SET_SETUP_CB(setup, NULL);
492 GREATEST_SET_TEARDOWN_CB(finish, NULL);
493 GREATEST_RUN_TEST(clear_run);
494 }
495
496 GREATEST_MAIN_DEFS();
497
498 int
499 main(int argc, char **argv)
500 {
501 GREATEST_MAIN_BEGIN();
502 GREATEST_RUN_SUITE(recents);
503 GREATEST_RUN_SUITE(get);
504 GREATEST_RUN_SUITE(search);
505 GREATEST_RUN_SUITE(clear);
506 GREATEST_MAIN_END();
507 }