1 #include <gtest/gtest.h>
28 const std::string json =
"{\"MI\":[{\"At\":[{\"uid\":\"2\"},{\"sid\":\"1\"},{\"lang\":\"255\"}],\"Id\":\"9659189393397617664\",\"W\":\"00000000000000000002000000000000000000ff00000000000000000001\"}],\"RI\":[{\"WI\":[{\"d\":5231,\"h\":28086,\"w\":\"appl\"}],\"f\":5231,\"max\":1,\"node\":\"test_node\",\"order\":2,\"q\":\"apple\",\"qid\":123,\"r\":1,\"time\":1}]}";
33 bool ret = resultSerializator.unserialize(json);
35 std::cout <<
"ErrorCode: " << resultSerializator.getErrorCode() <<
"\tErrorMsg: " << resultSerializator.getErrorMsg() <<
std::endl;
38 ASSERT_TRUE(resultSerializator.getErrorCode() ==
NO_ERROR);
39 ASSERT_TRUE(resultSerializator.getErrorMsg().empty());
42 printSuccess(
"Unserialize (FILL_MATCH_INFO | FILL_REQUEST_INFO | FILL_ATTRIBUTES | FILL_WORD_INFO)");
44 std::string jsonString;
47 std::cout <<
"ErrorCode: " << resultSerializator.getErrorCode() <<
"\tErrorMsg: " << resultSerializator.getErrorMsg() <<
std::endl;
50 ASSERT_TRUE(resultSerializator.getErrorCode() ==
NO_ERROR);
51 ASSERT_TRUE(resultSerializator.getErrorMsg().empty());
52 std::cout <<
" json: " << json <<
std::endl;
53 std::cout <<
"jsonString: " << jsonString <<
std::endl;
56 printSuccess(
"Serialize (FILL_MATCH_INFO | FILL_REQUEST_INFO | FILL_ATTRIBUTES | FILL_WORD_INFO)");
60 std::cout <<
"ErrorCode: " << resultSerializator.getErrorCode() <<
"\tErrorMsg: " << resultSerializator.getErrorMsg() <<
std::endl;
63 ASSERT_TRUE(resultSerializator.getErrorCode() ==
NO_ERROR);
64 ASSERT_TRUE(resultSerializator.getErrorMsg().empty());
65 printSuccess(
"Serialize (FILL_MATCH_INFO | FILL_REQUEST_INFO | FILL_ATTRIBUTES)");
67 ret = outResultSerializator.
unserialize(jsonString);
73 ASSERT_TRUE(outResultSerializator.
getErrorMsg().empty());
76 for (
size_t i=0;i<requestCount;++i)
80 printSuccess(
"Unserialize (FILL_MATCH_INFO | FILL_REQUEST_INFO | FILL_ATTRIBUTES)");
84 std::cout <<
"ErrorCode: " << resultSerializator.getErrorCode() <<
"\tErrorMsg: " << resultSerializator.getErrorMsg() <<
std::endl;
87 ASSERT_TRUE(resultSerializator.getErrorCode() ==
NO_ERROR);
88 ASSERT_TRUE(resultSerializator.getErrorMsg().empty());
89 printSuccess(
"Serialize (FILL_MATCH_INFO | FILL_REQUEST_INFO)");
91 ret = outResultSerializator.
unserialize(jsonString);
97 ASSERT_TRUE(outResultSerializator.
getErrorMsg().empty());
100 for (
size_t i=0;i<matchesCount;++i)
104 printSuccess(
"Unserialize (FILL_MATCH_INFO | FILL_REQUEST_INFO)");
108 std::cout <<
"ErrorCode: " << resultSerializator.getErrorCode() <<
"\tErrorMsg: " << resultSerializator.getErrorMsg() <<
std::endl;
111 ASSERT_TRUE(resultSerializator.getErrorCode() ==
NO_ERROR);
112 ASSERT_TRUE(resultSerializator.getErrorMsg().empty());
113 printSuccess(
"Serialize (FILL_MATCH_INFO)");
115 ret = outResultSerializator.
unserialize(jsonString);
121 ASSERT_TRUE(outResultSerializator.
getErrorMsg().empty());
123 printSuccess(
"Unserialize (FILL_MATCH_INFO)");
125 ret = resultSerializator.serialize((
NO_FILL), jsonString);
127 std::cout <<
"ErrorCode: " << resultSerializator.getErrorCode() <<
"\tErrorMsg: " << resultSerializator.getErrorMsg() <<
std::endl;
130 assert(resultSerializator.getErrorCode() ==
NO_ERROR);
131 assert(resultSerializator.getErrorMsg().empty());
132 printSuccess(
"Serialize (NO_FILL)");
134 ret = outResultSerializator.
unserialize(jsonString);
140 ASSERT_TRUE(outResultSerializator.
getErrorMsg().empty());
142 printSuccess(
"Unserialize (NO_FILL)");
148 ASSERT_FALSE(defaultJson.
isError());
151 ASSERT_FALSE(defaultJson.
getJSON().empty());
152 printSuccess(
"Generate default JSON");
161 ASSERT_FALSE(defaultJson.
getJSON().empty());
163 printSuccess(
"Test SphinxResultDataSerializator");
167 const std::string
query =
"q";
168 const unsigned int totalRes = 10;
170 const unsigned int time = 1;
173 Poco::SharedPtr<SphinxResultData> sphinxResultData;
185 AttrInfo pAttrInfo1(
"first",
"value");
192 sphinxRequestInfo.
setTotal(totalRes);
199 sphinxResultData->addMatchInfo(sphinxMatchInfo1);
200 sphinxResultData->addMatchInfo(sphinxMatchInfo2);
201 sphinxResultData->addRequestInfo(sphinxRequestInfo);
204 const size_t matchInfoCount = sphinxResultData->getMatchInfoCount();
208 attributesCount = sphinxResultData->getMatchInfoItem(0).getAttributesCount();
214 std::string resultJson;
215 bool ret = resultSerializator.
serialize(resultJson, attributesCount, attributesCount);
216 if (resultSerializator.
isError())
220 std::cout <<
"JSON: " << resultJson <<
std::endl;
222 sphinxResultData->reset();
224 if (resultSerializator.
isError())
228 const size_t macthInfoCount = sphinxResultData->getMatchInfoCount();
229 ASSERT_EQ(macthInfoCount, 2);
230 ASSERT_EQ(sphinxResultData->getMatchInfoItem(0).getWeight(),
toHexString(111));
231 ASSERT_EQ(sphinxResultData->getMatchInfoItem(1).getWeight(),
toHexString(222));
232 printSuccess(
"Test SerializationExt");
240 size_t matchesCount = 100;
243 resultMemoryManager.
resize(matchesCount, attributesCount);
248 for (
size_t j=0;j<matchesCount;++j)
254 for (
size_t i=0;i<matchesCount;++i)
256 printSuccess(
"Allocated memory for 100 MatchInfo each have 10 attributes");
261 resultMemoryManager.
resize(matchesCount, attributesCount);
266 for (
size_t j=0;j<matchesCount;++j)
272 for (
size_t i=0;i<matchesCount;++i)
274 printSuccess(
"Reallocated memory for 10 MatchInfo each have 5 attributes");
276 resultMemoryManager.
reset();
278 for (
size_t i=0;i<matchesCount;++i)
280 printSuccess(
"Reset allocated memory");
282 for (
size_t i=0;i<(matchesCount/2);++i)
286 for (
size_t j=0;j<(attributesCount/2);++j)
292 for (
size_t i=0;i<(matchesCount/2);++i)
294 printSuccess(
"Filling half allocated memory");
296 resultMemoryManager.
resize(1, 0);
297 for (
size_t i=0;i<matchesCount;++i)
308 printSuccess(
"Resize allocated memory for 1 MatchInfo and without attributes");
310 resultMemoryManager.
resize(0, 0);
311 for (
size_t i=0;i<matchesCount;++i)
320 printSuccess(
"Resize allocated memory for 0 MatchInfo and without attributes");
322 const size_t MATCHES_SZ = 2;
323 const size_t ATTRIBUTES_SZ = 3;
324 resultMemoryManager.
resize(MATCHES_SZ, ATTRIBUTES_SZ);
325 unsigned long long docId[MATCHES_SZ] = {123, 456};
326 std::string
weight[MATCHES_SZ] = {
"00000000000000000001",
"00000000000000000002"};
327 std::vector<std::pair<std::string, std::string> > attr[ATTRIBUTES_SZ] = {{
338 for (
size_t i=0;i<MATCHES_SZ;++i)
342 for (
size_t j=0;j<ATTRIBUTES_SZ;++j)
349 for (
size_t i=0;i<matchesCount;++i)
360 printSuccess(
"Test SphinxResultDataMemoryMananger");
364 const std::vector<std::pair<std::string, std::string> >&
queryParameters,
365 const std::vector<std::pair<std::string, std::string> >&
orderParameters,
366 const std::vector<std::string>& externalFields,
367 const std::vector<std::string>& orderFields,
383 for (
size_t i=0;i<externalFields.size();++i)
386 for (
size_t i=0;i<orderFields.size();++i)
390 printSuccess(
"Serialize SphinxInputJsonMessageSearch");
393 handler.
setType(SphinxInputJsonMessage::MessageType::mtSearch);
396 std::stringstream str;
401 std::cout <<
"ret: " << ret <<
std::endl;
404 ASSERT_FALSE(outputMessage.
isError());
407 ASSERT_FALSE(resultSerializator.
isError());
409 ASSERT_TRUE(resultSerializator.
getErrorMsg().empty());
410 printSuccess(
"Make Search Request");
415 const std::string title =
"Test Searcher Process";
420 const std::vector<std::pair<std::string, std::string> >
queryParameters = {
421 {
"queryId",
"7475839"},
424 {
"max_results", std::to_string(maxResults)},
431 const std::vector<std::pair<std::string, std::string> >
orderParameters = {
435 const std::vector<std::string> externalFields = {
"uid",
"sid",
"lang"};
436 const std::vector<std::string> orderFields = {
"uid",
"lang",
"sid"};
448 const std::string title =
"Test Get Result Use Offset";
452 std::set<unsigned long long> ids;
456 {
"queryId",
"7475839"},
459 {
"max_results", std::to_string(maxResults)},
460 {
"offset", std::to_string(offset)},
466 const std::vector<std::pair<std::string, std::string> >
orderParameters = {
470 const std::vector<std::string> externalFields = {
"uid",
"sid",
"lang"};
471 const std::vector<std::string> orderFields = {
"uid",
"lang",
"sid"};
476 for (
size_t i=0;i<requestInfoCount;++i)
479 std::cout <<
"TOTAL FOUND: " << totalFound <<
std::endl;
481 std::cout <<
"offset = " << offset <<
std::endl;
485 for (
size_t j=0;j<queryParameters.size();++j)
487 if (queryParameters[j].first==
"offset")
489 queryParameters[j].second = std::to_string(offset);
490 std::cout <<
"offset = " << queryParameters[j].second <<
std::endl;
493 if (queryParameters[j].first==
"max_results")
495 queryParameters[j].second = std::to_string(offset+maxResults);
496 std::cout <<
"max_results = " << queryParameters[j].second <<
std::endl;
501 std::cout <<
"MATCH INFO COUNT: " << matchInfoCount <<
std::endl;
502 for (
size_t q=0;q<matchInfoCount;++q)
506 for (
size_t q=0;q<requestInfoCount;++q)
509 std::cout <<
"TOTAL FOUND: " << totalFound <<
std::endl;
511 offset += (maxResults-1);
515 std::cout <<
"UNIQUE ID FOUND: " << ids.size() <<
std::endl;
527 unsigned int resultTime = 0;
539 printSuccess(
"Serialize SphinxInputJsonMessageSearch");
542 inputJsonMessage.
setType(SphinxInputJsonMessage::MessageType::mtSearch);
543 inputJsonMessage.
setData(json);
545 for (
size_t i=0;i<repeatCount;++i)
551 assert(!outputMessage.
isError());
552 resultTime += outputMessage.
getTime();;
566 ASSERT_FALSE(inputJsonMessage.
isError());
568 ASSERT_TRUE(inputJsonMessage.
getErrorMsg().empty());
569 printSuccess(
"Unserialize SphinxInputJsonMessage");
572 std::string dataString = inputJsonMessage.
getData();
581 ASSERT_FALSE(msgSearch.
isError());
584 printSuccess(
"Unserialize SphinxInputJsonMessageSearch");
586 std::string restoreString;
596 ASSERT_FALSE(msgSearch.
isError());
599 printSuccess(
"Serialize SphinxInputJsonMessageSearch");
601 std::string restoreJson;
603 restoreInputJsonMessage.
setType(type);
604 restoreInputJsonMessage.
setData(restoreString);
605 ret = restoreInputJsonMessage.
serialize(restoreJson);
611 ASSERT_FALSE(restoreInputJsonMessage.
isError());
613 ASSERT_TRUE(restoreInputJsonMessage.
getErrorMsg().empty());
614 printSuccess(
"Serialize SphinxInputJsonMessage");
620 ASSERT_FALSE(restoreSearch.isError());
621 ASSERT_TRUE(restoreSearch.getErrorCode()==
NO_ERROR);
622 ASSERT_TRUE(restoreSearch.getErrorMsg().empty());
623 printSuccess(
"Unserialize SphinxInputJsonMessageSearch");
626 ASSERT_EQ(msgSearch.
getQueryString(), restoreSearch.getQueryString());
628 std::vector<std::pair<std::string, std::string> >& sourceParams = msgSearch.
getQueryParameters();
629 std::vector<std::pair<std::string, std::string> >& restoreParams = restoreSearch.getQueryParameters();
639 ASSERT_EQ(sourceParams.size(), restoreParams.size());
642 for (
size_t i=0;i<sourceParams.size();++i)
645 for (
size_t k=0;k<restoreParams.size();++k)
646 if (sourceParams[i].first==restoreParams[k].first)
657 for (
size_t i=0;i<sourceFilters.
getFilters().size();++i)
659 ASSERT_EQ(sourceFilters.
getFilters()[i]->getFilterType(), restoreFilters.
getFilters()[i]->getFilterType());
660 ASSERT_EQ(sourceFilters.
getFilters()[i]->getAttributeName(), restoreFilters.
getFilters()[i]->getAttributeName());
661 ASSERT_EQ(sourceFilters.
getFilters()[i]->getExcludeValue(), restoreFilters.
getFilters()[i]->getExcludeValue());
662 ASSERT_EQ(sourceFilters.
getFilters()[i]->getAttributeValues().size(), restoreFilters.
getFilters()[i]->getAttributeValues().size());
663 for (
size_t k=0;k<sourceFilters.
getFilters()[i]->getAttributeValues().size();++k)
665 ASSERT_EQ(sourceFilters.
getFilters()[i]->getAttributeValues()[k], restoreFilters.
getFilters()[i]->getAttributeValues()[k]);
668 printSuccess(
"Test Serialization Search Process");
672 const std::string&
query, std::vector<Poco::SharedPtr<SphinxFilter> >& filters)
683 for (
size_t i=0;i<filters.size();++i)
684 inputJsonMessageSearch.
addFilter(filters[i]);
692 inputJsonMessage.
setType(SphinxInputJsonMessage::MessageType::mtSearch);
693 inputJsonMessage.
setData(json);
708 printSuccess(
"Search with filters");
710 return outputMessage;
718 const std::string
query =
"some";
721 std::vector<std::string> attrs = {
"123"};
728 printSuccess(
"Serialize SphinxInputJsonMessageSearch");
731 handler.
setType(SphinxInputJsonMessage::MessageType::mtSearch);
734 std::stringstream str;
739 std::string outputJson = fObj.
Process(str.str());
750 ASSERT_FALSE(outputMessage.
isError());
756 ASSERT_TRUE(ResultSerializator.
getErrorMsg().empty());
759 printSuccess(
"Test use filter to Search Process");
765 size_t fieldsCount = 0;
766 std::stringstream str;
767 for (
size_t i=45567;i<45570;++i)
773 std::string tmp = str.str();
775 for (
size_t i=0;i<=fieldsCount+1;++i)
777 std::string packedData = packer.
pack(tmp, i);
778 std::string unpackedData = packer.
unpack(packedData);
779 ASSERT_EQ(tmp, unpackedData);
782 tmp.resize(fieldsCount*fieldWidth+5);
783 for (
size_t i=0;i<=fieldsCount+1;++i)
785 std::string packetData = packer.
pack(tmp, i);
786 std::string unpackedData = packer.
unpack(packetData);
787 ASSERT_TRUE(tmp.compare(0, fieldsCount*fieldWidth, unpackedData)==0 || tmp==unpackedData);
790 tmp.resize(fieldsCount*fieldWidth-5);
791 for (
size_t i=0;i<=fieldsCount+1;++i)
793 std::string packetData = packer.
pack(tmp, i);
794 std::string unpackedData = packer.
unpack(packetData);
795 ASSERT_TRUE(unpackedData.compare(0, fieldsCount*fieldWidth-5, tmp)==0);
797 printSuccess(
"Test Weight Packer");
814 const std::vector<std::string> orderFields = {
"sid",
"lang",
"uid"};
817 calculator.
calculate(orderFields, resultData);
820 for (
size_t i=0;i<matchInfoCount;++i)
827 calculator.
calculate(orderFields, resultData);
829 for (
size_t i=0;i<matchInfoCount;++i)
832 printSuccess(
"Test Weight Calculator");