File: test-db-copy-mgr.cpp

package info (click to toggle)
osm2pgsql 1.8.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,536 kB
  • sloc: cpp: 46,707; ansic: 1,804; python: 797; sh: 25; makefile: 14
file content (240 lines) | stat: -rw-r--r-- 6,582 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This file is part of osm2pgsql (https://osm2pgsql.org/).
 *
 * Copyright (C) 2006-2023 by the osm2pgsql developer community.
 * For a full list of authors see the git log.
 */

#include <catch.hpp>

#include <string>
#include <utility>
#include <vector>

#include "common-pg.hpp"
#include "db-copy-mgr.hpp"

static testing::pg::tempdb_t db;

using copy_mgr_t = db_copy_mgr_t<db_deleter_by_id_t>;

static std::shared_ptr<db_target_descr_t> setup_table(std::string const &cols)
{
    auto const conn = db.connect();
    conn.exec("DROP TABLE IF EXISTS test_copy_mgr");
    conn.exec("CREATE TABLE test_copy_mgr (id int8{}{})",
              cols.empty() ? "" : ",", cols);

    auto table = std::make_shared<db_target_descr_t>();
    table->name = "test_copy_mgr";
    table->id = "id";

    return table;
}

template <typename... ARGS>
void add_row(copy_mgr_t *mgr, std::shared_ptr<db_target_descr_t> const &t,
             ARGS &&...args)
{
    mgr->new_line(t);
    mgr->add_columns(std::forward<ARGS>(args)...);
    mgr->finish_line();

    mgr->sync();
}

template <typename T>
void add_array(copy_mgr_t *mgr, std::shared_ptr<db_target_descr_t> const &t,
               int id, std::vector<T> const &values)
{
    mgr->new_line(t);
    mgr->add_column(id);
    mgr->new_array();
    for (auto const &v : values) {
        mgr->add_array_elem(v);
    }
    mgr->finish_array();
    mgr->finish_line();

    mgr->sync();
}

static void
add_hash(copy_mgr_t *mgr, std::shared_ptr<db_target_descr_t> const &t, int id,
         std::vector<std::pair<std::string, std::string>> const &values)
{
    mgr->new_line(t);

    mgr->add_column(id);
    mgr->new_hash();
    for (auto const &[k, v] : values) {
        mgr->add_hash_elem(k, v);
    }
    mgr->finish_hash();
    mgr->finish_line();

    mgr->sync();
}

static void check_row(std::vector<std::string> const &row)
{
    auto const conn = db.connect();
    auto const res = conn.require_row("SELECT * FROM test_copy_mgr");

    for (std::size_t i = 0; i < row.size(); ++i) {
        CHECK(res.get_value(0, (int)i) == row[i]);
    }
}

TEST_CASE("copy_mgr_t")
{
    copy_mgr_t mgr{std::make_shared<db_copy_thread_t>(db.conninfo())};

    SECTION("Insert null")
    {
        auto const t = setup_table("big int8, t text");

        mgr.new_line(t);
        mgr.add_column(0);
        mgr.add_null_column();
        mgr.add_null_column();
        mgr.finish_line();
        mgr.sync();

        auto const conn = db.connect();
        auto const res = conn.require_row("SELECT * FROM test_copy_mgr");

        CHECK(res.is_null(0, 1));
        CHECK(res.is_null(0, 2));
    }

    SECTION("Insert numbers")
    {
        auto const t = setup_table("big int8, small smallint");

        add_row(&mgr, t, 34, 0xfff12345678ULL, -4457);
        check_row({"34", "17588196497016", "-4457"});
    }

    SECTION("Insert strings")
    {
        auto const t = setup_table("s0 text, s1 varchar");

        SECTION("Simple strings")
        {
            add_row(&mgr, t, -2, "foo", "l");
            check_row({"-2", "foo", "l"});
        }

        SECTION("Strings with special characters")
        {
            add_row(&mgr, t, -2, "va\tr", "meme\n");
            check_row({"-2", "va\tr", "meme\n"});
        }

        SECTION("Strings with more special characters")
        {
            add_row(&mgr, t, -2, "\rrun", "K\\P");
            check_row({"-2", "\rrun", "K\\P"});
        }

        SECTION("Strings with space and quote")
        {
            add_row(&mgr, t, 1, "with space", "name \"quoted\"");
            check_row({"1", "with space", "name \"quoted\""});
        }
    }

    SECTION("Insert int arrays")
    {
        auto const t = setup_table("a int[]");

        add_array<int>(&mgr, t, -9000, {45, -2, 0, 56});
        check_row({"-9000", "{45,-2,0,56}"});
    }

    SECTION("Insert string arrays")
    {
        auto const t = setup_table("a text[]");

        add_array<std::string>(&mgr, t, 3,
                               {"foo", "", "with space", "with \"quote\"",
                                "the\t", "line\nbreak", "rr\rrr", "s\\l"});
        check_row({"3", "{foo,\"\",\"with space\",\"with "
                        "\\\"quote\\\"\",\"the\t\",\"line\nbreak\","
                        "\"rr\rrr\",\"s\\\\l\"}"});

        auto const c = db.connect();
        CHECK(c.result_as_string("SELECT a[4] FROM test_copy_mgr") ==
              "with \"quote\"");
        CHECK(c.result_as_string("SELECT a[5] FROM test_copy_mgr") == "the\t");
        CHECK(c.result_as_string("SELECT a[6] FROM test_copy_mgr") ==
              "line\nbreak");
        CHECK(c.result_as_string("SELECT a[7] FROM test_copy_mgr") == "rr\rrr");
        CHECK(c.result_as_string("SELECT a[8] FROM test_copy_mgr") == "s\\l");
    }

    SECTION("Insert hashes")
    {
        auto const t = setup_table("h hstore");

        std::vector<std::pair<std::string, std::string>> const values = {
            {"one", "two"},           {"key 1", "value 1"},
            {"\"key\"", "\"value\""}, {"key\t2", "value\t2"},
            {"key\n3", "value\n3"},   {"key\r4", "value\r4"},
            {"key\\5", "value\\5"}};

        add_hash(&mgr, t, 42, values);

        auto const c = db.connect();

        for (auto const &[k, v] : values) {
            auto const res = c.result_as_string(
                fmt::format("SELECT h->'{}' FROM test_copy_mgr", k));
            CHECK(res == v);
        }
    }

    SECTION("Insert something and roll back")
    {
        auto const t = setup_table("t text");

        mgr.new_line(t);
        mgr.add_column(0);
        mgr.add_column("foo");
        mgr.rollback_line();
        mgr.sync();

        auto const conn = db.connect();
        CHECK(conn.get_count("test_copy_mgr") == 0);
    }

    SECTION("Insert something, insert more, roll back, insert something else")
    {
        auto const t = setup_table("t text");

        mgr.new_line(t);
        mgr.add_column(0);
        mgr.add_column("good");
        mgr.finish_line();

        mgr.new_line(t);
        mgr.add_column(1);
        mgr.add_column("bad");
        mgr.rollback_line();

        mgr.new_line(t);
        mgr.add_column(2);
        mgr.add_column("better");
        mgr.finish_line();
        mgr.sync();

        auto const conn = db.connect();
        auto const res = conn.exec("SELECT t FROM test_copy_mgr ORDER BY id");
        CHECK(res.num_tuples() == 2);
        CHECK(res.get(0, 0) == "good");
        CHECK(res.get(1, 0) == "better");
    }
}