From 320201bb5b7860840b4d44fa70fb6bc7bf9bfedb Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sun, 27 Oct 2019 02:07:01 +0200 Subject: [PATCH] add: init unit test --- config.json | 57 ++------------------------- config.testing.json | 12 ++++++ helpers/migrate_sql.cc | 2 +- main.cc | 23 +++++++---- tavern_tests/test_minimal.tavern.yaml | 9 +++++ 5 files changed, 40 insertions(+), 63 deletions(-) create mode 100644 config.testing.json create mode 100644 tavern_tests/test_minimal.tavern.yaml diff --git a/config.json b/config.json index 4371a26..c760809 100644 --- a/config.json +++ b/config.json @@ -6,58 +6,7 @@ "https": false } ], - "app": { - "threads_num": 1, - "enable_session": false, - "session_timeout": 0, - "document_root": "./static/", - "home_page": "index.html", - "upload_path": "uploads", - "file_types": [ - "gif", - "png", - "jpg", - "js", - "css", - "html", - "ico", - "swf", - "xap", - "apk", - "cur", - "xml" - ], - "max_connections": 100000, - "max_connections_per_ip": 0, - "load_dynamic_views": false, - "dynamic_views_path": [ - "./views" - ], - "log": { - "logfile_base_name": "", - "log_size_limit": 100000000, - "log_level": "DEBUG" - }, - "run_as_daemon": false, - "relaunch_on_error": false, - "use_sendfile": true, - "use_gzip": true, - "static_files_cache_time": 5, - "idle_connection_timeout": 60, - "server_header_field": "", - "keepalive_requests": 0, - "pipelining_requests": 0, - "gzip_static": true, - "client_max_body_size": "1M", - "client_max_memory_body_size": "64K", - "client_max_websocket_message_size": "128K" - }, - "plugins": [{ - "dependencies": [], - "config": { - "heartbeat_interval": 2 - } - - }], - "custom_config": {} + "custom_config": { + "db_name": "core.sqlite" + } } diff --git a/config.testing.json b/config.testing.json new file mode 100644 index 0000000..d3c8b7b --- /dev/null +++ b/config.testing.json @@ -0,0 +1,12 @@ +{ + "listeners": [ + { + "address": "0.0.0.0", + "port": 5000, + "https": false + } + ], + "custom_config": { + "db_name": "core.testing.sqlite" + } +} diff --git a/helpers/migrate_sql.cc b/helpers/migrate_sql.cc index 9495fb6..3eea6f0 100644 --- a/helpers/migrate_sql.cc +++ b/helpers/migrate_sql.cc @@ -70,5 +70,5 @@ helpers::migrate_sql() LOG_FATAL << "Couldn't write new Schema Version"; } - return rc == SQLITE_DONE; + return rc != SQLITE_DONE; } diff --git a/main.cc b/main.cc index 00f700d..011441b 100644 --- a/main.cc +++ b/main.cc @@ -29,31 +29,38 @@ terminate(int signum) }*/ int -main() +main(int argc, char** argv) { signal(SIGINT, terminate); signal(SIGABRT, terminate); signal(SIGTERM, terminate); signal(SIGKILL, terminate); - int rc; + const char* config_file_name = "config.json"; + + if(argc == 2) + { + config_file_name = argv[1]; + } + + //Load config file + drogon::app().loadConfigFile(config_file_name); + + const char* db_name = drogon::app().instance().getCustomConfig()["db_name"].asCString(); /* Open database */ - rc = sqlite3_open("core.sqlite", &globals::db); + int rc = sqlite3_open(db_name, &globals::db); - if( rc ) { + if(rc) { LOG_FATAL << "Can't open database: " << sqlite3_errmsg(globals::db); return 1; } - if(!helpers::migrate_sql()) + if(helpers::migrate_sql()) { terminate(1); } - //Load config file - drogon::app().loadConfigFile("config.json"); - drogon::app().registerPostHandlingAdvice(add_cors_headers); drogon::app().instance().setCustom404Page(drogon::HttpResponse::newFileResponse("./static/index.html", "", drogon::CT_TEXT_HTML)); diff --git a/tavern_tests/test_minimal.tavern.yaml b/tavern_tests/test_minimal.tavern.yaml new file mode 100644 index 0000000..31cff10 --- /dev/null +++ b/tavern_tests/test_minimal.tavern.yaml @@ -0,0 +1,9 @@ +test_name: Test basic requests + +stages: + - name: Make sure we get any response + request: + url: http://localhost:5000/api/v1/schedules/ + method: GET + response: + status_code: 200