1 /*
2  fts_test.cpp     MindForger fullt text search test
3 
4  Copyright (C) 2016-2020 Martin Dvorak <martin.dvorak@mindforger.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <stddef.h>
21 #include <iostream>
22 #include <iterator>
23 #include <string>
24 #include <vector>
25 
26 #include <gtest/gtest.h>
27 
28 #include "../../../src/config/configuration.h"
29 #include "../../../src/mind/mind.h"
30 
31 extern char* getMindforgerGitHomePath();
32 
33 using namespace std;
34 
printFtsResult(vector<m8r::Note * > * result)35 void printFtsResult(vector<m8r::Note*>* result)
36 {
37     if(result->size()) {
38         cout << " FOUND " << result->size() << " result(s):" << endl;
39         for(size_t i=0; i<result->size(); i++) {
40             cout << "  #" << i << " " <<
41                     result->at(i)->getOutline()->getName() << " / " <<
42                     result->at(i)->getName() << endl;
43         }
44     } else {
45         cout << " NOTHING found" << endl;
46     }
47 }
48 
TEST(FtsTestCase,FTS)49 TEST(FtsTestCase, FTS) {
50     string repositoryPath{"/lib/test/resources/basic-repository"};
51     repositoryPath.insert(0, getMindforgerGitHomePath());
52 
53     m8r::Configuration& config = m8r::Configuration::getInstance();
54     config.clear();
55     config.setConfigFilePath("/tmp/cfg-mtc-f.md");
56     config.setActiveRepository(config.addRepository(m8r::RepositoryIndexer::getRepositoryForPath(repositoryPath)));
57 
58     m8r::Mind mind(config);
59     mind.learn();
60     mind.think().get();
61 
62     cout << endl << "Statistics:" << endl;
63     cout << "  Outlines: " << mind.remind().getOutlinesCount() << endl;
64     cout << "  Bytes   : " << mind.remind().getOutlineMarkdownsSize() << endl;
65 
66     cout << "EXACT search..." << endl;
67 
68     string pattern("hash");
69     vector<m8r::Note*>* result = mind.findNoteFts(pattern, m8r::FtsSearch::EXACT);
70     printFtsResult(result);
71     EXPECT_NE(nullptr, result);
72     EXPECT_EQ(2, result->size());
73     delete result;
74 
75     cout << "IGNORE CASE search..." << endl;
76 
77     pattern.assign("hash");
78     result = mind.findNoteFts(pattern, m8r::FtsSearch::IGNORE_CASE);
79     printFtsResult(result);
80     EXPECT_NE(nullptr, result);
81     EXPECT_EQ(3, result->size());
82     delete result;
83 
84     cout << "REGEX search..." << endl;
85 
86     pattern.assign("lo*king");
87     result = mind.findNoteFts(pattern, m8r::FtsSearch::REGEXP);
88     printFtsResult(result);
89     EXPECT_NE(nullptr, result);
90     EXPECT_EQ(2, result->size());
91     delete result;
92 }
93