~singpolyma/biboumi

ref: 577984faf2befaa7f11a1e4a115dc8d80805fec7 biboumi/tests/database.cpp -rw-r--r-- 6.4 KiB
577984fa — louiz’ Allow the execution of multiple commands after the IRC connection 5 years ago
                                                                                
3c1889fb Florent Le Coz
24dc05dd louiz’
3c1889fb Florent Le Coz
24dc05dd louiz’
53fc926d louiz’
24dc05dd louiz’
53fc926d louiz’
24dc05dd louiz’
0168b96b louiz’
3c1889fb Florent Le Coz
421c960d Florent Le Coz
0168b96b louiz’
50cadf3d louiz’
0168b96b louiz’
421c960d Florent Le Coz
0168b96b louiz’
421c960d Florent Le Coz
3c1889fb Florent Le Coz
421c960d Florent Le Coz
50cadf3d louiz’
3c1889fb Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
03714c6c louiz’
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
03714c6c louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
50cadf3d louiz’
421c960d Florent Le Coz
79cdf170 Florent Le Coz
50cadf3d louiz’
79cdf170 Florent Le Coz
421c960d Florent Le Coz
66887c22 Florent Le Coz
577984fa louiz’
66887c22 Florent Le Coz
3c1889fb Florent Le Coz
53fc926d louiz’
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
#include "catch.hpp"

#include <biboumi.h>

#ifdef USE_DATABASE

#include <cstdlib>

#include <database/database.hpp>

#include <config/config.hpp>

TEST_CASE("Database")
{
#ifdef PQ_FOUND
  std::string postgresql_uri{"postgresql://"};
  const char* env_value = ::getenv("TEST_POSTGRES_URI");
  if (env_value != nullptr)
    Database::open("postgresql://"s + env_value);
  else
#endif
    Database::open(":memory:");

  Database::raw_exec("DELETE FROM " + Database::irc_server_options.get_name());
  Database::raw_exec("DELETE FROM " + Database::irc_channel_options.get_name());

  SECTION("Basic retrieve and update")
    {
      auto o = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
      CHECK(Database::count(Database::irc_server_options) == 0);
      o.save(Database::db);
      CHECK(Database::count(Database::irc_server_options) == 1);
      o.col<Database::Realname>() = "Different realname";
      CHECK(o.col<Database::Realname>() == "Different realname");
      o.save(Database::db);
      CHECK(o.col<Database::Realname>() == "Different realname");
      CHECK(Database::count(Database::irc_server_options) == 1);

      auto a = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
      CHECK(a.col<Database::Realname>() == "Different realname");
      auto b = Database::get_irc_server_options("moumou@example.com", "irc.example.com");

      // b does not yet exist in the db, the object is created but not yet
      // inserted
      CHECK(1 == Database::count(Database::irc_server_options));

      b.save(Database::db);
      CHECK(2 == Database::count(Database::irc_server_options));

      CHECK(b.col<Database::Pass>() == "");
    }

  SECTION("channel options")
    {
      auto o = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");

      CHECK(o.col<Database::EncodingIn>() == "");
      o.col<Database::EncodingIn>() = "ISO-8859-1";
      CHECK(o.col<Database::RecordHistoryOptional>().is_set == false);
      o.col<Database::RecordHistoryOptional>().set_value(false);
      o.save(Database::db);
      auto b = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");
      CHECK(o.col<Database::EncodingIn>() == "ISO-8859-1");
      CHECK(o.col<Database::RecordHistoryOptional>().is_set == true);
      CHECK(o.col<Database::RecordHistoryOptional>().value == false);
    }

  SECTION("Channel options with server default")
    {
      const std::string owner{"CACA@example.com"};
      const std::string server{"irc.example.com"};
      const std::string chan1{"#foo"};

      auto c = Database::get_irc_channel_options(owner, server, chan1);
      auto s = Database::get_irc_server_options(owner, server);

      GIVEN("An option defined for the channel but not the server")
      {
        c.col<Database::EncodingIn>() = "channelEncoding";
        c.save(Database::db);
        WHEN("we fetch that option")
          {
            auto r = Database::get_irc_channel_options_with_server_default(owner, server, chan1);
            THEN("we get the channel option")
              CHECK(r.col<Database::EncodingIn>() == "channelEncoding");
          }
      }
      GIVEN("An option defined for the server but not the channel")
        {
          s.col<Database::EncodingIn>() = "serverEncoding";
          s.save(Database::db);
        WHEN("we fetch that option")
          {
            auto r = Database::get_irc_channel_options_with_server_default(owner, server, chan1);
            THEN("we get the server option")
              CHECK(r.col<Database::EncodingIn>() == "serverEncoding");
          }
        }
      GIVEN("An option defined for both the server and the channel")
        {
          s.col<Database::EncodingIn>() = "serverEncoding";
          s.save(Database::db);
          c.col<Database::EncodingIn>() = "channelEncoding";
          c.save(Database::db);
        WHEN("we fetch that option")
          {
            auto r = Database::get_irc_channel_options_with_server_default(owner, server, chan1);
            THEN("we get the channel option")
              CHECK(r.col<Database::EncodingIn>() == "channelEncoding");
          }
        WHEN("we fetch that option, with no channel specified")
          {
            auto r = Database::get_irc_channel_options_with_server_default(owner, server, "");
            THEN("we get the server option")
              CHECK(r.col<Database::EncodingIn>() == "serverEncoding");
          }
        }
    }

  SECTION("Server options")
    {
      const std::string owner{"toto@example.com"};
      const std::string owner2{"toto2@example.com"};
      const std::string server{"irc.example.com"};

      auto soptions = Database::get_irc_server_options(owner, server);
      auto soptions2 = Database::get_irc_server_options(owner2, server);

      auto after_connection_commands =  Database::get_after_connection_commands(soptions);
      CHECK(after_connection_commands.empty());

      soptions.save(Database::db);
      soptions2.save(Database::db);
      auto com = Database::after_connection_commands.row();
      com.col<Database::AfterConnectionCommand>() = "first";
      after_connection_commands.push_back(com);
      com.col<Database::AfterConnectionCommand>() = "second";
      after_connection_commands.push_back(com);
      Database::set_after_connection_commands(soptions, after_connection_commands);

      after_connection_commands.clear();
      com.col<Database::AfterConnectionCommand>() = "first";
      after_connection_commands.push_back(com);
      com.col<Database::AfterConnectionCommand>() = "second";
      after_connection_commands.push_back(com);
      Database::set_after_connection_commands(soptions2, after_connection_commands);

      after_connection_commands =  Database::get_after_connection_commands(soptions);
      CHECK(after_connection_commands.size() == 2);
      after_connection_commands =  Database::get_after_connection_commands(soptions2);
      CHECK(after_connection_commands.size() == 2);

      after_connection_commands.clear();
      after_connection_commands.push_back(com);
      Database::set_after_connection_commands(soptions, after_connection_commands);

      after_connection_commands =  Database::get_after_connection_commands(soptions);
      CHECK(after_connection_commands.size() == 1);
      after_connection_commands =  Database::get_after_connection_commands(soptions2);
      CHECK(after_connection_commands.size() == 2);
    }

  Database::close();
}
#endif