database.cpp 3.12 KB
Newer Older
louiz’'s avatar
louiz’ committed
1 2 3 4 5 6 7 8 9
#include "catch.hpp"

#include <database/database.hpp>

#include <config/config.hpp>

TEST_CASE("Database")
{
#ifdef USE_DATABASE
10
  Database::open(":memory:");
louiz’'s avatar
louiz’ committed
11 12
  Database::set_verbose(false);

13 14 15 16 17 18
  SECTION("Basic retrieve and update")
    {
      auto o = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
      o.update();
      auto a = Database::get_irc_server_options("zouzou@example.com", "irc.example.com");
      auto b = Database::get_irc_server_options("moumou@example.com", "irc.example.com");
louiz’'s avatar
louiz’ committed
19

20 21 22
      // b does not yet exist in the db, the object is created but not yet
      // inserted
      CHECK(1 == Database::count<db::IrcServerOptions>());
louiz’'s avatar
louiz’ committed
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
      b.update();
      CHECK(2 == Database::count<db::IrcServerOptions>());

      CHECK(b.pass == "");
      CHECK(b.pass.value() == "");
    }

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

      CHECK(o.encodingIn == "");
      o.encodingIn = "ISO-8859-1";
      o.update();
      auto b = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo");
      CHECK(o.encodingIn == "ISO-8859-1");
    }

  SECTION("Channel options with server default")
    {
      const std::string owner{"zouzou@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.encodingIn = "channelEncoding";
        c.update();
        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.encodingIn == "channelEncoding");
          }
      }
      GIVEN("An option defined for the server but not the channel")
        {
          s.encodingIn = "serverEncoding";
          s.update();
        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.encodingIn == "serverEncoding");
          }
        }
      GIVEN("An option defined for both the server and the channel")
        {
          s.encodingIn = "serverEncoding";
          s.update();
          c.encodingIn = "channelEncoding";
          c.update();
        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.encodingIn == "channelEncoding");
          }
86 87 88 89 90 91
        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.encodingIn == "serverEncoding");
          }
92 93
        }
    }
louiz’'s avatar
louiz’ committed
94 95

  Database::close();
louiz’'s avatar
louiz’ committed
96 97
#endif
}