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
#define BOOST_TEST_MODULE config
#include <boost/test/included/unit_test.hpp>
namespace utf = boost::unit_test;

#include <kooling/database/database.h>

#include <cstdlib>
#include <iostream>
#include <string>

namespace {

bool test_db(utf::test_unit_id)
{
    return std::getenv("KOOLING_TEST_DATABASE_NAME");
}

kooling::database::database get_db()
{
    const json_t j = R"( {
        "name": "::env::KOOLING_TEST_DATABASE_NAME",
        "host": "::env::KOOLING_TEST_DATABASE_HOST",
        "port": "::env::KOOLING_TEST_DATABASE_PORT",
        "user": "::env::KOOLING_TEST_DATABASE_USER",
        "pass": "::env::KOOLING_TEST_DATABASE_PASS"
    } )"_json;

    const auto config{ j.template get<kooling::database::config>() };
    return kooling::database::database{ config };
}

} // unnamed namespace

BOOST_AUTO_TEST_SUITE(test_database)<--- There is an unknown macro here somewhere. Configuration is required. If BOOST_AUTO_TEST_SUITE is a macro then please configure it.

BOOST_AUTO_TEST_CASE(test_database_query, * utf::precondition(test_db))
{
    auto db{ get_db() };
    const bool ok{ db.query(
            R"(
                select userid, lastdatareceived
                from serverassignments2
                order by lastdatareceived asc
                limit 5
            )",
            [](const std::string& user, const std::string& timestamp)
            {
                std::cout << user << ", " << timestamp << "\n";
            }) };
    BOOST_TEST(ok);
}

BOOST_AUTO_TEST_CASE(test_database_query_with_params, * utf::precondition(test_db))
{
    auto db{ get_db() };
    const bool ok{ db.query(
            R"(
                select userid, lastdatareceived
                from serverassignments2
                where userid like $1
                and lastdatareceived > $2
                order by lastdatareceived asc
            )",
            kooling::database::params{ "DavidTest", "2024-12-31" },
            [](const std::string& user, const std::string& timestamp)
            {
                std::cout << user << ", " << timestamp << "\n";
            }) };
    BOOST_TEST(ok);
}

BOOST_AUTO_TEST_SUITE_END()