aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/jsonpath/ut/strict_ut.cpp
blob: c8414581e4afeca4e21595b2afd72274b1aab2e4 (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
#include "test_base.h"

class TJsonPathStrictTest : public TJsonPathTestBase {
public:
    TJsonPathStrictTest()
        : TJsonPathTestBase()
    {
    }

    UNIT_TEST_SUITE(TJsonPathStrictTest);
        UNIT_TEST(TestRuntimeErrors);
        UNIT_TEST(TestIncomparableTypes);
        UNIT_TEST(TestLikeRegexPredicate);
        UNIT_TEST(TestStartsWithPredicate);
    UNIT_TEST_SUITE_END();

    void TestRuntimeErrors() {
        const TVector<TRuntimeErrorTestCase> testCases = {
            {R"([
                {"key": 1},
                {"key": 2}
            ])", "$.key", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
            {R"([
                {"key": 1},
                {"key": 2}
            ])", "$.*", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
            {R"({
                "first": {"key": 1},
                "second": []
            })", "$.*.key", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
            {R"({
                "first": {"key": 1},
                "second": []
            })", "$.*.*", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
            {R"({"another_key": 123})", "$.key", C(TIssuesIds::JSONPATH_MEMBER_NOT_FOUND)},
            {R"([1, 2])", "$[*][0]", C(TIssuesIds::JSONPATH_EXPECTED_ARRAY)},
            {R"([[1], 2, [3]])", "$[*][0]", C(TIssuesIds::JSONPATH_EXPECTED_ARRAY)},
            {R"({
                "idx": -1,
                "array": [1, 2, 3]
            })", "$.array[$.idx]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
            {R"({
                "from": -1,
                "to": 3,
                "array": [1, 2, 3]
            })", "$.array[$.from to $.to]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
            {R"({
                "from": 0,
                "to": -1,
                "array": [1, 2, 3]
            })", "$.array[$.from to $.to]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
            {R"({
                "from": -20,
                "to": -10,
                "array": [1, 2, 3]
            })", "$.array[$.from to $.to]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
            {R"([1, 2, 3, 4, 5])", "$[3 to 0]", C(TIssuesIds::JSONPATH_INVALID_ARRAY_INDEX_RANGE)},
            {R"([[1, 2], [3, 4, 5], []])", "$[*][2]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
            {"[]", "$[last]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
            {"[]", "$[last to 0]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
        };

        for (const auto& testCase : testCases) {
            for (const auto mode : STRICT_MODES) {
                RunRuntimeErrorTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Error);
            }
        }
    }

    void TestIncomparableTypes() {
        const TVector<TMultiOutputTestCase> testCases = {
            {R"({
                "left": [1, 2, "string"],
                "right": [4, 5, 6]
            })", "$.left < $.right", {"null"}},
            {R"({
                "left": ["string", 2, 3],
                "right": [4, 5, 6]
            })", "$.left < $.right", {"null"}},
        };

        for (const auto& testCase : testCases) {
            for (const auto mode : STRICT_MODES) {
                RunTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Result);
            }
        }
    }

    void TestLikeRegexPredicate() {
        const TVector<TMultiOutputTestCase> testCases = {
            {R"(["123", 123])", R"($[*] like_regex "[0-9]+")", {"null"}},
            {R"([123, "123"])", R"($[*] like_regex "[0-9]+")", {"null"}},
        };

        for (const auto& testCase : testCases) {
            for (const auto mode : STRICT_MODES) {
                RunTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Result);
            }
        }
    }

    void TestStartsWithPredicate() {
        const TVector<TMultiOutputTestCase> testCases = {
            {R"(["a", "b", "c"])", R"("abcd" starts with $[*])", {"true"}},
            {R"(["a", 1.45, 50])", R"("abcd" starts with $[*])", {"null"}},
            {R"([1.45, 50, "a"])", R"("abcd" starts with $[*])", {"null"}},
            {R"(["b", "c"])", R"("abcd" starts with $[*])", {"false"}},
        };

        for (const auto& testCase : testCases) {
            for (const auto mode : STRICT_MODES) {
                RunTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Result);
            }
        }
    }
};

UNIT_TEST_SUITE_REGISTRATION(TJsonPathStrictTest);