1
0
Fork 0
arangodb/tests/IResearch/IResearchViewSort-test.cpp

319 lines
11 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2019 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Andrey Abramov
/// @author Vasiliy Nabatchikov
////////////////////////////////////////////////////////////////////////////////
#include "gtest/gtest.h"
#include "IResearch/IResearchViewSort.h"
#include "velocypack/Iterator.h"
#include "velocypack/Parser.h"
TEST(IResearchViewSortTest, test_defaults) {
arangodb::iresearch::IResearchViewSort sort;
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
arangodb::velocypack::Builder builder;
EXPECT_TRUE(!sort.toVelocyPack(builder));
{
arangodb::velocypack::ArrayBuilder arrayScope(&builder);
EXPECT_TRUE(sort.toVelocyPack(builder));
}
auto slice = builder.slice();
EXPECT_TRUE(slice.isArray());
EXPECT_TRUE(0 == slice.length());
}
TEST(IResearchViewSortTest, equality) {
arangodb::iresearch::IResearchViewSort lhs;
lhs.emplace_back({{"some", false}, {"Nested", false}, {"field", false}}, true);
lhs.emplace_back({{"another", false}, {"field", false}}, false);
lhs.emplace_back({{"simpleField", false}}, true);
EXPECT_TRUE(lhs == lhs);
EXPECT_TRUE(lhs != arangodb::iresearch::IResearchViewSort());
{
arangodb::iresearch::IResearchViewSort rhs;
rhs.emplace_back({{"some", false}, {"Nested", false}, {"field", false}}, true);
rhs.emplace_back({{"another", false}, {"field", false}}, false);
EXPECT_TRUE(lhs != rhs);
}
{
arangodb::iresearch::IResearchViewSort rhs;
rhs.emplace_back({{"some", false}, {"Nested", false}, {"field", false}}, true);
rhs.emplace_back({{"another", false}, {"field", false}}, false);
rhs.emplace_back({{"simpleField", false}}, false);
EXPECT_TRUE(lhs != rhs);
}
{
arangodb::iresearch::IResearchViewSort rhs;
rhs.emplace_back({{"some", false}, {"Nested", false}, {"field", false}}, true);
rhs.emplace_back({{"another", false}, {"fielD", false}}, false);
rhs.emplace_back({{"simpleField", false}}, true);
EXPECT_TRUE(lhs != rhs);
}
}
TEST(IResearchViewSortTest, deserialize) {
arangodb::iresearch::IResearchViewSort sort;
{
auto json = arangodb::velocypack::Parser::fromJson("{ }");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE(errorField.empty());
}
{
auto json = arangodb::velocypack::Parser::fromJson("[ ]");
std::string errorField;
EXPECT_TRUE(true == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE(errorField.empty());
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
}
{
auto json = arangodb::velocypack::Parser::fromJson("[ [ ] ]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE("[0]" == errorField);
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
}
{
auto json = arangodb::velocypack::Parser::fromJson("[ { } ]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE("[0]" == errorField);
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ { \"field\": \"my.nested.field\" } ]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE("[0]" == errorField);
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ { \"field\": \"my.nested.field\", \"direction\": \"asc\" } ]");
std::string errorField;
EXPECT_TRUE(true == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE(errorField.empty());
EXPECT_TRUE(!sort.empty());
EXPECT_TRUE(1 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
EXPECT_TRUE((sort.field(0) == std::vector<arangodb::basics::AttributeName>{
{"my", false}, {"nested", false}, {"field", false}}));
EXPECT_TRUE(true == sort.direction(0));
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ \
{ \"field\": \"my.nested.field\", \"direction\": \"asc\" }, \
{ \"field\": \"my.nested.field\", \"direction\": \"desc\" }, \
{ \"field\": \"another.nested.field\", \"asc\": false }, \
{ \"field\": \"yet.another.nested.field\", \"asc\": true } \
]");
std::string errorField;
EXPECT_TRUE(true == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_TRUE(errorField.empty());
EXPECT_TRUE(!sort.empty());
EXPECT_TRUE(4 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
EXPECT_TRUE((std::vector<arangodb::basics::AttributeName>{
{"my", false}, {"nested", false}, {"field", false}} == sort.field(0)));
EXPECT_TRUE(true == sort.direction(0));
EXPECT_TRUE((std::vector<arangodb::basics::AttributeName>{
{"my", false}, {"nested", false}, {"field", false}} == sort.field(1)));
EXPECT_TRUE(false == sort.direction(1));
EXPECT_TRUE((std::vector<arangodb::basics::AttributeName>{{"another", false},
{"nested", false},
{"field", false}} ==
sort.field(2)));
EXPECT_TRUE(false == sort.direction(2));
EXPECT_TRUE((std::vector<arangodb::basics::AttributeName>{
{"yet", false}, {"another", false}, {"nested", false}, {"field", false}} ==
sort.field(3)));
EXPECT_TRUE(true == sort.direction(3));
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ \
{ \"field\": 1, \"direction\": \"asc\" }, \
{ \"field\": \"my.nested.field\", \"direction\": \"desc\" }, \
{ \"field\": \"another.nested.field\", \"asc\": false }, \
{ \"field\": \"yet.another.nested.field\", \"asc\": true } \
]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_EQ("[0].field", errorField);
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ \
{ \"field\": \"my.nested.field\", \"direction\": \"dasc\" }, \
{ \"field\": \"my.nested.field\", \"direction\": \"desc\" }, \
{ \"field\": \"another.nested.field\", \"asc\": false }, \
{ \"field\": \"yet.another.nested.field\", \"asc\": true } \
]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_EQ("[0].direction", errorField);
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ \
{ \"field\": \"my.nested.field\", \"direction\": \"asc\" }, \
{ \"field\": \"my.nested.field\", \"direction\": \"fdesc\" }, \
{ \"field\": \"another.nested.field\", \"asc\": false }, \
{ \"field\": \"yet.another.nested.field\", \"asc\": true } \
]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_EQ("[1].direction", errorField);
}
{
auto json = arangodb::velocypack::Parser::fromJson(
"[ \
{ \"field\": \"my.nested.field\", \"direction\": \"asc\" }, \
{ \"field\": \"my.nested.field\", \"direction\": \"desc\" }, \
{ \"field\": \"another.nested.field\", \"asc\": \"false\" }, \
{ \"field\": \"yet.another.nested.field\", \"asc\": true } \
]");
std::string errorField;
EXPECT_TRUE(false == sort.fromVelocyPack(json->slice(), errorField));
EXPECT_EQ("[2].asc", errorField);
}
}
TEST(IResearchViewSortTest, serialize) {
arangodb::iresearch::IResearchViewSort sort;
sort.emplace_back({{"some", false}, {"Nested", false}, {"field", false}}, true);
sort.emplace_back({{"another", false}, {"field", false}}, false);
sort.emplace_back({{"simpleField", false}}, true);
EXPECT_TRUE(!sort.empty());
EXPECT_TRUE(3 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
arangodb::velocypack::Builder builder;
EXPECT_TRUE(!sort.toVelocyPack(builder));
{
arangodb::velocypack::ArrayBuilder arrayScope(&builder);
EXPECT_TRUE(sort.toVelocyPack(builder));
}
auto slice = builder.slice();
EXPECT_TRUE(slice.isArray());
EXPECT_TRUE(3 == slice.length());
arangodb::velocypack::ArrayIterator it(slice);
EXPECT_TRUE(it.valid());
{
auto sortSlice = *it;
EXPECT_TRUE(sortSlice.isObject());
EXPECT_TRUE(2 == sortSlice.length());
auto const fieldSlice = sortSlice.get("field");
EXPECT_TRUE(fieldSlice.isString());
EXPECT_TRUE("some.Nested.field" == fieldSlice.copyString());
auto const directionSlice = sortSlice.get("asc");
EXPECT_TRUE(directionSlice.isBoolean());
EXPECT_TRUE(directionSlice.getBoolean());
}
it.next();
EXPECT_TRUE(it.valid());
{
auto sortSlice = *it;
EXPECT_TRUE(sortSlice.isObject());
EXPECT_TRUE(2 == sortSlice.length());
auto const fieldSlice = sortSlice.get("field");
EXPECT_TRUE(fieldSlice.isString());
EXPECT_TRUE("another.field" == fieldSlice.copyString());
auto const directionSlice = sortSlice.get("asc");
EXPECT_TRUE(directionSlice.isBoolean());
EXPECT_TRUE(!directionSlice.getBoolean());
}
it.next();
EXPECT_TRUE(it.valid());
{
auto sortSlice = *it;
EXPECT_TRUE(sortSlice.isObject());
EXPECT_TRUE(2 == sortSlice.length());
auto const fieldSlice = sortSlice.get("field");
EXPECT_TRUE(fieldSlice.isString());
EXPECT_TRUE("simpleField" == fieldSlice.copyString());
auto const directionSlice = sortSlice.get("asc");
EXPECT_TRUE(directionSlice.isBoolean());
EXPECT_TRUE(directionSlice.getBoolean());
}
it.next();
EXPECT_TRUE(!it.valid());
sort.clear();
EXPECT_TRUE(sort.empty());
EXPECT_TRUE(0 == sort.size());
EXPECT_TRUE(sort.memory() > 0);
{
arangodb::velocypack::Builder builder;
EXPECT_TRUE(!sort.toVelocyPack(builder));
{
arangodb::velocypack::ArrayBuilder arrayScope(&builder);
EXPECT_TRUE(sort.toVelocyPack(builder));
}
auto slice = builder.slice();
EXPECT_TRUE(slice.isArray());
EXPECT_TRUE(0 == slice.length());
}
}