Files
scylladb/test/lib/result_set_assertions.cc
Kefu Chai c37f4e5252 treewide: use fmt::join() when appropriate
now that fmtlib provides fmt::join(). see
https://fmt.dev/latest/api.html#_CPPv4I0EN3fmt4joinE9join_viewIN6detail10iterator_tI5RangeEEN6detail10sentinel_tI5RangeEEERR5Range11string_view
there is not need to revent the wheel. so in this change, the homebrew
join() is replaced with fmt::join().

as fmt::join() returns an join_view(), this could improve the
performance under certain circumstances where the fully materialized
string is not needed.

please note, the goal of this change is to use fmt::join(), and this
change does not intend to improve the performance of existing
implementation based on "operator<<" unless the new implementation is
much more complicated. we will address the unnecessarily materialized
strings in a follow-up commit.

some noteworthy things related to this change:

* unlike the existing `join()`, `fmt::join()` returns a view. so we
  have to materialize the view if what we expect is a `sstring`
* `fmt::format()` does not accept a view, so we cannot pass the
  return value of `fmt::join()` to `fmt::format()`
* fmtlib does not format a typed pointer, i.e., it does not format,
  for instance, a `const std::string*`. but operator<<() always print
  a typed pointer. so if we want to format a typed pointer, we either
  need to cast the pointer to `void*` or use `fmt::ptr()`.
* fmtlib is not able to pick up the overload of
  `operator<<(std::ostream& os, const column_definition* cd)`, so we
  have to use a wrapper class of `maybe_column_definition` for printing
  a pointer to `column_definition`. since the overload is only used
  by the two overloads of
  `statement_restrictions::add_single_column_parition_key_restriction()`,
  the operator<< for `const column_definition*` is dropped.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
2023-03-16 20:34:18 +08:00

99 lines
2.7 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <boost/test/unit_test.hpp>
#include "test/lib/result_set_assertions.hh"
#include "utils/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);
const data_value* val = row.get_data_value(ss_name);
if (val == nullptr) {
if (!value.is_null()) {
return false;
}
} else {
if (*val != value) {
return false;
}
}
}
if (_only_that) {
for (auto&& e : row.cells()) {
auto name = to_bytes(e.first);
if (!_expected_values.contains(name)) {
return false;
}
}
}
return true;
}
sstring
row_assertion::describe(schema_ptr schema) const {
return format("{{{}}}", fmt::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(format("Schema is missing column definition for '{}'", name));
}
if (value.is_null()) {
return format("{}=null", to_sstring(name));
} else {
return format("{}=\"{}\"", 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(format("Row {} not found in {}", 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(format("Expected {} but got {}", 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;
}