Code

data: Use a predefined format for sdb_strftime().
[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_host("h1", 1 * SDB_INTERVAL_SECOND);
48         sdb_store_host("h2", 3 * SDB_INTERVAL_SECOND);
50         datum.type = SDB_TYPE_STRING;
51         datum.data.string = "v1";
52         sdb_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
53         datum.data.string = "v2";
54         sdb_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
55         datum.data.string = "v3";
56         sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
58         /* make sure that older updates don't overwrite existing values */
59         datum.data.string = "fail";
60         sdb_store_attribute("h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
61         sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
63         sdb_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
64         sdb_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
65         sdb_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
67         sdb_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND);
68         sdb_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND);
70         datum.type = SDB_TYPE_INTEGER;
71         datum.data.integer = 42;
72         sdb_store_metric_attr("h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
74         datum.data.integer = 123;
75         sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
76         datum.data.integer = 4711;
77         sdb_store_service_attr("h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
79         /* don't overwrite k1 */
80         datum.data.integer = 666;
81         sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
82 } /* populate */
84 static int
85 scan_tojson(sdb_store_obj_t *obj,
86                 sdb_store_matcher_t __attribute__((unused)) *filter,
87                 void *user_data)
88 {
89         sdb_store_json_formatter_t *f = user_data;
90         return sdb_store_json_emit(f, obj);
91 } /* scan_tojson */
93 static int
94 scan_tojson_full(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
95                 void *user_data)
96 {
97         sdb_store_json_formatter_t *f = user_data;
98         return sdb_store_json_emit_full(f, obj, filter);
99 } /* scan_tojson_full */
101 static void
102 verify_json_output(sdb_strbuf_t *buf, const char *expected)
104         const char *got = sdb_strbuf_string(buf);
105         size_t len1 = strlen(got);
106         size_t len2 = strlen(expected);
108         size_t i;
109         int pos = -1;
111         if (len1 != len2)
112                 pos = (int)SDB_MIN(len1, len2);
114         for (i = 0; i < SDB_MIN(len1, len2); ++i) {
115                 if (got[i] != expected[i]) {
116                         pos = (int)i;
117                         break;
118                 }
119         }
121         fail_unless(pos == -1,
122                         "Serializing hosts to JSON returned unexpected result\n"
123                         "         got: %s\n              %*s\n    expected: %s",
124                         got, pos + 1, "^", expected);
125 } /* verify_json_output */
127 struct {
128         struct {
129                 sdb_store_matcher_t *(*m)(sdb_store_expr_t *, sdb_store_expr_t *);
130                 int field;
131                 sdb_data_t value;
132         } filter;
133         int type;
134         int (*f)(sdb_store_obj_t *, sdb_store_matcher_t *, void *);
135         const char *expected;
136 } store_tojson_data[] = {
137         { { NULL, 0, SDB_DATA_INIT },
138                 SDB_HOST, scan_tojson_full,
139                 "["
140                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
141                                 "\"update_interval\": \"0s\", \"backends\": [], "
142                                 "\"attributes\": ["
143                                         "{\"name\": \"k1\", \"value\": \"v1\", "
144                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
145                                                 "\"update_interval\": \"0s\", \"backends\": []},"
146                                         "{\"name\": \"k2\", \"value\": \"v2\", "
147                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
148                                                 "\"update_interval\": \"0s\", \"backends\": []},"
149                                         "{\"name\": \"k3\", \"value\": \"v3\", "
150                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
151                                                 "\"update_interval\": \"0s\", \"backends\": []}"
152                                 "], "
153                                 "\"metrics\": ["
154                                         "{\"name\": \"m1\", "
155                                                 "\"timeseries\": false, "
156                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
157                                                 "\"update_interval\": \"0s\", \"backends\": [], "
158                                                 "\"attributes\": ["
159                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
160                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
161                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
162                                                         "{\"name\": \"k3\", \"value\": 42, "
163                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
164                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
165                                                 "]},"
166                                         "{\"name\": \"m2\", "
167                                                 "\"timeseries\": false, "
168                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
169                                                 "\"update_interval\": \"0s\", \"backends\": [], "
170                                                 "\"attributes\": ["
171                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
172                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
173                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
174                                                 "]}"
175                                 "]},"
176                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
177                                 "\"update_interval\": \"0s\", \"backends\": [], "
178                                 "\"metrics\": ["
179                                         "{\"name\": \"m1\", "
180                                                 "\"timeseries\": false, "
181                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
182                                                 "\"update_interval\": \"0s\", \"backends\": [], "
183                                                 "\"attributes\": ["
184                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
185                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
186                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
187                                                 "]}"
188                                 "], "
189                                 "\"services\": ["
190                                         "{\"name\": \"s1\", "
191                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
192                                                 "\"update_interval\": \"0s\", \"backends\": [], "
193                                                 "\"attributes\": ["
194                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
195                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
196                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
197                                                 "]},"
198                                         "{\"name\": \"s2\", "
199                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
200                                                 "\"update_interval\": \"0s\", \"backends\": [], "
201                                                 "\"attributes\": ["
202                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
203                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
204                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
205                                                         "{\"name\": \"k1\", \"value\": 123, "
206                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
207                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
208                                                         "{\"name\": \"k2\", \"value\": 4711, "
209                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
210                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
211                                                 "]}"
212                                 "]}"
213                 "]" },
214         { { NULL, 0, SDB_DATA_INIT },
215                 SDB_HOST, scan_tojson,
216                 "["
217                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
218                                 "\"update_interval\": \"0s\", \"backends\": []},"
219                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
220                                 "\"update_interval\": \"0s\", \"backends\": []}"
221                 "]" },
222         { { sdb_store_eq_matcher, SDB_FIELD_NAME,
223                         { SDB_TYPE_STRING, { .string = "h1" } } },
224                 SDB_HOST, scan_tojson_full,
225                 "["
226                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
227                                 "\"update_interval\": \"0s\", \"backends\": []}"
228                 "]" },
229         { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
230                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
231                 SDB_HOST, scan_tojson_full,
232                 "["
233                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
234                                 "\"update_interval\": \"0s\", \"backends\": [], "
235                                 "\"services\": ["
236                                         "{\"name\": \"s2\", "
237                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
238                                                 "\"update_interval\": \"0s\", \"backends\": [], "
239                                                 "\"attributes\": ["
240                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
241                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
242                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
243                                                         "{\"name\": \"k1\", \"value\": 123, "
244                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
245                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
246                                                 "]}"
247                                 "]}"
248                 "]" },
249         { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
250                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
251                 SDB_HOST, scan_tojson_full,
252                 "["
253                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
254                                 "\"update_interval\": \"0s\", \"backends\": [], "
255                                 "\"attributes\": ["
256                                         "{\"name\": \"k1\", \"value\": \"v1\", "
257                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
258                                                 "\"update_interval\": \"0s\", \"backends\": []}"
259                                 "], "
260                                 "\"metrics\": ["
261                                         "{\"name\": \"m2\", "
262                                                 "\"timeseries\": false, "
263                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
264                                                 "\"update_interval\": \"0s\", \"backends\": [], "
265                                                 "\"attributes\": ["
266                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
267                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
268                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
269                                                 "]}"
270                                 "]}"
271                 "]" },
272         { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
273                         { SDB_TYPE_DATETIME, { .datetime = 3 * SDB_INTERVAL_SECOND } } },
274                 SDB_HOST, scan_tojson_full,
275                 "["
276                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
277                                 "\"update_interval\": \"0s\", \"backends\": []}"
278                 "]" },
279         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
280                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
281                 SDB_HOST, scan_tojson_full,
282                 "[]" },
284         { { NULL, 0, SDB_DATA_INIT },
285                 SDB_SERVICE, scan_tojson_full,
286                 "["
287                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
288                                 "\"update_interval\": \"0s\", \"backends\": [], "
289                                 "\"services\": ["
290                                         "{\"name\": \"s1\", "
291                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
292                                                 "\"update_interval\": \"0s\", \"backends\": [], "
293                                                 "\"attributes\": ["
294                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
295                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
296                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
297                                                 "]},"
298                                         "{\"name\": \"s2\", "
299                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
300                                                 "\"update_interval\": \"0s\", \"backends\": [], "
301                                                 "\"attributes\": ["
302                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
303                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
304                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
305                                                         "{\"name\": \"k1\", \"value\": 123, "
306                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
307                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
308                                                         "{\"name\": \"k2\", \"value\": 4711, "
309                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
310                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
311                                                 "]}"
312                                 "]}"
313                 "]" },
314         { { NULL, 0, SDB_DATA_INIT },
315                 SDB_SERVICE, scan_tojson,
316                 "["
317                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
318                                 "\"update_interval\": \"0s\", \"backends\": [], "
319                                 "\"services\": ["
320                                         "{\"name\": \"s1\", "
321                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
322                                                 "\"update_interval\": \"0s\", \"backends\": []},"
323                                         "{\"name\": \"s2\", "
324                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
325                                                 "\"update_interval\": \"0s\", \"backends\": []}"
326                                 "]}"
327                 "]" },
328         { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
329                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
330                 SDB_SERVICE, scan_tojson_full,
331                 "["
332                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
333                                 "\"update_interval\": \"0s\", \"backends\": [], "
334                                 "\"services\": ["
335                                         "{\"name\": \"s2\", "
336                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
337                                                 "\"update_interval\": \"0s\", \"backends\": [], "
338                                                 "\"attributes\": ["
339                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
340                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
341                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
342                                                         "{\"name\": \"k1\", \"value\": 123, "
343                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
344                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
345                                                 "]}"
346                                 "]}"
347                 "]" },
348         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
349                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
350                 SDB_SERVICE, scan_tojson_full,
351                 "[]" },
352         { { NULL, 0, SDB_DATA_INIT },
353                 SDB_METRIC, scan_tojson_full,
354                 "["
355                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
356                                 "\"update_interval\": \"0s\", \"backends\": [], "
357                                 "\"metrics\": ["
358                                         "{\"name\": \"m1\", "
359                                                 "\"timeseries\": false, "
360                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
361                                                 "\"update_interval\": \"0s\", \"backends\": [], "
362                                                 "\"attributes\": ["
363                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
364                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
365                                                                 "\"update_interval\": \"0s\", \"backends\": []},"
366                                                         "{\"name\": \"k3\", \"value\": 42, "
367                                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
368                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
369                                                 "]},"
370                                         "{\"name\": \"m2\", "
371                                                 "\"timeseries\": false, "
372                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
373                                                 "\"update_interval\": \"0s\", \"backends\": [], "
374                                                 "\"attributes\": ["
375                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
376                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
377                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
378                                                 "]}"
379                                 "]},"
380                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
381                                 "\"update_interval\": \"0s\", \"backends\": [], "
382                                 "\"metrics\": ["
383                                         "{\"name\": \"m1\", "
384                                                 "\"timeseries\": false, "
385                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
386                                                 "\"update_interval\": \"0s\", \"backends\": [], "
387                                                 "\"attributes\": ["
388                                                         "{\"name\": \"hostname\", \"value\": \"h2\", "
389                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
390                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
391                                                 "]}"
392                                 "]}"
393                 "]" },
394         { { NULL, 0, SDB_DATA_INIT },
395                 SDB_METRIC, scan_tojson,
396                 "["
397                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
398                                 "\"update_interval\": \"0s\", \"backends\": [], "
399                                 "\"metrics\": ["
400                                         "{\"name\": \"m1\", "
401                                                 "\"timeseries\": false, "
402                                                 "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
403                                                 "\"update_interval\": \"0s\", \"backends\": []},"
404                                         "{\"name\": \"m2\", "
405                                                 "\"timeseries\": false, "
406                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
407                                                 "\"update_interval\": \"0s\", \"backends\": []}"
408                                 "]},"
409                         "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
410                                 "\"update_interval\": \"0s\", \"backends\": [], "
411                                 "\"metrics\": ["
412                                         "{\"name\": \"m1\", "
413                                                 "\"timeseries\": false, "
414                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
415                                                 "\"update_interval\": \"0s\", \"backends\": []}"
416                                 "]}"
417                 "]" },
418         { { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
419                         { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
420                 SDB_METRIC, scan_tojson_full,
421                 "["
422                         "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
423                                 "\"update_interval\": \"0s\", \"backends\": [], "
424                                 "\"metrics\": ["
425                                         "{\"name\": \"m2\", "
426                                                 "\"timeseries\": false, "
427                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
428                                                 "\"update_interval\": \"0s\", \"backends\": [], "
429                                                 "\"attributes\": ["
430                                                         "{\"name\": \"hostname\", \"value\": \"h1\", "
431                                                                 "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
432                                                                 "\"update_interval\": \"0s\", \"backends\": []}"
433                                                 "]}"
434                                 "]}"
435                 "]" },
436         { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
437                         { SDB_TYPE_DATETIME, { .datetime = 0 } } },
438                 SDB_METRIC, scan_tojson_full,
439                 "[]" },
440 };
442 START_TEST(test_store_tojson)
444         sdb_strbuf_t *buf = sdb_strbuf_create(0);
445         sdb_store_matcher_t *filter = NULL;
446         sdb_store_json_formatter_t *f;
447         int status;
449         if (store_tojson_data[_i].filter.m) {
450                 sdb_store_expr_t *field;
451                 sdb_store_expr_t *value;
453                 field = sdb_store_expr_fieldvalue(store_tojson_data[_i].filter.field);
454                 fail_unless(field != NULL,
455                                 "INTERNAL ERROR: sdb_store_expr_fieldvalue() = NULL");
456                 value = sdb_store_expr_constvalue(&store_tojson_data[_i].filter.value);
457                 fail_unless(value != NULL,
458                                 "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL");
460                 filter = store_tojson_data[_i].filter.m(field, value);
461                 fail_unless(filter != NULL,
462                                 "INTERNAL ERROR: sdb_store_*_matcher() = NULL");
464                 sdb_object_deref(SDB_OBJ(field));
465                 sdb_object_deref(SDB_OBJ(value));
466         }
468         sdb_strbuf_clear(buf);
469         f = sdb_store_json_formatter(buf,
470                         store_tojson_data[_i].type, SDB_WANT_ARRAY);
471         ck_assert(f != NULL);
473         status = sdb_store_scan(store_tojson_data[_i].type,
474                         /* m = */ NULL, filter, store_tojson_data[_i].f, f);
475         fail_unless(status == 0,
476                         "sdb_store_scan(HOST, ..., tojson) = %d; expected: 0",
477                         status);
478         sdb_store_json_finish(f);
480         verify_json_output(buf, store_tojson_data[_i].expected);
482         free(f);
483         sdb_object_deref(SDB_OBJ(filter));
484         sdb_strbuf_destroy(buf);
486 END_TEST
488 TEST_MAIN("core::store_json")
490         TCase *tc = tcase_create("core");
491         TC_ADD_LOOP_TEST(tc, store_tojson);
492         tcase_add_unchecked_fixture(tc, populate, sdb_store_clear);
493         ADD_TCASE(tc);
495 TEST_MAIN_END
497 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */