test_base.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "test_base.h"
  2. #include <yql/essentials/types/binary_json/write.h>
  3. using namespace NKikimr::NBinaryJson;
  4. TJsonPathTestBase::TJsonPathTestBase()
  5. : FunctionRegistry(CreateFunctionRegistry(CreateBuiltinRegistry()))
  6. , Alloc(__LOCATION__)
  7. , Env(Alloc)
  8. , MemInfo("Memory")
  9. , HolderFactory(Alloc.Ref(), MemInfo, FunctionRegistry.Get())
  10. , ValueBuilder(HolderFactory)
  11. {
  12. }
  13. TIssueCode TJsonPathTestBase::C(TIssuesIds::EIssueCode code) {
  14. return static_cast<TIssueCode>(code);
  15. }
  16. TUnboxedValue TJsonPathTestBase::ParseJson(TStringBuf raw) {
  17. return TryParseJsonDom(raw, &ValueBuilder);
  18. }
  19. void TJsonPathTestBase::RunTestCase(const TString& rawJson, const TString& rawJsonPath, const TVector<TString>& expectedResult) {
  20. try {
  21. const auto unboxedValueJson = TValue(ParseJson(rawJson));
  22. const auto binaryJson = std::get<TBinaryJson>(SerializeToBinaryJson(rawJson));
  23. auto reader = TBinaryJsonReader::Make(binaryJson);
  24. auto binaryJsonRoot = TValue(reader->GetRootCursor());
  25. TIssues issues;
  26. const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, MAX_PARSE_ERRORS);
  27. UNIT_ASSERT_C(issues.Empty(), "Parse errors found");
  28. for (const auto& json : {unboxedValueJson, binaryJsonRoot}) {
  29. const auto result = ExecuteJsonPath(jsonPath, json, TVariablesMap{}, &ValueBuilder);
  30. UNIT_ASSERT_C(!result.IsError(), "Runtime errors found");
  31. const auto& nodes = result.GetNodes();
  32. UNIT_ASSERT_VALUES_EQUAL(nodes.size(), expectedResult.size());
  33. for (size_t i = 0; i < nodes.size(); i++) {
  34. const auto converted = nodes[i].ConvertToUnboxedValue(&ValueBuilder);
  35. UNIT_ASSERT_VALUES_EQUAL(SerializeJsonDom(converted), expectedResult[i]);
  36. }
  37. }
  38. } catch (...) {
  39. TStringBuilder message;
  40. message << "Exception: " << CurrentExceptionMessage() << Endl
  41. << "Input JSON: " << rawJson << Endl
  42. << "Jsonpath: " << rawJsonPath << Endl
  43. << "Expected output:";
  44. for (const auto& item : expectedResult) {
  45. message << " " << item;
  46. }
  47. message << Endl;
  48. UNIT_FAIL(message);
  49. }
  50. }
  51. void TJsonPathTestBase::RunParseErrorTestCase(const TString& rawJsonPath) {
  52. try {
  53. TIssues issues;
  54. const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, 2);
  55. UNIT_ASSERT_C(!issues.Empty(), "Expected parse errors");
  56. } catch (...) {
  57. UNIT_FAIL(
  58. "Exception: " << CurrentExceptionMessage() << Endl
  59. << "Jsonpath: " << rawJsonPath << Endl
  60. );
  61. }
  62. }
  63. void TJsonPathTestBase::RunRuntimeErrorTestCase(const TString& rawJson, const TString& rawJsonPath, TIssueCode error) {
  64. try {
  65. const auto unboxedValueJson = TValue(ParseJson(rawJson));
  66. const auto binaryJson = std::get<TBinaryJson>(SerializeToBinaryJson(rawJson));
  67. auto reader = TBinaryJsonReader::Make(binaryJson);
  68. auto binaryJsonRoot = TValue(reader->GetRootCursor());
  69. TIssues issues;
  70. const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, MAX_PARSE_ERRORS);
  71. UNIT_ASSERT_C(issues.Empty(), "Parse errors found");
  72. for (const auto& json : {unboxedValueJson, binaryJsonRoot}) {
  73. const auto result = ExecuteJsonPath(jsonPath, json, TVariablesMap{}, &ValueBuilder);
  74. UNIT_ASSERT_C(result.IsError(), "Expected runtime error");
  75. UNIT_ASSERT_VALUES_EQUAL(result.GetError().GetCode(), error);
  76. }
  77. } catch (...) {
  78. UNIT_FAIL(
  79. TStringBuilder()
  80. << "Exception: " << CurrentExceptionMessage() << Endl
  81. << "Input JSON: " << rawJson << Endl
  82. << "Jsonpath: " << rawJsonPath << Endl
  83. << "Expected error: " << error << Endl
  84. );
  85. }
  86. }
  87. void TJsonPathTestBase::RunVariablesTestCase(const TString& rawJson, const THashMap<TStringBuf, TStringBuf>& variables, const TString& rawJsonPath, const TVector<TString>& expectedResult) {
  88. try {
  89. const auto unboxedValueJson = TValue(ParseJson(rawJson));
  90. const auto binaryJson = std::get<TBinaryJson>(SerializeToBinaryJson(rawJson));
  91. auto reader = TBinaryJsonReader::Make(binaryJson);
  92. auto binaryJsonRoot = TValue(reader->GetRootCursor());
  93. TVariablesMap unboxedValueVariables;
  94. for (const auto& it : variables) {
  95. unboxedValueVariables[it.first] = TValue(ParseJson(it.second));
  96. }
  97. TVariablesMap binaryJsonVariables;
  98. TVector<TBinaryJson> storage;
  99. TVector<TBinaryJsonReaderPtr> readers;
  100. storage.reserve(variables.size());
  101. readers.reserve(variables.size());
  102. for (const auto& it : variables) {
  103. storage.push_back(std::get<TBinaryJson>(SerializeToBinaryJson(it.second)));
  104. readers.push_back(TBinaryJsonReader::Make(storage.back()));
  105. binaryJsonVariables[it.first] = TValue(readers.back()->GetRootCursor());
  106. }
  107. TIssues issues;
  108. const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, MAX_PARSE_ERRORS);
  109. UNIT_ASSERT_C(issues.Empty(), "Parse errors found");
  110. TVector<std::pair<TValue, TVariablesMap>> testCases = {
  111. {unboxedValueJson, unboxedValueVariables},
  112. {binaryJsonRoot, binaryJsonVariables},
  113. };
  114. for (const auto& testCase : testCases) {
  115. const auto result = ExecuteJsonPath(jsonPath, testCase.first, testCase.second, &ValueBuilder);
  116. UNIT_ASSERT_C(!result.IsError(), "Runtime errors found");
  117. const auto& nodes = result.GetNodes();
  118. UNIT_ASSERT_VALUES_EQUAL(nodes.size(), expectedResult.size());
  119. for (size_t i = 0; i < nodes.size(); i++) {
  120. const auto converted = nodes[i].ConvertToUnboxedValue(&ValueBuilder);
  121. UNIT_ASSERT_VALUES_EQUAL(SerializeJsonDom(converted), expectedResult[i]);
  122. }
  123. }
  124. } catch (...) {
  125. TStringBuilder message;
  126. message << "Exception: " << CurrentExceptionMessage() << Endl
  127. << "Input JSON: " << rawJson << Endl
  128. << "Variables:" << Endl;
  129. for (const auto& it : variables) {
  130. message << "\t" << it.first << " = " << it.second;
  131. }
  132. message << Endl
  133. << "Jsonpath: " << rawJsonPath << Endl
  134. << "Expected output:";
  135. for (const auto& item : expectedResult) {
  136. message << " " << item;
  137. }
  138. message << Endl;
  139. UNIT_FAIL(message);
  140. }
  141. }