2022-02-22 20:33:45 +08:00
|
|
|
// Copyright 2022 Memgraph Ltd.
|
2021-10-26 14:53:56 +08:00
|
|
|
//
|
|
|
|
// Use of this software is governed by the Business Source License
|
|
|
|
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
|
|
|
|
// License, and you may not use this file except in compliance with the Business Source License.
|
|
|
|
//
|
|
|
|
// As of the Change Date specified in that file, in accordance with
|
|
|
|
// the Business Source License, use of this software will be governed
|
|
|
|
// by the Apache License, Version 2.0, included in the file
|
|
|
|
// licenses/APL.txt.
|
|
|
|
|
2021-05-14 21:38:59 +08:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "query/serialization/property_value.hpp"
|
2021-06-21 22:42:58 +08:00
|
|
|
#include "storage/v2/temporal.hpp"
|
2021-05-14 21:38:59 +08:00
|
|
|
#include "utils/logging.hpp"
|
|
|
|
|
|
|
|
namespace {
|
2022-02-22 20:33:45 +08:00
|
|
|
void ExpectPropEq(const memgraph::storage::PropertyValue &a, const memgraph::storage::PropertyValue &b) {
|
2021-05-14 21:38:59 +08:00
|
|
|
ASSERT_EQ(a.type(), b.type());
|
|
|
|
ASSERT_EQ(a, b);
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
void CheckJsonConversion(const memgraph::storage::PropertyValue &property_value) {
|
|
|
|
const auto json_string = memgraph::query::serialization::SerializePropertyValue(property_value).dump();
|
2021-05-14 21:38:59 +08:00
|
|
|
const auto json_object = nlohmann::json::parse(json_string);
|
2022-02-22 20:33:45 +08:00
|
|
|
ExpectPropEq(property_value, memgraph::query::serialization::DeserializePropertyValue(json_object));
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
TEST(PropertyValueSerializationTest, Null) { CheckJsonConversion(memgraph::storage::PropertyValue{}); }
|
2021-05-14 21:38:59 +08:00
|
|
|
|
|
|
|
TEST(PropertyValueSerializationTest, Bool) {
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{true});
|
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{false});
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PropertyValueSerializationTest, Int) {
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{1});
|
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{100});
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PropertyValueSerializationTest, Double) {
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{1.0});
|
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{2.321});
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PropertyValueSerializationTest, String) {
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{"TestString"});
|
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{""});
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 22:42:58 +08:00
|
|
|
TEST(PropertyValueSerializationTest, TemporalData) {
|
|
|
|
const auto test_temporal_data_conversion = [](const auto type, const auto microseconds) {
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{memgraph::storage::TemporalData{type, microseconds}});
|
2021-06-21 22:42:58 +08:00
|
|
|
};
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
test_temporal_data_conversion(memgraph::storage::TemporalType::Date, 20);
|
|
|
|
test_temporal_data_conversion(memgraph::storage::TemporalType::LocalDateTime, -20);
|
|
|
|
test_temporal_data_conversion(memgraph::storage::TemporalType::Duration, 10000);
|
2021-06-21 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:38:59 +08:00
|
|
|
namespace {
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
std::vector<memgraph::storage::PropertyValue> GetPropertyValueListWithBasicTypes() {
|
|
|
|
return {memgraph::storage::PropertyValue{}, memgraph::storage::PropertyValue{true},
|
|
|
|
memgraph::storage::PropertyValue{"string"}, memgraph::storage::PropertyValue{1},
|
|
|
|
memgraph::storage::PropertyValue{1.0}};
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
std::map<std::string, memgraph::storage::PropertyValue> GetPropertyValueMapWithBasicTypes() {
|
|
|
|
return {{"null", memgraph::storage::PropertyValue{}},
|
|
|
|
{"bool", memgraph::storage::PropertyValue{true}},
|
|
|
|
{"int", memgraph::storage::PropertyValue{1}},
|
|
|
|
{"double", memgraph::storage::PropertyValue{1.0}},
|
|
|
|
{"string", memgraph::storage::PropertyValue{"string"}}};
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(PropertyValueSerializationTest, List) {
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::storage::PropertyValue list = memgraph::storage::PropertyValue{GetPropertyValueListWithBasicTypes()};
|
2021-05-14 21:38:59 +08:00
|
|
|
|
2021-06-21 22:42:58 +08:00
|
|
|
{
|
|
|
|
SCOPED_TRACE("Basic list");
|
|
|
|
CheckJsonConversion(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("Nested list");
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{std::vector<memgraph::storage::PropertyValue>{list, list}});
|
2021-06-21 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("List with map");
|
|
|
|
list.ValueList().emplace_back(GetPropertyValueMapWithBasicTypes());
|
|
|
|
CheckJsonConversion(list);
|
|
|
|
}
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PropertyValueSerializationTest, Map) {
|
|
|
|
auto map = GetPropertyValueMapWithBasicTypes();
|
2021-06-21 22:42:58 +08:00
|
|
|
{
|
|
|
|
SCOPED_TRACE("Basic map");
|
2022-02-22 20:33:45 +08:00
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{map});
|
2021-06-21 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("Nested map");
|
2022-02-22 20:33:45 +08:00
|
|
|
map.emplace("map", memgraph::storage::PropertyValue{map});
|
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{map});
|
2021-06-21 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("Map with list");
|
2022-02-22 20:33:45 +08:00
|
|
|
map.emplace("list", memgraph::storage::PropertyValue{GetPropertyValueListWithBasicTypes()});
|
|
|
|
CheckJsonConversion(memgraph::storage::PropertyValue{map});
|
2021-06-21 22:42:58 +08:00
|
|
|
}
|
2021-05-14 21:38:59 +08:00
|
|
|
}
|