libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 8ba23a504e76d16e936daac8d5f474cf5f5df6e8
parent f39c72bd43f0cfddd273208257cff01f6ac0b379
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 27 Jan 2017 07:57:53 +0100

Fix(Databases): Now supporting a not default filename

Diffstat:
Minc/database.h | 2+-
Msrc/database.c | 10++++++++--
Mtests/test_database.c | 30+++++++++++++++++++++++++++++-
3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/inc/database.h b/inc/database.h @@ -25,7 +25,7 @@ typedef struct s_database { sqlite3 *sql; } mdatabase_t; -mdatabase_t *mpm_database_open(u8_t *ret); +mdatabase_t *mpm_database_open(u8_t *ret, const char *fn); u8_t mpm_database_close(mdatabase_t *ptr); #endif /* DATABASE_H */ diff --git a/src/database.c b/src/database.c @@ -19,19 +19,25 @@ /*! * \brief Open a connection to a database * \param ret Return code, if any error + * \param fn Database path * \return A fresh mdatabase_t structure * * This function will create a new mdatabase_t struct, allocate it, and open * a new database connection. In case of any error, the return value will be * NULL, and the ret pointer set to an error code. + * If a value is passed to the param fn, the library will use this value as a + * database file path. */ -mdatabase_t *mpm_database_open(u8_t *ret) { +mdatabase_t *mpm_database_open(u8_t *ret, const char *fn) { mdatabase_t *ptr; u8_t error = 0; ptr = malloc(sizeof(mdatabase_t)); assert(ptr != NULL); - error = sqlite3_open(DB_FN, &ptr->sql); + if (fn != NULL) + error = sqlite3_open(fn, &ptr->sql); + else + error = sqlite3_open(DB_FN, &ptr->sql); if (error != 0) goto error; return ptr; diff --git a/tests/test_database.c b/tests/test_database.c @@ -4,7 +4,7 @@ TEST(database_open_1) { mdatabase_t *ptr = NULL; u8_t ret = 0; - ptr = mpm_database_open(&ret); + ptr = mpm_database_open(&ret, NULL); TEST_ASSERT(ret == 0, "Database can't be open"); TEST_ASSERT((ptr != NULL), "Database can't be open"); ret = mpm_database_close(ptr); @@ -12,6 +12,34 @@ TEST(database_open_1) { return TEST_SUCCESS; } +TEST(database_open_2) { + mdatabase_t *ptr = NULL; + u8_t ret = 0; + + ptr = mpm_database_open(&ret, "/illogical/path"); + TEST_ASSERT((ret == 14), "Database has be open ?!"); + TEST_ASSERT((ptr == NULL), "Database can't be open"); + return TEST_SUCCESS; +} + + +TEST(database_close_1) { + mdatabase_t *ptr = NULL; + u8_t ret = 0; + + /* Fake mallocs */ + ptr = malloc(sizeof(mdatabase_t)); + ptr->sql = malloc(10); + + ret = mpm_database_close(ptr); + TEST_ASSERT(ret != 0, "Error did not trigger"); + free(ptr->sql); + free(ptr); + return TEST_SUCCESS; +} + void register_test_database(void) { reg_test("database", database_open_1); + reg_test("database", database_open_2); + reg_test("database", database_close_1); }