blitz_query_cpp

Форк
0
/
sql_query_resolver_tests.cpp 
154 строки · 4.6 Кб
1
#include <gtest/gtest.h>
2
#include <gmock/gmock.h>
3
#include <type_system/schema.hpp>
4
#include <type_system/schema_parser.hpp>
5
#include <processing/sql_query_resolver.hpp>
6
#include <processing/parse_document.hpp>
7
#include <processing/processing_pipeline.hpp>
8
#include <processing/render_sql.hpp>
9
#include <data/sql/postgresql_renderer.hpp>
10
#include <data/sql/postgresql_data_reader.hpp>
11
#include <serialization/json/json_serializer.hpp>
12
#include <serialization/buffer_writer.hpp>
13
#include <struct/query_context.hpp>
14
#include <type_system/builtin_types.hpp>
15

16
#include <string>
17

18
using namespace blitz_query_cpp;
19

20
TEST(SqlResolver, SimpleSelect)
21
{
22
    std::string scm = R""""(
23
        schema { query: Query }
24
        type Query
25
        {
26
           Users :[User]
27
        }
28
        type User @table(table: "user" schema: "test")
29
        {
30
           Id: Int @column(name: "id" IsPK: True)
31
           Name: String @column(name: "name")
32
           Age: Int  @column(name: "age")
33
           AccountId :Int @always_projected
34
        }
35

36
        directive @table(table: String schema: String) on OBJECT
37
        directive @column(name: String IsPK: Boolean = False) on FIELD_DEFINITION
38
        )"""";
39

40
    std::string query =
41
        R""""(
42
        {
43
            Users {     
44
                Name
45
                Age
46
                Id
47
            }
48
        }
49
        )"""";
50

51
    schema_t my_schema;
52
    EXPECT_EQ(add_introspection_types(my_schema), true);
53

54
    schema_parser_t parser;
55
    bool res = parser.parse(my_schema, scm);
56
    EXPECT_EQ(res, true);
57
    EXPECT_EQ(my_schema.query_type_name, "Query");
58
    std::cout << parser.get_error_msg() << std::endl;
59

60
    options_t options{
61
        {"connection_string", "user=postgres password=Welcome01 host=127.0.0.1 dbname=test"}};
62

63
    writer_options_t wtiter_options;
64
    db_connection_options db_options{};
65

66
    query_context context(query, &my_schema, options);
67

68
    processing_pipeline<parse_document,
69
                        sql_query_resolver,
70
                        render_sql<sql::postgresql_renderer>
71
                       >
72
        pipeline;
73

74
    pipeline.process(context);
75
    EXPECT_EQ(context.error_msgs.size(), 0ul);
76
    EXPECT_EQ(context.data["sql"], "SELECT _a1.name, _a1.age, _a1.id, _a1.\"AccountId\" FROM test.user as _a1");
77
}
78

79

80

81
TEST(SqlResolver, SelectWithOffsetPagination)
82
{
83
    std::string scm = R""""(
84
        schema { query: Query }
85
        type Query
86
        {
87
           Users :UserCollectionSegment
88
        }
89

90
        "Information about pagination"
91
        type CollectionSegmentInfo
92
        {
93
            "Indicates whether more items exist following the set defined by the clients arguments."
94
            hasNextPage: Boolean!
95
            "Indicates whether more items exist prior the set defined by the clients arguments."
96
            hasPreviousPage Boolean!
97
        }
98
        type UserCollectionSegment @page_segment_info
99
        {
100
            pageInfo: CollectionSegmentInfo!
101
            items:[User]
102
            totalCount: Int!
103
        }
104
        type User @table(table: "user" schema: "test")
105
        {
106
           Id: Int @column(name: "id" IsPK: True)
107
           Name: String @column(name: "name")
108
           Age: Int  @column(name: "age")
109
           AccountId :Int @always_projected
110
        }
111

112
        directive @table(table: String schema: String) on OBJECT
113
        directive @column(name: String IsPK: Boolean = False) on FIELD_DEFINITION
114
        directive @page_segment_info on OBJECT
115
        )"""";
116

117
    std::string query =
118
        R""""(
119
        {
120
            Users {     
121
                Name
122
                Age
123
                Id
124
            }
125
        }
126
        )"""";
127

128
    schema_t my_schema;
129
    EXPECT_EQ(add_introspection_types(my_schema), true);
130

131
    schema_parser_t parser;
132
    bool res = parser.parse(my_schema, scm);
133
    EXPECT_EQ(res, true);
134
    EXPECT_EQ(my_schema.query_type_name, "Query");
135
    std::cout << parser.get_error_msg() << std::endl;
136

137
    options_t options{
138
        {"connection_string", "user=postgres password=Welcome01 host=127.0.0.1 dbname=test"}};
139

140
    writer_options_t wtiter_options;
141
    db_connection_options db_options{};
142

143
    query_context context(query, &my_schema, options);
144

145
    processing_pipeline<parse_document,
146
                        sql_query_resolver,
147
                        render_sql<sql::postgresql_renderer>
148
                       >
149
        pipeline;
150

151
    pipeline.process(context);
152
    EXPECT_EQ(context.error_msgs.size(), 0ul);
153
    EXPECT_EQ(context.data["sql"], "SELECT _a1.name, _a1.age, _a1.id, _a1.\"AccountId\" FROM test.user as _a1");
154
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.