Try fix unit tests.

This commit is contained in:
Nikolai Kochetov 2020-04-20 12:42:56 +03:00
parent 67b3487207
commit 052598534a
3 changed files with 54 additions and 37 deletions

View File

@ -2,17 +2,24 @@
#include <Interpreters/Context.h>
inline DB::Context createContext()
struct ContextHolder
{
static DB::SharedContextHolder shared_context = DB::Context::createShared();
auto context = DB::Context::createGlobal(shared_context.get());
context.makeGlobalContext();
context.setPath("./");
return context;
}
DB::SharedContextHolder shared_context;
DB::Context context;
inline const DB::Context & getContext()
ContextHolder()
: shared_context(DB::Context::createShared())
, context(DB::Context::createGlobal(shared_context.get()))
{
}
ContextHolder(ContextHolder &&) = default;
};
inline ContextHolder getContext()
{
static DB::Context global_context = createContext();
return global_context;
ContextHolder holder;
holder.context.makeGlobalContext();
holder.context.setPath("./");
return holder;
}

View File

@ -68,7 +68,7 @@ using DiskImplementations = testing::Types<DB::DiskMemory, DB::DiskLocal>;
TYPED_TEST_SUITE(StorageLogTest, DiskImplementations);
// Returns data written to table in Values format.
std::string writeData(int rows, DB::StoragePtr & table)
std::string writeData(int rows, DB::StoragePtr & table, DB::Context & context)
{
using namespace DB;
@ -96,23 +96,23 @@ std::string writeData(int rows, DB::StoragePtr & table)
block.insert(column);
}
BlockOutputStreamPtr out = table->write({}, getContext());
BlockOutputStreamPtr out = table->write({}, context);
out->write(block);
return data;
}
// Returns all table data in Values format.
std::string readData(DB::StoragePtr & table)
std::string readData(DB::StoragePtr & table, DB::Context & context)
{
using namespace DB;
Names column_names;
column_names.push_back("a");
QueryProcessingStage::Enum stage = table->getQueryProcessingStage(getContext());
QueryProcessingStage::Enum stage = table->getQueryProcessingStage(context);
BlockInputStreamPtr in = std::make_shared<TreeExecutorBlockInputStream>(std::move(table->read(column_names, {}, getContext(), stage, 8192, 1)[0]));
BlockInputStreamPtr in = std::make_shared<TreeExecutorBlockInputStream>(std::move(table->read(column_names, {}, context, stage, 8192, 1)[0]));
Block sample;
{
@ -123,7 +123,7 @@ std::string readData(DB::StoragePtr & table)
std::ostringstream ss;
WriteBufferFromOStream out_buf(ss);
BlockOutputStreamPtr output = FormatFactory::instance().getOutput("Values", out_buf, sample, getContext());
BlockOutputStreamPtr output = FormatFactory::instance().getOutput("Values", out_buf, sample, context);
copyData(*in, *output);
@ -135,15 +135,16 @@ std::string readData(DB::StoragePtr & table)
TYPED_TEST(StorageLogTest, testReadWrite)
{
using namespace DB;
auto context_holder = getContext();
std::string data;
// Write several chunks of data.
data += writeData(10, this->getTable());
data += writeData(10, this->getTable(), context_holder.context);
data += ",";
data += writeData(20, this->getTable());
data += writeData(20, this->getTable(), context_holder.context);
data += ",";
data += writeData(10, this->getTable());
data += writeData(10, this->getTable(), context_holder.context);
ASSERT_EQ(data, readData(this->getTable()));
ASSERT_EQ(data, readData(this->getTable(), context_holder.context));
}

View File

@ -18,7 +18,7 @@ using namespace DB;
/// NOTE How to do better?
struct State
{
Context context = getContext();
Context & context;
NamesAndTypesList columns{
{"column", std::make_shared<DataTypeUInt8>()},
{"apply_id", std::make_shared<DataTypeUInt64>()},
@ -27,7 +27,7 @@ struct State
{"create_time", std::make_shared<DataTypeDateTime>()},
};
State()
State(Context & context_) : context(context_)
{
registerFunctions();
DatabasePtr database = std::make_shared<DatabaseMemory>("test");
@ -38,12 +38,6 @@ struct State
}
};
static State & state()
{
static State res;
return res;
}
static void check(const std::string & query, const std::string & expected, const Context & context, const NamesAndTypesList & columns)
{
@ -60,47 +54,62 @@ static void check(const std::string & query, const std::string & expected, const
TEST(TransformQueryForExternalDatabase, InWithSingleElement)
{
auto context_holder = getContext();
State state(context_holder.context);
check("SELECT column FROM test.table WHERE 1 IN (1)",
R"(SELECT "column" FROM "test"."table" WHERE 1)",
state().context, state().columns);
state.context, state.columns);
check("SELECT column FROM test.table WHERE column IN (1, 2)",
R"(SELECT "column" FROM "test"."table" WHERE "column" IN (1, 2))",
state().context, state().columns);
state.context, state.columns);
check("SELECT column FROM test.table WHERE column NOT IN ('hello', 'world')",
R"(SELECT "column" FROM "test"."table" WHERE "column" NOT IN ('hello', 'world'))",
state().context, state().columns);
state.context, state.columns);
}
TEST(TransformQueryForExternalDatabase, Like)
{
auto context_holder = getContext();
State state(context_holder.context);
check("SELECT column FROM test.table WHERE column LIKE '%hello%'",
R"(SELECT "column" FROM "test"."table" WHERE "column" LIKE '%hello%')",
state().context, state().columns);
state.context, state.columns);
check("SELECT column FROM test.table WHERE column NOT LIKE 'w%rld'",
R"(SELECT "column" FROM "test"."table" WHERE "column" NOT LIKE 'w%rld')",
state().context, state().columns);
state.context, state.columns);
}
TEST(TransformQueryForExternalDatabase, Substring)
{
auto context_holder = getContext();
State state(context_holder.context);
check("SELECT column FROM test.table WHERE left(column, 10) = RIGHT(column, 10) AND SUBSTRING(column FROM 1 FOR 2) = 'Hello'",
R"(SELECT "column" FROM "test"."table")",
state().context, state().columns);
state.context, state.columns);
}
TEST(TransformQueryForExternalDatabase, MultipleAndSubqueries)
{
auto context_holder = getContext();
State state(context_holder.context);
check("SELECT column FROM test.table WHERE 1 = 1 AND toString(column) = '42' AND column = 42 AND left(column, 10) = RIGHT(column, 10) AND column IN (1, 42) AND SUBSTRING(column FROM 1 FOR 2) = 'Hello' AND column != 4",
R"(SELECT "column" FROM "test"."table" WHERE 1 AND ("column" = 42) AND ("column" IN (1, 42)) AND ("column" != 4))",
state().context, state().columns);
state.context, state.columns);
check("SELECT column FROM test.table WHERE toString(column) = '42' AND left(column, 10) = RIGHT(column, 10) AND column = 42",
R"(SELECT "column" FROM "test"."table" WHERE ("column" = 42))",
state().context, state().columns);
state.context, state.columns);
}
TEST(TransformQueryForExternalDatabase, Issue7245)
{
auto context_holder = getContext();
State state(context_holder.context);
check("select apply_id from test.table where apply_type = 2 and create_time > addDays(toDateTime('2019-01-01 01:02:03'),-7) and apply_status in (3,4)",
R"(SELECT "apply_id", "apply_type", "apply_status", "create_time" FROM "test"."table" WHERE ("apply_type" = 2) AND ("create_time" > '2018-12-25 01:02:03') AND ("apply_status" IN (3, 4)))",
state().context, state().columns);
state.context, state.columns);
}