aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/purecalc/ut/test_user_data.cpp
blob: b87940ab6b2491041d861929f4a3dc542024677d (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
#include <yql/essentials/public/purecalc/purecalc.h>
#include <yql/essentials/public/purecalc/io_specs/protobuf/spec.h>
#include <yql/essentials/public/purecalc/ut/protos/test_structs.pb.h>
#include <yql/essentials/public/purecalc/ut/empty_stream.h>

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

Y_UNIT_TEST_SUITE(TestUserData) {
    Y_UNIT_TEST(TestUserData) {
        using namespace NYql::NPureCalc;

        auto options = TProgramFactoryOptions()
            .AddFile(NYql::NUserData::EDisposition::INLINE, "my_file.txt", "my content!");

        auto factory = MakeProgramFactory(options);

        auto program = factory->MakePullListProgram(
            TProtobufInputSpec<NPureCalcProto::TStringMessage>(),
            TProtobufOutputSpec<NPureCalcProto::TStringMessage>(),
            "SELECT UNWRAP(CAST(FileContent(\"my_file.txt\") AS Utf8)) AS X",
            ETranslationMode::SQL
        );

        auto stream = program->Apply(EmptyStream<NPureCalcProto::TStringMessage*>());

        NPureCalcProto::TStringMessage* message;

        UNIT_ASSERT(message = stream->Fetch());
        UNIT_ASSERT_EQUAL(message->GetX(), "my content!");
        UNIT_ASSERT(!stream->Fetch());
    }

    Y_UNIT_TEST(TestUserDataLibrary) {
        using namespace NYql::NPureCalc;

        try {
            auto options = TProgramFactoryOptions()
                .AddLibrary(NYql::NUserData::EDisposition::INLINE, "a.sql", "$x = 1; EXPORT $x;")
                .AddLibrary(NYql::NUserData::EDisposition::INLINE, "b.sql", "IMPORT a SYMBOLS $x; $y = CAST($x + 1 AS String); EXPORT $y;");

            auto factory = MakeProgramFactory(options);

            auto program = factory->MakePullListProgram(
                TProtobufInputSpec<NPureCalcProto::TStringMessage>(),
                TProtobufOutputSpec<NPureCalcProto::TStringMessage>(),
                "IMPORT b SYMBOLS $y; SELECT CAST($y AS Utf8) ?? '' AS X;",
                ETranslationMode::SQL
            );

            auto stream = program->Apply(EmptyStream<NPureCalcProto::TStringMessage*>());

            NPureCalcProto::TStringMessage* message;

            UNIT_ASSERT(message = stream->Fetch());
            UNIT_ASSERT_EQUAL(message->GetX(), "2");
            UNIT_ASSERT(!stream->Fetch());
        } catch (const TCompileError& e) {
            Cerr << e;
            throw e;
        }
    }
}