format_ut.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "common_ut.h"
  2. #include <yt/cpp/mapreduce/interface/common.h>
  3. #include <yt/cpp/mapreduce/interface/errors.h>
  4. #include <yt/cpp/mapreduce/interface/format.h>
  5. #include <yt/cpp/mapreduce/interface/ut/proto3_ut.pb.h>
  6. #include <yt/cpp/mapreduce/interface/ut/protobuf_table_schema_ut.pb.h>
  7. #include <library/cpp/testing/gtest/gtest.h>
  8. using namespace NYT;
  9. static TNode GetColumns(const TFormat& format, int tableIndex = 0)
  10. {
  11. return format.Config.GetAttributes()["tables"][tableIndex]["columns"];
  12. }
  13. TEST(TProtobufFormatTest, TIntegral)
  14. {
  15. const auto format = TFormat::Protobuf<NUnitTesting::TIntegral>();
  16. auto columns = GetColumns(format);
  17. struct TColumn
  18. {
  19. TString Name;
  20. TString ProtoType;
  21. int FieldNumber;
  22. };
  23. auto expected = TVector<TColumn>{
  24. {"DoubleField", "double", 1},
  25. {"FloatField", "float", 2},
  26. {"Int32Field", "int32", 3},
  27. {"Int64Field", "int64", 4},
  28. {"Uint32Field", "uint32", 5},
  29. {"Uint64Field", "uint64", 6},
  30. {"Sint32Field", "sint32", 7},
  31. {"Sint64Field", "sint64", 8},
  32. {"Fixed32Field", "fixed32", 9},
  33. {"Fixed64Field", "fixed64", 10},
  34. {"Sfixed32Field", "sfixed32", 11},
  35. {"Sfixed64Field", "sfixed64", 12},
  36. {"BoolField", "bool", 13},
  37. {"EnumField", "enum_string", 14},
  38. };
  39. EXPECT_EQ(columns.Size(), expected.size());
  40. for (int i = 0; i < static_cast<int>(columns.Size()); ++i) {
  41. EXPECT_EQ(columns[i]["name"], expected[i].Name);
  42. EXPECT_EQ(columns[i]["proto_type"], expected[i].ProtoType);
  43. EXPECT_EQ(columns[i]["field_number"], expected[i].FieldNumber);
  44. }
  45. }
  46. TEST(TProtobufFormatTest, TRowFieldSerializationOption)
  47. {
  48. const auto format = TFormat::Protobuf<NUnitTesting::TRowFieldSerializationOption>();
  49. auto columns = GetColumns(format);
  50. EXPECT_EQ(columns[0]["name"], "UrlRow_1");
  51. EXPECT_EQ(columns[0]["proto_type"], "structured_message");
  52. EXPECT_EQ(columns[0]["field_number"], 1);
  53. const auto& fields = columns[0]["fields"];
  54. EXPECT_EQ(fields[0]["name"], "Host");
  55. EXPECT_EQ(fields[0]["proto_type"], "string");
  56. EXPECT_EQ(fields[0]["field_number"], 1);
  57. EXPECT_EQ(fields[1]["name"], "Path");
  58. EXPECT_EQ(fields[1]["proto_type"], "string");
  59. EXPECT_EQ(fields[1]["field_number"], 2);
  60. EXPECT_EQ(fields[2]["name"], "HttpCode");
  61. EXPECT_EQ(fields[2]["proto_type"], "sint32");
  62. EXPECT_EQ(fields[2]["field_number"], 3);
  63. EXPECT_EQ(columns[1]["name"], "UrlRow_2");
  64. EXPECT_EQ(columns[1]["proto_type"], "message");
  65. EXPECT_EQ(columns[1]["field_number"], 2);
  66. }
  67. TEST(TProtobufFormatTest, TPacked)
  68. {
  69. const auto format = TFormat::Protobuf<NUnitTesting::TPacked>();
  70. auto column = GetColumns(format)[0];
  71. EXPECT_EQ(column["name"], "PackedListInt64");
  72. EXPECT_EQ(column["proto_type"], "int64");
  73. EXPECT_EQ(column["field_number"], 1);
  74. EXPECT_EQ(column["packed"], true);
  75. EXPECT_EQ(column["repeated"], true);
  76. }
  77. TEST(TProtobufFormatTest, TCyclic)
  78. {
  79. EXPECT_THROW(TFormat::Protobuf<NUnitTesting::TCyclic>(), TApiUsageError);
  80. EXPECT_THROW(TFormat::Protobuf<NUnitTesting::TCyclic::TA>(), TApiUsageError);
  81. EXPECT_THROW(TFormat::Protobuf<NUnitTesting::TCyclic::TB>(), TApiUsageError);
  82. EXPECT_THROW(TFormat::Protobuf<NUnitTesting::TCyclic::TC>(), TApiUsageError);
  83. EXPECT_THROW(TFormat::Protobuf<NUnitTesting::TCyclic::TD>(), TApiUsageError);
  84. const auto format = TFormat::Protobuf<NUnitTesting::TCyclic::TE>();
  85. auto column = GetColumns(format)[0];
  86. EXPECT_EQ(column["name"], "d");
  87. EXPECT_EQ(column["proto_type"], "message");
  88. EXPECT_EQ(column["field_number"], 1);
  89. }
  90. TEST(TProtobufFormatTest, Map)
  91. {
  92. const auto format = TFormat::Protobuf<NUnitTesting::TWithMap>();
  93. auto columns = GetColumns(format);
  94. EXPECT_EQ(columns.Size(), 5u);
  95. {
  96. const auto& column = columns[0];
  97. EXPECT_EQ(column["name"], "MapDefault");
  98. EXPECT_EQ(column["proto_type"], "structured_message");
  99. EXPECT_EQ(column["fields"].Size(), 2u);
  100. EXPECT_EQ(column["fields"][0]["proto_type"], "int64");
  101. EXPECT_EQ(column["fields"][1]["proto_type"], "message");
  102. }
  103. {
  104. const auto& column = columns[1];
  105. EXPECT_EQ(column["name"], "MapListOfStructsLegacy");
  106. EXPECT_EQ(column["proto_type"], "structured_message");
  107. EXPECT_EQ(column["fields"].Size(), 2u);
  108. EXPECT_EQ(column["fields"][0]["proto_type"], "int64");
  109. EXPECT_EQ(column["fields"][1]["proto_type"], "message");
  110. }
  111. {
  112. const auto& column = columns[2];
  113. EXPECT_EQ(column["name"], "MapListOfStructs");
  114. EXPECT_EQ(column["proto_type"], "structured_message");
  115. EXPECT_EQ(column["fields"].Size(), 2u);
  116. EXPECT_EQ(column["fields"][0]["proto_type"], "int64");
  117. EXPECT_EQ(column["fields"][1]["proto_type"], "structured_message");
  118. }
  119. {
  120. const auto& column = columns[3];
  121. EXPECT_EQ(column["name"], "MapOptionalDict");
  122. EXPECT_EQ(column["proto_type"], "structured_message");
  123. EXPECT_EQ(column["fields"].Size(), 2u);
  124. EXPECT_EQ(column["fields"][0]["proto_type"], "int64");
  125. EXPECT_EQ(column["fields"][1]["proto_type"], "structured_message");
  126. }
  127. {
  128. const auto& column = columns[4];
  129. EXPECT_EQ(column["name"], "MapDict");
  130. EXPECT_EQ(column["proto_type"], "structured_message");
  131. EXPECT_EQ(column["fields"].Size(), 2u);
  132. EXPECT_EQ(column["fields"][0]["proto_type"], "int64");
  133. EXPECT_EQ(column["fields"][1]["proto_type"], "structured_message");
  134. }
  135. }
  136. TEST(TProtobufFormatTest, Oneof)
  137. {
  138. const auto format = TFormat::Protobuf<NUnitTesting::TWithOneof>();
  139. auto columns = GetColumns(format);
  140. EXPECT_EQ(columns.Size(), 4u);
  141. auto check = [] (const TNode& column, TStringBuf name, TStringBuf oneof2Name) {
  142. EXPECT_EQ(column["name"], name);
  143. EXPECT_EQ(column["proto_type"], "structured_message");
  144. EXPECT_EQ(column["fields"].Size(), 5u);
  145. EXPECT_EQ(column["fields"][0]["name"], "field");
  146. const auto& oneof2 = column["fields"][1];
  147. EXPECT_EQ(oneof2["name"], oneof2Name);
  148. EXPECT_EQ(oneof2["proto_type"], "oneof");
  149. EXPECT_EQ(oneof2["fields"][0]["name"], "y2");
  150. EXPECT_EQ(oneof2["fields"][1]["name"], "z2");
  151. EXPECT_EQ(oneof2["fields"][1]["proto_type"], "structured_message");
  152. const auto& embeddedOneof = oneof2["fields"][1]["fields"][0];
  153. EXPECT_EQ(embeddedOneof["name"], "Oneof");
  154. EXPECT_EQ(embeddedOneof["fields"][0]["name"], "x");
  155. EXPECT_EQ(embeddedOneof["fields"][1]["name"], "y");
  156. EXPECT_EQ(oneof2["fields"][2]["name"], "x2");
  157. EXPECT_EQ(column["fields"][2]["name"], "x1");
  158. EXPECT_EQ(column["fields"][3]["name"], "y1");
  159. EXPECT_EQ(column["fields"][4]["name"], "z1");
  160. };
  161. check(columns[0], "DefaultSeparateFields", "variant_field_name");
  162. check(columns[1], "NoDefault", "Oneof2");
  163. {
  164. const auto& column = columns[2];
  165. EXPECT_EQ(column["name"], "SerializationProtobuf");
  166. EXPECT_EQ(column["proto_type"], "structured_message");
  167. EXPECT_EQ(column["fields"].Size(), 3u);
  168. EXPECT_EQ(column["fields"][0]["name"], "x1");
  169. EXPECT_EQ(column["fields"][1]["name"], "y1");
  170. EXPECT_EQ(column["fields"][2]["name"], "z1");
  171. }
  172. {
  173. const auto& column = columns[3];
  174. EXPECT_EQ(column["name"], "TopLevelOneof");
  175. EXPECT_EQ(column["proto_type"], "oneof");
  176. EXPECT_EQ(column["fields"].Size(), 1u);
  177. EXPECT_EQ(column["fields"][0]["name"], "MemberOfTopLevelOneof");
  178. }
  179. }
  180. TEST(TProto3Test, TWithOptional)
  181. {
  182. const auto format = TFormat::Protobuf<NTestingProto3::TWithOptional>();
  183. auto columns = GetColumns(format);
  184. EXPECT_EQ(columns[0]["name"], "x");
  185. EXPECT_EQ(columns[0]["proto_type"], "int64");
  186. EXPECT_EQ(columns[0]["field_number"], 1);
  187. }
  188. TEST(TProto3Test, TWithOptionalMessage)
  189. {
  190. const auto format = TFormat::Protobuf<NTestingProto3::TWithOptionalMessage>();
  191. auto columns = GetColumns(format);
  192. EXPECT_EQ(columns[0]["name"], "x");
  193. EXPECT_EQ(columns[0]["proto_type"], "structured_message");
  194. EXPECT_EQ(columns[0]["field_number"], 1);
  195. EXPECT_EQ(columns[0]["fields"].Size(), 1u);
  196. EXPECT_EQ(columns[0]["fields"][0]["name"], "x");
  197. EXPECT_EQ(columns[0]["fields"][0]["proto_type"], "int64");
  198. EXPECT_EQ(columns[0]["fields"][0]["field_number"], 1);
  199. }