aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/jsonpath/ut/test_base.cpp
blob: c9b4f5669d125d785b6564d8bcaa864a759d6da6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "test_base.h"

#include <yql/essentials/types/binary_json/write.h>

using namespace NKikimr::NBinaryJson;

TJsonPathTestBase::TJsonPathTestBase()
    : FunctionRegistry(CreateFunctionRegistry(CreateBuiltinRegistry()))
    , Alloc(__LOCATION__)
    , Env(Alloc)
    , MemInfo("Memory")
    , HolderFactory(Alloc.Ref(), MemInfo, FunctionRegistry.Get())
    , ValueBuilder(HolderFactory)
{
}

TIssueCode TJsonPathTestBase::C(TIssuesIds::EIssueCode code) {
    return static_cast<TIssueCode>(code);
}

TUnboxedValue TJsonPathTestBase::ParseJson(TStringBuf raw) {
    return TryParseJsonDom(raw, &ValueBuilder);
}

void TJsonPathTestBase::RunTestCase(const TString& rawJson, const TString& rawJsonPath, const TVector<TString>& expectedResult) {
    try {
        const auto unboxedValueJson = TValue(ParseJson(rawJson));

        const auto binaryJson = std::get<TBinaryJson>(SerializeToBinaryJson(rawJson));
        auto reader = TBinaryJsonReader::Make(binaryJson);
        auto binaryJsonRoot = TValue(reader->GetRootCursor());

        TIssues issues;
        const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, MAX_PARSE_ERRORS);
        UNIT_ASSERT_C(issues.Empty(), "Parse errors found");

        for (const auto& json : {unboxedValueJson, binaryJsonRoot}) {
            const auto result = ExecuteJsonPath(jsonPath, json, TVariablesMap{}, &ValueBuilder);
            UNIT_ASSERT_C(!result.IsError(), "Runtime errors found");

            const auto& nodes = result.GetNodes();
            UNIT_ASSERT_VALUES_EQUAL(nodes.size(), expectedResult.size());
            for (size_t i = 0; i < nodes.size(); i++) {
                const auto converted = nodes[i].ConvertToUnboxedValue(&ValueBuilder);
                UNIT_ASSERT_VALUES_EQUAL(SerializeJsonDom(converted), expectedResult[i]);
            }
        }
    } catch (...) {
        TStringBuilder message;
        message << "Exception: " << CurrentExceptionMessage() << Endl
                << "Input JSON: " << rawJson << Endl
                << "Jsonpath: " << rawJsonPath << Endl
                << "Expected output:";
        for (const auto& item : expectedResult) {
            message << " " << item;
        }
        message << Endl;

        UNIT_FAIL(message);
    }
}

void TJsonPathTestBase::RunParseErrorTestCase(const TString& rawJsonPath) {
    try {
        TIssues issues;
        const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, 2);
        UNIT_ASSERT_C(!issues.Empty(), "Expected parse errors");
    } catch (...) {
        UNIT_FAIL(
            "Exception: " << CurrentExceptionMessage() << Endl
            << "Jsonpath: " << rawJsonPath << Endl
        );
    }
}

void TJsonPathTestBase::RunRuntimeErrorTestCase(const TString& rawJson, const TString& rawJsonPath, TIssueCode error) {
    try {
        const auto unboxedValueJson = TValue(ParseJson(rawJson));

        const auto binaryJson = std::get<TBinaryJson>(SerializeToBinaryJson(rawJson));
        auto reader = TBinaryJsonReader::Make(binaryJson);
        auto binaryJsonRoot = TValue(reader->GetRootCursor());

        TIssues issues;
        const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, MAX_PARSE_ERRORS);
        UNIT_ASSERT_C(issues.Empty(), "Parse errors found");

        for (const auto& json : {unboxedValueJson, binaryJsonRoot}) {
            const auto result = ExecuteJsonPath(jsonPath, json, TVariablesMap{}, &ValueBuilder);
            UNIT_ASSERT_C(result.IsError(), "Expected runtime error");
            UNIT_ASSERT_VALUES_EQUAL(result.GetError().GetCode(), error);
        }
    } catch (...) {
        UNIT_FAIL(
            TStringBuilder()
                 << "Exception: " << CurrentExceptionMessage() << Endl
                << "Input JSON: " << rawJson << Endl
                << "Jsonpath: " << rawJsonPath << Endl
                << "Expected error: " << error << Endl
        );
    }
}

void TJsonPathTestBase::RunVariablesTestCase(const TString& rawJson, const THashMap<TStringBuf, TStringBuf>& variables, const TString& rawJsonPath, const TVector<TString>& expectedResult) {
    try {
        const auto unboxedValueJson = TValue(ParseJson(rawJson));

        const auto binaryJson = std::get<TBinaryJson>(SerializeToBinaryJson(rawJson));
        auto reader = TBinaryJsonReader::Make(binaryJson);
        auto binaryJsonRoot = TValue(reader->GetRootCursor());

        TVariablesMap unboxedValueVariables;
        for (const auto& it : variables) {
            unboxedValueVariables[it.first] = TValue(ParseJson(it.second));
        }

        TVariablesMap binaryJsonVariables;
        TVector<TBinaryJson> storage;
        TVector<TBinaryJsonReaderPtr> readers;
        storage.reserve(variables.size());
        readers.reserve(variables.size());
        for (const auto& it : variables) {
            storage.push_back(std::get<TBinaryJson>(SerializeToBinaryJson(it.second)));
            readers.push_back(TBinaryJsonReader::Make(storage.back()));
            binaryJsonVariables[it.first] = TValue(readers.back()->GetRootCursor());
        }

        TIssues issues;
        const TJsonPathPtr jsonPath = ParseJsonPath(rawJsonPath, issues, MAX_PARSE_ERRORS);
        UNIT_ASSERT_C(issues.Empty(), "Parse errors found");

        TVector<std::pair<TValue, TVariablesMap>> testCases = {
            {unboxedValueJson, unboxedValueVariables},
            {binaryJsonRoot, binaryJsonVariables},
        };
        for (const auto& testCase : testCases) {
            const auto result = ExecuteJsonPath(jsonPath, testCase.first, testCase.second, &ValueBuilder);
            UNIT_ASSERT_C(!result.IsError(), "Runtime errors found");

            const auto& nodes = result.GetNodes();
            UNIT_ASSERT_VALUES_EQUAL(nodes.size(), expectedResult.size());
            for (size_t i = 0; i < nodes.size(); i++) {
                const auto converted = nodes[i].ConvertToUnboxedValue(&ValueBuilder);
                UNIT_ASSERT_VALUES_EQUAL(SerializeJsonDom(converted), expectedResult[i]);
            }
        }
    } catch (...) {
        TStringBuilder message;
        message << "Exception: " << CurrentExceptionMessage() << Endl
                << "Input JSON: " << rawJson << Endl
                << "Variables:" << Endl;
        for (const auto& it : variables) {
            message << "\t" << it.first << " = " << it.second;
        }

        message << Endl
                << "Jsonpath: " << rawJsonPath << Endl
                << "Expected output:";
        for (const auto& item : expectedResult) {
            message << " " << item;
        }
        message << Endl;

        UNIT_FAIL(message);
    }
}