Code

store: Make the actual store a separate object.
[sysdb.git] / t / unit / core / store_json_test.c
1 /*
2  * SysDB - t/unit/core/store_json_test.c
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "core/store.h"
33 #include "testutils.h"
35 #include <check.h>
36 #include <stdlib.h>
38 /* Make SDB_INTERVAL_SECOND a constant initializer. */
39 #undef SDB_INTERVAL_SECOND
40 #define SDB_INTERVAL_SECOND 1000000000L
42 static void
43 populate(void)
44 {
45         sdb_data_t datum;
47         sdb_store_init();
49         sdb_store_host("h1", 1 * SDB_INTERVAL_SECOND);
50         sdb_store_host("h2", 3 * SDB_INTERVAL_SECOND);
52         datum.type = SDB_TYPE_STRING;
53         datum.data.string = "v1";
54         sdb_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
55         datum.data.string = "v2";
56         sdb_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
57         datum.data.string = "v3";
58         sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
60         /* make sure that older updates don't overwrite existing values */
61         datum.data.string = "fail";
62         sdb_store_attribute("h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
63         sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
65         sdb_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
66         sdb_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
67         sdb_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
69         sdb_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND);
70         sdb_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND);
72         datum.type = SDB_TYPE_INTEGER;
73         datum.data.integer = 42;
74         sdb_store_metric_attr("h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
76         datum.data.integer = 123;
77         sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
78         datum.data.integer = 4711;
79         sdb_store_service_attr("h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
81         /* don't overwrite k1 */
82         datum.data.integer = 666;
83         sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
84 } /* populate */
86 static int
87 scan_tojson(sdb_store_obj_t *obj,
88                 sdb_store_matcher_t __attribute__((unused)) *filter,
89                 void *user_data)
90 {
91         sdb_store_json_formatter_t *f = user_data;
92         return sdb_store_json_emit(f, obj);
93 } /* scan_tojson */
95 static int
96 scan_tojson_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
97                 void *user_data)
98 {
99         sdb_store_json_formatter_t *f = user_data;
100         return sdb_store_json_emit_full(f, obj, filter);
101 } /* scan_tojson_full */
103 static void
104 verify_json_output(sdb_strbuf_t *buf, const char *expected)
106         const char *got = sdb_strbuf_string(buf);
107         size_t len1 = strlen(got);
108         size_t len2 = strlen(expected);
110         size_t i;
111         int pos = -1;
113         if (len1 != len2)
114                 pos = (int)SDB_MIN(len1, len2);
116         for (i = 0; i < SDB_MIN(len1, len2); ++i) {
117                 if (got[i] != expected[i]) {
118                         pos = (int)i;
119                         break;
120                 }
121         }
123         fail_unless(pos == -1,
124                         "Serializing hosts to JSON returned unexpected result\n"
125                         "         got: %s\n              %*s\n    expected: %s",
126                         got, pos + 1, "^", expected);
127 } /* verify_json_output */
129 struct {
130         struct {
131                 sdb_store_matcher_t *(*m)(sdb_store_expr_t *, sdb_store_expr_t *);
132                 int field;
133                 sdb_data_t value;
134         } filter;
135         int type;
136         int (*f)(sdb_store_obj_t *, sdb_store_matcher_t *, void *);
137         const char *expected;
138 } store_tojson_data[] = {
139         { { NULL, 0, SDB_DATA_INIT },
140                 SDB_HOST, scan_tojson_full,
141                 "["
142                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
143                                 "\"update_interval\": \"0s\", \"backends\": [], "
144                                 "\"attributes\": ["
145                                         "{\"name\": \"k1\", \"value\": \"v1\", "
146                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
147                                                 "\"update_interval\": \"0s\", \"backends\": []},"
148                                         "{\"name\": \"k2\", \"value\": \"v2\", "
149                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
150                                                 "\"update_interval\": \"0s\", \"backends\": []},"
151                                         "{\"name\": \"k3\", \"value\": \"v3\", "
152                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
153                                                 "\"update_interval\": \"0s\", \"backends\": []}"
154                                 "], "
155                                 "\"metrics\": ["
156                                         "{\"name\": \"m1\", "
157                                                 "\"timeseries\": false, "
158                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
159                                                 "\"update_interval\": \"0s\", \"backends\": [], "
160                                                 "\"attributes\": ["
161                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
162                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
163                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
164                                                         "{\"name\": \"k3\", \"value\": 42, "
165                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
166                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
167                                                 "]},"
168                                         "{\"name\": \"m2\", "
169                                                 "\"timeseries\": false, "
170                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
171                                                 "\"update_interval\": \"0s\", \"backends\": [], "
172                                                 "\"attributes\": ["
173                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
174                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
175                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
176                                                 "]}"
177                                 "]},"
178                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
179                                 "\"update_interval\": \"0s\", \"backends\": [], "
180                                 "\"metrics\": ["
181                                         "{\"name\": \"m1\", "
182                                                 "\"timeseries\": false, "
183                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
184                                                 "\"update_interval\": \"0s\", \"backends\": [], "
185                                                 "\"attributes\": ["
186                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
187                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
188                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
189                                                 "]}"
190                                 "], "
191                                 "\"services\": ["
192                                         "{\"name\": \"s1\", "
193                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
194                                                 "\"update_interval\": \"0s\", \"backends\": [], "
195                                                 "\"attributes\": ["
196                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
197                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
198                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
199                                                 "]},"
200                                         "{\"name\": \"s2\", "
201                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
202                                                 "\"update_interval\": \"0s\", \"backends\": [], "
203                                                 "\"attributes\": ["
204                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
205                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
206                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
207                                                         "{\"name\": \"k1\", \"value\": 123, "
208                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
209                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
210                                                         "{\"name\": \"k2\", \"value\": 4711, "
211                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
212                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
213                                                 "]}"
214                                 "]}"
215                 "]" },
216         { { NULL, 0, SDB_DATA_INIT },
217                 SDB_HOST, scan_tojson,
218                 "["
219                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
220                                 "\"update_interval\": \"0s\", \"backends\": []},"
221                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
222                                 "\"update_interval\": \"0s\", \"backends\": []}"
223                 "]" },
224         { { sdb_store_eq_matcher, SDB_FIELD_NAME,
225                         { SDB_TYPE_STRING, { .string = "h1" } } },
226                 SDB_HOST, scan_tojson_full,
227                 "["
228                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
229                                 "\"update_interval\": \"0s\", \"backends\": []}"
230                 "]" },
231         { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
232                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
233                 SDB_HOST, scan_tojson_full,
234                 "["
235                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
236                                 "\"update_interval\": \"0s\", \"backends\": [], "
237                                 "\"services\": ["
238                                         "{\"name\": \"s2\", "
239                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
240                                                 "\"update_interval\": \"0s\", \"backends\": [], "
241                                                 "\"attributes\": ["
242                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
243                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
244                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
245                                                         "{\"name\": \"k1\", \"value\": 123, "
246                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
247                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
248                                                 "]}"
249                                 "]}"
250                 "]" },
251         { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
252                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
253                 SDB_HOST, scan_tojson_full,
254                 "["
255                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
256                                 "\"update_interval\": \"0s\", \"backends\": [], "
257                                 "\"attributes\": ["
258                                         "{\"name\": \"k1\", \"value\": \"v1\", "
259                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
260                                                 "\"update_interval\": \"0s\", \"backends\": []}"
261                                 "], "
262                                 "\"metrics\": ["
263                                         "{\"name\": \"m2\", "
264                                                 "\"timeseries\": false, "
265                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
266                                                 "\"update_interval\": \"0s\", \"backends\": [], "
267                                                 "\"attributes\": ["
268                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
269                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
270                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
271                                                 "]}"
272                                 "]}"
273                 "]" },
274         { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
275                         { SDB_TYPE_DATETIME, { .datetime = 3 * SDB_INTERVAL_SECOND } } },
276                 SDB_HOST, scan_tojson_full,
277                 "["
278                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
279                                 "\"update_interval\": \"0s\", \"backends\": []}"
280                 "]" },
281         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
282                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
283                 SDB_HOST, scan_tojson_full,
284                 "[]" },
286         { { NULL, 0, SDB_DATA_INIT },
287                 SDB_SERVICE, scan_tojson_full,
288                 "["
289                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
290                                 "\"update_interval\": \"0s\", \"backends\": [], "
291                                 "\"services\": ["
292                                         "{\"name\": \"s1\", "
293                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
294                                                 "\"update_interval\": \"0s\", \"backends\": [], "
295                                                 "\"attributes\": ["
296                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
297                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
298                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
299                                                 "]},"
300                                         "{\"name\": \"s2\", "
301                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
302                                                 "\"update_interval\": \"0s\", \"backends\": [], "
303                                                 "\"attributes\": ["
304                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
305                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
306                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
307                                                         "{\"name\": \"k1\", \"value\": 123, "
308                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
309                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
310                                                         "{\"name\": \"k2\", \"value\": 4711, "
311                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
312                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
313                                                 "]}"
314                                 "]}"
315                 "]" },
316         { { NULL, 0, SDB_DATA_INIT },
317                 SDB_SERVICE, scan_tojson,
318                 "["
319                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
320                                 "\"update_interval\": \"0s\", \"backends\": [], "
321                                 "\"services\": ["
322                                         "{\"name\": \"s1\", "
323                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
324                                                 "\"update_interval\": \"0s\", \"backends\": []},"
325                                         "{\"name\": \"s2\", "
326                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
327                                                 "\"update_interval\": \"0s\", \"backends\": []}"
328                                 "]}"
329                 "]" },
330         { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
331                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
332                 SDB_SERVICE, scan_tojson_full,
333                 "["
334                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
335                                 "\"update_interval\": \"0s\", \"backends\": [], "
336                                 "\"services\": ["
337                                         "{\"name\": \"s2\", "
338                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
339                                                 "\"update_interval\": \"0s\", \"backends\": [], "
340                                                 "\"attributes\": ["
341                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
342                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
343                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
344                                                         "{\"name\": \"k1\", \"value\": 123, "
345                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
346                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
347                                                 "]}"
348                                 "]}"
349                 "]" },
350         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
351                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
352                 SDB_SERVICE, scan_tojson_full,
353                 "[]" },
354         { { NULL, 0, SDB_DATA_INIT },
355                 SDB_METRIC, scan_tojson_full,
356                 "["
357                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
358                                 "\"update_interval\": \"0s\", \"backends\": [], "
359                                 "\"metrics\": ["
360                                         "{\"name\": \"m1\", "
361                                                 "\"timeseries\": false, "
362                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
363                                                 "\"update_interval\": \"0s\", \"backends\": [], "
364                                                 "\"attributes\": ["
365                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
366                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
367                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
368                                                         "{\"name\": \"k3\", \"value\": 42, "
369                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
370                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
371                                                 "]},"
372                                         "{\"name\": \"m2\", "
373                                                 "\"timeseries\": false, "
374                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
375                                                 "\"update_interval\": \"0s\", \"backends\": [], "
376                                                 "\"attributes\": ["
377                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
378                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
379                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
380                                                 "]}"
381                                 "]},"
382                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
383                                 "\"update_interval\": \"0s\", \"backends\": [], "
384                                 "\"metrics\": ["
385                                         "{\"name\": \"m1\", "
386                                                 "\"timeseries\": false, "
387                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
388                                                 "\"update_interval\": \"0s\", \"backends\": [], "
389                                                 "\"attributes\": ["
390                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
391                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
392                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
393                                                 "]}"
394                                 "]}"
395                 "]" },
396         { { NULL, 0, SDB_DATA_INIT },
397                 SDB_METRIC, scan_tojson,
398                 "["
399                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
400                                 "\"update_interval\": \"0s\", \"backends\": [], "
401                                 "\"metrics\": ["
402                                         "{\"name\": \"m1\", "
403                                                 "\"timeseries\": false, "
404                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
405                                                 "\"update_interval\": \"0s\", \"backends\": []},"
406                                         "{\"name\": \"m2\", "
407                                                 "\"timeseries\": false, "
408                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
409                                                 "\"update_interval\": \"0s\", \"backends\": []}"
410                                 "]},"
411                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
412                                 "\"update_interval\": \"0s\", \"backends\": [], "
413                                 "\"metrics\": ["
414                                         "{\"name\": \"m1\", "
415                                                 "\"timeseries\": false, "
416                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
417                                                 "\"update_interval\": \"0s\", \"backends\": []}"
418                                 "]}"
419                 "]" },
420         { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
421                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
422                 SDB_METRIC, scan_tojson_full,
423                 "["
424                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
425                                 "\"update_interval\": \"0s\", \"backends\": [], "
426                                 "\"metrics\": ["
427                                         "{\"name\": \"m2\", "
428                                                 "\"timeseries\": false, "
429                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
430                                                 "\"update_interval\": \"0s\", \"backends\": [], "
431                                                 "\"attributes\": ["
432                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
433                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
434                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
435                                                 "]}"
436                                 "]}"
437                 "]" },
438         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
439                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
440                 SDB_METRIC, scan_tojson_full,
441                 "[]" },
442 };
444 START_TEST(test_store_tojson)
446         sdb_strbuf_t *buf = sdb_strbuf_create(0);
447         sdb_store_matcher_t *filter = NULL;
448         sdb_store_json_formatter_t *f;
449         int status;
451         if (store_tojson_data[_i].filter.m) {
452                 sdb_store_expr_t *field;
453                 sdb_store_expr_t *value;
455                 field = sdb_store_expr_fieldvalue(store_tojson_data[_i].filter.field);
456                 fail_unless(field != NULL,
457                                 "INTERNAL ERROR: sdb_store_expr_fieldvalue() = NULL");
458                 value = sdb_store_expr_constvalue(&store_tojson_data[_i].filter.value);
459                 fail_unless(value != NULL,
460                                 "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL");
462                 filter = store_tojson_data[_i].filter.m(field, value);
463                 fail_unless(filter != NULL,
464                                 "INTERNAL ERROR: sdb_store_*_matcher() = NULL");
466                 sdb_object_deref(SDB_OBJ(field));
467                 sdb_object_deref(SDB_OBJ(value));
468         }
470         sdb_strbuf_clear(buf);
471         f = sdb_store_json_formatter(buf,
472                         store_tojson_data[_i].type, SDB_WANT_ARRAY);
473         ck_assert(f != NULL);
475         status = sdb_store_scan(store_tojson_data[_i].type,
476                         /* m = */ NULL, filter, store_tojson_data[_i].f, f);
477         fail_unless(status == 0,
478                         "sdb_store_scan(HOST, ..., tojson) = %d; expected: 0",
479                         status);
480         sdb_store_json_finish(f);
482         verify_json_output(buf, store_tojson_data[_i].expected);
484         free(f);
485         sdb_object_deref(SDB_OBJ(filter));
486         sdb_strbuf_destroy(buf);
488 END_TEST
490 TEST_MAIN("core::store_json")
492         TCase *tc = tcase_create("core");
493         TC_ADD_LOOP_TEST(tc, store_tojson);
494         tcase_add_unchecked_fixture(tc, populate, sdb_store_clear);
495         ADD_TCASE(tc);
497 TEST_MAIN_END
499 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */