Revision b9ed1747
Von admin vor mehr als 5 Jahren hinzugefügt
test/main.c | ||
---|---|---|
/****************************************************************************
|
||
* This file is part of the project AqDatabase.
|
||
* AqDatabase (c) by 2020 Martin Preuss, all rights reserved.
|
||
*
|
||
* The license for this file can be found in the file COPYING which you
|
||
* should have received along with this file.
|
||
****************************************************************************/
|
||
|
||
|
||
#ifdef HAVE_CONFIG_H
|
||
# include <config.h>
|
||
#endif
|
||
|
||
|
||
#undef BUILDING_AQDATABASE
|
||
|
||
... | ... | |
|
||
#include <gwenhywfar/gwenhywfar.h>
|
||
#include <gwenhywfar/cgui.h>
|
||
#include <gwenhywfar/args.h>
|
||
#include <gwenhywfar/debug.h>
|
||
|
||
#include <unistd.h>
|
||
|
||
|
||
#include "createdb.h"
|
||
|
||
#include "test1.h"
|
||
#include "test2.h"
|
||
#include "test3.h"
|
||
... | ... | |
#include "test7.h"
|
||
|
||
|
||
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
|
||
#define I18S(msg) msg
|
||
|
||
|
||
static int _readArgs(int argc, char **argv, GWEN_DB_NODE *dbArgs);
|
||
static int _setUrl(GWEN_TEST_MODULE *modRoot, const char *sUrl);
|
||
|
||
|
||
|
||
|
||
|
||
|
||
int main(int argc, char **argv) {
|
||
int rv;
|
||
GWEN_GUI *gui;
|
||
GWEN_TEST_FRAMEWORK *tf;
|
||
GWEN_TEST_MODULE *modRoot;
|
||
GWEN_DB_NODE *dbArgs;
|
||
const char *s;
|
||
const char *cmd;
|
||
|
||
rv=GWEN_Init();
|
||
if (rv) {
|
||
DBG_ERROR_ERR(0, rv);
|
||
return rv;
|
||
return 2;
|
||
}
|
||
|
||
rv=AQDB_Init();
|
||
if (rv) {
|
||
DBG_ERROR_ERR(0, rv);
|
||
return rv;
|
||
return 2;
|
||
}
|
||
|
||
gui=GWEN_Gui_CGui_new();
|
||
... | ... | |
GWEN_Gui_SetGui(gui);
|
||
GWEN_Logger_SetLevel(AQDB_LOGDOMAIN, GWEN_LoggerLevel_Info);
|
||
|
||
tf=TestFramework_new();
|
||
modRoot=TestFramework_GetModulesRoot(tf);
|
||
GWEN_Logger_Open(0, "aqdbtest", 0,
|
||
GWEN_LoggerType_Console,
|
||
GWEN_LoggerFacility_User);
|
||
//GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);
|
||
GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Debug);
|
||
|
||
GWEN_Test_Module_SetCharParam(modRoot, "url", "file:///tmp/aqdatabase-db1.db");
|
||
GWEN_Test_Module_SetCharParam(modRoot, "fname", "/tmp/aqdatabase-db1.db");
|
||
dbArgs=GWEN_DB_Group_new("arguments");
|
||
rv=_readArgs(argc, argv, dbArgs);
|
||
if (rv<0) {
|
||
DBG_ERROR(0, "here (%d)", rv);
|
||
return 1;
|
||
}
|
||
if (rv>0) {
|
||
argc-=rv-1;
|
||
argv+=rv-1;
|
||
}
|
||
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_objects", test_create_objects, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_multi", test_create_multi, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_rollback", test_rollback, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_rollback_and_add", test_rollback_and_add, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_remove_objects", test_remove_objects, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_modify", test_create_modify, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_modify2", test_create_modify2, NULL);
|
||
cmd=GWEN_DB_GetCharValue(dbArgs, "params", 0, NULL);
|
||
if (cmd && *cmd) {
|
||
if (strcasecmp(cmd, "test")==0) {
|
||
GWEN_TEST_FRAMEWORK *tf;
|
||
|
||
/* do the tests */
|
||
unlink("/tmp/aqdatabase-db1.db");
|
||
rv=TestFramework_Run(tf, argc, argv);
|
||
if (rv) {
|
||
fprintf(stderr, "SomeError in tests failed.\n");
|
||
tf=TestFramework_new();
|
||
modRoot=TestFramework_GetModulesRoot(tf);
|
||
|
||
s=GWEN_DB_GetCharValue(dbArgs, "dbUrl", 0, "file:///tmp/aqdatabase-db1.db");
|
||
rv=_setUrl(modRoot, s);
|
||
if (rv<0) {
|
||
DBG_ERROR_ERR(0, rv);
|
||
return 1;
|
||
}
|
||
|
||
destroy_database(s,
|
||
GWEN_Test_Module_GetCharParam(modRoot, "dirname", NULL),
|
||
GWEN_Test_Module_GetCharParam(modRoot, "fname", NULL));
|
||
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_objects", test_create_objects, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_multi", test_create_multi, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_rollback", test_rollback, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_rollback_and_add", test_rollback_and_add, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_remove_objects", test_remove_objects, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_modify", test_create_modify, NULL);
|
||
GWEN_Test_Module_AddTest(modRoot, "test_create_modify2", test_create_modify2, NULL);
|
||
|
||
/* do the tests */
|
||
rv=TestFramework_Run(tf, argc, argv);
|
||
if (rv) {
|
||
fprintf(stderr, "SomeError in tests failed.\n");
|
||
GWEN_Gui_SetGui(NULL);
|
||
GWEN_Gui_free(gui);
|
||
return 2;
|
||
}
|
||
TestFramework_free(tf);
|
||
}
|
||
else {
|
||
DBG_ERROR(0, "Unknown command \"%s\"", cmd);
|
||
GWEN_Gui_SetGui(NULL);
|
||
GWEN_Gui_free(gui);
|
||
return 1;
|
||
}
|
||
}
|
||
else {
|
||
DBG_ERROR(0, "No command");
|
||
GWEN_DB_Dump(dbArgs, 2);
|
||
GWEN_Gui_SetGui(NULL);
|
||
GWEN_Gui_free(gui);
|
||
return 2;
|
||
return 1;
|
||
}
|
||
TestFramework_free(tf);
|
||
|
||
/* done */
|
||
rv=AQDB_Fini();
|
||
... | ... | |
|
||
|
||
|
||
int _readArgs(int argc, char **argv, GWEN_DB_NODE *dbArgs)
|
||
{
|
||
int rv;
|
||
const GWEN_ARGS args[]={
|
||
{
|
||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||
GWEN_ArgsType_Char, /* type */
|
||
"dburl", /* name */
|
||
0, /* minnum */
|
||
1, /* maxnum */
|
||
"D", /* short option */
|
||
"dburl", /* long option */
|
||
"database url", /* short description */
|
||
"database url" /* long description */
|
||
},
|
||
{
|
||
GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
|
||
GWEN_ArgsType_Int, /* type */
|
||
"help", /* name */
|
||
0, /* minnum */
|
||
0, /* maxnum */
|
||
"h", /* short option */
|
||
"help",
|
||
I18S("Show this help screen. For help on commands, "
|
||
"run aqdbtool <COMMAND> --help."),
|
||
I18S("Show this help screen. For help on commands, run aqdbtool <COMMAND> --help.")
|
||
}
|
||
};
|
||
|
||
rv=GWEN_Args_Check(argc, argv, 1,
|
||
GWEN_ARGS_MODE_ALLOW_FREEPARAM |
|
||
GWEN_ARGS_MODE_STOP_AT_FREEPARAM,
|
||
args,
|
||
dbArgs);
|
||
if (rv==GWEN_ARGS_RESULT_ERROR) {
|
||
DBG_ERROR(0, "ERROR: Could not parse arguments main");
|
||
return GWEN_ERROR_GENERIC;
|
||
}
|
||
else if (rv==GWEN_ARGS_RESULT_HELP) {
|
||
GWEN_BUFFER *ubuf;
|
||
|
||
ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
|
||
GWEN_Buffer_AppendString(ubuf, I18N("This is version "));
|
||
GWEN_Buffer_AppendString(ubuf, AQDB_VERSION_STRING "\n");
|
||
GWEN_Buffer_AppendString(ubuf,
|
||
I18N("Usage: "));
|
||
GWEN_Buffer_AppendString(ubuf, argv[0]);
|
||
GWEN_Buffer_AppendString(ubuf,
|
||
I18N(" [OPTIONS]\n"));
|
||
GWEN_Buffer_AppendString(ubuf,
|
||
I18N("\nOptions:\n"));
|
||
if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
|
||
fprintf(stderr, "ERROR: Could not create help string\n");
|
||
return GWEN_ERROR_GENERIC;
|
||
}
|
||
GWEN_Buffer_AppendString(ubuf, "\n");
|
||
|
||
fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
|
||
GWEN_Buffer_free(ubuf);
|
||
return GWEN_ERROR_GENERIC;
|
||
}
|
||
|
||
return rv;
|
||
}
|
||
|
||
|
||
|
||
int _setUrl(GWEN_TEST_MODULE *modRoot, const char *sUrl)
|
||
{
|
||
GWEN_URL *url;
|
||
GWEN_BUFFER *dbuf;
|
||
const char *s;
|
||
|
||
url=GWEN_Url_fromString(sUrl);
|
||
if (url==NULL) {
|
||
DBG_ERROR(0, "Missing url");
|
||
return GWEN_ERROR_GENERIC;
|
||
}
|
||
|
||
GWEN_Test_Module_SetCharParam(modRoot, "url", sUrl);
|
||
|
||
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
||
|
||
s=GWEN_Url_GetServer(url);
|
||
if (s && *s)
|
||
GWEN_Buffer_AppendString(dbuf, s);
|
||
|
||
s=GWEN_Url_GetPath(url);
|
||
if (s && *s) {
|
||
if (GWEN_Buffer_GetUsedBytes(dbuf))
|
||
GWEN_Buffer_AppendString(dbuf, "/");
|
||
GWEN_Buffer_AppendString(dbuf, s);
|
||
}
|
||
|
||
s=GWEN_Url_GetProtocol(url);
|
||
if (s && *s) {
|
||
if (strcasecmp(s, "file")==0) {
|
||
GWEN_Test_Module_SetCharParam(modRoot, "fname", GWEN_Buffer_GetStart(dbuf));
|
||
}
|
||
else if (strcasecmp(s, "dir")==0) {
|
||
GWEN_Test_Module_SetCharParam(modRoot, "dirname", GWEN_Buffer_GetStart(dbuf));
|
||
}
|
||
else {
|
||
DBG_ERROR(0, "Unknown protocol \"%s\"", s);
|
||
GWEN_Buffer_free(dbuf);
|
||
GWEN_Url_free(url);
|
||
return GWEN_ERROR_GENERIC;
|
||
}
|
||
}
|
||
else {
|
||
DBG_ERROR(0, "No protocol given in url");
|
||
GWEN_Buffer_free(dbuf);
|
||
GWEN_Url_free(url);
|
||
return GWEN_ERROR_GENERIC;
|
||
}
|
||
|
||
GWEN_Buffer_free(dbuf);
|
||
GWEN_Url_free(url);
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
|
||
|
Auch abrufbar als: Unified diff
Test: Allow for setting some variables from outside
Currently only the DB url can be set from outside.
Did some tests with both plugins:
- ./aqdbtest
D "file:///tmp/test-db.db" test -L /tmp/test-db.log
./adbtest -D "dir:///tmp/test-db/db" test -L /tmp/test-db/log