summaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/ut/pg_ops_ut.cpp
blob: 6142c0a6abdcc18b50c53bbf2c01a5ee777ad178 (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
#include <yql/essentials/parser/pg_wrapper/pg_ops.h>

#include <yql/essentials/minikql/mkql_alloc.h>
#include <yql/essentials/parser/pg_catalog/catalog.h>

#include <library/cpp/testing/unittest/registar.h>

namespace NYql {

using namespace NKikimr::NMiniKQL;

namespace {

ui32 PgTypeId(TStringBuf typeName) {
    return NPg::LookupType(TString(typeName)).TypeId;
}

class TTestContext {
public:
    TTestContext()
        : Alloc(__LOCATION__)
    {
    }

private:
    TScopedAlloc Alloc;
    TThrowingBindTerminator BindTerminator;
};

void AssertSign(const TPgConst& value, i32 expectedSign) {
    auto signFunc = TPgSign::Create(value.GetTypeId());
    auto result = signFunc->MakeCallState()->GetSign(value.ExtractConst());
    UNIT_ASSERT_C(result.Defined(), "Sign returned NULL");
    UNIT_ASSERT_VALUES_EQUAL(*result, expectedSign);
}

} // namespace

Y_UNIT_TEST_SUITE(TPgOpsTests) {

Y_UNIT_TEST(TestSignInt4) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("int4"), "42");
    TPgConst negative(PgTypeId("int4"), "-42");
    TPgConst zero(PgTypeId("int4"), "0");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignInt8) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("int8"), "9223372036854775807");
    TPgConst negative(PgTypeId("int8"), "-9223372036854775808");
    TPgConst zero(PgTypeId("int8"), "0");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignFloat4) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("float4"), "3.14");
    TPgConst negative(PgTypeId("float4"), "-2.71");
    TPgConst zero(PgTypeId("float4"), "0.0");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignFloat8) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("float8"), "1.23456789012345");
    TPgConst negative(PgTypeId("float8"), "-1.23456789012345");
    TPgConst zero(PgTypeId("float8"), "0.0");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignNumeric) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("numeric"), "123456789012345678901234567890.123456");
    TPgConst negative(PgTypeId("numeric"), "-123456789012345678901234567890.123456");
    TPgConst zero(PgTypeId("numeric"), "0");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignInt2) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("int2"), "100");
    TPgConst negative(PgTypeId("int2"), "-100");
    TPgConst zero(PgTypeId("int2"), "0");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignInterval) {
    TTestContext ctx;

    TPgConst positive(PgTypeId("interval"), "1 day");
    TPgConst negative(PgTypeId("interval"), "-1 day");
    TPgConst zero(PgTypeId("interval"), "0 seconds");

    AssertSign(positive, 1);
    AssertSign(negative, -1);
    AssertSign(zero, 0);
}

Y_UNIT_TEST(TestSignTextThrowsException) {
    TTestContext ctx;

    TPgConst textVal(PgTypeId("text"), "hello");
    UNIT_ASSERT_EXCEPTION(
        TPgSign::Create(textVal.GetTypeId()),
        yexception);
}

Y_UNIT_TEST(TestConstInvalidInt) {
    TTestContext ctx;
    UNIT_ASSERT_EXCEPTION(
        TPgConst(PgTypeId("int4"), "not_a_number").ExtractConst(),
        yexception);
}

Y_UNIT_TEST(TestConstEmptyInt) {
    TTestContext ctx;
    UNIT_ASSERT_EXCEPTION(
        TPgConst(PgTypeId("int4"), "").ExtractConst(),
        yexception);
}

} // Y_UNIT_TEST_SUITE(TPgOpsTests)

} // namespace NYql