22 #include "robot_memory_test.h"
24 #include <interfaces/Position3DInterface.h>
27 #include <bsoncxx/exception/exception.hpp>
30 #include <mongocxx/exception/exception.hpp>
33 using namespace mongocxx;
56 ASSERT_FALSE(robot_memory == NULL);
62 robot_memory->insert(bsoncxx::from_json(
"{'insert':'something to have the namespace'}")));
63 auto qres = robot_memory->query(bsoncxx::from_json(
"{somekey:'should_not_exist'}"));
64 ASSERT_EQ(qres.begin(), qres.end());
69 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testkey':'value'}")));
70 auto qres = robot_memory->query(bsoncxx::from_json(
"{'testkey':'value'}"));
71 ASSERT_TRUE(contains_pairs(qres, bsoncxx::from_json(
"{'testkey':'value'}")));
77 robot_memory->insert(bsoncxx::from_json(
"{'testkey':'value'}"),
"robmem.othercollection"));
79 robot_memory->query(bsoncxx::from_json(
"{'testkey':'value'}"),
"robmem.othercollection");
80 ASSERT_TRUE(contains_pairs(qres, bsoncxx::from_json(
"{'testkey':'value'}")));
85 ASSERT_TRUE(robot_memory->insert(
"{'inserting':'something',as:0.5}"));
87 robot_memory->update(bsoncxx::from_json(
"{'inserting':'something',as:0.5}"),
88 bsoncxx::from_json(
"{'updated':'something',as:3.0,extra:true}")));
89 auto qres = robot_memory->query(bsoncxx::from_json(
"{'updated':'something'}"));
91 contains_pairs(qres, bsoncxx::from_json(
"{'updated':'something',as:3.0,extra:true}")));
96 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{to_be:'removed'}")));
97 ASSERT_TRUE(robot_memory->remove(bsoncxx::from_json(
"{to_be:'removed'}")));
98 auto qres = robot_memory->query(bsoncxx::from_json(
"{to_be:'removed'}"));
99 ASSERT_EQ(qres.begin(), qres.end());
104 ASSERT_TRUE(robot_memory->update(bsoncxx::from_json(
"{upsert:'not existing'}"),
105 bsoncxx::from_json(
"{upsert:'should not exist'}"),
108 auto qres = robot_memory->query(bsoncxx::from_json(
"{upsert:'should not exist'}"));
109 ASSERT_EQ(qres.begin(), qres.end());
110 ASSERT_TRUE(robot_memory->update(bsoncxx::from_json(
"{upsert:'not existing'}"),
111 bsoncxx::from_json(
"{upsert:'should exist'}"),
114 qres = robot_memory->query(bsoncxx::from_json(
"{upsert:'should exist'}"));
115 ASSERT_NE(qres.begin(), qres.end());
120 ASSERT_THROW(robot_memory->query(bsoncxx::from_json(
"{key-:+'not existing'}")),
126 ASSERT_THROW(robot_memory->insert(bsoncxx::from_json(
"{'testkey'::'value'}")),
128 ASSERT_THROW(robot_memory->insert(bsoncxx::from_json(
"warbagarbl")), bsoncxx::exception);
133 ASSERT_THROW(robot_memory->update(bsoncxx::from_json(
"{'testkey':'good'}"),
134 bsoncxx::from_json(
"{'bad':1.2.3}")),
136 ASSERT_THROW(robot_memory->update(bsoncxx::from_json(
"{([})]"), bsoncxx::from_json(
"{'key':4}")),
142 ASSERT_THROW(robot_memory->remove(bsoncxx::from_json(
"{___:4.56!}")), bsoncxx::exception);
143 ASSERT_THROW(robot_memory->remove(bsoncxx::from_json(
"{([})]")), bsoncxx::exception);
163 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testname':'js-query',a:1,b:2}")));
164 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testname':'js-query',a:2,b:4}")));
165 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testname':'js-query',a:3,b:5}")));
166 auto qres = robot_memory->query(
167 bsoncxx::from_json(
"{'testname':'js-query', $where: \"return obj.a * 2 == obj.b\"}"));
168 ASSERT_NE(qres.begin(), qres.end());
169 ASSERT_NE(qres.begin(), qres.end());
170 ASSERT_EQ(qres.begin(), qres.end());
175 ASSERT_TRUE(robot_memory->drop_collection(
"robmem.test"));
176 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testkey':'value',v:1}")));
177 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testkey':'value',v:2}")));
178 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testkey':'value',v:3}")));
179 ASSERT_TRUE(robot_memory->dump_collection(
"robmem.test"));
180 ASSERT_TRUE(robot_memory->drop_collection(
"robmem.test"));
181 ASSERT_TRUE(robot_memory->restore_collection(
"robmem.test"));
182 auto qres = robot_memory->query(bsoncxx::from_json(
"{'testkey':'value'}"));
183 std::list<int> values = {3, 2, 1};
184 for (
auto __attribute__((unused)) i : values) {
185 auto doc = qres.begin();
186 ASSERT_NE(doc, qres.end());
187 int got = (*doc)[
"v"].get_int64();
188 ASSERT_TRUE(std::find(values.begin(), values.end(), got) != values.end());
191 ASSERT_EQ(0, values.size());
192 ASSERT_EQ(qres.begin(), qres.end());
199 EventTrigger *trigger1 = robot_memory->register_trigger(bsoncxx::from_json(
"{test:1}"),
203 EventTrigger *trigger2 = robot_memory->register_trigger(bsoncxx::from_json(
"{test:2}"),
207 robot_memory->insert(bsoncxx::from_json(
"{test:0, updateid:55}"),
"robmem.test");
208 robot_memory->insert(bsoncxx::from_json(
"{test:1, updateid:42}"),
"robmem.test");
209 robot_memory->update(bsoncxx::from_json(
"{updateid:42}"),
210 bsoncxx::from_json(
"{test:2, updateid:42}"),
218 robot_memory->remove_trigger(trigger1);
219 robot_memory->remove_trigger(trigger2);
226 EventTrigger *trigger1 = robot_memory->register_trigger(bsoncxx::from_json(
"{test:1}"),
230 EventTrigger *trigger2 = robot_memory->register_trigger(bsoncxx::from_json(
"{test:2}"),
235 robot_memory->insert(bsoncxx::from_json(
"{test:0, updateid:55}"),
"syncedrobmem.test");
236 robot_memory->insert(bsoncxx::from_json(
"{test:1, updateid:42}"),
"syncedrobmem.test");
237 robot_memory->update(bsoncxx::from_json(
"{updateid:42}"),
238 bsoncxx::from_json(
"{test:2, updateid:42}"),
239 "syncedrobmem.test");
246 robot_memory->remove_trigger(trigger1);
247 robot_memory->remove_trigger(trigger2);
256 ::testing::AssertionResult
258 const bsoncxx::document::view_or_value &exp)
260 for (
auto expected_element : exp.view()) {
261 if (obj.view().find(expected_element.key()) == obj.view().end()
262 || obj.view()[expected_element.key()].get_value() != expected_element.get_value()) {
263 return ::testing::AssertionFailure()
264 << bsoncxx::to_json(obj) <<
" does not include {"
265 << bsoncxx::to_json(expected_element.get_document()) <<
"}";
268 return ::testing::AssertionSuccess();
278 ::testing::AssertionResult
280 const bsoncxx::document::view_or_value &exp)
282 bsoncxx::builder::basic::document doc;
283 for (
auto e : cursor) {
284 doc.append(bsoncxx::builder::concatenate(e));
286 return contains_pairs(doc.extract(), exp);
293 robot_memory->insert(
"{'testname':'mapreduce',order:1, product:1, amount:1}",
"robmem.test"));
295 robot_memory->insert(
"{'testname':'mapreduce',order:2, product:1, amount:2}",
"robmem.test"));
297 robot_memory->insert(
"{'testname':'mapreduce',order:3, product:2, amount:3}",
"robmem.test"));
299 robot_memory->insert(
"{'testname':'mapreduce',order:4, product:2, amount:4}",
"robmem.test"));
300 ASSERT_TRUE(robot_memory->insert(
"{'testname':'not mapreduce',order:1, product:1, amount:2}"));
301 auto res = robot_memory->mapreduce(bsoncxx::from_json(
"{'testname':'mapreduce'}"),
303 "function() { emit( this.product, this.amount);}",
304 "function(key, values) { return Array.sum( values )}");
305 ASSERT_TRUE(contains_pairs(
307 bsoncxx::from_json(
"{ok: 1.0, results:[{_id:1.0, value:3.0}, {_id:2.0, value: 7.0}]}")));
313 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testname':'agg', v:1}"),
"robmem.test"));
314 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testname':'agg', v:333}"),
"robmem.test"));
315 ASSERT_TRUE(robot_memory->insert(bsoncxx::from_json(
"{'testname':'agg', v:-20}"),
"robmem.test"));
317 robot_memory->insert(bsoncxx::from_json(
"{'testname':'not agg', v:666}"),
"robmem.test"));
318 auto qres = robot_memory->aggregate(
319 bsoncxx::from_json(
"[{$match:{testname:'agg'}}, {$group: {_id:null, max:{$max: '$v'}}}]"),
321 ASSERT_TRUE(contains_pairs(qres, bsoncxx::from_json(
"{max: 333}")));
327 Computable * comp = robot_memory->register_computable(bsoncxx::from_json(
"{somekey:'value'}"),
331 robot_memory->remove_computable(comp);
337 Computable * comp = robot_memory->register_computable(bsoncxx::from_json(
"{computed:true}"),
341 auto qres = robot_memory->query(bsoncxx::from_json(
"{computed:true}"),
"robmem.test");
342 ASSERT_TRUE(contains_pairs(qres, bsoncxx::from_json(
"{result:'this is computed'}")));
343 robot_memory->remove_computable(comp);
350 robot_memory->register_computable(bsoncxx::from_json(
351 "{compute:'sum',x:{$exists:true},y:{$exists:true}}"),
355 auto qres = robot_memory->query(bsoncxx::from_json(
"{compute:'sum',x:15,y:4}"),
"robmem.test");
356 ASSERT_TRUE(contains_pairs(qres, bsoncxx::from_json(
"{sum:19}")));
357 robot_memory->remove_computable(comp);
363 Computable *comp = robot_memory->register_computable(bsoncxx::from_json(
"{compute:'multiple'}"),
367 auto qres = robot_memory->query(bsoncxx::from_json(
"{compute:'multiple'}"),
"robmem.test");
368 std::list<int> values = {3, 2, 1};
369 for (
auto __attribute__((unused)) i : values) {
370 auto doc = qres.begin();
371 ASSERT_NE(doc, qres.end());
372 int got = (*doc)[
"count"].get_int32();
373 ASSERT_TRUE(std::find(values.begin(), values.end(), got) != values.end());
376 ASSERT_EQ(0, values.size());
377 ASSERT_EQ(qres.begin(), qres.end());
378 robot_memory->remove_computable(comp);
390 robot_memory->query(bsoncxx::from_json(
"{interface:'Position3DInterface',id:'test1'}"),
391 "robmem.blackboard");
394 bsoncxx::from_json(
"{interface:'Position3DInterface',id:'test1',frame:'test_"
395 "frame',translation:[1.1, 2.2, 3.3]}")));
396 blackboard->
close(if3d);
407 auto qres = robot_memory->query(bsoncxx::from_json(
"{interface:'Position3DInterface',id:'test'}"),
408 "robmem.blackboard");
409 ASSERT_TRUE(contains_pairs(qres, bsoncxx::from_json(
"{interface:'Position3DInterface'}")));
410 blackboard->
close(if3d);
411 blackboard->
close(if3d_2);
416 robot_memory->insert(bsoncxx::from_json(
417 "{name:'test pos', frame:'cam_tag', translation:[0.0, 0.0, 0.0], "
418 "rotation:[0.0, 0.0, 0.0, 1.0]}"),
421 robot_memory->query(bsoncxx::from_json(
"{name:'test pos', frame:'base_link', allow_tf:true}"),
423 auto res = *(qres.begin());
424 ASSERT_EQ(
"base_link", res[
"frame"].get_utf8().value.to_string());
425 bsoncxx::array::view trans_view{res[
"translation"].get_array()};
426 ASSERT_TRUE(fabs(0.1 - trans_view[0].get_double()) < 0.001);
427 bsoncxx::array::view rot_view{res[
"rotation"].get_array()};
428 ASSERT_TRUE(fabs(-0.5 - rot_view[0].get_double()) < 0.001);