Skip to content

Commit

Permalink
Added unit test for TMultiTableCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriyPA committed May 27, 2024
1 parent 975049f commit f2e0dad
Showing 1 changed file with 75 additions and 26 deletions.
101 changes: 75 additions & 26 deletions ydb/library/table_creator/table_creator_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,58 @@

namespace NKikimr {

namespace {

class TTestTablesCreator : public NTableCreator::TMultiTableCreator {
using TBase = NTableCreator::TMultiTableCreator;

public:
explicit TTestTablesCreator(NThreading::TPromise<void> promise)
: TBase({
GetFirstCreator(),
GetSecondCreator()
})
, Promise(promise)
{}

private:
static IActor* GetFirstCreator() {
return CreateTableCreator(
{ "path", "to", "table" },
{
Col("key", "Uint32"),
Col("value", "String"),
},
{ "key" },
NKikimrServices::STATISTICS
);
}

static IActor* GetSecondCreator() {
return CreateTableCreator(
{ "path", "to", "other", "table" },
{
Col("key", NScheme::NTypeIds::Uint32),
Col("expire_at", NScheme::NTypeIds::Timestamp),
},
{ "key" },
NKikimrServices::STATISTICS,
TtlCol("expire_at", TDuration::Zero(), TDuration::Minutes(60))
);
}

void OnTablesCreated() override {
Promise.SetValue();
}

private:
NThreading::TPromise<void> Promise;
};

} // namespace

Y_UNIT_TEST_SUITE(TableCreator) {
Y_UNIT_TEST(CreateTable) {
Y_UNIT_TEST(CreateTables) {
TPortManager tp;
ui16 mbusPort = tp.GetPort();
ui16 grpcPort = tp.GetPort();
Expand All @@ -23,26 +73,11 @@ Y_UNIT_TEST_SUITE(TableCreator) {
client.InitRootScheme();
auto runtime = server.GetRuntime();

TVector<TString> pathComponents = {"path", "to", "table"};

TVector<NKikimrSchemeOp::TColumnDescription> columns;
NKikimrSchemeOp::TColumnDescription descKey;
descKey.SetName("key");
descKey.SetType("Uint32");
columns.push_back(descKey);
NKikimrSchemeOp::TColumnDescription descValue;
descValue.SetName("value");
descValue.SetType("String");
columns.push_back(descValue);

TVector<TString> keyColumns = {"key"};

auto promise = NThreading::NewPromise();
TActorId edgeActor = server.GetRuntime()->AllocateEdgeActor(0);
runtime->Register(CreateTableCreator(
std::move(pathComponents), std::move(columns), std::move(keyColumns), NKikimrServices::STATISTICS),
runtime->Register(new TTestTablesCreator(promise),
0, 0, TMailboxType::Simple, 0, edgeActor);

runtime->GrabEdgeEvent<TEvTableCreator::TEvCreateTableResponse>(edgeActor);
promise.GetFuture().GetValueSync();

NYdb::TDriverConfig cfg;
cfg.SetEndpoint(TStringBuilder() << "localhost:" << grpcPort).SetDatabase(Tests::TestDomainName);
Expand All @@ -52,13 +87,27 @@ Y_UNIT_TEST_SUITE(TableCreator) {
UNIT_ASSERT_C(createSessionResult.IsSuccess(), createSessionResult.GetIssues().ToString());
NYdb::NTable::TSession session(createSessionResult.GetSession());

TString path = TString("/") + Tests::TestDomainName + "/path/to/table";
auto result = session.DescribeTable(path).ExtractValueSync();
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
const auto& createdColumns = result.GetTableDescription().GetColumns();
UNIT_ASSERT_C(createdColumns.size() == 2, "expected 2 columns");
UNIT_ASSERT_C(createdColumns[0].Name == "key", "expected key column");
UNIT_ASSERT_C(createdColumns[1].Name == "value", "expected value column");
{ // First table
auto path = TStringBuilder() << "/" << Tests::TestDomainName << "/path/to/table";
auto result = session.DescribeTable(path).ExtractValueSync();
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
const auto& createdColumns = result.GetTableDescription().GetColumns();
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns.size(), 2, "expected 2 columns");
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns[0].Name, "key", "expected key column");
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns[1].Name, "value", "expected value column");
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns[1].Type.ToString(), "String?", "expected type string");
}

{ // Second table
auto path = TStringBuilder() << "/" << Tests::TestDomainName << "/path/to/other/table";
auto result = session.DescribeTable(path).ExtractValueSync();
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
const auto& createdColumns = result.GetTableDescription().GetColumns();
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns.size(), 2, "expected 2 columns");
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns[0].Name, "key", "expected key column");
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns[1].Name, "expire_at", "expected expire_at column");
UNIT_ASSERT_VALUES_EQUAL_C(createdColumns[1].Type.ToString(), "Timestamp?", "expected type timestamp");
}
}
}

Expand Down

0 comments on commit f2e0dad

Please sign in to comment.