1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include "../lib/client.h"
#include "../lib/heap.h"
#include "../lib/runner.h"
#include "../lib/server.h"
#include "../lib/sqlite.h"
/******************************************************************************
*
* Handle client requests
*
******************************************************************************/
SUITE(client);
struct fixture {
struct test_server server;
struct client_proto *client;
struct rows rows;
};
static void *setUp(const MunitParameter params[], void *user_data)
{
struct fixture *f = munit_malloc(sizeof *f);
(void)user_data;
f->rows = (struct rows){};
test_heap_setup(params, user_data);
test_sqlite_setup(params);
test_server_setup(&f->server, 1, params);
test_server_start(&f->server, params);
f->client = test_server_client(&f->server);
HANDSHAKE;
OPEN;
return f;
}
static void tearDown(void *data)
{
struct fixture *f = data;
test_server_tear_down(&f->server);
test_sqlite_tear_down();
test_heap_tear_down(data);
clientCloseRows(&f->rows);
free(f);
}
TEST(client, exec, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
uint32_t stmt_id;
uint64_t last_insert_id;
uint64_t rows_affected;
(void)params;
PREPARE("CREATE TABLE test (n INT)", &stmt_id);
EXEC(stmt_id, &last_insert_id, &rows_affected);
return MUNIT_OK;
}
TEST(client, execWithOneParam, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
uint32_t stmt_id;
uint64_t last_insert_id;
uint64_t rows_affected;
struct value param = { 0 };
int rv;
(void)params;
PREPARE("CREATE TABLE test (n INT)", &stmt_id);
EXEC(stmt_id, &last_insert_id, &rows_affected);
PREPARE("INSERT INTO test (n) VALUES(?)", &stmt_id);
param.type = SQLITE_INTEGER;
param.integer = 17;
rv = clientSendExec(f->client, stmt_id, ¶m, 1, NULL);
munit_assert_int(rv, ==, 0);
rv = clientRecvResult(f->client, &last_insert_id, &rows_affected, NULL);
munit_assert_int(rv, ==, 0);
return MUNIT_OK;
}
TEST(client, execSql, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
uint64_t last_insert_id;
uint64_t rows_affected;
(void)params;
EXEC_SQL("CREATE TABLE test (n INT)", &last_insert_id, &rows_affected);
return MUNIT_OK;
}
TEST(client, query, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
uint32_t stmt_id;
uint64_t last_insert_id;
uint64_t rows_affected;
unsigned i;
(void)params;
PREPARE("CREATE TABLE test (n INT)", &stmt_id);
EXEC(stmt_id, &last_insert_id, &rows_affected);
PREPARE("BEGIN", &stmt_id);
EXEC(stmt_id, &last_insert_id, &rows_affected);
PREPARE("INSERT INTO test (n) VALUES(123)", &stmt_id);
for (i = 0; i < 256; i++) {
EXEC(stmt_id, &last_insert_id, &rows_affected);
}
PREPARE("COMMIT", &stmt_id);
EXEC(stmt_id, &last_insert_id, &rows_affected);
PREPARE("SELECT n FROM test", &stmt_id);
QUERY_DONE(stmt_id, &f->rows, {});
return MUNIT_OK;
}
TEST(client, querySql, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
uint32_t stmt_id;
uint64_t last_insert_id;
uint64_t rows_affected;
unsigned i;
(void)params;
EXEC_SQL("CREATE TABLE test (n INT)", &last_insert_id, &rows_affected);
EXEC_SQL("BEGIN", &last_insert_id, &rows_affected);
PREPARE("INSERT INTO test (n) VALUES(123)", &stmt_id);
for (i = 0; i < 256; i++) {
EXEC(stmt_id, &last_insert_id, &rows_affected);
}
EXEC_SQL("COMMIT", &last_insert_id, &rows_affected);
QUERY_SQL_DONE("SELECT n FROM test", &f->rows, {});
return MUNIT_OK;
}
/* Stress test of an EXEC_SQL with many ';'-separated statements. */
TEST(client, semicolons, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
(void)params;
static const char create_sql[] = "CREATE TABLE IF NOT EXISTS test (n INT);";
static const char insert_sql[] = "INSERT INTO test (n) VALUES (17);";
size_t n = 10000;
size_t create_len = sizeof(create_sql) - 1;
size_t insert_len = sizeof(insert_sql) - 1;
size_t len = n * create_len + insert_len + 1;
char *sql = munit_malloc(len);
char *p = sql;
for (size_t i = 0; i < n; i++) {
memcpy(p, create_sql, create_len);
p += create_len;
}
memcpy(p, insert_sql, insert_len);
p += insert_len;
munit_assert_ptr(p, ==, sql + len - 1);
*p = '\0';
uint64_t last_insert_id;
uint64_t rows_affected;
EXEC_SQL(sql, &last_insert_id, &rows_affected);
free(sql);
/* Check that all the statements were executed. */
struct row *row;
QUERY_SQL("SELECT n FROM test", &f->rows);
munit_assert_uint(f->rows.column_count, ==, 1);
munit_assert_string_equal(f->rows.column_names[0], "n");
row = f->rows.next;
munit_assert_int(row->values[0].type, ==, SQLITE_INTEGER);
munit_assert_int64(row->values[0].integer, ==, 17);
munit_assert_ptr_null(row->next);
return MUNIT_OK;
}
|