OpenTREP Logo  0.08.02
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
IndexBuildingTestSuite.cpp
Go to the documentation of this file.
1
5// //////////////////////////////////////////////////////////////////////
6// Import section
7// //////////////////////////////////////////////////////////////////////
8// STL
9#include <algorithm>
10#include <cassert>
11#include <iostream>
12#include <sstream>
13#include <fstream>
14#include <string>
15// Boost Unit Test Framework (UTF)
16#define BOOST_TEST_DYN_LINK
17#define BOOST_TEST_MAIN
18#define BOOST_TEST_MODULE IndexBuildingTestSuite
19#include <boost/test/unit_test.hpp>
20// OpenTrep
25#include <opentrep/Location.hpp>
27#include <opentrep/config/opentrep-paths.hpp>
28
29namespace boost_utf = boost::unit_test;
30
31// (Boost) Unit Test XML Report
32std::ofstream utfReportStream ("IndexBuildingTestSuite_utfresults.xml");
33
37struct UnitTestConfig {
40 boost_utf::unit_test_log.set_stream (utfReportStream);
41#if defined(BOOST_VERSION) && BOOST_VERSION >= 105900
42 boost_utf::unit_test_log.set_format (boost_utf::OF_XML);
43#else // BOOST_VERSION
44 boost_utf::unit_test_log.set_format (boost_utf::XML);
45#endif // BOOST_VERSION
46 boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
47 //boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
48 }
49
52 }
53};
54
55
56// //////////// Constants for the tests ///////////////
60const std::string K_POR_FILEPATH (OPENTREP_POR_DATA_DIR
61 "/csv/test-optd-por-public.csv");
62
69const std::string K_POR_FILEPATH_QSW (OPENTREP_POR_DATA_DIR
70 "/csv/test-optd-por-qsw-as-suwayda.csv");
71
75const std::string X_XAPIAN_DB_FP ("/tmp/opentrep/test_traveldb");
76
80const std::string X_SQL_DB_STR ("");
81
86
90const OPENTREP::shouldIndexNonIATAPOR_T K_ALL_POR = false;
91
95const OPENTREP::shouldIndexPORInXapian_T K_XAPIAN_IDX = true;
96
100const OPENTREP::shouldAddPORInSQLDB_T K_SQLDB_ADD = false;
101
102
103// /////////////// Main: Unit Test Suite //////////////
104
105// Set the UTF configuration (re-direct the output to a specific file)
107
108// Start the test suite
109BOOST_AUTO_TEST_SUITE (master_test_suite)
110
111
114BOOST_AUTO_TEST_CASE (opentrep_simple_index) {
115
116 // Output log File
117 std::string lLogFilename ("IndexBuildingTestSuite.log");
118
119 // Set the log parameters
120 std::ofstream logOutputFile;
121 // Open and clean the log outputfile
122 logOutputFile.open (lLogFilename.c_str());
123 logOutputFile.clear();
124
125 // Initialise the context
126 const OPENTREP::PORFilePath_T lPORFilePath (K_POR_FILEPATH);
127 const OPENTREP::TravelDBFilePath_T lTravelDBFilePath (X_XAPIAN_DB_FP);
130 const OPENTREP::DeploymentNumber_T lDeploymentNumber (X_DEPLOYMENT_NUMBER);
131 const OPENTREP::shouldIndexNonIATAPOR_T lShouldIndexNonIATAPOR (K_ALL_POR);
132 const OPENTREP::shouldIndexPORInXapian_T lShouldIndexPORInXapian(K_XAPIAN_IDX);
133 const OPENTREP::shouldAddPORInSQLDB_T lShouldAddPORInSQLDB (K_SQLDB_ADD);
134 OPENTREP::OPENTREP_Service opentrepService (logOutputFile, lPORFilePath,
135 lTravelDBFilePath,
136 lDBType, lSQLDBConnStr,
137 lDeploymentNumber,
138 lShouldIndexNonIATAPOR,
139 lShouldIndexPORInXapian,
140 lShouldAddPORInSQLDB);
141
142 // Query the Xapian database (index)
143 OPENTREP::WordList_T lNonMatchedWordList;
144 OPENTREP::LocationList_T lLocationList;
145 // Launch the indexation
146 const OPENTREP::NbOfDBEntries_T nbOfEntries =
147 opentrepService.insertIntoDBAndXapian();
148
149 BOOST_CHECK_MESSAGE (nbOfEntries == 9,
150 "The Xapian index ('" << lTravelDBFilePath
151 << "') contains " << nbOfEntries
152 << " entries, where as 9 are expected.");
153
154 // Close the Log outputFile
155 logOutputFile.close();
156}
157
162BOOST_AUTO_TEST_CASE (opentrep_default_pg_connection_string) {
163 const OPENTREP::SQLDBConnectionString_T lDefaultConnection (
165 const OPENTREP::StringMap_T lDefaultParams =
166 OPENTREP::parsePGConnectionString (lDefaultConnection);
167
168 BOOST_REQUIRE (lDefaultParams.find ("dbname") != lDefaultParams.end());
169 BOOST_REQUIRE (lDefaultParams.find ("host") != lDefaultParams.end());
170 BOOST_CHECK_EQUAL (lDefaultParams.find ("dbname")->second, "trep");
171 BOOST_CHECK_EQUAL (lDefaultParams.find ("host")->second, "localhost");
172
173 const OPENTREP::SQLDBConnectionString_T lDeploymentConnection =
174 OPENTREP::buildPGConnectionString (lDefaultParams, 0);
175 const OPENTREP::StringMap_T lDeploymentParams =
176 OPENTREP::parsePGConnectionString (lDeploymentConnection);
177
178 BOOST_REQUIRE (lDeploymentParams.find ("dbname") != lDeploymentParams.end());
179 BOOST_REQUIRE (lDeploymentParams.find ("host") != lDeploymentParams.end());
180 BOOST_CHECK_EQUAL (lDeploymentParams.find ("dbname")->second, "trep0");
181 BOOST_CHECK_EQUAL (lDeploymentParams.find ("host")->second, "localhost");
182}
183
188BOOST_AUTO_TEST_CASE (opentrep_alt_name_with_equals_sign) {
189 std::ifstream lPORFile (K_POR_FILEPATH.c_str());
190 BOOST_REQUIRE (lPORFile.good());
191
192 std::string lHeader;
193 std::string lPORRecord;
194 std::getline (lPORFile, lHeader);
195 std::getline (lPORFile, lPORRecord);
196
197 const std::string lOriginalAltName ("ja|ケプラヴィーク国際空港|");
198 const std::string lAltNameWithEquals ("ja|ケプラ=ヴィーク国際空港|");
199 const std::string::size_type lAltNamePos =
200 lPORRecord.find (lOriginalAltName);
201 BOOST_REQUIRE (lAltNamePos != std::string::npos);
202 lPORRecord.replace (lAltNamePos, lOriginalAltName.size(),
203 lAltNameWithEquals);
204
205 OPENTREP::PORStringParser lPORParser (lPORRecord);
206 const OPENTREP::Location& lLocation = lPORParser.generateLocation();
207 BOOST_CHECK_EQUAL (lLocation.getIataCode(), "KEF");
208}
209
217BOOST_AUTO_TEST_CASE (opentrep_alt_name_with_embedded_equals_and_valid_lang_code) {
218 std::ifstream lPORFile (K_POR_FILEPATH_QSW.c_str());
219 BOOST_REQUIRE (lPORFile.good());
220
221 std::string lHeader;
222 std::string lPORRecord;
223 std::getline (lPORFile, lHeader);
224 std::getline (lPORFile, lPORRecord);
225 BOOST_REQUIRE (lPORRecord.find ("ja|アス=スワイダ|") != std::string::npos);
226
227 OPENTREP::PORStringParser lPORParser (lPORRecord);
228 const OPENTREP::Location& lLocation = lPORParser.generateLocation();
229 BOOST_CHECK_EQUAL (lLocation.getIataCode(), "QSW");
230
231 OPENTREP::NameList_T lJapaneseNames;
232 BOOST_REQUIRE (lLocation.getNameList (OPENTREP::LanguageCode_T ("ja"),
233 lJapaneseNames));
234 BOOST_CHECK (std::find (lJapaneseNames.begin(), lJapaneseNames.end(),
235 "アス=スワイダ") != lJapaneseNames.end());
236
237 OPENTREP::NameList_T lJavaneseNames;
238 BOOST_REQUIRE (lLocation.getNameList (OPENTREP::LanguageCode_T ("jv"),
239 lJavaneseNames));
240 BOOST_CHECK (std::find (lJavaneseNames.begin(), lJavaneseNames.end(),
241 "As-Suwayda") != lJavaneseNames.end());
242}
243
248BOOST_AUTO_TEST_CASE (opentrep_empty_alt_name_before_equals_sign) {
249 std::ifstream lPORFile (K_POR_FILEPATH.c_str());
250 BOOST_REQUIRE (lPORFile.good());
251
252 std::string lHeader;
253 std::string lPORRecord;
254 std::getline (lPORFile, lHeader);
255 std::getline (lPORFile, lPORRecord);
256
257 std::vector<std::string> lFields;
258 std::stringstream lRecordStream (lPORRecord);
259 std::string lField;
260 while (std::getline (lRecordStream, lField, '^')) {
261 lFields.push_back (lField);
262 }
263 BOOST_REQUIRE (lFields.size() == 51);
264 lFields[43] = "";
265 lFields[47] = "AUBWS|=AUWW3|";
266
267 std::ostringstream lModifiedRecord;
268 for (std::vector<std::string>::const_iterator itField = lFields.begin();
269 itField != lFields.end(); ++itField) {
270 if (itField != lFields.begin()) {
271 lModifiedRecord << '^';
272 }
273 lModifiedRecord << *itField;
274 }
275
276 OPENTREP::PORStringParser lPORParser (lModifiedRecord.str());
277 const OPENTREP::Location& lLocation = lPORParser.generateLocation();
278 BOOST_CHECK_EQUAL (lLocation.getIataCode(), "KEF");
279}
280
285BOOST_AUTO_TEST_CASE (opentrep_long_spelling_term) {
286 std::ifstream lPORFile (K_POR_FILEPATH.c_str());
287 BOOST_REQUIRE (lPORFile.good());
288
289 std::string lHeader;
290 std::string lPORRecord;
291 std::getline (lPORFile, lHeader);
292 std::getline (lPORFile, lPORRecord);
293
294 const std::string lOriginalAltName ("ja|ケプラヴィーク国際空港|");
295 const std::string lLongAltName ("ja|" + std::string (300, 'a') + "|");
296 const std::string::size_type lAltNamePos =
297 lPORRecord.find (lOriginalAltName);
298 BOOST_REQUIRE (lAltNamePos != std::string::npos);
299 lPORRecord.replace (lAltNamePos, lOriginalAltName.size(), lLongAltName);
300
301 const std::string lPORFilepath ("/tmp/opentrep-long-spelling-term.csv");
302 std::ofstream lModifiedPORFile (lPORFilepath.c_str());
303 BOOST_REQUIRE (lModifiedPORFile.good());
304 lModifiedPORFile << lHeader << '\n' << lPORRecord << '\n';
305 lModifiedPORFile.close();
306
307 std::ofstream lLogFile ("/tmp/opentrep-long-spelling-term.log");
309 lLogFile, OPENTREP::PORFilePath_T (lPORFilepath),
310 OPENTREP::TravelDBFilePath_T ("/tmp/opentrep-long-spelling-term-xapian"),
312 OPENTREP::SQLDBConnectionString_T (""), 0, true, true, false);
313
314 BOOST_CHECK_EQUAL (lService.insertIntoDBAndXapian(), 1);
315}
316
317// End the test suite
318BOOST_AUTO_TEST_SUITE_END()
319
320
std::ofstream utfReportStream("PartitionTestSuite_utfresults.xml")
BOOST_AUTO_TEST_CASE(partition_small_string)
BOOST_GLOBAL_FIXTURE(UnitTestConfig)
const std::string X_XAPIAN_DB_FP("/tmp/opentrep/test_traveldb")
const std::string X_SQL_DB_STR("")
const OPENTREP::DeploymentNumber_T X_DEPLOYMENT_NUMBER(0)
std::ofstream utfReportStream("UnicodeTestSuite_utfresults.xml")
Interface for the OPENTREP Services.
std::list< Word_T > WordList_T
const std::string DEFAULT_OPENTREP_PG_CONN_STRING
bool shouldAddPORInSQLDB_T
unsigned int NbOfDBEntries_T
SQLDBConnectionString_T buildPGConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
std::list< std::string > NameList_T
Definition Names.hpp:20
std::list< Location > LocationList_T
bool shouldIndexPORInXapian_T
unsigned short DeploymentNumber_T
StringMap_T parsePGConnectionString(const SQLDBConnectionString_T &iSQLDBConnStr)
bool shouldIndexNonIATAPOR_T
std::map< const std::string, std::string > StringMap_T
Definition Utilities.hpp:43
Enumeration of database types.
Definition DBType.hpp:17
Structure modelling a (geographical) location.
Definition Location.hpp:25
const IATACode_T & getIataCode() const
Definition Location.hpp:38
bool getNameList(const LanguageCode_T &iLanguageCode, NameList_T &ioNameList) const
Definition Location.hpp:402