Code

store, frontend: Renamed sdb_store_lookup() to sdb_store_scan().
[sysdb.git] / t / unit / core / store_lookup_test.c
index 6dd7bb3fa1916a2119cc1985f9c2dc7393a3db47..351ae18da7dfd899c918c897da267503597d644b 100644 (file)
@@ -85,7 +85,7 @@ populate(void)
 
 START_TEST(test_store_match_name)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        struct {
                int type;
@@ -170,7 +170,7 @@ END_TEST
 
 START_TEST(test_store_match_attr)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        struct {
                const char *attr_name;
@@ -184,8 +184,6 @@ START_TEST(test_store_match_attr)
                { "1",  NULL,   1, 0 },
                { "k3", NULL,   0, 0 },
                { "k3", NULL,   1, 0 },
-               { "k3", NULL,   0, 0 },
-               { "k3", NULL,   1, 0 },
                { "k1", "v1",   0, 1 },
                { "k1", "v1",   1, 1 },
                { "k1", "^v1$", 1, 1 },
@@ -247,7 +245,7 @@ END_TEST
 
 START_TEST(test_store_cond)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        struct {
                const char *attr;
@@ -327,7 +325,7 @@ END_TEST
 
 START_TEST(test_store_match_op)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
        sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
@@ -409,51 +407,53 @@ START_TEST(test_parse_cmp)
                const char *obj_type;
                const char *attr;
                const char *op;
-               const sdb_data_t value;
+               const sdb_data_t *value;
                int expected;
        } golden_data[] = {
-               { "host",      "name", "=",  hostname, MATCHER_NAME },
-               { "host",      "name", "!=", hostname, MATCHER_NOT },
-               { "host",      "name", "=~", hostname, MATCHER_NAME },
-               { "host",      "name", "!~", hostname, MATCHER_NOT },
-               { "host",      "attr", "=",  hostname, -1 },
-               { "host",      "attr", "!=", hostname, -1 },
-               { "host",      "name", "&^", hostname, -1 },
-               { "host",      "name", "<",  hostname, -1 },
-               { "host",      "name", "<=", hostname, -1 },
-               { "host",      "name", ">=", hostname, -1 },
-               { "host",      "name", ">",  hostname, -1 },
-               { "service",   "name", "=",  srvname,  MATCHER_NAME },
-               { "service",   "name", "!=", srvname,  MATCHER_NOT },
-               { "service",   "name", "=~", srvname,  MATCHER_NAME },
-               { "service",   "name", "!~", srvname,  MATCHER_NOT },
-               { "service",   "attr", "=",  srvname,  -1 },
-               { "service",   "attr", "!=", srvname,  -1 },
-               { "service",   "name", "&^", srvname,  -1 },
-               { "service",   "name", "<",  srvname,  -1 },
-               { "service",   "name", "<=", srvname,  -1 },
-               { "service",   "name", ">=", srvname,  -1 },
-               { "service",   "name", ">",  srvname,  -1 },
-               { "attribute", "name", "=",  attrname, MATCHER_NAME },
-               { "attribute", "name", "!=", attrname, MATCHER_NOT },
-               { "attribute", "name", "=~", attrname, MATCHER_NAME },
-               { "attribute", "name", "!~", attrname, MATCHER_NOT },
-               { "attribute", "name", "<",  attrname, -1 },
-               { "attribute", "name", "<=", attrname, -1 },
-               { "attribute", "name", ">=", attrname, -1 },
-               { "attribute", "name", ">",  attrname, -1 },
-               { "attribute", "attr", "=",  attrname, MATCHER_ATTR },
-               { "attribute", "attr", "!=", attrname, MATCHER_NOT },
-               { "attribute", "attr", "=~", attrname, MATCHER_ATTR },
-               { "attribute", "attr", "!~", attrname, MATCHER_NOT },
-               { "attribute", "attr", "&^", attrname, -1 },
-               { "attribute", "attr", "<",  attrname, MATCHER_LT },
-               { "attribute", "attr", "<=", attrname, MATCHER_LE },
-/*             { "attribute", "attr", "=",  attrname, MATCHER_EQ }, */
-               { "attribute", "attr", ">=", attrname, MATCHER_GE },
-               { "attribute", "attr", ">",  attrname, MATCHER_GT },
-               { "foo",       "name", "=",  attrname, -1 },
-               { "foo",       "attr", "=",  attrname, -1 },
+               { "host",      NULL,   "=",  &hostname, MATCHER_NAME },
+               { "host",      NULL,   "!=", &hostname, MATCHER_NOT },
+               { "host",      NULL,   "=~", &hostname, MATCHER_NAME },
+               { "host",      NULL,   "!~", &hostname, MATCHER_NOT },
+               { "host",      "attr", "=",  &hostname, -1 },
+               { "host",      "attr", "!=", &hostname, -1 },
+               { "host",      NULL,   "&^", &hostname, -1 },
+               { "host",      NULL,   "<",  &hostname, -1 },
+               { "host",      NULL,   "<=", &hostname, -1 },
+               { "host",      NULL,   ">=", &hostname, -1 },
+               { "host",      NULL,   ">",  &hostname, -1 },
+               { "service",   NULL,   "=",  &srvname,  MATCHER_NAME },
+               { "service",   NULL,   "!=", &srvname,  MATCHER_NOT },
+               { "service",   NULL,   "=~", &srvname,  MATCHER_NAME },
+               { "service",   NULL,   "!~", &srvname,  MATCHER_NOT },
+               { "service",   "attr", "=",  &srvname,  -1 },
+               { "service",   "attr", "!=", &srvname,  -1 },
+               { "service",   NULL,   "&^", &srvname,  -1 },
+               { "service",   NULL,   "<",  &srvname,  -1 },
+               { "service",   NULL,   "<=", &srvname,  -1 },
+               { "service",   NULL,   ">=", &srvname,  -1 },
+               { "service",   NULL,   ">",  &srvname,  -1 },
+               { "attribute", NULL,   "=",  &attrname, MATCHER_NAME },
+               { "attribute", NULL,   "!=", &attrname, MATCHER_NOT },
+               { "attribute", NULL,   "=~", &attrname, MATCHER_NAME },
+               { "attribute", NULL,   "!~", &attrname, MATCHER_NOT },
+               { "attribute", NULL,   "<",  &attrname, -1 },
+               { "attribute", NULL,   "<=", &attrname, -1 },
+               { "attribute", NULL,   ">=", &attrname, -1 },
+               { "attribute", NULL,   ">",  &attrname, -1 },
+               { "attribute", "attr", "=",  &attrname, MATCHER_ATTR },
+               { "attribute", "attr", "!=", &attrname, MATCHER_NOT },
+               { "attribute", "attr", "=~", &attrname, MATCHER_ATTR },
+               { "attribute", "attr", "!~", &attrname, MATCHER_NOT },
+               { "attribute", "attr", "&^", &attrname, -1 },
+               { "attribute", "attr", "<",  &attrname, MATCHER_LT },
+               { "attribute", "attr", "<=", &attrname, MATCHER_LE },
+/*             { "attribute", "attr", "=",  &attrname, MATCHER_EQ }, */
+               { "attribute", "attr", ">=", &attrname, MATCHER_GE },
+               { "attribute", "attr", ">",  &attrname, MATCHER_GT },
+               { "attribute", "attr", "IS", NULL,      MATCHER_ISNULL },
+               { "attribute", "attr", "IS", &attrname, -1 },
+               { "foo",       NULL,   "=",  &attrname, -1 },
+               { "foo",       "attr", "=",  &attrname, -1 },
        };
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
@@ -461,9 +461,9 @@ START_TEST(test_parse_cmp)
 
                check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
                                golden_data[i].attr, golden_data[i].op,
-                               &golden_data[i].value);
+                               golden_data[i].value);
 
-               if (sdb_data_format(&golden_data[i].value,
+               if (sdb_data_format(golden_data[i].value,
                                        buf, sizeof(buf), SDB_UNQUOTED) < 0)
                        snprintf(buf, sizeof(buf), "ERR");
 
@@ -491,22 +491,22 @@ START_TEST(test_parse_cmp)
 END_TEST
 
 static int
-lookup_cb(sdb_store_base_t *obj, void *user_data)
+scan_cb(sdb_store_obj_t *obj, void *user_data)
 {
        int *i = user_data;
 
        fail_unless(obj != NULL,
-                       "sdb_store_lookup callback received NULL obj; expected: "
+                       "sdb_store_scan callback received NULL obj; expected: "
                        "<store base obj>");
        fail_unless(i != NULL,
-                       "sdb_store_lookup callback received NULL user_data; "
+                       "sdb_store_scan callback received NULL user_data; "
                        "expected: <pointer to data>");
 
        ++(*i);
        return 0;
-} /* lookup_cb */
+} /* scan_cb */
 
-START_TEST(test_lookup)
+START_TEST(test_scan)
 {
 #define PTR_RE "0x[0-9a-f]+"
        struct {
@@ -514,44 +514,52 @@ START_TEST(test_lookup)
                int expected;
                const char *tostring_re;
        } golden_data[] = {
-               { "host.name = 'a'",       1,
+               { "host = 'a'",               1,
                        "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
-               { "host.name =~ 'a|b'",    2,
+               { "host =~ 'a|b'",            2,
                        "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
-               { "host.name =~ 'host'",   0,
+               { "host =~ 'host'",           0,
                        "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
-               { "host.name =~ '.'",      3,
+               { "host =~ '.'",              3,
                        "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
-               { "service.name = 's1'",   2,
+               { "service = 's1'",           2,
                        "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
-               { "service.name =~ 's'",   2,
+               { "service =~ 's'",           2,
                        "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
-               { "service.name !~ 's'",   1,
+               { "service !~ 's'",           1,
                        "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
-               { "attribute.name = 'k1'", 1,
+               { "attribute = 'k1'",         1,
                        "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
-               { "attribute.name = 'x'",  0,
+               { "attribute = 'x'",          0,
                        "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
-               { "attribute.k1 = 'v1'",   1,
+               { "attribute.k1 = 'v1'",      1,
                        "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
-               { "attribute.k2 < 123",    0,
+               { "attribute.k1 IS NULL",     2,
+                       "\\(IS NULL, ATTR\\[k1\\]\\)" },
+               { "attribute.x1 IS NULL",     3,
+                       "\\(IS NULL, ATTR\\[x1\\]\\)" },
+               { "attribute.k1 IS NOT NULL", 1,
+                       "\\(NOT, \\(IS NULL, ATTR\\[k1\\]\\)\\)" },
+               { "attribute.x1 IS NOT NULL", 0,
+                       "\\(NOT, \\(IS NULL, ATTR\\[x1\\]\\)\\)" },
+               { "attribute.k2 < 123",       0,
                        "ATTR\\[k2\\]\\{ < 123 \\}" },
-               { "attribute.k2 <= 123",   1,
+               { "attribute.k2 <= 123",      1,
                        "ATTR\\[k2\\]\\{ <= 123 \\}" },
-               { "attribute.k2 >= 123",   1,
+               { "attribute.k2 >= 123",      1,
                        "ATTR\\[k2\\]\\{ >= 123 \\}" },
-               { "attribute.k2 > 123",    0,
+               { "attribute.k2 > 123",       0,
                        "ATTR\\[k2\\]\\{ > 123 \\}" },
-               { "attribute.k2 = 123",    1,
+               { "attribute.k2 = 123",       1,
                        "ATTR\\[k2\\]\\{ = 123 \\}" },
-               { "attribute.k2 != 123",   2,
+               { "attribute.k2 != 123",      2,
                        "\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
-               { "attribute.k1 != 'v1'",  2,
+               { "attribute.k1 != 'v1'",     2,
                        "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
-               { "attribute.k1 != 'v2'",  3,
+               { "attribute.k1 != 'v2'",     3,
                        "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
-               { "attribute.name != 'x' "
-                 "AND attribute.y !~ 'x'", 3,
+               { "attribute != 'x' "
+                 "AND attribute.y !~ 'x'",   3,
                        "\\(AND, "
                                "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
                                "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
@@ -561,11 +569,11 @@ START_TEST(test_lookup)
        size_t i;
 
        n = 0;
-       check = sdb_store_lookup(NULL, lookup_cb, &n);
+       check = sdb_store_scan(NULL, scan_cb, &n);
        fail_unless(check == 0,
-                       "sdb_store_lookup() = %d; expected: 0", check);
+                       "sdb_store_scan() = %d; expected: 0", check);
        fail_unless(n == 3,
-                       "sdb_store_lookup called callback %d times; expected: 3", (int)n);
+                       "sdb_store_scan called callback %d times; expected: 3", (int)n);
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                sdb_store_matcher_t *m;
@@ -583,9 +591,9 @@ START_TEST(test_lookup)
                                golden_data[i].tostring_re);
 
                n = 0;
-               sdb_store_lookup(m, lookup_cb, &n);
+               sdb_store_scan(m, scan_cb, &n);
                fail_unless(n == golden_data[i].expected,
-                               "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
+                               "sdb_store_scan(matcher{%s}) found %d hosts; expected: %d",
                                golden_data[i].query, n, golden_data[i].expected);
                sdb_object_deref(SDB_OBJ(m));
        }
@@ -605,7 +613,7 @@ core_store_lookup_suite(void)
        tcase_add_test(tc, test_store_cond);
        tcase_add_test(tc, test_store_match_op);
        tcase_add_test(tc, test_parse_cmp);
-       tcase_add_test(tc, test_lookup);
+       tcase_add_test(tc, test_scan);
        suite_add_tcase(s, tc);
 
        return s;