Browse Source

(refactoring) Move TTestTableDescription to ut_helpers KIKIMR-21006 (#1985)

Ilnaz Nizametdinov 1 year ago
parent
commit
68ef3206f1

+ 5 - 56
ydb/core/tx/replication/service/table_writer_ut.cpp

@@ -2,6 +2,7 @@
 #include "worker.h"
 
 #include <ydb/core/tx/replication/ut_helpers/test_env.h>
+#include <ydb/core/tx/replication/ut_helpers/test_table.h>
 
 #include <library/cpp/string_utils/base64/base64.h>
 #include <library/cpp/testing/unittest/registar.h>
@@ -11,63 +12,11 @@
 namespace NKikimr::NReplication::NService {
 
 Y_UNIT_TEST_SUITE(LocalTableWriter) {
-    struct TTestTableDescription {
-        struct TColumn {
-            TString Name;
-            TString Type;
-
-            void SerializeTo(NKikimrSchemeOp::TColumnDescription& proto) const {
-                proto.SetName(Name);
-                proto.SetType(Type);
-            }
-        };
-
-        TString Name;
-        TVector<TString> KeyColumns;
-        TVector<TColumn> Columns;
-
-        void SerializeTo(NKikimrSchemeOp::TTableDescription& proto) const {
-            proto.SetName("Table");
-            proto.MutableReplicationConfig()->SetMode(NKikimrSchemeOp::TTableReplicationConfig::REPLICATION_MODE_READ_ONLY);
-            proto.MutableReplicationConfig()->SetConsistency(NKikimrSchemeOp::TTableReplicationConfig::CONSISTENCY_WEAK);
-
-            for (const auto& keyColumn : KeyColumns) {
-                proto.AddKeyColumnNames(keyColumn);
-            }
-
-            for (const auto& column : Columns) {
-                column.SerializeTo(*proto.AddColumns());
-            }
-        }
-    };
-
-    NKikimrSchemeOp::TTableDescription MakeTableDescription(const TTestTableDescription& desc) {
-        NKikimrSchemeOp::TTableDescription proto;
-        desc.SerializeTo(proto);
-        return proto;
-    }
-
-    template <typename Env>
-    auto GetDescription(Env& env, const TString& path) {
-        auto resp = env.Describe(path);
-        return resp->Record;
-    }
-
-    template <typename Env>
-    TPathId GetPathId(Env& env, const TString& path) {
-        const auto& desc = GetDescription(env, path);
-        UNIT_ASSERT(desc.HasPathDescription());
-        UNIT_ASSERT(desc.GetPathDescription().HasSelf());
-
-        const auto& self = desc.GetPathDescription().GetSelf();
-        return TPathId(self.GetSchemeshardId(), self.GetPathId());
-    }
-
     Y_UNIT_TEST(WriteTable) {
         TEnv env;
         env.GetRuntime().SetLogPriority(NKikimrServices::REPLICATION_SERVICE, NLog::PRI_DEBUG);
 
-        env.CreateTable("/Root", MakeTableDescription(TTestTableDescription{
+        env.CreateTable("/Root", *MakeTableDescription(TTestTableDescription{
             .Name = "Test",
             .KeyColumns = {"key"},
             .Columns = {
@@ -76,7 +25,7 @@ Y_UNIT_TEST_SUITE(LocalTableWriter) {
             },
         }));
 
-        auto writer = env.GetRuntime().Register(CreateLocalTableWriter(GetPathId(env, "/Root/Table")));
+        auto writer = env.GetRuntime().Register(CreateLocalTableWriter(env.GetPathId("/Root/Table")));
         env.Send<TEvWorker::TEvHandshake>(writer, new TEvWorker::TEvHandshake());
 
         using TRecord = TEvWorker::TEvData::TRecord;
@@ -91,7 +40,7 @@ Y_UNIT_TEST_SUITE(LocalTableWriter) {
         TEnv env;
         env.GetRuntime().SetLogPriority(NKikimrServices::REPLICATION_SERVICE, NLog::PRI_DEBUG);
 
-        env.CreateTable("/Root", MakeTableDescription(TTestTableDescription{
+        env.CreateTable("/Root", *MakeTableDescription(TTestTableDescription{
             .Name = "Test",
             .KeyColumns = {"key"},
             .Columns = {
@@ -117,7 +66,7 @@ Y_UNIT_TEST_SUITE(LocalTableWriter) {
             },
         }));
 
-        auto writer = env.GetRuntime().Register(CreateLocalTableWriter(GetPathId(env, "/Root/Table")));
+        auto writer = env.GetRuntime().Register(CreateLocalTableWriter(env.GetPathId("/Root/Table")));
         env.Send<TEvWorker::TEvHandshake>(writer, new TEvWorker::TEvHandshake());
 
         using TRecord = TEvWorker::TEvData::TRecord;

+ 16 - 0
ydb/core/tx/replication/ut_helpers/test_env.h

@@ -1,3 +1,5 @@
+#pragma once
+
 #include <ydb/core/base/ticket_parser.h>
 #include <ydb/core/protos/replication.pb.h>
 #include <ydb/core/testlib/test_client.h>
@@ -105,6 +107,20 @@ public:
         return Client.Ls(std::forward<Args>(args)...);
     }
 
+    auto GetDescription(const TString& path) {
+        auto resp = Describe(path);
+        return resp->Record;
+    }
+
+    TPathId GetPathId(const TString& path) {
+        const auto& desc = GetDescription(path);
+        UNIT_ASSERT(desc.HasPathDescription());
+        UNIT_ASSERT(desc.GetPathDescription().HasSelf());
+
+        const auto& self = desc.GetPathDescription().GetSelf();
+        return TPathId(self.GetSchemeshardId(), self.GetPathId());
+    }
+
     template <typename... Args>
     auto CreateTable(Args&&... args) {
         return Client.CreateTable(std::forward<Args>(args)...);

+ 32 - 0
ydb/core/tx/replication/ut_helpers/test_table.cpp

@@ -0,0 +1,32 @@
+#include "test_table.h"
+
+#include <ydb/core/protos/flat_scheme_op.pb.h>
+
+namespace NKikimr::NReplication {
+
+void TTestTableDescription::TColumn::SerializeTo(NKikimrSchemeOp::TColumnDescription& proto) const {
+    proto.SetName(Name);
+    proto.SetType(Type);
+}
+
+void TTestTableDescription::SerializeTo(NKikimrSchemeOp::TTableDescription& proto) const {
+    proto.SetName("Table");
+    proto.MutableReplicationConfig()->SetMode(NKikimrSchemeOp::TTableReplicationConfig::REPLICATION_MODE_READ_ONLY);
+    proto.MutableReplicationConfig()->SetConsistency(NKikimrSchemeOp::TTableReplicationConfig::CONSISTENCY_WEAK);
+
+    for (const auto& keyColumn : KeyColumns) {
+        proto.AddKeyColumnNames(keyColumn);
+    }
+
+    for (const auto& column : Columns) {
+        column.SerializeTo(*proto.AddColumns());
+    }
+}
+
+THolder<NKikimrSchemeOp::TTableDescription> MakeTableDescription(const TTestTableDescription& desc) {
+    auto result = MakeHolder<NKikimrSchemeOp::TTableDescription>();
+    desc.SerializeTo(*result);
+    return result;
+}
+
+}

+ 31 - 0
ydb/core/tx/replication/ut_helpers/test_table.h

@@ -0,0 +1,31 @@
+#pragma once
+
+#include <util/generic/ptr.h>
+#include <util/generic/string.h>
+#include <util/generic/vector.h>
+
+namespace NKikimrSchemeOp {
+    class TColumnDescription;
+    class TTableDescription;
+}
+
+namespace NKikimr::NReplication {
+
+struct TTestTableDescription {
+    struct TColumn {
+        TString Name;
+        TString Type;
+
+        void SerializeTo(NKikimrSchemeOp::TColumnDescription& proto) const;
+    };
+
+    TString Name;
+    TVector<TString> KeyColumns;
+    TVector<TColumn> Columns;
+
+    void SerializeTo(NKikimrSchemeOp::TTableDescription& proto) const;
+};
+
+THolder<NKikimrSchemeOp::TTableDescription> MakeTableDescription(const TTestTableDescription& desc);
+
+}

+ 1 - 0
ydb/core/tx/replication/ut_helpers/ya.make

@@ -11,6 +11,7 @@ PEERDIR(
 
 SRCS(
     test_env.h
+    test_table.cpp
     write_topic.h
 )