Code

t/: Use expression-based matchers for attribute comparison tests.
[sysdb.git] / t / unit / core / store_lookup_test.c
index c5aad2c51f3670cd6f469d7650de349d6504a56a..6edc453fcd6ced7218f2b72d43a6464a480a7911 100644 (file)
@@ -30,6 +30,8 @@
 #include "frontend/parser.h"
 #include "libsysdb_test.h"
 
+#include <assert.h>
+
 #include <check.h>
 #include <string.h>
 
@@ -271,9 +273,9 @@ START_TEST(test_store_match_attr)
 }
 END_TEST
 
-START_TEST(test_attr_cond)
+START_TEST(test_cmp_attr)
 {
-       sdb_store_obj_t *obj;
+       sdb_store_obj_t *host;
 
        struct {
                const char *attr;
@@ -299,64 +301,65 @@ START_TEST(test_attr_cond)
        int status;
        size_t i;
 
-       obj = sdb_store_get_host("a");
-       fail_unless(obj != NULL,
+       host = sdb_store_get_host("a");
+       fail_unless(host != NULL,
                        "sdb_store_get_host(a) = NULL; expected: <host>");
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
-               sdb_store_expr_t *expr;
-               sdb_store_cond_t *c;
-               char buf[1024];
+               sdb_store_expr_t *attr;
+               sdb_store_expr_t *value;
+               char value_str[1024];
                size_t j;
 
                struct {
-                       sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
+                       sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
+                                       sdb_store_expr_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 },
+                       { sdb_store_cmp_lt, golden_data[i].expected_lt },
+                       { sdb_store_cmp_le, golden_data[i].expected_le },
+                       { sdb_store_cmp_eq, golden_data[i].expected_eq },
+                       { sdb_store_cmp_ge, golden_data[i].expected_ge },
+                       { sdb_store_cmp_gt, golden_data[i].expected_gt },
                };
 
                sdb_data_format(&golden_data[i].value,
-                               buf, sizeof(buf), SDB_UNQUOTED);
+                               value_str, sizeof(value_str), SDB_UNQUOTED);
 
-               expr = sdb_store_expr_constvalue(&golden_data[i].value);
-               fail_unless(expr != NULL,
-                               "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
-                               buf);
+               attr = sdb_store_expr_attrvalue(golden_data[i].attr);
+               fail_unless(attr != NULL,
+                               "sdb_store_expr_attrvalue(%s) = NULL; expected: <expr>",
+                               golden_data[i].attr);
 
-               c = sdb_store_attr_cond(golden_data[i].attr, expr);
-               sdb_object_deref(SDB_OBJ(expr));
-               fail_unless(c != NULL,
-                               "sdb_store_attr_cond(%s, expr{%s}) = NULL; expected: <cond>",
-                               golden_data[i].attr, buf);
+               value = sdb_store_expr_constvalue(&golden_data[i].value);
+               fail_unless(value != NULL,
+                               "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
+                               value_str);
 
                for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
                        sdb_store_matcher_t *m;
 
-                       m = tests[j].matcher(c);
+                       m = tests[j].matcher(attr, value);
                        fail_unless(m != NULL,
                                        "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
 
-                       status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
+                       status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
                        fail_unless(status == tests[j].expected,
-                                       "sdb_store_matcher_matches(<m>, <obj>, NULL) = %d; "
+                                       "sdb_store_matcher_matches(<m>, <host>, NULL) = %d; "
                                        "expected: %d", status, tests[j].expected);
 
                        sdb_object_deref(SDB_OBJ(m));
                }
 
-               sdb_object_deref(SDB_OBJ(c));
+               sdb_object_deref(SDB_OBJ(attr));
+               sdb_object_deref(SDB_OBJ(value));
        }
 
-       sdb_object_deref(SDB_OBJ(obj));
+       sdb_object_deref(SDB_OBJ(host));
 }
 END_TEST
 
-START_TEST(test_obj_cond)
+START_TEST(test_cmp_obj)
 {
        struct {
                const char *host;
@@ -386,6 +389,8 @@ START_TEST(test_obj_cond)
                { "a", SDB_FIELD_INTERVAL,
                        { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
                /* type mismatch */
+               /* TODO: let matchers only use data_strcmp for attributes
+                *       everything else has a well-known type
                { "a", SDB_FIELD_LAST_UPDATE,
                        { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
                { "a", SDB_FIELD_AGE,
@@ -396,69 +401,82 @@ START_TEST(test_obj_cond)
                        { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
                { "a", SDB_FIELD_BACKEND,
                        { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
+               */
                /* (64bit) integer value without zero-bytes */
+               /*
                { "a", SDB_FIELD_BACKEND,
                        { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } },
                        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];
+               sdb_store_obj_t *host;
+               sdb_store_expr_t *field;
+               sdb_store_expr_t *value;
+               char value_str[1024];
                size_t j;
 
                struct {
-                       sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
+                       sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
+                                       sdb_store_expr_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 },
+                       { sdb_store_cmp_lt, golden_data[i].expected_lt },
+                       { sdb_store_cmp_le, golden_data[i].expected_le },
+                       { sdb_store_cmp_eq, golden_data[i].expected_eq },
+                       { sdb_store_cmp_ge, golden_data[i].expected_ge },
+                       { sdb_store_cmp_gt, golden_data[i].expected_gt },
                };
+               char *op_str[] = { "<", "<=", "=", ">=", ">" };
+
+               assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
 
-               obj = sdb_store_get_host(golden_data[i].host);
-               fail_unless(obj != NULL,
+               host = sdb_store_get_host(golden_data[i].host);
+               fail_unless(host != 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);
+                               value_str, sizeof(value_str), SDB_UNQUOTED);
 
-               expr = sdb_store_expr_constvalue(&golden_data[i].value);
-               fail_unless(expr != NULL,
-                               "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
-                               buf);
+               field = sdb_store_expr_fieldvalue(golden_data[i].field);
+               fail_unless(field != NULL,
+                               "sdb_store_expr_fieldvalue(%d) = NULL; "
+                               "expected: <expr>", golden_data[i].field);
 
-               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);
+               value = sdb_store_expr_constvalue(&golden_data[i].value);
+               fail_unless(value != NULL,
+                               "sdb_store_expr_constvalue(%s) = NULL; "
+                               "expected: <expr>", value_str);
 
                for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
+                       char m_str[1024];
                        sdb_store_matcher_t *m;
 
-                       m = tests[j].matcher(c);
+                       snprintf(m_str, sizeof(m_str), "%s %s %s",
+                                       SDB_FIELD_TO_NAME(golden_data[i].field),
+                                       op_str[j], value_str);
+
+                       m = tests[j].matcher(field, value);
                        fail_unless(m != NULL,
                                        "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
 
-                       status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
+                       status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
                        fail_unless(status == tests[j].expected,
-                                       "sdb_store_matcher_matches(<m>, <host '%s'>, NULL) = %d; "
-                                       "expected: %d", status, tests[j].expected);
+                                       "sdb_store_matcher_matches(<%s>, <host '%s'>, NULL) = %d; "
+                                       "expected: %d", m_str, golden_data[i].host, status,
+                                       tests[j].expected);
 
                        sdb_object_deref(SDB_OBJ(m));
                }
 
-               sdb_object_deref(SDB_OBJ(c));
-               sdb_object_deref(SDB_OBJ(obj));
+               sdb_object_deref(SDB_OBJ(field));
+               sdb_object_deref(SDB_OBJ(value));
+               sdb_object_deref(SDB_OBJ(host));
        }
 }
 END_TEST
@@ -658,96 +676,6 @@ 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[] = {
-               { "name",        "<",  &string,   MATCHER_LT },
-               { "name",        "<=", &string,   MATCHER_LE },
-               { "name",        "=",  &string,   MATCHER_EQ },
-               { "name",        ">=", &string,   MATCHER_GE },
-               { "name",        ">",  &string,   MATCHER_GT },
-               { "name",        "!=", &string,   MATCHER_NOT },
-               { "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)
 {
@@ -797,6 +725,11 @@ START_TEST(test_scan)
                        "NOT attribute['x'] = ''",         2 }, /* filter always matches */
                { "attribute = 'x'", NULL,             0 },
                { "attribute['k1'] = 'v1'", NULL,      1 },
+               { "attribute['k1'] =~ 'v'", NULL,      2 },
+               { "attribute['k1'] !~ 'v'", NULL,      1 },
+               { "attribute['x1'] =~ 'v'", NULL,      0 },
+               { "attribute['x1'] =~ 'NULL'", NULL,   0 },
+               { "attribute['x1'] !~ 'v'", NULL,      3 },
                { "attribute['k1'] IS NULL", NULL,     1 },
                { "attribute['x1'] IS NULL", NULL,     3 },
                { "attribute['k1'] IS NOT NULL", NULL, 2 },
@@ -861,11 +794,10 @@ 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_attr_cond);
-       tcase_add_test(tc, test_obj_cond);
+       tcase_add_test(tc, test_cmp_attr);
+       tcase_add_test(tc, test_cmp_obj);
        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);