Code

store_lookup: Added conditional for accessing store-object fields.
[sysdb.git] / t / unit / core / store_lookup_test.c
index 1adece554b031c6f94712b2a4977aa6ed2715e63..89ef451aba32dc285c3a6fd47c5ef61f7c13c782 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;
@@ -659,7 +757,8 @@ 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_scan);