summaryrefslogtreecommitdiffstats
path: root/searchsummary
diff options
context:
space:
mode:
authorGeir Storli <geirst@verizonmedia.com>2020-06-03 14:24:42 +0000
committerGeir Storli <geirst@verizonmedia.com>2020-06-05 13:58:10 +0000
commitabec8585d5ee314d0fb0d951fbf45e6e9427be06 (patch)
tree18e285e0cd78a40bbbbf94f37d1a2c2b98fb26fc /searchsummary
parenta8d71c6f344374184e90db2f3832345895f33c75 (diff)
Simplify validation of slime docsum by comparing slime objects directly.
They also have nice pretty printing if some tests fail.
Diffstat (limited to 'searchsummary')
-rw-r--r--searchsummary/CMakeLists.txt1
-rw-r--r--searchsummary/src/tests/docsummary/attribute_combiner/attribute_combiner_test.cpp100
-rw-r--r--searchsummary/src/tests/docsummary/matched_elements_filter/matched_elements_filter_test.cpp14
-rw-r--r--searchsummary/src/vespa/searchsummary/test/CMakeLists.txt6
-rw-r--r--searchsummary/src/vespa/searchsummary/test/slime_value.h24
5 files changed, 63 insertions, 82 deletions
diff --git a/searchsummary/CMakeLists.txt b/searchsummary/CMakeLists.txt
index 2a23dd4c495..787925521ef 100644
--- a/searchsummary/CMakeLists.txt
+++ b/searchsummary/CMakeLists.txt
@@ -20,6 +20,7 @@ vespa_define_module(
src/vespa/searchsummary
src/vespa/searchsummary/config
src/vespa/searchsummary/docsummary
+ src/vespa/searchsummary/test
TESTS
src/tests/docsumformat
diff --git a/searchsummary/src/tests/docsummary/attribute_combiner/attribute_combiner_test.cpp b/searchsummary/src/tests/docsummary/attribute_combiner/attribute_combiner_test.cpp
index ae8e5f88c28..c64c5b2ed7d 100644
--- a/searchsummary/src/tests/docsummary/attribute_combiner/attribute_combiner_test.cpp
+++ b/searchsummary/src/tests/docsummary/attribute_combiner/attribute_combiner_test.cpp
@@ -14,6 +14,7 @@
#include <vespa/searchsummary/docsummary/docsumstate.h>
#include <vespa/searchsummary/docsummary/docsum_field_writer_state.h>
#include <vespa/searchsummary/docsummary/attribute_combiner_dfw.h>
+#include <vespa/searchsummary/test/slime_value.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>
@@ -23,8 +24,8 @@ LOG_SETUP("attribute_combiner_test");
using search::AttributeFactory;
using search::AttributeManager;
using search::AttributeVector;
-using search::IntegerAttribute;
using search::FloatingPointAttribute;
+using search::IntegerAttribute;
using search::MatchingElements;
using search::StringAttribute;
using search::attribute::BasicType;
@@ -37,41 +38,10 @@ using search::docsummary::GetDocsumsState;
using search::docsummary::GetDocsumsStateCallback;
using search::docsummary::IDocsumEnvironment;
using search::docsummary::IDocsumFieldWriter;
+using search::docsummary::test::SlimeValue;
namespace {
-vespalib::string
-toCompactJsonString(const vespalib::Slime &slime)
-{
- vespalib::SimpleBuffer buf;
- vespalib::slime::JsonFormat::encode(slime, buf, true);
- return buf.get().make_string();
-}
-
-struct FieldBlock {
- vespalib::string input;
- vespalib::Slime slime;
- search::RawBuf binary;
- vespalib::string json;
-
- explicit FieldBlock(const vespalib::string &jsonInput);
- ~FieldBlock();
- const char *data() const { return binary.GetDrainPos(); }
- size_t dataLen() const { return binary.GetUsedLen(); }
-};
-
-FieldBlock::FieldBlock(const vespalib::string &jsonInput)
- : input(jsonInput), slime(), binary(1024), json()
-{
- size_t used = vespalib::slime::JsonFormat::decode(jsonInput, slime);
- EXPECT_TRUE(used > 0);
- json = toCompactJsonString(slime);
- search::SlimeOutputRawBufAdapter adapter(binary);
- vespalib::slime::BinaryFormat::encode(slime, adapter);
-}
-
-FieldBlock::~FieldBlock() = default;
-
struct AttributeManagerFixture
{
AttributeManager mgr;
@@ -228,83 +198,73 @@ AttributeCombinerTest::set_field(const vespalib::string &field_name, bool filter
}
void
-AttributeCombinerTest::assertWritten(const vespalib::string &expectedJson, uint32_t docId)
+AttributeCombinerTest::assertWritten(const vespalib::string &exp_slime_as_json, uint32_t docId)
{
- vespalib::Slime target;
- vespalib::slime::SlimeInserter inserter(target);
+ vespalib::Slime act;
+ vespalib::slime::SlimeInserter inserter(act);
writer->insertField(docId, nullptr, &state, search::docsummary::RES_JSONSTRING, inserter);
- search::RawBuf binary(1024);
- vespalib::string json = toCompactJsonString(target);
- search::SlimeOutputRawBufAdapter adapter(binary);
- vespalib::slime::BinaryFormat::encode(target, adapter);
- FieldBlock block(expectedJson);
- EXPECT_EQ(block.dataLen(), binary.GetUsedLen());
- EXPECT_EQ(0, memcmp(block.data(), binary.GetDrainPos(), block.dataLen()));
- if (block.dataLen() != binary.GetUsedLen() ||
- memcmp(block.data(), binary.GetDrainPos(), block.dataLen()) != 0) {
- LOG(error, "Expected '%s'", expectedJson.c_str());
- LOG(error, "Expected normalized '%s'", block.json.c_str());
- LOG(error, "Got '%s'", json.c_str());
- }
+
+ SlimeValue exp(exp_slime_as_json);
+ EXPECT_EQ(exp.slime, act);
}
TEST_F(AttributeCombinerTest, require_that_attribute_combiner_dfw_generates_correct_slime_output_for_array_of_struct)
{
set_field("array", false);
- assertWritten("[ { fval: 110.0, name: \"n1.1\", val: 10}, { name: \"n1.2\", val: 11}]", 1);
- assertWritten("[ { fval: 120.0, name: \"n2\", val: 20}, { fval: 121.0, val: 21 }]", 2);
- assertWritten("[ { fval: 130.0, name: \"n3.1\", val: 30}, { fval: 131.0, name: \"n3.2\"} ]", 3);
- assertWritten("[ { }, { fval: 141.0, name: \"n4.2\", val: 41} ]", 4);
+ assertWritten("[ { fval: 110.0, name: 'n1.1', val: 10}, { name: 'n1.2', val: 11}]", 1);
+ assertWritten("[ { fval: 120.0, name: 'n2', val: 20}, { fval: 121.0, val: 21 }]", 2);
+ assertWritten("[ { fval: 130.0, name: 'n3.1', val: 30}, { fval: 131.0, name: 'n3.2'} ]", 3);
+ assertWritten("[ { }, { fval: 141.0, name: 'n4.2', val: 41} ]", 4);
assertWritten("null", 5);
}
TEST_F(AttributeCombinerTest, require_that_attribute_combiner_dfw_generates_correct_slime_output_for_map_of_struct)
{
set_field("smap", false);
- assertWritten("[ { key: \"k1.1\", value: { fval: 110.0, name: \"n1.1\", val: 10} }, { key: \"k1.2\", value: { name: \"n1.2\", val: 11} }]", 1);
- assertWritten("[ { key: \"k2\", value: { fval: 120.0, name: \"n2\", val: 20} }, { key: \"\", value: { fval: 121.0, val: 21 } }]", 2);
- assertWritten("[ { key: \"k3.1\", value: { fval: 130.0, name: \"n3.1\", val: 30} }, { key: \"k3.2\", value: { fval: 131.0, name: \"n3.2\"} } ]", 3);
- assertWritten("[ { key: \"\", value: { } }, { key: \"k4.2\", value: { fval: 141.0, name: \"n4.2\", val: 41} } ]", 4);
+ assertWritten("[ { key: 'k1.1', value: { fval: 110.0, name: 'n1.1', val: 10} }, { key: 'k1.2', value: { name: 'n1.2', val: 11} }]", 1);
+ assertWritten("[ { key: 'k2', value: { fval: 120.0, name: 'n2', val: 20} }, { key: '', value: { fval: 121.0, val: 21 } }]", 2);
+ assertWritten("[ { key: 'k3.1', value: { fval: 130.0, name: 'n3.1', val: 30} }, { key: 'k3.2', value: { fval: 131.0, name: 'n3.2'} } ]", 3);
+ assertWritten("[ { key: '', value: { } }, { key: 'k4.2', value: { fval: 141.0, name: 'n4.2', val: 41} } ]", 4);
assertWritten("null", 5);
}
TEST_F(AttributeCombinerTest, require_that_attribute_combiner_dfw_generates_correct_slime_output_for_map_of_string)
{
set_field("map", false);
- assertWritten("[ { key: \"k1.1\", value: \"n1.1\" }, { key: \"k1.2\", value: \"n1.2\"}]", 1);
- assertWritten("[ { key: \"k2\", value: \"\" }]", 2);
- assertWritten("[ { key: \"k3.1\", value: \"n3.1\" }, { key: \"\", value: \"n3.2\"} ]", 3);
- assertWritten("[ { key: \"\", value: \"\" }, { key: \"k4.2\", value: \"n4.2\" } ]", 4);
+ assertWritten("[ { key: 'k1.1', value: 'n1.1' }, { key: 'k1.2', value: 'n1.2'}]", 1);
+ assertWritten("[ { key: 'k2', value: '' }]", 2);
+ assertWritten("[ { key: 'k3.1', value: 'n3.1' }, { key: '', value: 'n3.2'} ]", 3);
+ assertWritten("[ { key: '', value: '' }, { key: 'k4.2', value: 'n4.2' } ]", 4);
assertWritten("null", 5);
}
TEST_F(AttributeCombinerTest, require_that_attribute_combiner_dfw_generates_correct_slime_output_for_filtered_array_of_struct)
{
set_field("array", true);
- assertWritten("[ { name: \"n1.2\", val: 11}]", 1);
+ assertWritten("[ { name: 'n1.2', val: 11}]", 1);
assertWritten("[ ]", 2);
- assertWritten("[ { fval: 130.0, name: \"n3.1\", val: 30} ]", 3);
- assertWritten("[ { fval: 141.0, name: \"n4.2\", val: 41} ]", 4);
+ assertWritten("[ { fval: 130.0, name: 'n3.1', val: 30} ]", 3);
+ assertWritten("[ { fval: 141.0, name: 'n4.2', val: 41} ]", 4);
assertWritten("null", 5);
}
TEST_F(AttributeCombinerTest, require_that_attribute_combiner_dfw_generates_correct_slime_output_for_filtered_map_of_struct)
{
set_field("smap", true);
- assertWritten("[ { key: \"k1.2\", value: { name: \"n1.2\", val: 11} }]", 1);
+ assertWritten("[ { key: 'k1.2', value: { name: 'n1.2', val: 11} }]", 1);
assertWritten("[ ]", 2);
- assertWritten("[ { key: \"k3.1\", value: { fval: 130.0, name: \"n3.1\", val: 30} } ]", 3);
- assertWritten("[ { key: \"k4.2\", value: { fval: 141.0, name: \"n4.2\", val: 41} } ]", 4);
+ assertWritten("[ { key: 'k3.1', value: { fval: 130.0, name: 'n3.1', val: 30} } ]", 3);
+ assertWritten("[ { key: 'k4.2', value: { fval: 141.0, name: 'n4.2', val: 41} } ]", 4);
assertWritten("null", 5);
}
TEST_F(AttributeCombinerTest, require_that_attribute_combiner_dfw_generates_correct_slime_output_for_filtered_map_of_string)
{
set_field("map", true);
- assertWritten("[ { key: \"k1.2\", value: \"n1.2\"}]", 1);
+ assertWritten("[ { key: 'k1.2', value: 'n1.2'}]", 1);
assertWritten("[ ]", 2);
- assertWritten("[ { key: \"k3.1\", value: \"n3.1\" } ]", 3);
- assertWritten("[ { key: \"k4.2\", value: \"n4.2\" } ]", 4);
+ assertWritten("[ { key: 'k3.1', value: 'n3.1' } ]", 3);
+ assertWritten("[ { key: 'k4.2', value: 'n4.2' } ]", 4);
assertWritten("null", 5);
}
diff --git a/searchsummary/src/tests/docsummary/matched_elements_filter/matched_elements_filter_test.cpp b/searchsummary/src/tests/docsummary/matched_elements_filter/matched_elements_filter_test.cpp
index ed91d4a88eb..0ac2f09e1b0 100644
--- a/searchsummary/src/tests/docsummary/matched_elements_filter/matched_elements_filter_test.cpp
+++ b/searchsummary/src/tests/docsummary/matched_elements_filter/matched_elements_filter_test.cpp
@@ -15,7 +15,7 @@
#include <vespa/searchsummary/docsummary/resultconfig.h>
#include <vespa/searchsummary/docsummary/resultpacker.h>
#include <vespa/searchsummary/docsummary/summaryfieldconverter.h>
-#include <vespa/vespalib/data/slime/json_format.h>
+#include <vespa/searchsummary/test/slime_value.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <iostream>
@@ -32,6 +32,7 @@ using search::attribute::CollectionType;
using search::attribute::Config;
using search::attribute::IAttributeContext;
using search::attribute::IAttributeVector;
+using search::docsummary::test::SlimeValue;
using vespalib::Slime;
using namespace document;
@@ -40,17 +41,6 @@ using namespace vespalib::slime;
using ElementVector = std::vector<uint32_t>;
-struct SlimeValue {
- Slime slime;
-
- SlimeValue(const std::string& json_input)
- : slime()
- {
- size_t used = JsonFormat::decode(json_input, slime);
- EXPECT_GT(used, 0);
- }
-};
-
StructDataType::UP
make_struct_elem_type()
{
diff --git a/searchsummary/src/vespa/searchsummary/test/CMakeLists.txt b/searchsummary/src/vespa/searchsummary/test/CMakeLists.txt
new file mode 100644
index 00000000000..aea75ffb244
--- /dev/null
+++ b/searchsummary/src/vespa/searchsummary/test/CMakeLists.txt
@@ -0,0 +1,6 @@
+# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_library(searchsummary_test OBJECT
+ SOURCES
+ AFTER
+ searchsummary_config
+)
diff --git a/searchsummary/src/vespa/searchsummary/test/slime_value.h b/searchsummary/src/vespa/searchsummary/test/slime_value.h
new file mode 100644
index 00000000000..3cc461d04ca
--- /dev/null
+++ b/searchsummary/src/vespa/searchsummary/test/slime_value.h
@@ -0,0 +1,24 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#pragma once
+
+#include <vespa/vespalib/data/slime/slime.h>
+#include <cassert>
+
+namespace search::docsummary::test {
+
+/**
+ * Utility class that wraps a slime object generated from json.
+ */
+struct SlimeValue {
+ vespalib::Slime slime;
+
+ SlimeValue(const vespalib::string& json_input)
+ : slime()
+ {
+ size_t used = vespalib::slime::JsonFormat::decode(json_input, slime);
+ assert(used > 0);
+ }
+};
+
+}