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
|
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <catch.hpp>
#include "common-pg.hpp"
#include "db-copy.hpp"
namespace {
testing::pg::tempdb_t db;
int table_count(testing::pg::conn_t const &conn, std::string const &where = "")
{
return conn.result_as_int("SELECT count(*) FROM test_copy_thread " + where);
}
} // anonymous namespace
TEST_CASE("db_copy_thread_t with db_deleter_by_id_t")
{
auto const conn = db.connect();
conn.exec("DROP TABLE IF EXISTS test_copy_thread");
conn.exec("CREATE TABLE test_copy_thread (id int8)");
auto const table =
std::make_shared<db_target_descr_t>("public", "test_copy_thread", "id");
db_copy_thread_t t{db.connection_params()};
using cmd_copy_t = db_cmd_copy_delete_t<db_deleter_by_id_t>;
SECTION("simple copy command")
{
SECTION("add one copy line and sync")
{
cmd_copy_t cmd{table};
cmd.buffer += "42\n";
t.send_command(std::move(cmd));
t.sync_and_wait();
REQUIRE(conn.result_as_int("SELECT id FROM test_copy_thread") ==
42);
}
SECTION("add multiple rows and sync")
{
cmd_copy_t cmd{table};
cmd.buffer += "101\n 23\n 900\n";
t.send_command(std::move(cmd));
t.sync_and_wait();
REQUIRE(table_count(conn) == 3);
}
SECTION("add one line and finish")
{
cmd_copy_t cmd{table};
cmd.buffer += "2\n";
t.send_command(std::move(cmd));
t.finish();
REQUIRE(conn.result_as_int("SELECT id FROM test_copy_thread") == 2);
}
}
SECTION("delete command")
{
cmd_copy_t cmd{table};
cmd.buffer += "42\n43\n133\n223\n224\n";
t.send_command(std::move(cmd));
t.sync_and_wait();
SECTION("simple delete of existing rows")
{
cmd = cmd_copy_t{table};
cmd.add_deletable(223);
cmd.add_deletable(42);
t.send_command(std::move(cmd));
t.sync_and_wait();
REQUIRE(table_count(conn, "WHERE id = 42") == 0);
REQUIRE(table_count(conn, "WHERE id = 223") == 0);
}
SECTION("delete one and add another")
{
cmd = cmd_copy_t{table};
cmd.add_deletable(133);
cmd.buffer += "134\n";
t.send_command(std::move(cmd));
t.sync_and_wait();
REQUIRE(table_count(conn, "WHERE id = 133") == 0);
REQUIRE(table_count(conn, "WHERE id = 134") == 1);
}
SECTION("delete one and add the same")
{
cmd = cmd_copy_t{table};
cmd.add_deletable(133);
cmd.buffer += "133\n";
t.send_command(std::move(cmd));
t.sync_and_wait();
REQUIRE(table_count(conn, "WHERE id = 133") == 1);
}
}
SECTION("multi buffer add without delete")
{
cmd_copy_t cmd{table};
cmd.buffer += "542\n5543\n10133\n";
t.send_command(std::move(cmd));
cmd = cmd_copy_t{table};
cmd.buffer += "12\n784\n523\n";
t.send_command(std::move(cmd));
t.finish();
REQUIRE(table_count(conn) == 6);
REQUIRE(table_count(conn, "WHERE id = 10133") == 1);
REQUIRE(table_count(conn, "WHERE id = 523") == 1);
}
SECTION("multi buffer add with delete")
{
cmd_copy_t cmd{table};
cmd.buffer += "542\n5543\n10133\n";
t.send_command(std::move(cmd));
cmd = cmd_copy_t{table};
cmd.add_deletable(542);
cmd.buffer += "12\n";
t.send_command(std::move(cmd));
t.finish();
REQUIRE(table_count(conn) == 3);
REQUIRE(table_count(conn, "WHERE id = 542") == 0);
REQUIRE(table_count(conn, "WHERE id = 12") == 1);
}
}
|