libmpm

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

commit f27d5341b5f03aca41043cf64a0a92566b53b84e
parent 3303b95273798d20dcef1887c87ca60b6bd9d7dd
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  3 Feb 2017 09:54:32 +0100

Add(Categories): Add SQL table init and tests for categories

Diffstat:
Minc/category.h | 14++++++++++++++
Minc/database.h | 2+-
Minc/files.h | 2+-
Minc/package.h | 4+++-
Msrc/category.c | 5+++++
Msrc/database.c | 9++++++++-
Mtests/test_database.c | 35+++++++++++++++++++++++++++++++++--
7 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/inc/category.h b/inc/category.h @@ -19,10 +19,24 @@ # include <morphux.h> +typedef struct s_category category_t; +# include <libmpm.h> + + +# define CAT_TABLE "categ" +# define CAT_COL_ID "id" +# define CAT_COL_NAME "name" +# define CAT_COL_PARENT "parent" +# define CAT_COL_PARENT_NAME "parent_name" + typedef struct s_category { + u64_t id; char *name; + char *parent_name; + package_t *parent; } category_t; + int mpm_category_free(void *tmp); void mpm_category_init(category_t *ptr); diff --git a/inc/database.h b/inc/database.h @@ -18,8 +18,8 @@ # define DATABASE_H # include <sqlite3.h> # include <morphux.h> -# include <package.h> # include <files.h> +# include <package.h> # define DB_FN "test.db" # define SQL_CALLBACK_PTR(name) int (*name)(void *, int, char**, char**) diff --git a/inc/files.h b/inc/files.h @@ -18,7 +18,6 @@ # define FILES_H # include <morphux.h> -# include <package.h> # define FILE_TABLE "files" # define FILE_COL_ID "id" @@ -50,4 +49,5 @@ typedef struct s_file { void mpm_file_init(file_t *ptr); int mpm_file_free(void *tmp); +# include <package.h> #endif /* FILES_H */ diff --git a/inc/package.h b/inc/package.h @@ -18,7 +18,6 @@ # define PACKAGE_H # include <morphux.h> -# include <category.h> # include <files.h> enum { @@ -27,6 +26,8 @@ enum { PACKAGE_STATE_ORPHAN }; +typedef struct s_category category_t; + # define PKG_TABLE "pkgs" # define PKG_COL_ID "id" # define PKG_COL_NAME "name" @@ -57,4 +58,5 @@ typedef struct s_package { int mpm_package_free(void *tmp); void mpm_package_init(package_t *ptr); +# include <category.h> #endif /* PACKAGE_H */ diff --git a/src/category.c b/src/category.c @@ -25,6 +25,8 @@ int mpm_category_free(void *tmp) { if (ptr) { free(ptr->name); + free(ptr->parent_name); + mpm_package_free(ptr->parent); } return 1; } @@ -36,5 +38,8 @@ int mpm_category_free(void *tmp) { void mpm_category_init(category_t *ptr) { if (ptr) { ptr->name = NULL; + ptr->parent_name = NULL; + ptr->parent = NULL; + ptr->id = 0; } } diff --git a/src/database.c b/src/database.c @@ -243,7 +243,14 @@ u8_t mpm_database_init(database_t *ptr) { FILE_COL_PARENT SQL_TYPE_INT SQL_TYPE_NOT_NULL "," \ FILE_COL_PARENT_NAME SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ FILE_COL_HASH SQL_TYPE_TEXT SQL_TYPE_NOT_NULL \ - ");" \ + ");", \ + /* Category table */ + SQL_CREATE_TABLE CAT_TABLE "(" \ + CAT_COL_ID SQL_TYPE_INT SQL_TYPE_PRIMARY_KEY "," \ + CAT_COL_NAME SQL_TYPE_TEXT SQL_TYPE_NOT_NULL "," \ + CAT_COL_PARENT SQL_TYPE_INT SQL_TYPE_NOT_NULL "," \ + CAT_COL_PARENT_NAME SQL_TYPE_TEXT SQL_TYPE_NOT_NULL \ + ");" }; char *err = NULL; u8_t ret = 0; diff --git a/tests/test_database.c b/tests/test_database.c @@ -161,6 +161,35 @@ TEST(database_init_test_files_table) { return TEST_SUCCESS; } +TEST(database_init_test_categ_table) { + database_t *ptr = NULL; + u8_t ret = 0; + mlist_t *res = NULL, *tmp, *tmp2, *tmp3, *test = NULL; + char *err = NULL; + sql_result_t *result; + + ptr = mpm_database_open(&ret, NULL); + TEST_ASSERT((ret == 0), "Can't open the database"); + TEST_ASSERT((ptr != NULL), "Can't open the database"); + ret = mpm_database_exec(ptr, "PRAGMA table_info([" CAT_TABLE "])", + &exec_callback, &res, &err); + TEST_ASSERT((ret == 0), "An error happened"); + TEST_ASSERT((err == NULL), "An error happened"); + mpm_database_close(ptr); + + list_for_each(res, tmp, tmp2) { + list_for_each(tmp2, tmp3, result) { + if (!strcmp(result->name, "name")) + list_add(test, result->value, strlen(result->value)); + } + } + TEST_ASSERT((list_size(test) == 4), "Number of columns is wrong"); + list_free(test, NULL); + free_sql_results(res); + return TEST_SUCCESS; +} + + TEST(database_init_3) { database_t *ptr = NULL; @@ -204,10 +233,11 @@ TEST(database_add_pkg_1) { assert(pkg != NULL && cat != NULL); mpm_package_init(pkg); - mpm_category_init(pkg->categ); + mpm_category_init(cat); pkg->name = strdup("test"); pkg->version = strdup("1.0"); - cat->name = strdup("test"); + cat->name = strdup("test2"); + cat->parent_name = strdup("test"); pkg->categ = cat; pkg->desc = strdup("This a test package."); pkg->state = PACKAGE_STATE_USER_INSTALLED; @@ -434,6 +464,7 @@ void register_test_database(void) { reg_test("database", database_init_2); reg_test("database", database_init_test_pkg_table); reg_test("database", database_init_test_files_table); + reg_test("database", database_init_test_categ_table); reg_test("database", database_init_3); reg_test("database", database_add_pkg_1); reg_test("database", database_add_pkg_2);