Skip to content

Commit d091c2c

Browse files
committed
refactor: improve error handling and add in memory peers test
1 parent 8d3f202 commit d091c2c

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

test/main.c

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
#include <stdlib.h>
1111
#include "sqlite3.h"
1212

13-
#define PEERS 50
13+
#define PEERS 50
1414
#define DB_PATH "health-track.sqlite"
15-
#define EXT_PATH "./dist/cloudsync"
16-
#define ABORT_TEST abort_test: if (rc != SQLITE_OK) printf("Error: %s\n", sqlite3_errmsg(db)); if (db) sqlite3_close(db); return rc;
15+
#define EXT_PATH "./dist/cloudsync"
16+
#define RCHECK if (rc != SQLITE_OK) goto abort_test;
17+
#define ERROR_MSG if (rc != SQLITE_OK) printf("Error: %s\n", sqlite3_errmsg(db));
18+
#define ABORT_TEST abort_test: ERROR_MSG if (db) sqlite3_close(db); return rc;
1719

1820
typedef enum { PRINT, NOPRINT, INT, GT0 } expected_type;
1921

@@ -113,14 +115,14 @@ int db_expect_gt0 (sqlite3 *db, const char *sql) {
113115
int open_load_ext(const char *db_path, sqlite3 **out_db) {
114116
sqlite3 *db = NULL;
115117
int rc = sqlite3_open(db_path, &db);
116-
if (rc != SQLITE_OK) goto abort_test;
118+
RCHECK
117119

118120
// enable load extension
119121
rc = sqlite3_enable_load_extension(db, 1);
120-
if (rc != SQLITE_OK) goto abort_test;
122+
RCHECK
121123

122124
rc = db_exec(db, "SELECT load_extension('"EXT_PATH"');");
123-
if (rc != SQLITE_OK) goto abort_test;
125+
RCHECK
124126

125127
*out_db = db;
126128
return rc;
@@ -130,12 +132,9 @@ ABORT_TEST
130132

131133
// MARK: -
132134

133-
int db_init (const char *db_path){
134-
sqlite3 *db = NULL;
135-
int rc = sqlite3_open(db_path, &db);
136-
if (rc != SQLITE_OK) goto abort_test;
135+
int db_init (sqlite3 *db){
137136

138-
rc = db_exec(db, "\
137+
int rc = db_exec(db, "\
139138
CREATE TABLE IF NOT EXISTS users (\
140139
id TEXT PRIMARY KEY NOT NULL,\
141140
name TEXT UNIQUE NOT NULL DEFAULT ''\
@@ -157,37 +156,40 @@ int db_init (const char *db_path){
157156
max_time TEXT\
158157
);\
159158
");
160-
if (rc != SQLITE_OK) goto abort_test;
161159

162-
ABORT_TEST
160+
ERROR_MSG
161+
return SQLITE_OK;
162+
163163
}
164164

165165
int test_init (const char *db_path, int init) {
166166
int rc = SQLITE_OK;
167167

168+
sqlite3 *db = NULL;
169+
rc = open_load_ext(db_path, &db); RCHECK
170+
168171
if(init){
169-
rc = db_init(db_path);
172+
rc = db_init(db);
173+
RCHECK
170174
}
171175

172-
sqlite3 *db = NULL;
173-
rc = open_load_ext(db_path, &db);
174-
175-
rc = db_exec(db, "SELECT cloudsync_init('users');"); if (rc != SQLITE_OK) goto abort_test;
176-
rc = db_exec(db, "SELECT cloudsync_init('activities');"); if (rc != SQLITE_OK) goto abort_test;
177-
rc = db_exec(db, "SELECT cloudsync_init('workouts');"); if (rc != SQLITE_OK) goto abort_test;
176+
rc = db_exec(db, "SELECT cloudsync_init('users');"); RCHECK
177+
rc = db_exec(db, "SELECT cloudsync_init('activities');"); RCHECK
178+
rc = db_exec(db, "SELECT cloudsync_init('workouts');"); RCHECK
178179

179180
// init network with connection string + apikey
180181
char network_init[512];
181182
snprintf(network_init, sizeof(network_init), "SELECT cloudsync_network_init('%s?apikey=%s');", getenv("CONNECTION_STRING"), getenv("APIKEY"));
182-
rc = db_exec(db, network_init); if (rc != SQLITE_OK) goto abort_test;
183-
184-
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM users;", 0); if (rc != SQLITE_OK) goto abort_test;
185-
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM activities;", 0); if (rc != SQLITE_OK) goto abort_test;
186-
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM workouts;", 0); if (rc != SQLITE_OK) goto abort_test;
187-
rc = db_expect_gt0(db, "SELECT cloudsync_network_sync();"); if (rc != SQLITE_OK) goto abort_test;
188-
rc = db_expect_gt0(db, "SELECT COUNT(*) as count FROM users;"); if (rc != SQLITE_OK) goto abort_test;
189-
rc = db_expect_gt0(db, "SELECT COUNT(*) as count FROM activities;"); if (rc != SQLITE_OK) goto abort_test;
190-
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM workouts;", 0); if (rc != SQLITE_OK) goto abort_test;
183+
rc = db_exec(db, network_init); RCHECK
184+
185+
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM users;", 0); RCHECK
186+
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM activities;", 0); RCHECK
187+
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM workouts;", 0); RCHECK
188+
//rc = db_exec(db, "INSERT INTO users (id, name) VALUES ('test', 'test')"); RCHECK
189+
rc = db_expect_gt0(db, "SELECT cloudsync_network_sync();"); RCHECK
190+
rc = db_expect_gt0(db, "SELECT COUNT(*) as count FROM users;"); RCHECK
191+
rc = db_expect_gt0(db, "SELECT COUNT(*) as count FROM activities;"); RCHECK
192+
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM workouts;", 0); RCHECK
191193
rc = db_exec(db, "SELECT cloudsync_terminate();");
192194

193195
ABORT_TEST
@@ -197,8 +199,8 @@ int test_is_enabled(const char *db_path) {
197199
sqlite3 *db = NULL;
198200
int rc = open_load_ext(db_path, &db);
199201

200-
rc = db_expect_int(db, "SELECT cloudsync_is_enabled('users');", 1); if (rc != SQLITE_OK) goto abort_test;
201-
rc = db_expect_int(db, "SELECT cloudsync_is_enabled('activities');", 1); if (rc != SQLITE_OK) goto abort_test;
202+
rc = db_expect_int(db, "SELECT cloudsync_is_enabled('users');", 1); RCHECK
203+
rc = db_expect_int(db, "SELECT cloudsync_is_enabled('activities');", 1); RCHECK
202204
rc = db_expect_int(db, "SELECT cloudsync_is_enabled('workouts');", 1);
203205

204206
ABORT_TEST
@@ -208,7 +210,7 @@ int test_db_version(const char *db_path) {
208210
sqlite3 *db = NULL;
209211
int rc = open_load_ext(db_path, &db);
210212

211-
rc = db_expect_gt0(db, "SELECT cloudsync_db_version();"); if (rc != SQLITE_OK) goto abort_test;
213+
rc = db_expect_gt0(db, "SELECT cloudsync_db_version();"); RCHECK
212214
rc = db_expect_gt0(db, "SELECT cloudsync_db_version_next();");
213215

214216
ABORT_TEST
@@ -218,17 +220,17 @@ int test_enable_disable(const char *db_path) {
218220
sqlite3 *db = NULL;
219221
int rc = open_load_ext(db_path, &db);
220222

221-
rc = db_exec(db, "SELECT cloudsync_init('*');"); if (rc != SQLITE_OK) goto abort_test;
222-
rc = db_exec(db, "SELECT cloudsync_disable('users');"); if (rc != SQLITE_OK) goto abort_test;
223-
rc = db_exec(db, "INSERT INTO users (id, name) VALUES ('12afb', 'provaCmeaakbefa');"); if (rc != SQLITE_OK) goto abort_test;
224-
rc = db_exec(db, "SELECT cloudsync_enable('users');"); if (rc != SQLITE_OK) goto abort_test;
223+
rc = db_exec(db, "SELECT cloudsync_init('*');"); RCHECK
224+
rc = db_exec(db, "SELECT cloudsync_disable('users');"); RCHECK
225+
rc = db_exec(db, "INSERT INTO users (id, name) VALUES ('12afb', 'provaCmeaakbefa');"); RCHECK
226+
rc = db_exec(db, "SELECT cloudsync_enable('users');"); RCHECK
225227

226228
// init network with connection string + apikey
227229
char network_init[512];
228230
snprintf(network_init, sizeof(network_init), "SELECT cloudsync_network_init('%s?apikey=%s');", getenv("CONNECTION_STRING"), getenv("APIKEY"));
229-
rc = db_exec(db, network_init); if (rc != SQLITE_OK) goto abort_test;
231+
rc = db_exec(db, network_init); RCHECK
230232

231-
rc = db_exec(db, "SELECT cloudsync_network_sync();"); if (rc != SQLITE_OK) goto abort_test;
233+
rc = db_exec(db, "SELECT cloudsync_network_sync();"); RCHECK
232234
rc = db_exec(db, "SELECT cloudsync_cleanup('*');");
233235

234236
ABORT_TEST
@@ -239,7 +241,7 @@ int version(){
239241
int rc = open_load_ext(":memory:", &db);
240242

241243
rc = db_print(db, "SELECT cloudsync_version();");
242-
if (rc != SQLITE_OK) goto abort_test;
244+
RCHECK
243245

244246
ABORT_TEST
245247
}
@@ -259,20 +261,23 @@ int main (void) {
259261
printf("===========================================\n");
260262
test_report("Version Test:", rc);
261263

262-
rc += db_init(DB_PATH);
264+
sqlite3 *db = NULL;
265+
rc += open_load_ext(DB_PATH, &db);
266+
rc += db_init(db);
267+
263268
rc += test_report("Init+Sync Test:", test_init(DB_PATH, 0));
264269
rc += test_report("Is Enabled Test:", test_is_enabled(DB_PATH));
265270
rc += test_report("DB Version Test:", test_db_version(DB_PATH));
266271
rc += test_report("Enable Disable Test:", test_enable_disable(DB_PATH));
267272

273+
remove(DB_PATH); // remove the database file
274+
268275
for(int i=0; i<PEERS; i++){
269-
remove(DB_PATH); // clean previous db
270276
char description[32];
271277
snprintf(description, sizeof(description), "%d/%d Peer Test", i+1, PEERS);
272-
rc += test_report(description, test_init(DB_PATH, 1));
278+
rc += test_report(description, test_init(":memory:", 1));
273279
}
274280

275-
remove(DB_PATH); // clean up the database file
276281
printf("\n");
277282
return rc;
278283
}

0 commit comments

Comments
 (0)