Debian Codegraph @

test/integration/test_client.c

raw
1 #include "../lib/client.h"
2 #include "../lib/heap.h"
3 #include "../lib/runner.h"
4 #include "../lib/server.h"
5 #include "../lib/sqlite.h"
6
7 #ifndef FLAKY
8
9 /******************************************************************************
10 *
11 * Handle client requests
12 *
13 ******************************************************************************/
14
15 SUITE(client);
16
17 struct fixture
18 {
19 struct test_server server;
20 struct client_proto *client;
21 };
22
23 static void *setUp(const MunitParameter params[], void *user_data)
24 {
25 struct fixture *f = munit_malloc(sizeof *f);
26 (void)user_data;
27 test_heap_setup(params, user_data);
28 test_sqlite_setup(params);
29 test_server_setup(&f->server, 1, params);
30 test_server_start(&f->server, params);
31 f->client = test_server_client(&f->server);
32 HANDSHAKE;
33 OPEN;
34 return f;
35 }
36
37 static void tearDown(void *data)
38 {
39 struct fixture *f = data;
40 test_server_tear_down(&f->server);
41 test_sqlite_tear_down();
42 test_heap_tear_down(data);
43
44 free(f);
45 }
46
47 TEST(client, exec, setUp, tearDown, 0, NULL)
48 {
49 struct fixture *f = data;
50 uint32_t stmt_id;
51 uint64_t last_insert_id;
52 uint64_t rows_affected;
53 (void)params;
54 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
55 EXEC(stmt_id, &last_insert_id, &rows_affected);
56 return MUNIT_OK;
57 }
58
59 TEST(client, execWithOneParam, setUp, tearDown, 0, NULL)
60 {
61 struct fixture *f = data;
62 uint32_t stmt_id;
63 uint64_t last_insert_id;
64 uint64_t rows_affected;
65 struct value param = {0};
66 int rv;
67 (void)params;
68 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
69 EXEC(stmt_id, &last_insert_id, &rows_affected);
70 PREPARE("INSERT INTO test (n) VALUES(?)", &stmt_id);
71 param.type = SQLITE_INTEGER;
72 param.integer = 17;
73 rv = clientSendExec(f->client, stmt_id, &param, 1, NULL);
74 munit_assert_int(rv, ==, 0);
75 rv = clientRecvResult(f->client, &last_insert_id, &rows_affected, NULL);
76 munit_assert_int(rv, ==, 0);
77 return MUNIT_OK;
78 }
79
80 TEST(client, execSql, setUp, tearDown, 0, NULL)
81 {
82 struct fixture *f = data;
83 uint64_t last_insert_id;
84 uint64_t rows_affected;
85 (void)params;
86 EXEC_SQL("CREATE TABLE test (n INT)", &last_insert_id, &rows_affected);
87 return MUNIT_OK;
88 }
89
90 TEST(client, query, setUp, tearDown, 0, NULL)
91 {
92 struct fixture *f = data;
93 uint32_t stmt_id;
94 uint64_t last_insert_id;
95 uint64_t rows_affected;
96 unsigned i;
97 struct rows rows;
98 (void)params;
99 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
100 EXEC(stmt_id, &last_insert_id, &rows_affected);
101
102 PREPARE("BEGIN", &stmt_id);
103 EXEC(stmt_id, &last_insert_id, &rows_affected);
104
105 PREPARE("INSERT INTO test (n) VALUES(123)", &stmt_id);
106 for (i = 0; i < 256; i++) {
107 EXEC(stmt_id, &last_insert_id, &rows_affected);
108 }
109
110 PREPARE("COMMIT", &stmt_id);
111 EXEC(stmt_id, &last_insert_id, &rows_affected);
112
113 PREPARE("SELECT n FROM test", &stmt_id);
114 QUERY(stmt_id, &rows);
115
116 clientCloseRows(&rows);
117
118 return MUNIT_OK;
119 }
120
121 TEST(client, querySql, setUp, tearDown, 0, NULL)
122 {
123 struct fixture *f = data;
124 uint32_t stmt_id;
125 uint64_t last_insert_id;
126 uint64_t rows_affected;
127 unsigned i;
128 struct rows rows;
129 (void)params;
130 EXEC_SQL("CREATE TABLE test (n INT)", &last_insert_id, &rows_affected);
131
132 EXEC_SQL("BEGIN", &last_insert_id, &rows_affected);
133
134 PREPARE("INSERT INTO test (n) VALUES(123)", &stmt_id);
135 for (i = 0; i < 256; i++) {
136 EXEC(stmt_id, &last_insert_id, &rows_affected);
137 }
138
139 EXEC_SQL("COMMIT", &last_insert_id, &rows_affected);
140
141 QUERY_SQL("SELECT n FROM test", &rows);
142
143 clientCloseRows(&rows);
144
145 return MUNIT_OK;
146 }
147
148 #endif /* not FLAKY */