Code

frontend/parser: Let ‘LIST’ and ‘FETCH’ accept optional filters as well.
[sysdb.git] / t / unit / core / store_lookup_test.c
index 1adece554b031c6f94712b2a4977aa6ed2715e63..82688833f2f1e1c3fa76cbc12a72bcac12f89d04 100644 (file)
@@ -248,7 +248,7 @@ START_TEST(test_store_match_attr)
 }
 END_TEST
 
-START_TEST(test_store_cond)
+START_TEST(test_attr_cond)
 {
        sdb_store_obj_t *obj;
 
@@ -287,13 +287,13 @@ START_TEST(test_store_cond)
 
                struct {
                        sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
-                       int *expected;
+                       int expected;
                } tests[] = {
-                       { sdb_store_lt_matcher, &golden_data[i].expected_lt },
-                       { sdb_store_le_matcher, &golden_data[i].expected_le },
-                       { sdb_store_eq_matcher, &golden_data[i].expected_eq },
-                       { sdb_store_ge_matcher, &golden_data[i].expected_ge },
-                       { sdb_store_gt_matcher, &golden_data[i].expected_gt },
+                       { sdb_store_lt_matcher, golden_data[i].expected_lt },
+                       { sdb_store_le_matcher, golden_data[i].expected_le },
+                       { sdb_store_eq_matcher, golden_data[i].expected_eq },
+                       { sdb_store_ge_matcher, golden_data[i].expected_ge },
+                       { sdb_store_gt_matcher, golden_data[i].expected_gt },
                };
 
                sdb_data_format(&golden_data[i].value,
@@ -319,11 +319,11 @@ START_TEST(test_store_cond)
                                        "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
 
                        status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
-                       fail_unless(status == *tests[j].expected,
+                       fail_unless(status == tests[j].expected,
                                        "sdb_store_matcher_matches(%s, <obj>, NULL) = %d; "
                                        "expected: %d",
                                        sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
-                                       status, *tests[j].expected);
+                                       status, tests[j].expected);
 
                        sdb_object_deref(SDB_OBJ(m));
                }
@@ -335,6 +335,104 @@ START_TEST(test_store_cond)
 }
 END_TEST
 
+START_TEST(test_obj_cond)
+{
+       struct {
+               const char *host;
+               int field;
+               const sdb_data_t value;
+               int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
+       } golden_data[] = {
+               /* last-update = 1 for all objects */
+               { "a", SDB_FIELD_LAST_UPDATE,
+                       { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
+               { "a", SDB_FIELD_LAST_UPDATE,
+                       { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
+               { "a", SDB_FIELD_LAST_UPDATE,
+                       { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
+               /* age > 0 for all objects */
+               { "a", SDB_FIELD_AGE,
+                       { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
+               /* interval = 0 for all objects */
+               { "a", SDB_FIELD_INTERVAL,
+                       { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
+               { "a", SDB_FIELD_INTERVAL,
+                       { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
+               /* type mismatch */
+               { "a", SDB_FIELD_LAST_UPDATE,
+                       { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+               { "a", SDB_FIELD_AGE,
+                       { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+               { "a", SDB_FIELD_INTERVAL,
+                       { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+               { "a", SDB_FIELD_BACKEND,
+                       { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+       };
+
+       int status;
+       size_t i;
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_store_obj_t *obj;
+               sdb_store_expr_t *expr;
+               sdb_store_cond_t *c;
+               char buf[1024];
+               size_t j;
+
+               struct {
+                       sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
+                       int expected;
+               } tests[] = {
+                       { sdb_store_lt_matcher, golden_data[i].expected_lt },
+                       { sdb_store_le_matcher, golden_data[i].expected_le },
+                       { sdb_store_eq_matcher, golden_data[i].expected_eq },
+                       { sdb_store_ge_matcher, golden_data[i].expected_ge },
+                       { sdb_store_gt_matcher, golden_data[i].expected_gt },
+               };
+
+               obj = sdb_store_get_host(golden_data[i].host);
+               fail_unless(obj != NULL,
+                               "sdb_store_get_host(%s) = NULL; expected: <host>",
+                               golden_data[i].host);
+
+               sdb_data_format(&golden_data[i].value,
+                               buf, sizeof(buf), SDB_UNQUOTED);
+
+               expr = sdb_store_expr_constvalue(&golden_data[i].value);
+               fail_unless(expr != NULL,
+                               "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
+                               buf);
+
+               c = sdb_store_obj_cond(golden_data[i].field, expr);
+               sdb_object_deref(SDB_OBJ(expr));
+               fail_unless(c != NULL,
+                               "sdb_store_obj_cond(%d, expr{%s}) = NULL; expected: <cond>",
+                               golden_data[i].field, buf);
+
+               for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
+                       sdb_store_matcher_t *m;
+                       char m_str[1024];
+
+                       m = tests[j].matcher(c);
+                       fail_unless(m != NULL,
+                                       "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
+
+                       status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
+                       fail_unless(status == tests[j].expected,
+                                       "sdb_store_matcher_matches(%s, <obj>, NULL) = %d; "
+                                       "expected: %d",
+                                       sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
+                                       status, tests[j].expected);
+
+                       sdb_object_deref(SDB_OBJ(m));
+               }
+
+               sdb_object_deref(SDB_OBJ(c));
+               sdb_object_deref(SDB_OBJ(obj));
+       }
+}
+END_TEST
+
 START_TEST(test_store_match_op)
 {
        sdb_store_obj_t *obj;
@@ -512,6 +610,90 @@ START_TEST(test_parse_cmp)
 }
 END_TEST
 
+START_TEST(test_parse_field_cmp)
+{
+       sdb_data_t datetime = { SDB_TYPE_DATETIME, { .datetime = 1 } };
+       sdb_data_t string = { SDB_TYPE_STRING, { .string = "s" } };
+
+       struct {
+               const char *field;
+               const char *op;
+               const sdb_data_t *value;
+               int expected;
+       } golden_data[] = {
+               { "last_update", "<",  &datetime, MATCHER_LT },
+               { "last_update", "<=", &datetime, MATCHER_LE },
+               { "last_update", "=",  &datetime, MATCHER_EQ },
+               { "last_update", ">=", &datetime, MATCHER_GE },
+               { "last_update", ">",  &datetime, MATCHER_GT },
+               { "last_update", "!=", &datetime, MATCHER_NOT },
+               { "age",         "<",  &datetime, MATCHER_LT },
+               { "age",         "<=", &datetime, MATCHER_LE },
+               { "age",         "=",  &datetime, MATCHER_EQ },
+               { "age",         ">=", &datetime, MATCHER_GE },
+               { "age",         ">",  &datetime, MATCHER_GT },
+               { "age",         "!=", &datetime, MATCHER_NOT },
+               { "interval",    "<",  &datetime, MATCHER_LT },
+               { "interval",    "<=", &datetime, MATCHER_LE },
+               { "interval",    "=",  &datetime, MATCHER_EQ },
+               { "interval",    ">=", &datetime, MATCHER_GE },
+               { "interval",    ">",  &datetime, MATCHER_GT },
+               { "interval",    "!=", &datetime, MATCHER_NOT },
+               { "backend",     "=",  &string,   MATCHER_EQ },
+               { "backend",     "!=", &string,   MATCHER_NOT },
+               /* the behavior for other operators on :backend
+                * is currently unspecified */
+               { "last_update", "=",  NULL,      -1 },
+               { "last_update", "IS", NULL,      -1 },
+               { "age",         "=",  NULL,      -1 },
+               { "interval",    "=",  NULL,      -1 },
+               { "backend",     "=",  NULL,      -1 },
+               { "backend",     "=~", &string,   -1 },
+       };
+
+       size_t i;
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_store_matcher_t *check;
+               sdb_store_expr_t *expr;
+               char buf[1024];
+
+               if (sdb_data_format(golden_data[i].value,
+                                       buf, sizeof(buf), SDB_UNQUOTED) < 0)
+                       snprintf(buf, sizeof(buf), "ERR");
+
+               expr = sdb_store_expr_constvalue(golden_data[i].value);
+               fail_unless(expr != NULL || golden_data[i].value == NULL,
+                               "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
+                               buf);
+
+               check = sdb_store_matcher_parse_field_cmp(golden_data[i].field,
+                               golden_data[i].op, expr);
+               sdb_object_deref(SDB_OBJ(expr));
+
+               if (golden_data[i].expected == -1) {
+                       fail_unless(check == NULL,
+                                       "sdb_store_matcher_parse_field_cmp(%s, %s, expr{%s}) = %p; "
+                                       "expected: NULL", golden_data[i].field,
+                                       golden_data[i].op, buf, check);
+                       continue;
+               }
+
+               fail_unless(check != NULL,
+                               "sdb_store_matcher_parse_field_cmp(%s, %s, %s) = %p; "
+                               "expected: NULL", golden_data[i].field,
+                               golden_data[i].op, buf, check);
+               fail_unless(M(check)->type == golden_data[i].expected,
+                               "sdb_store_matcher_parse_field_cmp(%s, %s, %s) returned "
+                               "matcher of type %d; expected: %d", golden_data[i].field,
+                               golden_data[i].op, buf, M(check)->type,
+                               golden_data[i].expected);
+
+               sdb_object_deref(SDB_OBJ(check));
+       }
+}
+END_TEST
+
 static int
 scan_cb(sdb_store_obj_t *obj, void *user_data)
 {
@@ -659,9 +841,11 @@ core_store_lookup_suite(void)
        tcase_add_checked_fixture(tc, populate, sdb_store_clear);
        tcase_add_test(tc, test_store_match_name);
        tcase_add_test(tc, test_store_match_attr);
-       tcase_add_test(tc, test_store_cond);
+       tcase_add_test(tc, test_attr_cond);
+       tcase_add_test(tc, test_obj_cond);
        tcase_add_test(tc, test_store_match_op);
        tcase_add_test(tc, test_parse_cmp);
+       tcase_add_test(tc, test_parse_field_cmp);
        tcase_add_test(tc, test_scan);
        suite_add_tcase(s, tc);