Skip to content

Commit

Permalink
Merge pull request #310 from mmd-osm/patch/unittest_options
Browse files Browse the repository at this point in the history
Added test case for command line options parsing
  • Loading branch information
mmd-osm authored Nov 22, 2023
2 parents 7d058d5 + 1d36960 commit 5bc5d95
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ cgimap_staticxml_include_HEADERS = \

TESTS = test/map.testcore test/node.testcore test/anon.testcore test/way.testcore test/relation.testcore
TESTS += test/empty.testcore test/way_full.testcore test/relation_full.testcore
TESTS += test/test_parse_id_list test/test_basicauth test/test_oauth test/test_oauth2 test/test_http test/test_parse_time
TESTS += test/test_parse_id_list test/test_basicauth test/test_oauth test/test_oauth2 test/test_http
TESTS += test/test_parse_options test/test_parse_time
TESTS += test/changesets.testcore test/message.testcore test/routes.testcore
TESTS += test/redactions.testcore
TESTS += test/test_parse_osmchange_input
Expand Down
8 changes: 7 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ check_PROGRAMS+=\
../test/test_http \
../test/test_parse_time \
../test/test_parse_osmchange_input \
../test/test_parse_changeset_input
../test/test_parse_changeset_input \
../test/test_parse_options
___test_test_parse_id_list_LDADD=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_basicauth_LDADD=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_oauth_LDADD=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
Expand All @@ -68,6 +69,7 @@ ___test_test_http_LDADD=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_L
___test_test_parse_time_LDADD=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@
___test_test_parse_osmchange_input_LDADD=libcgimap_core.la @LIBXML_LIBS@ @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@
___test_test_parse_changeset_input_LDADD=libcgimap_core.la @LIBXML_LIBS@ @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@
___test_test_parse_options_LDADD=libcgimap_core.la @LIBXML_LIBS@ @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@

if ENABLE_APIDB
___test_test_apidb_backend_nodes_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
Expand Down Expand Up @@ -114,6 +116,10 @@ ___test_test_parse_osmchange_input_SOURCES=\
___test_test_parse_changeset_input_SOURCES=\
../test/test_parse_changeset_input.cpp

___test_test_parse_options_CPPFLAGS=$(AM_CPPFLAGS) $(CATCH2_CPPFLAGS)
___test_test_parse_options_SOURCES=\
../test/test_parse_options.cpp

if ENABLE_APIDB
___test_test_apidb_backend_nodes_SOURCES=\
../test/test_apidb_backend_nodes.cpp \
Expand Down
69 changes: 69 additions & 0 deletions test/test_parse_options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

#include "cgimap/options.hpp"

#include <string>
#include <stdexcept>

#include <boost/program_options.hpp>

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

namespace po = boost::program_options;

void check_options(const po::variables_map& options)
{
global_settings::set_configuration(std::make_unique<global_settings_via_options>(options));
}

TEST_CASE("No command line options", "[options]") {
po::variables_map vm;
REQUIRE_NOTHROW(check_options(vm));
}

TEST_CASE("Invalid max-payload", "[options]") {
po::variables_map vm;
vm.emplace("max-payload", po::variable_value((long) -1, false));
REQUIRE_THROWS_AS(check_options(vm), std::invalid_argument);
}

TEST_CASE("Set all supported options" "[options]") {
po::variables_map vm;
vm.emplace("max-payload", po::variable_value((long) 40000, false));
vm.emplace("map-nodes", po::variable_value((int) 1000, false));
vm.emplace("map-area", po::variable_value((double) 0.1, false));
vm.emplace("changeset-timeout-open", po::variable_value(std::string("10 minutes"), false));
vm.emplace("changeset-timeout-idle", po::variable_value(std::string("1 hour"), false));
vm.emplace("max-changeset-elements", po::variable_value((int) 1000, false));
vm.emplace("max-way-nodes", po::variable_value((int) 100, false));
vm.emplace("scale", po::variable_value((long) 100, false));
vm.emplace("max-way-nodes", po::variable_value((int) 200, false));
vm.emplace("max-relation-members", po::variable_value((int) 50, false));
vm.emplace("max-element-tags", po::variable_value((int) 10, false));
vm.emplace("basic_auth_support", po::variable_value(false, false));
vm.emplace("oauth_10_support", po::variable_value(false, false));
vm.emplace("ratelimit", po::variable_value((long) 1000000, false));
vm.emplace("moderator-ratelimit", po::variable_value((long) 10000000, false));
vm.emplace("maxdebt", po::variable_value((long) 500, false));
vm.emplace("moderator-maxdebt", po::variable_value((long) 1000, false));
vm.emplace("ratelimit-upload", po::variable_value(true, false));
REQUIRE_NOTHROW(check_options(vm));

REQUIRE( global_settings::get_payload_max_size() == 40000 );
REQUIRE( global_settings::get_map_max_nodes() == 1000 );
REQUIRE( global_settings::get_map_area_max() == 0.1 );
REQUIRE( global_settings::get_changeset_timeout_open_max() == "10 minutes" );
REQUIRE( global_settings::get_changeset_timeout_idle() == "1 hour" );
REQUIRE( global_settings::get_changeset_max_elements() == 1000 );
REQUIRE( global_settings::get_way_max_nodes() == 100 );
REQUIRE( global_settings::get_scale() == 100 );
REQUIRE( global_settings::get_relation_max_members() == 50 );
REQUIRE( global_settings::get_element_max_tags() == 10 );
REQUIRE( global_settings::get_basic_auth_support() == false );
REQUIRE( global_settings::get_oauth_10_support() == false );
REQUIRE( global_settings::get_ratelimiter_ratelimit(false) == 1000000 );
REQUIRE( global_settings::get_ratelimiter_maxdebt(false) == 500l * 1024 * 1024 );
REQUIRE( global_settings::get_ratelimiter_ratelimit(true) == 10000000 );
REQUIRE( global_settings::get_ratelimiter_maxdebt(true) == 1000l * 1024 * 1024 );
REQUIRE( global_settings::get_ratelimiter_upload() == true );
}

0 comments on commit 5bc5d95

Please sign in to comment.