LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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
16namespace 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
23using OralFactory = lco::SQLiteImplFactory;
24
25#elif ORAL_FACTORY == ORAL_FACTORY_POSTGRES
26
27#include <oral/pgimpl.h>
28
29using OralFactory = lco::PostgreSQLImplFactory;
30
31#else
32
33#error "Unknown oral tests factory"
34
35#endif
36
37template<typename T, typename = decltype (T {}.AsTuple ())>
38auto operator== (const T& left, const T& right)
39{
40 return left.AsTuple () == right.AsTuple ();
41}
42
43namespace 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
54template<typename T, typename TupleType = decltype (T {}.AsTuple ())>
55char* 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
71namespace 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}
static UTIL_DB_API void DumpError(const QSqlError &error)
Dumps the error to the qWarning() stream.
Definition dblock.cpp:68
char * toString(const char *name, const T &t)
Definition common.h:55
auto operator==(const T &left, const T &right)
Definition common.h:38
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition util.cpp:18
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition util.cpp:51
ObjectInfo_ptr< T > AdaptPtr(const QSqlDatabase &db)
Definition oral.h:1662
detail::SQLite::ImplFactory SQLiteImplFactory
Definition sqliteimpl.h:57
detail::PostgreSQL::ImplFactory PostgreSQLImplFactory
Definition pgimpl.h:52
char * toString(const PKey< T, Args... > &pkey)
Definition common.h:46
auto PrepareRecords(QSqlDatabase db, int count=3)
Definition common.h:104
QSqlDatabase MakeDatabase(const QString &name=":memory:")
Definition common.h:73