hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxResultBuilderTest.cpp
Go to the documentation of this file.
1 /*
2  * SphinxResultBuilderTest.cpp
3  *
4  * Created on: May 14, 2013
5  * Author: igor
6  */
7 #include <gtest/gtest.h>
9 #include "SphinxReduceResult.hpp"
11 #include "Exceptions.hpp"
12 
13 using namespace HCE::sphinx;
14 using namespace HCE::sphinx::reduce_task;
15 using namespace HCE::reduce;
16 using namespace std;
17 using namespace Poco;
18 
19 TEST(SphinxResultBuilder, buildFromEmptyData)
20 {
21  SharedPtr<SphinxReduceDataStorage>sphinxReduceDataStorage(new SphinxReduceDataStorage());
22  SharedPtr<SphinxReduceResult>sphinxReduceResult(new SphinxReduceResult);
23  vector<SphinxRequestInfo> requestInfo;
24 
25  SphinxResultBuilder sphinxResultBuilder;
26  SharedPtr<SphinxResultData> sphinxResult = sphinxResultBuilder.build(
27  sphinxReduceDataStorage, sphinxReduceResult, requestInfo);
28 
29  ASSERT_EQ(sphinxResult->getMatchInfoCount(), 0U);
30  ASSERT_EQ(sphinxResult->getRequestInfoCount(), 0U);
31 }
32 
33 
34 class SphinxResultBuilderTest: public ::testing::Test{
35  protected:
36  virtual void SetUp(){
37  nodeName = "node1";
38  query = "My query";
39  totalRes = 10;
40  totalFound = 2;
41  time = 1020;
42 
43  WordInfo word1 = WordInfo("word1", 10, 3);
44  matchInfo1 = buildSimpleMatchInfo(7LLU, 111LLU);
45  matchInfo2 = buildSimpleMatchInfo(9LLU, 222LLU);
46  matchInfo3 = buildSimpleMatchInfo(15LLU, 444LLU);
47  matchInfo4 = buildSimpleMatchInfo(3LLU, 99LLU);
48  AttrInfo attrInfo("first", "value");
49 
50  matchInfo1.addAttrInfo(attrInfo);
51  matchInfo2.addAttrInfo(attrInfo);
52  matchInfo3.addAttrInfo(attrInfo);
53  matchInfo4.addAttrInfo(attrInfo);
54 
55  sphinxRequestInfo.setQuery(query);
56  sphinxRequestInfo.setTotal(totalRes);
57  sphinxRequestInfo.setTotalFound(totalFound);
58  sphinxRequestInfo.setTimeMsec(time);
59  sphinxRequestInfo.addWordInfo(word1);
60 
61  sphinxResultData.assign(new SphinxResultData());
62  sphinxResultData->addMatchInfo(matchInfo1);
63  sphinxResultData->addMatchInfo(matchInfo2);
64  sphinxResultData->addMatchInfo(matchInfo3);
65  sphinxResultData->addMatchInfo(matchInfo4);
66  sphinxResultData->addRequestInfo(sphinxRequestInfo);
67 
68  expectedSelectResultData.addMatchInfo(matchInfo1);
69  expectedSelectResultData.addMatchInfo(matchInfo4);
70  expectedSelectResultData.addRequestInfo(sphinxRequestInfo);
71 
72  expectedCopyResultData = *sphinxResultData.get();
73 
74  sphinxReduceDataStorage.assign(new SphinxReduceDataStorage());
75  sphinxReduceResult.assign(new SphinxReduceResult());
76  }
77 
78  string nodeName;
79  string query;
80  int totalRes;
82  int time;
83  SharedPtr<SphinxResultData> sphinxResultData;
91 
92  vector<SphinxRequestInfo> requestInfo;
93  SharedPtr<SphinxReduceDataStorage>sphinxReduceDataStorage;
94  SharedPtr<SphinxReduceResult>sphinxReduceResult;
95 };
96 
97 
99 {
100  sphinxReduceDataStorage->addData(matchInfo1);
101  sphinxReduceDataStorage->addData(matchInfo2);
102  sphinxReduceDataStorage->addData(matchInfo3);
103  sphinxReduceDataStorage->addData(matchInfo4);
104 
105  sphinxReduceResult->addKeyValue(to_string(matchInfo1.getDocId()), matchInfo1.getWeight());
106  sphinxReduceResult->addKeyValue(to_string(matchInfo2.getDocId()), matchInfo2.getWeight());
107  sphinxReduceResult->addKeyValue(to_string(matchInfo3.getDocId()), matchInfo3.getWeight());
108  sphinxReduceResult->addKeyValue(to_string(matchInfo4.getDocId()), matchInfo4.getWeight());
109 
110  requestInfo.push_back(sphinxRequestInfo);
111 
112  SphinxResultBuilder sphinxResultBuilder;
113  SharedPtr<SphinxResultData> sphinxResult = sphinxResultBuilder.build(
114  sphinxReduceDataStorage, sphinxReduceResult, requestInfo);
115 
116  ASSERT_TRUE(isEqualSphinxResultData(*sphinxResult.get(), *sphinxResultData.get()));
117 }
118 
119 
120 TEST_F(SphinxResultBuilderTest, throwExceptionWhileResourceIsntPresentInDataStorage)
121 {
122  sphinxReduceDataStorage->addData(matchInfo1);
123  sphinxReduceDataStorage->addData(matchInfo2);
124 
125  sphinxReduceResult->addKeyValue(to_string(matchInfo1.getDocId()), matchInfo1.getWeight());
126  sphinxReduceResult->addKeyValue("fake key", "fake weight");
127 
128  SphinxResultBuilder sphinxResultBuilder;
129  ASSERT_THROW(sphinxResultBuilder.build(sphinxReduceDataStorage, sphinxReduceResult, requestInfo),
131 }
132 
133 
134 TEST_F(SphinxResultBuilderTest, selectFirstDataElementFromEquals)
135 {
136  SphinxMatchInfo matchInfo1Equal = buildSimpleMatchInfo(7LLU, 111LLU);
137 
138  sphinxReduceDataStorage->addData(matchInfo1);
139  sphinxReduceDataStorage->addData(matchInfo1Equal);
140 
141  sphinxReduceResult->addKeyValue(to_string(matchInfo1.getDocId()), matchInfo1.getWeight());
142 
143  SphinxResultBuilder sphinxResultBuilder;
144  SharedPtr<SphinxResultData> sphinxResult = sphinxResultBuilder.build(
145  sphinxReduceDataStorage, sphinxReduceResult, requestInfo);
146 
147  ASSERT_EQ(sphinxResult->getMatchInfoItem(0).getAttributes().size(), 1U);
148 }
149 
150 
151 TEST_F(SphinxResultBuilderTest, selectiveBuildBaseOnKeysList_emptyList)
152 {
153  vector<pair<string, unsigned long long> > weightKeyPairs;
154 
155  SphinxResultBuilder sphinxResultBuilder;
156  SharedPtr<SphinxResultData> sphinxResult = sphinxResultBuilder.buildFromUsingKeys(
157  sphinxResultData, weightKeyPairs);
158 
159  ASSERT_EQ(sphinxResult->getMatchInfoCount(), 0U);
160  ASSERT_EQ(sphinxResult->getRequestInfoCount(), 1U);
161 }
162 
163 
164 TEST_F(SphinxResultBuilderTest, selectiveBuildBaseOnKeysList)
165 {
166  vector<pair<string, unsigned long long> > weightKeyPairs;
167  weightKeyPairs.push_back(make_pair("111", 7LLU));
168  weightKeyPairs.push_back(make_pair("099", 3LLU));
169 
170  SphinxResultBuilder sphinxResultBuilder;
171  SharedPtr<SphinxResultData> sphinxResult = sphinxResultBuilder.buildFromUsingKeys(
172  sphinxResultData, weightKeyPairs);
173 
174  ASSERT_TRUE(isEqualSphinxResultData(*sphinxResult.get(), expectedSelectResultData));
175 }
176 
177 
178 TEST_F(SphinxResultBuilderTest, selectiveBuildBaseOnKeysList_copy)
179 {
180  vector<pair<string, unsigned long long> > weightKeyPairs;
181  weightKeyPairs.push_back(make_pair("111", 7LLU));
182  weightKeyPairs.push_back(make_pair("222", 9LLU));
183  weightKeyPairs.push_back(make_pair("444", 15LLU));
184  weightKeyPairs.push_back(make_pair("099", 3LLU));
185 
186  SphinxResultBuilder sphinxResultBuilder;
187  SharedPtr<SphinxResultData> sphinxResult = sphinxResultBuilder.buildFromUsingKeys(
188  sphinxResultData, weightKeyPairs);
189 
190  ASSERT_TRUE(isEqualSphinxResultData(*sphinxResult.get(), expectedCopyResultData));
191 }