diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-03 14:38:26 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-03 14:38:26 +0200 |
commit | 9d079a832ec9c70fccb3446843bd8d7579e3aafd (patch) | |
tree | 5ebd132b5ddf31f5183c71ecd030826b12b38895 /common/test.cpp | |
parent | a5eb20dd70160f437835d4f5dffb27d8a912709d (diff) | |
download | sink-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.cpp | 124 |
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 | |||
31 | using namespace Sink; | ||
26 | 32 | ||
27 | void Sink::Test::initTest() | 33 | void 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 | |||
51 | template <typename T> | ||
52 | class TestFacade : public Sink::StoreFacade<T> | ||
53 | { | ||
54 | public: | ||
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 | |||
121 | Test::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 | |||
134 | template<typename DomainType> | ||
135 | void Sink::Test::TestAccount::addEntity(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) | ||
136 | { | ||
137 | mEntities[ApplicationDomain::getTypeName<DomainType>()].append(domainObject); | ||
138 | } | ||
139 | |||
140 | template<typename DomainType> | ||
141 | typename 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 | |||
148 | template<typename DomainType> | ||
149 | QList<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 | |||
161 | REGISTER_TYPE(ApplicationDomain::Event); | ||
162 | REGISTER_TYPE(ApplicationDomain::Mail); | ||
163 | REGISTER_TYPE(ApplicationDomain::Folder); | ||
164 | REGISTER_TYPE(ApplicationDomain::SinkResource); | ||
165 | REGISTER_TYPE(ApplicationDomain::SinkAccount); | ||
166 | REGISTER_TYPE(ApplicationDomain::Identity); | ||