aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/bindings/py_optional_ut.cpp
blob: 4cc45f1184093032a55d789c0cb09e262d91a80c (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
#include "py_test_engine.h"

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


using namespace NPython;

Y_UNIT_TEST_SUITE(FromPyNone) {
    Y_UNIT_TEST(FromPyNone) {
        TPythonTestEngine engine;
        engine.ToMiniKQL<NUdf::TOptional<ui32>>(
            "def Test(): return None",
            [](const NUdf::TUnboxedValuePod& value) {
                UNIT_ASSERT(!value);
        });
    }

    Y_UNIT_TEST(FromPyObject) {
        TPythonTestEngine engine;
        engine.ToMiniKQL<NUdf::TOptional<ui32>>(
                "def Test(): return 42",
                [](const NUdf::TUnboxedValuePod& value) {
                    UNIT_ASSERT(value);
                    UNIT_ASSERT_EQUAL(value.Get<ui32>(), 42);
                });
    }

    Y_UNIT_TEST(ToPyNone) {
        TPythonTestEngine engine;
        engine.ToPython<NUdf::TOptional<char*>>(
                [](const TType* type, const NUdf::IValueBuilder& vb) {
                    Y_UNUSED(type); Y_UNUSED(vb);
                    return NUdf::TUnboxedValuePod();
                },
                "def Test(value):\n"
                "    assert value == None\n");
    }

    Y_UNIT_TEST(ToPyFilledOptional) {
        TPythonTestEngine engine;
        engine.ToPython<NUdf::TOptional<NUdf::TTuple<NUdf::TUtf8, bool>>>(
                [](const TType* type, const NUdf::IValueBuilder& vb) {
                    const TOptionalType* optType =
                            static_cast<const TOptionalType*>(type);
                    NUdf::TUnboxedValue* items = nullptr;
                    auto tuple = vb.NewArray(static_cast<const TTupleType*>(optType->GetItemType())->GetElementsCount(), items);
                    items[0] = vb.NewString("test string");
                    items[1] = NUdf::TUnboxedValuePod(false);
                    return NUdf::TUnboxedValue(tuple);
                },
                "def Test(value):\n"
                "    assert isinstance(value, tuple)\n"
                "    assert len(value) == 2\n"
                "    assert value == ('test string', False)\n");
    }
}