Skip to content

Commit

Permalink
adding tests + making single process
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresvic committed Feb 11, 2013
1 parent f586d85 commit 9ce8317
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 25 deletions.
7 changes: 1 addition & 6 deletions data/document04.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>Input document</title>
<input1> some &lt; text</input1>
<input2>72.8/km²</input2>
</document>
<?xml version="1.0" encoding="utf-8"?><document><input1> some &lt; text</input1><input2>72.8/km²</input2><title>Input document</title></document>
5 changes: 5 additions & 0 deletions src/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace zsearch
{
const std::string LOCK_FILE = "/var/tmp/zsearch.pid";

const std::string LEVELDB_STORE = "/var/tmp/store";
const std::string LEVELDB_TEST_STORE = "/var/tmp/test_store";

const std::string QUERY_PARSER_DELIMITERS = " \t\n\r.,;";

namespace server
Expand Down
40 changes: 31 additions & 9 deletions src/DocumentStoreLevelDb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ using namespace std;
class DocumentStoreLevelDb : public IDocumentStore
{
private:

std::shared_ptr<KVStore::IKVStore> store;

public:

DocumentStoreLevelDb(std::shared_ptr<KVStore::IKVStore> store) : store(store)
{
store->Open();
}

~DocumentStoreLevelDb()
{
std::cerr << "Destroyed DocumentStoreLevelDb" << std::endl;
}

void addDoc(unsigned int docId, const shared_ptr<IDocument>& doc)
{
stringstream ss;
Expand All @@ -51,20 +51,42 @@ class DocumentStoreLevelDb : public IDocumentStore
{
throw ZException("Not implemented - will be deprecated");
}

int Get(unsigned int docId, shared_ptr<IDocument>& doc) const
{
string d;

if (store->Get(docId, d).ok())
{
doc->construct(d);

/*
cout << "got docId " << docId << endl << d << endl;
try
{
doc->construct(d);
}
catch (const string& ex)
{
cerr << ex << endl;
}
catch (const exception& ex)
{
cerr << ex.what() << endl;
}
catch (...)
{
cerr << "wtf" << endl;
}
*/

return 1;
}

return 0;
}

};

#endif
5 changes: 2 additions & 3 deletions src/ZUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ZUtil
char* ptr = EncodeVarint64(buf, v);
dst.append(buf, ptr - buf);
}

static string getString(uint64_t v)
{
std::stringstream ss;
Expand All @@ -58,7 +58,7 @@ class ZUtil
ss << number;
return ss.str();
}

static int getInt(const std::string& str)
{
int x;
Expand Down Expand Up @@ -109,7 +109,6 @@ class ZUtil
return ss.str();
}


};

#endif
37 changes: 30 additions & 7 deletions tests/DocumentImplTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ struct DocumentImplTest : tpunit::TestFixture
TEST(DocumentImplTest::testParsingDocumentCDATA),
TEST(DocumentImplTest::testDocumentWrite),
TEST(DocumentImplTest::testDocumentXmlEntity),
TEST(DocumentImplTest::testParsingDocumentTerrible)
TEST(DocumentImplTest::testParsingDocumentTerrible),
TEST(DocumentImplTest::testDocumentConstruct)
)
{ }

Expand Down Expand Up @@ -102,17 +103,19 @@ struct DocumentImplTest : tpunit::TestFixture

void testDocumentWrite()
{
string docStr = "<document><input1> some text</input1><input2> some more text</input2></document>";
string docStr = "<document>\n\t<input1> some text</input1>\n\t<input2> some more text</input2>\n</document>";

shared_ptr<IDocument> document = make_shared<DocumentImpl>(docStr);

stringstream ss;
document->write(ss);

string docStrExpected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + docStr;
string docStrActual = ss.str();
string docStrExpected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + docStr;
docStrExpected = stripSpecialCharacters(docStrExpected);

string docStrActual = stripSpecialCharacters(ss.str());

// cout << docStrExpected << endl << docStrActual << endl;
cout << docStrExpected << endl << docStrActual << endl;

ASSERT_TRUE(docStrExpected.compare(docStrActual) == 0);
}
Expand All @@ -136,11 +139,12 @@ struct DocumentImplTest : tpunit::TestFixture
stringstream ss;
document->write(ss);

string docStrActual = ss.str();
string docStrActual = stripSpecialCharacters(ss.str());
string docStrExpected = stripSpecialCharacters(docStr);

cout << docStr << endl << docStrActual << endl;

// ASSERT_TRUE(docStr.compare(docStrActual) == 0);
ASSERT_TRUE(docStrExpected.compare(docStrActual) == 0);
}

void testParsingDocumentTerrible()
Expand Down Expand Up @@ -170,4 +174,23 @@ struct DocumentImplTest : tpunit::TestFixture

}

void testDocumentConstruct()
{
string docStr1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document>\n\t<input1> some text</input1>\n\t<input2> some more text</input2>\n</document>";

shared_ptr<IDocument> document = make_shared<DocumentImpl>(docStr1);

string docStr2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document>\n\t<construct>bla bla</construct>\n\n</document>";

document->construct(docStr2);

stringstream ss;
document->write(ss);

string docStrActual = stripSpecialCharacters(ss.str());
string docStrExpected = stripSpecialCharacters(docStr2);

ASSERT_TRUE(docStrExpected.compare(docStrActual) == 0);
}

};
15 changes: 15 additions & 0 deletions tests/TestUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,19 @@ string readFile(const string& fileName)
return fileStr;
}

string stripSpecialCharacters(const string& input)
{
string clean;

for (char c : input)
{
if (c == '\n' || c == '\t')
continue;

clean += c;
}

return clean;
}

#endif

0 comments on commit 9ce8317

Please sign in to comment.