summaryrefslogtreecommitdiffstats
path: root/common/test.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-05-03 14:38:26 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-05-03 14:38:26 +0200
commit9d079a832ec9c70fccb3446843bd8d7579e3aafd (patch)
tree5ebd132b5ddf31f5183c71ecd030826b12b38895 /common/test.cpp
parenta5eb20dd70160f437835d4f5dffb27d8a912709d (diff)
downloadsink-9d079a832ec9c70fccb3446843bd8d7579e3aafd.tar.gz
sink-9d079a832ec9c70fccb3446843bd8d7579e3aafd.zip
An in memory testaccount that can be used for application testing.
Diffstat (limited to 'common/test.cpp')
-rw-r--r--common/test.cpp124
1 files changed, 124 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);