test-dict-sql.c revision b5ab29780f74cf88212a547ebbe3b6bc0cb867c5
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen if (dict_init("mysql:" DICT_SRC_DIR "/dict.conf", &set, &dict, &error) < 0)
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic void test_set_expected(struct dict *_dict,
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen sql_driver_test_add_expected_result(dict->db, result);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenstatic void test_lookup_one(void)
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen .row_data = (const char **[]){(const char*[]){"one", NULL}},
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen .queries = (const char *[]){"SELECT value FROM table WHERE a = 'hello' AND b = 'world'", NULL},
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);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic void test_atomic_inc(void)
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'",
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 i_error("dict_transaction_commit failed: %s", error);
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen dict_atomic_inc(ctx, "priv/quota/bytes", 128);
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 0);
1c6dd898551d7d4d61970b24a8372438f6b72f97Timo Sirainen i_error("dict_transaction_commit failed: %s", error);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic void test_set(void)
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 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 i_error("dict_transaction_commit failed: %s", error);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 1);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde i_error("dict_transaction_commit failed: %s", error);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainenstatic void test_unset(void)
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'",
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 i_error("dict_transaction_commit failed: %s", error);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_assert(dict_transaction_commit(&ctx, &error) == 1);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen i_error("dict_transaction_commit failed: %s", error);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainenstatic void test_iterate(void)
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen const char *key = NULL, *value = NULL, *error;
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen .col_names = (const char *[]){"value", "name", NULL},
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen .row_data = (const char **[]){
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen .queries = (const char *[]){
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen "SELECT value,name FROM counters WHERE class = 'global' AND name = 'counter'",
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen dict_iterate_init(dict, "shared/counters/global/counter",
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen test_assert_idx(strcmp(key, "shared/counters/global/counter") == 0 &&
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen strcmp(value, rset.row_data[idx][0]) == 0, idx);
61767c7537efe3eb51f80d37cda12d69c67f3c05Timo Sirainen test_assert(dict_iterate_deinit(&iter, &error) == 0);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen i_error("dict_iterate_deinit failed: %s", error);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen "SELECT value,name FROM counters WHERE class = 'global' AND name LIKE '%' AND name NOT LIKE '%/%'",
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen iter = dict_iterate_init(dict, "shared/counters/global/", 0);
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 test_assert(dict_iterate_deinit(&iter, &error) == 0);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen i_error("dict_iterate_deinit failed: %s", error);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen static void (*const test_functions[])(void) = {