Code

store: Renamed sdb_store_cmp_XX to sdb_store_XX_matcher.
[sysdb.git] / t / unit / core / store_test.c
index 796c50cb1eddda2c6b030ccfc50ed73b23e3c83c..f459b6e0afd461be0754fb64cd192928e8b3e2c9 100644 (file)
@@ -408,7 +408,7 @@ END_TEST
 
 static void
 verify_json_output(sdb_strbuf_t *buf, const char *expected,
-               const char *filter_str, int flags)
+               sdb_store_matcher_t *filter, int flags)
 {
        int pos;
        size_t len1, len2;
@@ -429,9 +429,9 @@ verify_json_output(sdb_strbuf_t *buf, const char *expected,
        }
 
        fail_unless(pos == -1,
-                       "sdb_store_tojson(<buf>, %s, %x) returned unexpected result\n"
+                       "sdb_store_tojson(<buf>, %p, %x) returned unexpected result\n"
                        "         got: %s\n              %*s\n    expected: %s",
-                       filter_str, flags, sdb_strbuf_string(buf), pos + 1, "^",
+                       filter, flags, sdb_strbuf_string(buf), pos + 1, "^",
                        expected);
 } /* verify_json_output */
 
@@ -442,7 +442,8 @@ START_TEST(test_store_tojson)
 
        struct {
                struct {
-                       sdb_store_matcher_t *(*m)(sdb_store_cond_t *);
+                       sdb_store_matcher_t *(*m)(sdb_store_expr_t *,
+                                       sdb_store_expr_t *);
                        int field;
                        sdb_data_t value;
                } filter;
@@ -604,6 +605,13 @@ START_TEST(test_store_tojson)
                                "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
                                        "\"update_interval\": \"0s\", \"backends\": []}"
                        "]" },
+               { { sdb_store_eq_matcher, SDB_FIELD_NAME,
+                               { SDB_TYPE_STRING, { .string = "h1" } } }, 0,
+                       "["
+                               "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                       "\"attributes\": [], \"metrics\": [], \"services\": []}"
+                       "]" },
                { { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
                                { SDB_TYPE_DATETIME, { .datetime = 1 } } }, 0,
                        "["
@@ -657,39 +665,36 @@ START_TEST(test_store_tojson)
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                sdb_store_matcher_t *filter = NULL;
-               char filter_str[1024] = "<none>";
                int status;
 
                sdb_strbuf_clear(buf);
 
                if (golden_data[i].filter.m) {
-                       sdb_store_expr_t *expr;
-                       sdb_store_cond_t *c = NULL;
-
-                       expr = sdb_store_expr_constvalue(&golden_data[i].filter.value);
-                       fail_unless(expr != NULL,
+                       sdb_store_expr_t *field;
+                       sdb_store_expr_t *value;
+
+                       field = sdb_store_expr_fieldvalue(golden_data[i].filter.field);
+                       fail_unless(field != NULL,
+                                       "INTERNAL ERROR: sdb_store_expr_fieldvalue() = NULL");
+                       value = sdb_store_expr_constvalue(&golden_data[i].filter.value);
+                       fail_unless(value != NULL,
                                        "INTERNAL ERROR: sdb_store_expr_constvalue() = NULL");
-                       c = sdb_store_obj_cond(golden_data[i].filter.field, expr);
-                       sdb_object_deref(SDB_OBJ(expr));
-                       fail_unless(c != NULL,
-                                       "INTERNAL ERROR: sdb_store_obj_cond() = NULL");
-                       filter = golden_data[i].filter.m(c);
-                       sdb_object_deref(SDB_OBJ(c));
+
+                       filter = golden_data[i].filter.m(field, value);
                        fail_unless(filter != NULL,
                                        "INTERNAL ERROR: sdb_store_*_matcher() = NULL");
 
-                       if (sdb_store_matcher_tostring(filter,
-                                               filter_str, sizeof(filter_str)))
-                               snprintf(filter_str, sizeof(filter_str), "ERR");
+                       sdb_object_deref(SDB_OBJ(field));
+                       sdb_object_deref(SDB_OBJ(value));
                }
 
                status = sdb_store_tojson(buf, filter, golden_data[i].flags);
                fail_unless(status == 0,
-                               "sdb_store_tojson(<buf>, %s, %x) = %d; expected: 0",
-                               filter_str, golden_data[i].flags, status);
+                               "sdb_store_tojson(<buf>, %p, %x) = %d; expected: 0",
+                               filter, golden_data[i].flags, status);
 
                verify_json_output(buf, golden_data[i].expected,
-                               filter_str, golden_data[i].flags);
+                               filter, golden_data[i].flags);
                sdb_object_deref(SDB_OBJ(filter));
        }
        sdb_strbuf_destroy(buf);
@@ -716,15 +721,33 @@ START_TEST(test_get_field)
        fail_unless(check < 0,
                        "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
                        "expected: <0");
-       check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
-       fail_unless(check < 0,
-                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
-                       "expected: <0");
        check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, &value);
        fail_unless(check < 0,
                        "sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, <value>) = %d; "
                        "expected: <0");
 
+       check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+                       "expected: 0");
+       /* 'name' is dynamically allocated; make sure it's not leaked even
+        * if there is no result parameter */
+       check = sdb_store_get_field(host, SDB_FIELD_NAME, NULL);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+                       "expected: 0");
+
+       check = sdb_store_get_field(host, SDB_FIELD_NAME, &value);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) = "
+                       "%d; expected: 0");
+       fail_unless((value.type == SDB_TYPE_STRING)
+                       && (! strcmp(value.data.string, "host")),
+                       "sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) "
+                       "returned value {%d, %s}; expected {%d, host}",
+                       value.type, value.data.string, SDB_TYPE_STRING);
+       sdb_data_free_datum(&value);
+
        check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, &value);
        fail_unless(check == 0,
                        "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, <value>) = "
@@ -756,6 +779,19 @@ START_TEST(test_get_field)
                        "sdb_store_get_field(<host>, SDB_FIELD_INTERVAL, <value>) "
                        "returned value {%d, %lu}; expected {%d, 10}",
                        value.type, value.data.datetime, SDB_TYPE_DATETIME);
+
+       check = sdb_store_get_field(host, SDB_FIELD_BACKEND, &value);
+       fail_unless(check == 0,
+                       "sdb_store_get_field(<host>, SDB_FIELD_BACKEND, <value>) = "
+                       "%d; expected: 0");
+       /* there are no backends in this test */
+       fail_unless((value.type == (SDB_TYPE_ARRAY | SDB_TYPE_STRING))
+                       && (value.data.array.length == 0)
+                       && (value.data.array.values == NULL),
+                       "sdb_store_get_field(<host>, SDB_FIELD_BACKEND, <value>) "
+                       "returned value {%d, %lu, %p}; expected {%d, 0, NULL}",
+                       value.type, value.data.array.length, value.data.array.values,
+                       SDB_TYPE_ARRAY | SDB_TYPE_STRING);
 }
 END_TEST