TEST(RepoBSONTest, CloneAndShrink)
{
	//shrinking a bson without any binary fields should yield an identical bson
	RepoBSON shrunkBson = testBson.cloneAndShrink();

	EXPECT_EQ(testBson, shrunkBson);
	EXPECT_EQ(testBson.getFilesMapping().size(), shrunkBson.getFilesMapping().size());
	
	mongo::BSONObjBuilder builder;
	std::vector < uint8_t > in, out, ref;
	
	size_t size = 100;

	in.resize(size);
	ref.resize(size);

	builder << "stringTest" << "hello";
	builder << "numTest" << 1.35;
	builder.appendBinData("binDataTest", in.size(), mongo::BinDataGeneral, in.data());

	std::unordered_map < std::string, std::pair<std::string, std::vector<uint8_t>>> mapping, outMapping;
	mapping["orgRef"] = std::pair<std::string, std::vector<uint8_t>>("blah", ref);

	RepoBSON binBson(builder.obj(), mapping);

	shrunkBson = binBson.cloneAndShrink();
	outMapping = shrunkBson.getFilesMapping();

	EXPECT_NE(shrunkBson, binBson);
	EXPECT_FALSE(shrunkBson.hasField("binDataTest"));
	EXPECT_EQ(2, outMapping.size());
	EXPECT_TRUE(outMapping.find("orgRef") != outMapping.end());

	//Check the binary still obtainable
	EXPECT_TRUE(shrunkBson.getBinaryFieldAsVector("binDataTest", out));


	ASSERT_EQ(in.size(), out.size());
	for (size_t i = 0; i < out.size(); ++i)
	{
		EXPECT_EQ(in[i], out[i]);
	}

	//Check the out referenced bigfile is still sane
	
	EXPECT_EQ(ref.size(), outMapping["orgRef"].second.size());
	for (size_t i = 0; i < ref.size(); ++i)
	{
		EXPECT_EQ(ref[i], outMapping["orgRef"].second[i]);
	}

}
TEST(RepoBSONTest, GetFilesMapping)
{
	std::vector < uint8_t > in;

	size_t size = 100;

	in.resize(size);

	std::unordered_map < std::string, std::pair<std::string, std::vector<uint8_t>>> mapping, outMapping;
	mapping["orgRef"] = std::pair<std::string, std::vector<uint8_t>>("blah", in);

	RepoBSON binBson(testBson, mapping);
	outMapping = binBson.getFilesMapping();

	EXPECT_EQ(1, outMapping.size());
	EXPECT_FALSE(outMapping.find("orgRef") == outMapping.end());


	EXPECT_EQ(0, testBson.getFilesMapping().size());
	EXPECT_EQ(0, emptyBson.getFilesMapping().size());
}
TEST(RepoBSONTest, Swap)
{
	RepoBSON test = testBson;

	//Test with bigfile mapping
	std::vector < uint8_t > in;

	in.resize(100);

	std::unordered_map<std::string, std::pair<std::string, std::vector<uint8_t>>> map, mapout;
	map["testingfile"] = std::pair<std::string, std::vector<uint8_t>>("blah", in);

	RepoBSON testDiff_org(BSON("entirely" << "different"), map);
	RepoBSON testDiff = testDiff_org;

	EXPECT_TRUE(testDiff_org.toString() == testDiff.toString());
	EXPECT_TRUE(testDiff_org.getFilesMapping().size() == testDiff.getFilesMapping().size());


	test.swap(testDiff);
	EXPECT_EQ(testDiff_org.toString(), test.toString());


	mapout = test.getFilesMapping();
	ASSERT_EQ(map.size(), mapout.size());

	auto mapIt = map.begin();
	auto mapoutIt = mapout.begin();

	for (; mapIt != map.end(); ++mapIt, ++mapoutIt)
	{
		EXPECT_EQ(mapIt->first, mapIt->first);
		EXPECT_EQ(mapIt->second.first, mapIt->second.first);
		std::vector<uint8_t> dataOut = mapoutIt->second.second;
		std::vector<uint8_t> dataIn = mapIt->second.second;
		EXPECT_EQ(dataIn.size(), dataOut.size());
		if (dataIn.size()>0)
			EXPECT_EQ(0, strncmp((char*)dataOut.data(), (char*)dataIn.data(), dataIn.size()));
	}
}
TEST(RepoBSONTest, AssignOperator)
{
	RepoBSON test = testBson;

	EXPECT_TRUE(test.toString() == testBson.toString());

	EXPECT_EQ(test.getFilesMapping().size(), testBson.getFilesMapping().size());
	
	//Test with bigfile mapping
	std::vector < uint8_t > in;

	in.resize(100);

	std::unordered_map<std::string, std::pair<std::string, std::vector<uint8_t>>> map, mapout;
	map["testingfile"] = std::pair<std::string, std::vector<uint8_t>>( "field", in);

	RepoBSON test2(testBson, map);


	mapout = test2.getFilesMapping();
	ASSERT_EQ(mapout.size(), map.size());
	
	auto mapIt = map.begin();
	auto mapoutIt = mapout.begin();

	for (; mapIt != map.end(); ++mapIt, ++mapoutIt)
	{
		EXPECT_EQ(mapIt->first, mapIt->first);
		EXPECT_EQ(mapIt->second.first, mapIt->second.first);
		std::vector<uint8_t> dataOut = mapoutIt->second.second;
		std::vector<uint8_t> dataIn = mapIt->second.second;
		EXPECT_EQ(dataOut.size(), dataIn.size());
		if (dataIn.size()>0)
			EXPECT_EQ(0, strncmp((char*)&dataOut[0], (char*)&dataIn[0], dataIn.size()));
	}

}
Exemple #5
0
RepoNode::RepoNode(RepoBSON bson,
	const std::unordered_map<std::string, std::pair<std::string, std::vector<uint8_t>>> &binMapping) : RepoBSON(bson, binMapping){
	if (binMapping.size() == 0)
		bigFiles = bson.getFilesMapping();
}