OpenTREP Logo  0.08.02
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
Command-Line Test to Demonstrate How To Test the OpenTREP Project
/
// //////////////////////////////////////////////////////////////////////
// Import section
// //////////////////////////////////////////////////////////////////////
// STL
#include <algorithm>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
// Boost Unit Test Framework (UTF)
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE IndexBuildingTestSuite
#include <boost/test/unit_test.hpp>
// OpenTrep
#include <opentrep/config/opentrep-paths.hpp>
namespace boost_utf = boost::unit_test;
// (Boost) Unit Test XML Report
std::ofstream utfReportStream ("IndexBuildingTestSuite_utfresults.xml");
boost_utf::unit_test_log.set_stream (utfReportStream);
#if defined(BOOST_VERSION) && BOOST_VERSION >= 105900
boost_utf::unit_test_log.set_format (boost_utf::OF_XML);
#else // BOOST_VERSION
boost_utf::unit_test_log.set_format (boost_utf::XML);
#endif // BOOST_VERSION
boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
//boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
}
}
};
// //////////// Constants for the tests ///////////////
const std::string K_POR_FILEPATH (OPENTREP_POR_DATA_DIR
"/csv/test-optd-por-public.csv");
const std::string K_POR_FILEPATH_QSW (OPENTREP_POR_DATA_DIR
"/csv/test-optd-por-qsw-as-suwayda.csv");
const std::string X_XAPIAN_DB_FP ("/tmp/opentrep/test_traveldb");
const std::string X_SQL_DB_STR ("");
const OPENTREP::shouldIndexNonIATAPOR_T K_ALL_POR = false;
const OPENTREP::shouldIndexPORInXapian_T K_XAPIAN_IDX = true;
const OPENTREP::shouldAddPORInSQLDB_T K_SQLDB_ADD = false;
// /////////////// Main: Unit Test Suite //////////////
// Set the UTF configuration (re-direct the output to a specific file)
// Start the test suite
BOOST_AUTO_TEST_SUITE (master_test_suite)
BOOST_AUTO_TEST_CASE (opentrep_simple_index) {
// Output log File
std::string lLogFilename ("IndexBuildingTestSuite.log");
// Set the log parameters
std::ofstream logOutputFile;
// Open and clean the log outputfile
logOutputFile.open (lLogFilename.c_str());
logOutputFile.clear();
// Initialise the context
const OPENTREP::PORFilePath_T lPORFilePath (K_POR_FILEPATH);
const OPENTREP::TravelDBFilePath_T lTravelDBFilePath (X_XAPIAN_DB_FP);
const OPENTREP::shouldIndexNonIATAPOR_T lShouldIndexNonIATAPOR (K_ALL_POR);
const OPENTREP::shouldIndexPORInXapian_T lShouldIndexPORInXapian(K_XAPIAN_IDX);
const OPENTREP::shouldAddPORInSQLDB_T lShouldAddPORInSQLDB (K_SQLDB_ADD);
OPENTREP::OPENTREP_Service opentrepService (logOutputFile, lPORFilePath,
lTravelDBFilePath,
lDBType, lSQLDBConnStr,
lDeploymentNumber,
lShouldIndexNonIATAPOR,
lShouldIndexPORInXapian,
lShouldAddPORInSQLDB);
// Query the Xapian database (index)
OPENTREP::WordList_T lNonMatchedWordList;
OPENTREP::LocationList_T lLocationList;
// Launch the indexation
const OPENTREP::NbOfDBEntries_T nbOfEntries =
opentrepService.insertIntoDBAndXapian();
BOOST_CHECK_MESSAGE (nbOfEntries == 9,
"The Xapian index ('" << lTravelDBFilePath
<< "') contains " << nbOfEntries
<< " entries, where as 9 are expected.");
// Close the Log outputFile
logOutputFile.close();
}
BOOST_AUTO_TEST_CASE (opentrep_default_pg_connection_string) {
const OPENTREP::SQLDBConnectionString_T lDefaultConnection (
const OPENTREP::StringMap_T lDefaultParams =
OPENTREP::parsePGConnectionString (lDefaultConnection);
BOOST_REQUIRE (lDefaultParams.find ("dbname") != lDefaultParams.end());
BOOST_REQUIRE (lDefaultParams.find ("host") != lDefaultParams.end());
BOOST_CHECK_EQUAL (lDefaultParams.find ("dbname")->second, "trep");
BOOST_CHECK_EQUAL (lDefaultParams.find ("host")->second, "localhost");
const OPENTREP::SQLDBConnectionString_T lDeploymentConnection =
OPENTREP::buildPGConnectionString (lDefaultParams, 0);
const OPENTREP::StringMap_T lDeploymentParams =
OPENTREP::parsePGConnectionString (lDeploymentConnection);
BOOST_REQUIRE (lDeploymentParams.find ("dbname") != lDeploymentParams.end());
BOOST_REQUIRE (lDeploymentParams.find ("host") != lDeploymentParams.end());
BOOST_CHECK_EQUAL (lDeploymentParams.find ("dbname")->second, "trep0");
BOOST_CHECK_EQUAL (lDeploymentParams.find ("host")->second, "localhost");
}
BOOST_AUTO_TEST_CASE (opentrep_alt_name_with_equals_sign) {
std::ifstream lPORFile (K_POR_FILEPATH.c_str());
BOOST_REQUIRE (lPORFile.good());
std::string lHeader;
std::string lPORRecord;
std::getline (lPORFile, lHeader);
std::getline (lPORFile, lPORRecord);
const std::string lOriginalAltName ("ja|ケプラヴィーク国際空港|");
const std::string lAltNameWithEquals ("ja|ケプラ=ヴィーク国際空港|");
const std::string::size_type lAltNamePos =
lPORRecord.find (lOriginalAltName);
BOOST_REQUIRE (lAltNamePos != std::string::npos);
lPORRecord.replace (lAltNamePos, lOriginalAltName.size(),
lAltNameWithEquals);
OPENTREP::PORStringParser lPORParser (lPORRecord);
const OPENTREP::Location& lLocation = lPORParser.generateLocation();
BOOST_CHECK_EQUAL (lLocation.getIataCode(), "KEF");
}
BOOST_AUTO_TEST_CASE (opentrep_alt_name_with_embedded_equals_and_valid_lang_code) {
std::ifstream lPORFile (K_POR_FILEPATH_QSW.c_str());
BOOST_REQUIRE (lPORFile.good());
std::string lHeader;
std::string lPORRecord;
std::getline (lPORFile, lHeader);
std::getline (lPORFile, lPORRecord);
BOOST_REQUIRE (lPORRecord.find ("ja|アス=スワイダ|") != std::string::npos);
OPENTREP::PORStringParser lPORParser (lPORRecord);
const OPENTREP::Location& lLocation = lPORParser.generateLocation();
BOOST_CHECK_EQUAL (lLocation.getIataCode(), "QSW");
OPENTREP::NameList_T lJapaneseNames;
BOOST_REQUIRE (lLocation.getNameList (OPENTREP::LanguageCode_T ("ja"),
lJapaneseNames));
BOOST_CHECK (std::find (lJapaneseNames.begin(), lJapaneseNames.end(),
"アス=スワイダ") != lJapaneseNames.end());
OPENTREP::NameList_T lJavaneseNames;
BOOST_REQUIRE (lLocation.getNameList (OPENTREP::LanguageCode_T ("jv"),
lJavaneseNames));
BOOST_CHECK (std::find (lJavaneseNames.begin(), lJavaneseNames.end(),
"As-Suwayda") != lJavaneseNames.end());
}
BOOST_AUTO_TEST_CASE (opentrep_empty_alt_name_before_equals_sign) {
std::ifstream lPORFile (K_POR_FILEPATH.c_str());
BOOST_REQUIRE (lPORFile.good());
std::string lHeader;
std::string lPORRecord;
std::getline (lPORFile, lHeader);
std::getline (lPORFile, lPORRecord);
std::vector<std::string> lFields;
std::stringstream lRecordStream (lPORRecord);
std::string lField;
while (std::getline (lRecordStream, lField, '^')) {
lFields.push_back (lField);
}
BOOST_REQUIRE (lFields.size() == 51);
lFields[43] = "";
lFields[47] = "AUBWS|=AUWW3|";
std::ostringstream lModifiedRecord;
for (std::vector<std::string>::const_iterator itField = lFields.begin();
itField != lFields.end(); ++itField) {
if (itField != lFields.begin()) {
lModifiedRecord << '^';
}
lModifiedRecord << *itField;
}
OPENTREP::PORStringParser lPORParser (lModifiedRecord.str());
const OPENTREP::Location& lLocation = lPORParser.generateLocation();
BOOST_CHECK_EQUAL (lLocation.getIataCode(), "KEF");
}
BOOST_AUTO_TEST_CASE (opentrep_long_spelling_term) {
std::ifstream lPORFile (K_POR_FILEPATH.c_str());
BOOST_REQUIRE (lPORFile.good());
std::string lHeader;
std::string lPORRecord;
std::getline (lPORFile, lHeader);
std::getline (lPORFile, lPORRecord);
const std::string lOriginalAltName ("ja|ケプラヴィーク国際空港|");
const std::string lLongAltName ("ja|" + std::string (300, 'a') + "|");
const std::string::size_type lAltNamePos =
lPORRecord.find (lOriginalAltName);
BOOST_REQUIRE (lAltNamePos != std::string::npos);
lPORRecord.replace (lAltNamePos, lOriginalAltName.size(), lLongAltName);
const std::string lPORFilepath ("/tmp/opentrep-long-spelling-term.csv");
std::ofstream lModifiedPORFile (lPORFilepath.c_str());
BOOST_REQUIRE (lModifiedPORFile.good());
lModifiedPORFile << lHeader << '\n' << lPORRecord << '\n';
lModifiedPORFile.close();
std::ofstream lLogFile ("/tmp/opentrep-long-spelling-term.log");
lLogFile, OPENTREP::PORFilePath_T (lPORFilepath),
OPENTREP::TravelDBFilePath_T ("/tmp/opentrep-long-spelling-term-xapian"),
OPENTREP::SQLDBConnectionString_T (""), 0, true, true, false);
BOOST_CHECK_EQUAL (lService.insertIntoDBAndXapian(), 1);
}
// End the test suite
BOOST_AUTO_TEST_SUITE_END()