memgraph/tests/unit/property_value_store.cpp
florijan 36fb7f9554 PropertyValueStore - remove Accept function (never used, never will be)
Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D781
2017-09-13 10:11:55 +02:00

146 lines
4.2 KiB
C++

//
// Copyright 2017 Memgraph
// Created by Florijan Stamenkovic on 24.01.17..
//
#include <vector>
#include "gtest/gtest.h"
#include "storage/property_value.hpp"
#include "storage/property_value_store.hpp"
using std::string;
TEST(PropertyValueStore, At) {
std::string some_string = "something";
PropertyValueStore<> props;
EXPECT_EQ(PropertyValue(props.at(0)).type(), PropertyValue::Type::Null);
props.set(0, some_string);
EXPECT_EQ(PropertyValue(props.at(0)).Value<string>(), some_string);
props.set(120, 42);
EXPECT_EQ(PropertyValue(props.at(120)).Value<int64_t>(), 42);
}
TEST(PropertyValueStore, AtNull) {
PropertyValueStore<> props;
EXPECT_EQ(props.at(0).type(), PropertyValue::Type::Null);
EXPECT_EQ(props.at(100).type(), PropertyValue::Type::Null);
// set one prop and test it's not null
props.set(0, true);
EXPECT_NE(props.at(0).type(), PropertyValue::Type::Null);
EXPECT_EQ(props.at(100).type(), PropertyValue::Type::Null);
}
TEST(PropertyValueStore, SetNull) {
PropertyValueStore<> props;
props.set(11, PropertyValue::Null);
EXPECT_EQ(0, props.size());
}
TEST(PropertyValueStore, Remove) {
// set some props
PropertyValueStore<> props;
props.set(11, "a");
props.set(30, "b");
EXPECT_NE(props.at(11).type(), PropertyValue::Type::Null);
EXPECT_NE(props.at(30).type(), PropertyValue::Type::Null);
EXPECT_EQ(props.size(), 2);
props.erase(11);
EXPECT_EQ(props.size(), 1);
EXPECT_EQ(props.at(11).type(), PropertyValue::Type::Null);
EXPECT_EQ(props.erase(30), 1);
EXPECT_EQ(props.size(), 0);
EXPECT_EQ(props.at(30).type(), PropertyValue::Type::Null);
EXPECT_EQ(props.erase(1000), 0);
}
TEST(PropertyValueStore, Clear) {
// set some props
PropertyValueStore<> props;
EXPECT_EQ(props.size(), 0);
props.clear();
EXPECT_EQ(props.size(), 0);
props.set(11, "a");
props.set(30, "b");
EXPECT_EQ(props.size(), 2);
props.clear();
EXPECT_EQ(props.size(), 0);
}
TEST(PropertyValueStore, Replace) {
PropertyValueStore<> props;
props.set(10, 42);
EXPECT_EQ(props.at(10).Value<int64_t>(), 42);
props.set(10, 0.25f);
EXPECT_EQ(props.at(10).type(), PropertyValue::Type::Double);
EXPECT_FLOAT_EQ(props.at(10).Value<double>(), 0.25);
}
TEST(PropertyValueStore, Size) {
PropertyValueStore<> props;
EXPECT_EQ(props.size(), 0);
props.set(0, "something");
EXPECT_EQ(props.size(), 1);
props.set(0, true);
EXPECT_EQ(props.size(), 1);
props.set(1, true);
EXPECT_EQ(props.size(), 2);
for (int i = 0; i < 100; ++i) props.set(i + 20, true);
EXPECT_EQ(props.size(), 102);
props.erase(0);
EXPECT_EQ(props.size(), 101);
props.erase(0);
EXPECT_EQ(props.size(), 101);
props.erase(1);
EXPECT_EQ(props.size(), 100);
}
TEST(PropertyValueStore, InsertRetrieveList) {
PropertyValueStore<> props;
props.set(0, std::vector<PropertyValue>{1, true, 2.5, "something",
PropertyValue::Null});
auto p = props.at(0);
EXPECT_EQ(p.type(), PropertyValue::Type::List);
auto l = p.Value<std::vector<PropertyValue>>();
EXPECT_EQ(l.size(), 5);
EXPECT_EQ(l[0].type(), PropertyValue::Type::Int);
EXPECT_EQ(l[0].Value<int64_t>(), 1);
EXPECT_EQ(l[1].type(), PropertyValue::Type::Bool);
EXPECT_EQ(l[1].Value<bool>(), true);
EXPECT_EQ(l[2].type(), PropertyValue::Type::Double);
EXPECT_EQ(l[2].Value<double>(), 2.5);
EXPECT_EQ(l[3].type(), PropertyValue::Type::String);
EXPECT_EQ(l[3].Value<std::string>(), "something");
EXPECT_EQ(l[4].type(), PropertyValue::Type::Null);
}
TEST(PropertyValueStore, InsertRetrieveMap) {
PropertyValueStore<> props;
props.set(0, std::map<std::string, PropertyValue>{
{"a", 1}, {"b", true}, {"c", "something"}});
auto p = props.at(0);
EXPECT_EQ(p.type(), PropertyValue::Type::Map);
auto m = p.Value<std::map<std::string, PropertyValue>>();
EXPECT_EQ(m.size(), 3);
auto get = [&m](const std::string &prop_name) {
return m.find(prop_name)->second;
};
EXPECT_EQ(get("a").type(), PropertyValue::Type::Int);
EXPECT_EQ(get("a").Value<int64_t>(), 1);
EXPECT_EQ(get("b").type(), PropertyValue::Type::Bool);
EXPECT_EQ(get("b").Value<bool>(), true);
EXPECT_EQ(get("c").type(), PropertyValue::Type::String);
EXPECT_EQ(get("c").Value<std::string>(), "something");
}