1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* This file is part of Strigi Desktop Search
*
* Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "indexwritertester.h"
#include "analysisresult.h"
#include "indexwriter.h"
#include "indexreader.h"
#include "fieldtypes.h"
#include "query.h"
#include "queryparser.h"
#include <sstream>
using namespace std;
using namespace strigiunittest;
using namespace Strigi;
void
IndexWriterTest::setUp() {
IndexTest::setUp();
m_streamAnalyzer = new StreamAnalyzer( m_analyzerConfiguration );
}
void
IndexWriterTest::tearDown() {
delete m_streamAnalyzer;
IndexTest::tearDown();
}
void
IndexWriterTest::testAddText() {
// make sure the index is empty
CPPUNIT_ASSERT_EQUAL( 0, m_reader->countDocuments() );
// test adding very simple text
string path( "/tmp/dummy.txt" );
{
Strigi::AnalysisResult result( path, 1, *m_writer, *m_streamAnalyzer );
std::string text( "Some dummy text for testing." );
m_writer->addText( &result, text.c_str(), (int32_t)text.length() );
}
m_writer->commit();
// at this point the new document is most likely not yet visible
// for query() calls, the reader is not forcibly refreshed
// it *is* refreshed for calls to countDocuments(), so we call that
CPPUNIT_ASSERT_EQUAL( 1, m_reader->countDocuments() );
std::vector<IndexedDocument> results = m_reader->query(
QueryParser::buildQuery("dummy"), 0, 100 );
CPPUNIT_ASSERT_EQUAL_MESSAGE( "got invalid number of results", 1,
(int)results.size() );
CPPUNIT_ASSERT_EQUAL_MESSAGE( "got invalid path result", path,
results[0].uri );
m_writer->deleteAllEntries();
// FIXME: test adding unicode text
}
void
IndexWriterTest::testDeleteAllEntries() {
// make sure the index is empty
CPPUNIT_ASSERT_EQUAL( 0, m_reader->countDocuments() );
// add some random data
string path( "/tmp/dummy.txt" );
{
Strigi::AnalysisResult result( path, 1, *m_writer, *m_streamAnalyzer );
m_writer->addValue( &result, m_fieldRegister.pathField, path );
string filename( "dummy.txt" );
m_writer->addValue( &result, m_fieldRegister.filenameField, filename );
}
m_writer->commit();
m_writer->deleteAllEntries();
CPPUNIT_ASSERT_EQUAL( 0, m_reader->countDocuments() );
// now make sure nothing is left
std::map<std::string, time_t> children;
m_reader->getChildren( std::string(), children );
CPPUNIT_ASSERT_MESSAGE( "Still files left after calling deleteAllEntries.",
children.empty() );
std::vector<IndexedDocument> results = m_reader->query(
QueryParser::buildQuery(
FieldRegister::pathFieldName + "=\"" + path + "\"" ), 0, 100 );
CPPUNIT_ASSERT_MESSAGE( "Query results not empty after deleteAllEntries",
results.empty() );
results = m_reader->query( QueryParser::buildQuery(
FieldRegister::filenameFieldName + "=\"dummy.txt\"" ), 0, 100 );
CPPUNIT_ASSERT_MESSAGE( "Query results not empty after deleteAllEntries",
results.empty() );
}
void
IndexWriterTest::testDeleteEntries() {
// make sure the index is empty
CPPUNIT_ASSERT_EQUAL( 0, m_reader->countDocuments() );
// add some random data
string path1( "/tmp/dummy1.txt" );
string path2( "/tmp/dummy2.txt" );
string filename1( "dummy1.txt" );
string filename2( "dummy2.txt" );
{
AnalysisResult anaRes1( path1, 1, *m_writer, *m_streamAnalyzer );
AnalysisResult anaRes2( path2, 1, *m_writer, *m_streamAnalyzer );
m_writer->addValue(&anaRes1, m_fieldRegister.pathField, anaRes1.path());
m_writer->addValue(&anaRes1, m_fieldRegister.filenameField, filename1);
m_writer->addValue(&anaRes2, m_fieldRegister.pathField, anaRes2.path());
m_writer->addValue(&anaRes2, m_fieldRegister.filenameField, filename2 );
}
m_writer->commit();
// now delete the first one
std::vector<std::string> entries;
entries.push_back( path1 );
m_writer->deleteEntries( entries );
m_writer->commit();
// now make sure only the second one it left
std::map<std::string, time_t> children;
m_reader->getChildren( std::string(), children );
CPPUNIT_ASSERT_EQUAL( 1, ( int )children.size() );
CPPUNIT_ASSERT_EQUAL( path2, children.begin()->first );
}
void
IndexWriterTest::testDeleteNestedEntries() {
// make sure the index is empty
CPPUNIT_ASSERT_EQUAL( 0, m_reader->countDocuments() );
// add some random data
string path1( "/tmp/dummydir" );
string path2( "/tmp/dummydir/dummydir2" );
string path3( "/tmp/dummydir/dummydir2/d.txt" );
{
AnalysisResult anaRes1(path1, 1, *m_writer, *m_streamAnalyzer);
{
AnalysisResult anaRes2(path2, 1, *m_writer, *m_streamAnalyzer,
path1);
{
AnalysisResult anaRes3(path3, 1, *m_writer, *m_streamAnalyzer,
path2);
}
}
}
m_writer->commit();
// check that three documents are in the index
CPPUNIT_ASSERT_EQUAL(3, m_reader->countDocuments());
m_writer->commit();
// check that three documents are in the index, even after committing twice
CPPUNIT_ASSERT_EQUAL(3, m_reader->countDocuments());
// check that three documents are in the index
CPPUNIT_ASSERT_EQUAL(3, m_reader->countDocuments());
// now delete the first one
std::vector<std::string> entries;
entries.push_back(path1);
m_writer->deleteEntries(entries);
m_writer->commit();
m_reader->countDocuments();
// now make sure none are left
CPPUNIT_ASSERT_EQUAL( 0, m_reader->countDocuments() );
}
|