summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/test.cpp124
-rw-r--r--common/test.h26
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/testaccounttest.cpp45
4 files changed, 196 insertions, 0 deletions
diff --git a/common/test.cpp b/common/test.cpp
index 0864f1a..c4dac89 100644
--- a/common/test.cpp
+++ b/common/test.cpp
@@ -23,6 +23,12 @@
23#include <QStandardPaths> 23#include <QStandardPaths>
24#include <QDir> 24#include <QDir>
25#include <QDebug> 25#include <QDebug>
26#include "facade.h"
27#include "facadefactory.h"
28#include "query.h"
29#include "resourceconfig.h"
30
31using namespace Sink;
26 32
27void Sink::Test::initTest() 33void Sink::Test::initTest()
28{ 34{
@@ -40,3 +46,121 @@ void Sink::Test::initTest()
40 // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation); 46 // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
41 QDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)).removeRecursively(); 47 QDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)).removeRecursively();
42} 48}
49
50
51template <typename T>
52class TestFacade : public Sink::StoreFacade<T>
53{
54public:
55 static std::shared_ptr<TestFacade<T>> registerFacade(Test::TestAccount *testAccount, const QByteArray &instanceIdentifier = QByteArray())
56 {
57 static QMap<QByteArray, std::shared_ptr<TestFacade<T>>> map;
58 auto facade = std::make_shared<TestFacade<T>>();
59 facade->mTestAccount = testAccount;
60 map.insert(instanceIdentifier, facade);
61 bool alwaysReturnFacade = instanceIdentifier.isEmpty();
62 Sink::FacadeFactory::instance().registerFacade<T, TestFacade<T>>("testresource", [alwaysReturnFacade](const QByteArray &instanceIdentifier) {
63 if (alwaysReturnFacade) {
64 return map.value(QByteArray());
65 }
66 return map.value(instanceIdentifier);
67 });
68 return facade;
69 }
70 ~TestFacade(){};
71 KAsync::Job<void> create(const T &domainObject) Q_DECL_OVERRIDE
72 {
73 mTestAccount->addEntity<T>(T::Ptr::create(domainObject));
74 return KAsync::null<void>();
75 };
76 KAsync::Job<void> modify(const T &domainObject) Q_DECL_OVERRIDE
77 {
78 // mTestAccount->modifyEntity<T>(domainObject);
79 return KAsync::null<void>();
80 };
81 KAsync::Job<void> remove(const T &domainObject) Q_DECL_OVERRIDE
82 {
83 //FIXME
84 // mTestAccount->removeEntity<T>(domainObject);
85 return KAsync::null<void>();
86 };
87 QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename T::Ptr>::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE
88 {
89 auto resultProvider = new Sink::ResultProvider<typename T::Ptr>();
90 resultProvider->onDone([resultProvider]() {
91 Trace() << "Result provider is done";
92 delete resultProvider;
93 });
94 // We have to do it this way, otherwise we're not setting the fetcher right
95 auto emitter = resultProvider->emitter();
96
97 resultProvider->setFetcher([query, resultProvider, this](const typename T::Ptr &parent) {
98 if (parent) {
99 Trace() << "Running the fetcher " << parent->identifier();
100 } else {
101 Trace() << "Running the fetcher.";
102 }
103 Trace() << "-------------------------.";
104 for (const auto &res : mTestAccount->entities<T>()) {
105 qDebug() << "Parent filter " << query.propertyFilter.value("parent").value.toByteArray() << res->identifier() << res->getProperty("parent").toByteArray();
106 auto parentProperty = res->getProperty("parent").toByteArray();
107 if ((!parent && parentProperty.isEmpty()) || (parent && parentProperty == parent->identifier()) || query.parentProperty.isEmpty()) {
108 qDebug() << "Found a match" << res->identifier();
109 resultProvider->add(res.template staticCast<T>());
110 }
111 }
112 resultProvider->initialResultSetComplete(parent);
113 });
114 auto job = KAsync::start<void>([query, resultProvider]() {});
115 return qMakePair(job, emitter);
116 }
117
118 Test::TestAccount *mTestAccount;
119};
120
121Test::TestAccount Sink::Test::TestAccount::registerAccount()
122{
123 Test::TestAccount account;
124 account.mFacades.insert(ApplicationDomain::getTypeName<ApplicationDomain::Folder>(), TestFacade<ApplicationDomain::Folder>::registerFacade(&account));
125 account.mFacades.insert(ApplicationDomain::getTypeName<ApplicationDomain::Mail>(), TestFacade<ApplicationDomain::Mail>::registerFacade(&account));
126 account.identifier = "testresource.instance1";
127 ResourceConfig::addResource(account.identifier, "testresource");
128 QMap<QByteArray, QVariant> configuration;
129 configuration.insert("account", account.identifier);
130 ResourceConfig::configureResource(account.identifier, configuration);
131 return account;
132}
133
134template<typename DomainType>
135void Sink::Test::TestAccount::addEntity(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)
136{
137 mEntities[ApplicationDomain::getTypeName<DomainType>()].append(domainObject);
138}
139
140template<typename DomainType>
141typename DomainType::Ptr Sink::Test::TestAccount::createEntity()
142{
143 auto entity = DomainType::Ptr::create(ApplicationDomain::ApplicationDomainType::createEntity<DomainType>(identifier));
144 addEntity<DomainType>(entity);
145 return entity;
146}
147
148template<typename DomainType>
149QList<Sink::ApplicationDomain::ApplicationDomainType::Ptr> Sink::Test::TestAccount::entities() const
150{
151 return mEntities.value(ApplicationDomain::getTypeName<DomainType>());
152}
153
154
155#define REGISTER_TYPE(T) \
156 template QList<Sink::ApplicationDomain::ApplicationDomainType::Ptr> Sink::Test::TestAccount::entities<T>() const; \
157 template void Sink::Test::TestAccount::addEntity<T>(const ApplicationDomain::ApplicationDomainType::Ptr &); \
158 template typename T::Ptr Sink::Test::TestAccount::createEntity<T>();
159
160
161REGISTER_TYPE(ApplicationDomain::Event);
162REGISTER_TYPE(ApplicationDomain::Mail);
163REGISTER_TYPE(ApplicationDomain::Folder);
164REGISTER_TYPE(ApplicationDomain::SinkResource);
165REGISTER_TYPE(ApplicationDomain::SinkAccount);
166REGISTER_TYPE(ApplicationDomain::Identity);
diff --git a/common/test.h b/common/test.h
index 146b4ac..e2bd1ea 100644
--- a/common/test.h
+++ b/common/test.h
@@ -21,6 +21,9 @@
21#pragma once 21#pragma once
22 22
23#include "sink_export.h" 23#include "sink_export.h"
24#include "applicationdomaintype.h"
25
26#include <memory>
24 27
25namespace Sink { 28namespace Sink {
26namespace Test { 29namespace Test {
@@ -31,5 +34,28 @@ namespace Test {
31 * and clears all data directories. 34 * and clears all data directories.
32 */ 35 */
33void SINK_EXPORT initTest(); 36void SINK_EXPORT initTest();
37
38class SINK_EXPORT TestAccount {
39public:
40 QByteArray identifier;
41 static TestAccount registerAccount();
42
43 template<typename DomainType>
44 void addEntity(const ApplicationDomain::ApplicationDomainType::Ptr &domainObject);
45
46 template<typename DomainType>
47 typename DomainType::Ptr createEntity();
48
49 template<typename DomainType>
50 QList<ApplicationDomain::ApplicationDomainType::Ptr> entities() const;
51
52private:
53 TestAccount(){};
54 TestAccount(const TestAccount &);
55 TestAccount &operator=(const TestAccount &);
56 QHash<QByteArray, QList<ApplicationDomain::ApplicationDomainType::Ptr> > mEntities;
57 QHash<QByteArray, std::shared_ptr<void> > mFacades;
58};
59
34} 60}
35} 61}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b25e7bc..b97596a 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -55,6 +55,7 @@ auto_tests (
55 modelinteractivitytest 55 modelinteractivitytest
56 inspectiontest 56 inspectiontest
57 accountstest 57 accountstest
58 testaccounttest
58) 59)
59target_link_libraries(dummyresourcetest sink_resource_dummy) 60target_link_libraries(dummyresourcetest sink_resource_dummy)
60target_link_libraries(dummyresourcebenchmark sink_resource_dummy) 61target_link_libraries(dummyresourcebenchmark sink_resource_dummy)
diff --git a/tests/testaccounttest.cpp b/tests/testaccounttest.cpp
new file mode 100644
index 0000000..c630846
--- /dev/null
+++ b/tests/testaccounttest.cpp
@@ -0,0 +1,45 @@
1#include <QtTest>
2#include <QDebug>
3#include <functional>
4
5#include "store.h"
6#include "test.h"
7#include "log.h"
8
9using namespace Sink;
10
11/**
12 * Test of the test account.
13 */
14class TestAccountTest : public QObject
15{
16 Q_OBJECT
17private slots:
18
19 void initTestCase()
20 {
21 // Sink::FacadeFactory::instance().resetFactory();
22 // ResourceConfig::clear();
23 Log::setDebugOutputLevel(Sink::Log::Trace);
24 Test::initTest();
25 }
26
27 void testLoad()
28 {
29 auto &&account = Test::TestAccount::registerAccount();
30 auto folder = ApplicationDomain::Folder::Ptr::create(ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Folder>());
31 account.addEntity<ApplicationDomain::Folder>(folder);
32
33 auto folders = account.entities<ApplicationDomain::Folder>();
34 QCOMPARE(folders.size(), 1);
35 QCOMPARE(account.entities<ApplicationDomain::Mail>().size(), 0);
36
37 auto mail = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Mail>();
38 Sink::Store::create(ApplicationDomain::Mail(account.identifier)).exec();
39 QCOMPARE(account.entities<ApplicationDomain::Mail>().size(), 1);
40 }
41
42};
43
44QTEST_MAIN(TestAccountTest)
45#include "testaccounttest.moc"