test-dict-sql.c revision b5ab29780f74cf88212a547ebbe3b6bc0cb867c5
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde#include "lib.h"
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen#include "test-lib.h"
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde#include "sql-api.h"
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde#include "dict.h"
9bb91f1dbf7cf8cfbd2df7784101df98d59fb46dTimo Sirainen#include "dict-private.h"
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde#include "dict-sql.h"
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde#include "dict-sql-private.h"
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde#include "driver-test.h"
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
a43145989f87ec68754e21234e7b6d892c4a4421Timo Sirainenstatic void test_setup(struct dict **dict_r)
d99107ddf4d9bccb710994482daf65276a9d6321Timo Sirainen{
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde const char *error = NULL;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde struct dict_settings set = {
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde .username = "testuser",
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde .base_dir = "."
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde };
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen struct dict *dict = NULL;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen if (dict_init("mysql:" DICT_SRC_DIR "/dict.conf", &set, &dict, &error) < 0)
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen i_fatal("cannot initialize dict: %s", error);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde *dict_r = dict;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen}
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenstatic void test_teardown(struct dict **_dict)
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen{
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen struct dict *dict = *_dict;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen *_dict = NULL;
c5a6a6565be93224fc26522eda855b0990f256e8Timo Sirainen if (dict != NULL) {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen dict_deinit(&dict);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde }
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen}
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic void test_set_expected(struct dict *_dict,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde const struct test_driver_result *result)
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen{
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen struct sql_dict *dict =
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde (struct sql_dict *)_dict;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen sql_driver_test_add_expected_result(dict->db, result);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen}
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenstatic void test_lookup_one(void)
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde{
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen const char *value = NULL, *error = NULL;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde struct test_driver_result_set rset = {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .rows = 1,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde .cols = 1,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde .col_names = (const char *[]){"value", NULL},
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen .row_data = (const char **[]){(const char*[]){"one", NULL}},
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen };
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen struct test_driver_result res = {
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen .nqueries = 1,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen .queries = (const char *[]){"SELECT value FROM table WHERE a = 'hello' AND b = 'world'", NULL},
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen .result = &rset,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen };
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen struct dict *dict;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen pool_t pool = pool_datastack_create();
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen test_begin("dict lookup one");
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen test_setup(&dict);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen test_set_expected(dict, &res);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen test_assert(dict_lookup(dict, pool, "shared/dictmap/hello/world", &value, &error) == 1);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen test_assert(value != NULL && strcmp(value, "one") == 0);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen if (error != NULL)
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen i_error("dict_lookup failed: %s", error);
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen test_teardown(&dict);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_end();
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen}
c5a6a6565be93224fc26522eda855b0990f256e8Timo Sirainen
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic void test_atomic_inc(void)
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen{
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde const char *error;
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen struct test_driver_result res = {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .nqueries = 2,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .queries = (const char *[]){
c5a6a6565be93224fc26522eda855b0990f256e8Timo Sirainen "UPDATE counters SET value=value+128 WHERE class = 'global' AND name = 'counter'",
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen "UPDATE quota SET bytes=bytes+128,count=count+1 WHERE username = 'testuser'",
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde NULL},
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde .result = NULL,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde };
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen struct dict *dict;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_begin("dict atomic inc");
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_setup(&dict);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_set_expected(dict, &res);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen struct dict_transaction_context *ctx = dict_transaction_begin(dict);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen dict_atomic_inc(ctx, "shared/counters/global/counter", 128);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_assert(dict_transaction_commit(&ctx, &error) == 0);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen if (error != NULL)
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen i_error("dict_transaction_commit failed: %s", error);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde error = NULL;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde ctx = dict_transaction_begin(dict);
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen dict_atomic_inc(ctx, "priv/quota/bytes", 128);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde dict_atomic_inc(ctx, "priv/quota/count", 1);
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 0);
1c6dd898551d7d4d61970b24a8372438f6b72f97Timo Sirainen if (error != NULL)
1c6dd898551d7d4d61970b24a8372438f6b72f97Timo Sirainen i_error("dict_transaction_commit failed: %s", error);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_teardown(&dict);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_end();
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde}
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic void test_set(void)
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen{
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen const char *error;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen struct test_driver_result res = {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .affected_rows = 1,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .nqueries = 2,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .queries = (const char *[]){
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen "INSERT INTO counters (value,class,name) VALUES (128,'global','counter') ON DUPLICATE KEY UPDATE value=128",
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen "INSERT INTO quota (bytes,count,username) VALUES (128,1,'testuser') ON DUPLICATE KEY UPDATE bytes=128,count=1",
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen NULL},
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen .result = NULL,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen };
3c296d819c54e21ce05c3d2eeeedc79be42ac593Timo Sirainen struct dict *dict;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_begin("dict set");
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_setup(&dict);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_set_expected(dict, &res);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen struct dict_transaction_context *ctx = dict_transaction_begin(dict);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen dict_set(ctx, "shared/counters/global/counter", "128");
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 1);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen if (error != NULL)
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen i_error("dict_transaction_commit failed: %s", error);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen error = NULL;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen ctx = dict_transaction_begin(dict);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen dict_set(ctx, "priv/quota/bytes", "128");
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen dict_set(ctx, "priv/quota/count", "1");
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 1);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde if (error != NULL)
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde i_error("dict_transaction_commit failed: %s", error);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_teardown(&dict);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_end();
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen}
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainenstatic void test_unset(void)
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen{
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen const char *error;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen struct test_driver_result res = {
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen .affected_rows = 1,
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen .nqueries = 3,
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen .queries = (const char *[]){
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen "DELETE FROM counters WHERE class = 'global' AND name = 'counter'",
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen "DELETE FROM quota WHERE username = 'testuser'",
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen "DELETE FROM quota WHERE username = 'testuser'",
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen NULL},
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen .result = NULL,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen };
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen struct dict *dict;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen test_begin("dict unset");
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen test_setup(&dict);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_set_expected(dict, &res);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen struct dict_transaction_context *ctx = dict_transaction_begin(dict);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen dict_unset(ctx, "shared/counters/global/counter");
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 1);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen if (error != NULL)
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen i_error("dict_transaction_commit failed: %s", error);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen error = NULL;
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen ctx = dict_transaction_begin(dict);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen dict_unset(ctx, "priv/quota/bytes");
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen dict_unset(ctx, "priv/quota/count");
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 1);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen if (error != NULL)
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen i_error("dict_transaction_commit failed: %s", error);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_teardown(&dict);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_end();
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen}
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainenstatic void test_iterate(void)
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen{
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen const char *key = NULL, *value = NULL, *error;
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen struct test_driver_result_set rset = {
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen .rows = 5,
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen .cols = 2,
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen .col_names = (const char *[]){"value", "name", NULL},
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen .row_data = (const char **[]){
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen (const char*[]){"one", "counter", NULL},
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen (const char*[]){"two", "counter", NULL},
31a574fda352ef4f71dbff9c30e15e4744e132c0Timo Sirainen (const char*[]){"three", "counter", NULL},
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen (const char*[]){"four", "counter", NULL},
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen (const char*[]){"five", "counter", NULL},
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen },
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen };
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen struct test_driver_result res = {
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen .nqueries = 1,
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen .queries = (const char *[]){
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen "SELECT value,name FROM counters WHERE class = 'global' AND name = 'counter'",
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen NULL},
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen .result = &rset,
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen };
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen struct dict *dict;
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_begin("dict iterate");
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_setup(&dict);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_set_expected(dict, &res);
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen
2ba63f475f74b2aa87f9fd9e28a6c5738deb0878Timo Sirainen struct dict_iterate_context *iter =
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen dict_iterate_init(dict, "shared/counters/global/counter",
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen DICT_ITERATE_FLAG_EXACT_KEY);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen size_t idx = 0;
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen while(dict_iterate(iter, &key, &value)) {
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen i_assert(idx < rset.rows);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_assert_idx(strcmp(key, "shared/counters/global/counter") == 0 &&
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen strcmp(value, rset.row_data[idx][0]) == 0, idx);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen idx++;
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen }
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen test_assert(idx == rset.rows);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_assert(dict_iterate_deinit(&iter, &error) == 0);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen if (error != NULL)
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen i_error("dict_iterate_deinit failed: %s", error);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen error = NULL;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen res.queries = (const char*[]){
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen "SELECT value,name FROM counters WHERE class = 'global' AND name LIKE '%' AND name NOT LIKE '%/%'",
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen NULL
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen };
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen res.cur = 0;
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen res.result->cur = 0;
3c296d819c54e21ce05c3d2eeeedc79be42ac593Timo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen test_set_expected(dict, &res);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen iter = dict_iterate_init(dict, "shared/counters/global/", 0);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen idx = 0;
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen while(dict_iterate(iter, &key, &value)) {
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen i_assert(idx < rset.rows);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen test_assert_idx(strcmp(key, "shared/counters/global/counter") == 0 &&
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen strcmp(value, rset.row_data[idx][0]) == 0, idx);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen idx++;
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen }
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen test_assert(idx == rset.rows);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen test_assert(dict_iterate_deinit(&iter, &error) == 0);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen if (error != NULL)
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen i_error("dict_iterate_deinit failed: %s", error);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen test_teardown(&dict);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen test_end();
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen}
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainenint main(void) {
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen sql_drivers_init();
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen sql_driver_test_register();
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde dict_sql_register();
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen static void (*const test_functions[])(void) = {
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_lookup_one,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_atomic_inc,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_set,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_unset,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde test_iterate,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde NULL
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen };
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen int ret = test_run(test_functions);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde dict_sql_unregister();
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde sql_driver_test_unregister();
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen sql_drivers_deinit();
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen return ret;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde}
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde