yql_constraint_ut.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "yql_constraint.h"
  2. #include "yql_expr.h"
  3. #include <yql/essentials/utils/yql_panic.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <library/cpp/yson/node/node_io.h>
  6. namespace NYql {
  7. Y_UNIT_TEST_SUITE(TSerializeConstrains) {
  8. Y_UNIT_TEST(SerializeSorted) {
  9. TExprContext ctx;
  10. auto c = ctx.MakeConstraint<TSortedConstraintNode>(TSortedConstraintNode::TContainerType{
  11. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a", "b"}}, true},
  12. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}}, false},
  13. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"e"}, TPartOfConstraintBase::TPathType{"f", "g"}}, false},
  14. });
  15. auto yson = c->ToYson();
  16. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([[[["a";"b"]];%true];[["d"];%false];[["e";["f";"g"]];%false]])");
  17. auto c2 = ctx.MakeConstraint<TSortedConstraintNode>(yson);
  18. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
  19. UNIT_ASSERT_EQUAL(c, c2);
  20. }
  21. Y_UNIT_TEST(SerializeChopped) {
  22. TExprContext ctx;
  23. auto c = ctx.MakeConstraint<TChoppedConstraintNode>(TPartOfConstraintBase::TSetOfSetsType{
  24. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
  25. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a", "b"}},
  26. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c", "d"}, TPartOfConstraintBase::TPathType{"e"}},
  27. });
  28. auto yson = c->ToYson();
  29. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"(["a";[["a";"b"]];[["c";"d"];"e"]])");
  30. auto c2 = ctx.MakeConstraint<TChoppedConstraintNode>(yson);
  31. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
  32. UNIT_ASSERT_EQUAL(c, c2);
  33. }
  34. template <class TUniqueConstraint>
  35. void CheckSerializeUnique() {
  36. TExprContext ctx;
  37. auto c = ctx.MakeConstraint<TUniqueConstraint>(typename TUniqueConstraint::TContentType{
  38. TConstraintWithFieldsNode::TSetOfSetsType{
  39. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
  40. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a", "b"}}
  41. },
  42. TConstraintWithFieldsNode::TSetOfSetsType{
  43. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c"}},
  44. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}, TPartOfConstraintBase::TPathType{"e"}}
  45. },
  46. });
  47. auto yson = c->ToYson();
  48. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([["a";[["a";"b"]]];["c";["d";"e"]]])");
  49. auto c2 = ctx.MakeConstraint<TUniqueConstraint>(yson);
  50. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
  51. UNIT_ASSERT_EQUAL(c, c2);
  52. }
  53. Y_UNIT_TEST(SerializeUnique) {
  54. CheckSerializeUnique<TUniqueConstraintNode>();
  55. }
  56. Y_UNIT_TEST(SerializeDistint) {
  57. CheckSerializeUnique<TDistinctConstraintNode>();
  58. }
  59. Y_UNIT_TEST(SerializeEmpty) {
  60. TExprContext ctx;
  61. auto c = ctx.MakeConstraint<TEmptyConstraintNode>();
  62. auto yson = c->ToYson();
  63. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"(#)");
  64. auto c2 = ctx.MakeConstraint<TEmptyConstraintNode>(yson);
  65. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
  66. UNIT_ASSERT_EQUAL(c, c2);
  67. }
  68. Y_UNIT_TEST(SerializeVarIndex) {
  69. TExprContext ctx;
  70. auto c = ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
  71. std::pair{1u, 3u},
  72. std::pair{0u, 1u},
  73. });
  74. auto yson = c->ToYson();
  75. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([[0u;1u];[1u;3u]])");
  76. auto c2 = ctx.MakeConstraint<TVarIndexConstraintNode>(yson);
  77. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
  78. UNIT_ASSERT_EQUAL(c, c2);
  79. }
  80. Y_UNIT_TEST(SerializeMulti) {
  81. TExprContext ctx;
  82. TConstraintSet s1;
  83. s1.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  84. s1.AddConstraint(
  85. ctx.MakeConstraint<TSortedConstraintNode>(TSortedConstraintNode::TContainerType{
  86. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}}, true},
  87. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}, false},
  88. })
  89. );
  90. TConstraintSet s2;
  91. s2.AddConstraint(
  92. ctx.MakeConstraint<TUniqueConstraintNode>(typename TUniqueConstraintNode::TContentType{
  93. TConstraintWithFieldsNode::TSetOfSetsType{
  94. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
  95. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}
  96. },
  97. TConstraintWithFieldsNode::TSetOfSetsType{
  98. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c"}},
  99. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}, TPartOfConstraintBase::TPathType{"e"}}
  100. },
  101. })
  102. );
  103. s2.AddConstraint(
  104. ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
  105. std::pair{0u, 1u},
  106. std::pair{1u, 2u},
  107. })
  108. );
  109. auto c = ctx.MakeConstraint<TMultiConstraintNode>(TMultiConstraintNode::TMapType{
  110. std::pair{0u, s1},
  111. std::pair{1u, s2},
  112. });
  113. auto yson = c->ToYson();
  114. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([[0u;{"Empty"=#;"Sorted"=[[["a"];%true];[["b"];%false]]}];[1u;{"Unique"=[["a";"b"];["c";["d";"e"]]];"VarIndex"=[[0u;1u];[1u;2u]]}]])");
  115. auto c2 = ctx.MakeConstraint<TMultiConstraintNode>(yson);
  116. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
  117. UNIT_ASSERT_EQUAL(c, c2);
  118. }
  119. Y_UNIT_TEST(SerializeConstrainSet) {
  120. TExprContext ctx;
  121. TConstraintSet s;
  122. s.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  123. s.AddConstraint(
  124. ctx.MakeConstraint<TSortedConstraintNode>(TSortedConstraintNode::TContainerType{
  125. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}}, true},
  126. std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}, false},
  127. })
  128. );
  129. s.AddConstraint(
  130. ctx.MakeConstraint<TUniqueConstraintNode>(typename TUniqueConstraintNode::TContentType{
  131. TConstraintWithFieldsNode::TSetOfSetsType{
  132. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
  133. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}
  134. },
  135. TConstraintWithFieldsNode::TSetOfSetsType{
  136. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c"}},
  137. TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}, TPartOfConstraintBase::TPathType{"e"}}
  138. },
  139. })
  140. );
  141. s.AddConstraint(
  142. ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
  143. std::pair{0u, 1u},
  144. std::pair{1u, 2u},
  145. })
  146. );
  147. s.AddConstraint(
  148. ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
  149. std::pair{0u, 1u},
  150. std::pair{1u, 2u},
  151. })
  152. );
  153. TConstraintSet inner;
  154. inner.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  155. s.AddConstraint(
  156. ctx.MakeConstraint<TMultiConstraintNode>(TMultiConstraintNode::TMapType{
  157. std::pair{0u, inner},
  158. })
  159. );
  160. auto yson = s.ToYson();
  161. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"({"Empty"=#;"Multi"=[[0u;{"Empty"=#}]];"Sorted"=[[["a"];%true];[["b"];%false]];"Unique"=[["a";"b"];["c";["d";"e"]]];"VarIndex"=[[0u;1u];[1u;2u]]})");
  162. auto s2 = ctx.MakeConstraintSet(yson);
  163. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(s2.ToYson()));
  164. UNIT_ASSERT_EQUAL(s, s2);
  165. s.Clear();
  166. yson = s.ToYson();
  167. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"({})");
  168. auto s3 = ctx.MakeConstraintSet(yson);
  169. UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(s3.ToYson()));
  170. UNIT_ASSERT_EQUAL(s, s3);
  171. }
  172. Y_UNIT_TEST(DeserializeBadConstrainSet) {
  173. TExprContext ctx;
  174. UNIT_ASSERT_EXCEPTION(
  175. ctx.MakeConstraintSet(NYT::NodeFromYsonString(R"(#)")),
  176. NYql::TYqlPanic
  177. );
  178. UNIT_ASSERT_EXCEPTION(
  179. ctx.MakeConstraintSet(NYT::NodeFromYsonString(R"({"Unknown"=[]})")),
  180. NYql::TYqlPanic
  181. );
  182. UNIT_ASSERT_EXCEPTION(
  183. ctx.MakeConstraintSet(NYT::NodeFromYsonString(R"({"Empty"=1u})")),
  184. NYql::TYqlPanic
  185. );
  186. }
  187. }
  188. } // namespace NYql