Files
scylladb/tests/result_set_assertions.cc
Avi Kivity 2c3591cbd9 data_value de-any-fication
We use boost::any to convert to and from database values (stored in
serlialized form) and native C++ values.  boost::any captures information
about the data type (how to copy/move/delete etc.) and stores it inside
the boost::any instance.  We later retrieve the real value using
boost::any_cast.

However, data_value (which has a boost::any member) already has type
information as a data_type instance.  By teaching data_type intances about
the corresponding native type, we can elimiante the use of boost::any.

While boost::any is evil and eliminating it improves efficiency somewhat,
the real goal is growing native type support in data_type.  We will use that
later to store native types in the cache, enabling O(log n) access to
collections, O(1) access to tuples, and more efficient large blob support.
2015-10-30 17:38:51 +01:00

104 lines
3.1 KiB
C++

/*
* Copyright 2015 Cloudius Systems
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/test/unit_test.hpp>
#include "result_set_assertions.hh"
#include "to_string.hh"
static inline
sstring to_sstring(const bytes& b) {
return sstring(b.begin(), b.end());
}
bool
row_assertion::matches(const query::result_set_row& row) const {
for (auto&& column_and_value : _expected_values) {
auto&& name = column_and_value.first;
auto&& value = column_and_value.second;
// FIXME: result_set_row works on sstring column names instead of more general "bytes".
auto ss_name = to_sstring(name);
if (!row.has(ss_name)) {
if (!value.is_null()) {
return false;
}
} else {
const data_value& val = row.get_data_value(ss_name);
if (val != value) {
return false;
}
}
}
return true;
}
sstring
row_assertion::describe(schema_ptr schema) const {
return "{" + ::join(", ", _expected_values | boost::adaptors::transformed([&schema] (auto&& e) {
auto&& name = e.first;
auto&& value = e.second;
const column_definition* def = schema->get_column_definition(name);
if (!def) {
BOOST_FAIL(sprint("Schema is missing column definition for '%s'", name));
}
if (value.is_null()) {
return sprint("%s=null", to_sstring(name));
} else {
return sprint("%s=\"%s\"", to_sstring(name), def->type->to_string(def->type->decompose(value)));
}
})) + "}";
}
const result_set_assertions&
result_set_assertions::has(const row_assertion& ra) const {
for (auto&& row : _rs.rows()) {
if (ra.matches(row)) {
return *this;
}
}
BOOST_FAIL(sprint("Row %s not found in %s", ra.describe(_rs.schema()), _rs));
return *this;
}
const result_set_assertions&
result_set_assertions::has_only(const row_assertion& ra) const {
BOOST_REQUIRE(_rs.rows().size() == 1);
auto& row = _rs.rows()[0];
if (!ra.matches(row)) {
BOOST_FAIL(sprint("Expected %s but got %s", ra.describe(_rs.schema()), row));
}
return *this;
}
const result_set_assertions&
result_set_assertions::is_empty() const {
BOOST_REQUIRE_EQUAL(_rs.rows().size(), 0);
return *this;
}
const result_set_assertions&
result_set_assertions::has_size(int row_count) const {
BOOST_REQUIRE_EQUAL(_rs.rows().size(), row_count);
return *this;
}