LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
common.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <tuple>
12 #include <QtTest>
13 #include <QSqlError>
14 #include <oral/oral.h>
15 
16 namespace lco = LC::Util::oral;
17 
18 #define ORAL_FACTORY_SQLITE 1
19 #define ORAL_FACTORY_POSTGRES 2
20 
21 #if ORAL_FACTORY == ORAL_FACTORY_SQLITE
22 
23 using OralFactory = lco::SQLiteImplFactory;
24 
25 #elif ORAL_FACTORY == ORAL_FACTORY_POSTGRES
26 
27 #include <oral/pgimpl.h>
28 
29 using OralFactory = lco::PostgreSQLImplFactory;
30 
31 #else
32 
33 #error "Unknown oral tests factory"
34 
35 #endif
36 
37 template<typename T, typename = decltype (T {}.AsTuple ())>
38 auto operator== (const T& left, const T& right)
39 {
40  return left.AsTuple () == right.AsTuple ();
41 }
42 
43 namespace LC::Util::oral
44 {
45  template<typename T, typename... Args>
46  char* toString (const PKey<T, Args...>& pkey)
47  {
48  return QTest::toString (pkey.Val_);
49  }
50 }
51 
52 #define TOSTRING(n) inline char* toString (const n& rec) { return toString (#n, rec); }
53 
54 template<typename T, typename TupleType = decltype (T {}.AsTuple ())>
55 char* toString (const char *name, const T& t)
56 {
57  using QTest::toString;
58 
59  QByteArray ba { name };
60  ba.append (" { ");
61 
62  std::apply ([&ba] (const auto&... args) { (ba.append (toString (args)).append (", "), ...); }, t.AsTuple ());
63 
64  if (std::tuple_size<TupleType>::value >= 1)
65  ba.chop (2);
66  ba.append (" }");
67 
68  return qstrdup (ba.data ());
69 }
70 
71 namespace LC::Util
72 {
73  QSqlDatabase MakeDatabase (const QString& name = ":memory:")
74  {
75 #if ORAL_FACTORY == ORAL_FACTORY_SQLITE
76  auto db = QSqlDatabase::addDatabase ("QSQLITE", Util::GenConnectionName ("TestConnection"));
77  db.setDatabaseName (name);
78  if (!db.open ())
79  throw std::runtime_error { "cannot create test database" };
80 
81  RunTextQuery (db, "PRAGMA foreign_keys = ON;");
82 
83  return db;
84 #elif ORAL_FACTORY == ORAL_FACTORY_POSTGRES
85  Q_UNUSED (name)
86 
87  auto db = QSqlDatabase::addDatabase ("QPSQL", Util::GenConnectionName ("TestConnection"));
88 
89  db.setHostName ("localhost");
90  db.setPort (5432);
91  db.setUserName (qgetenv ("TEST_POSTGRES_USERNAME"));
92 
93  if (!db.open ())
94  {
95  DBLock::DumpError (db.lastError ());
96  throw std::runtime_error { "cannot create test database" };
97  }
98 
99  return db;
100 #endif
101  }
102 
103  template<typename T>
104  auto PrepareRecords (QSqlDatabase db, int count = 3)
105  {
106  auto adapted = Util::oral::AdaptPtr<T, OralFactory> (db);
107  for (int i = 0; i < count; ++i)
108  adapted->Insert ({ i, QString::number (i) });
109  return adapted;
110  }
111 }
LC::Util::DBLock::DumpError
static UTIL_DB_API void DumpError(const QSqlError &error)
Dumps the error to the qWarning() stream.
Definition: dblock.cpp:74
LC::Util::oral::toString
char * toString(const PKey< T, Args... > &pkey)
Definition: common.h:46
LC::Util::oral::IndirectHolderBase< T, PKey< T, Tags... > >::Val_
T Val_
Definition: oraltypes.h:50
LC::Util::MakeDatabase
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition: common.h:73
LC::Util
Definition: icoreproxy.h:33
pgimpl.h
LC::Util::oral
Definition: impldefs.h:17
toString
char * toString(const char *name, const T &t)
Definition: common.h:55
LC::Util::PrepareRecords
auto PrepareRecords(QSqlDatabase db, int count=3)
Definition: common.h:104
LC::Util::oral::PostgreSQLImplFactory
detail::PostgreSQL::ImplFactory PostgreSQLImplFactory
Definition: pgimpl.h:103
LC::Util::oral::sph::count
constexpr detail::AggregateType< detail::AggregateFunction::Count, Ptr > count
Definition: oral.h:966
LC::Util::oral::PKey
Definition: oraltypes.h:88
LC::Util::RunTextQuery
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition: util.cpp:24
operator==
auto operator==(const T &left, const T &right)
Definition: common.h:38
LC::Util::GenConnectionName
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition: util.cpp:57
LC::Util::oral::SQLiteImplFactory
detail::SQLite::ImplFactory SQLiteImplFactory
Definition: sqliteimpl.h:75
oral.h