Changeset View
Changeset View
Standalone View
Standalone View
tests/testlib/akonadifakestorage.cpp
Show First 20 Lines • Show All 103 Lines • ▼ Show 20 Line(s) | |||||
104 | } | 104 | } | ||
105 | 105 | | |||
106 | KJob *AkonadiFakeStorage::createItem(Akonadi::Item item, Akonadi::Collection collection) | 106 | KJob *AkonadiFakeStorage::createItem(Akonadi::Item item, Akonadi::Collection collection) | ||
107 | { | 107 | { | ||
108 | Q_ASSERT(!item.isValid()); | 108 | Q_ASSERT(!item.isValid()); | ||
109 | 109 | | |||
110 | auto job = new FakeJob; | 110 | auto job = new FakeJob; | ||
111 | if (!m_data->item(item.id()).isValid()) { | 111 | if (!m_data->item(item.id()).isValid()) { | ||
112 | job->setExpectedError(m_data->storageBehavior().createNextItemErrorCode(), | ||||
113 | m_data->storageBehavior().createNextItemErrorText()); | ||||
112 | Utils::JobHandler::install(job, [=] () mutable { | 114 | Utils::JobHandler::install(job, [=] () mutable { | ||
115 | if (!job->error()) { | ||||
113 | item.setId(m_data->maxItemId() + 1); | 116 | item.setId(m_data->maxItemId() + 1); | ||
114 | item.setParentCollection(collection); | 117 | item.setParentCollection(collection); | ||
115 | // Force payload detach | 118 | // Force payload detach | ||
116 | item.setPayloadFromData(item.payloadData()); | 119 | item.setPayloadFromData(item.payloadData()); | ||
117 | m_data->createItem(item); | 120 | m_data->createItem(item); | ||
121 | } | ||||
118 | }); | 122 | }); | ||
119 | } else { | 123 | } else { | ||
120 | job->setExpectedError(1, QStringLiteral("Item already exists")); | 124 | job->setExpectedError(1, QStringLiteral("Item already exists")); | ||
121 | Utils::JobHandler::install(job, noop); | 125 | Utils::JobHandler::install(job, noop); | ||
122 | } | 126 | } | ||
123 | return job; | 127 | return job; | ||
124 | } | 128 | } | ||
125 | 129 | | |||
126 | KJob *AkonadiFakeStorage::updateItem(Akonadi::Item item, QObject *parent) | 130 | KJob *AkonadiFakeStorage::updateItem(Akonadi::Item item, QObject *parent) | ||
127 | { | 131 | { | ||
128 | auto job = new FakeJob(parent); | 132 | auto job = new FakeJob(parent); | ||
129 | auto startMode = startModeForParent(parent); | 133 | auto startMode = startModeForParent(parent); | ||
130 | 134 | | |||
131 | if (m_data->item(item.id()).isValid()) { | 135 | if (m_data->item(item.id()).isValid()) { | ||
136 | job->setExpectedError(m_data->storageBehavior().updateNextItemErrorCode(), | ||||
137 | m_data->storageBehavior().updateNextItemErrorText()); | ||||
132 | Utils::JobHandler::install(job, [=] () mutable { | 138 | Utils::JobHandler::install(job, [=] () mutable { | ||
139 | if (!job->error()) { | ||||
133 | // Force payload detach | 140 | // Force payload detach | ||
134 | item.setPayloadFromData(item.payloadData()); | 141 | item.setPayloadFromData(item.payloadData()); | ||
135 | m_data->modifyItem(item); | 142 | m_data->modifyItem(item); | ||
143 | } | ||||
136 | }, startMode); | 144 | }, startMode); | ||
137 | } else { | 145 | } else { | ||
138 | job->setExpectedError(1, QStringLiteral("Item doesn't exist")); | 146 | job->setExpectedError(1, QStringLiteral("Item doesn't exist")); | ||
139 | Utils::JobHandler::install(job, noop, startMode); | 147 | Utils::JobHandler::install(job, noop, startMode); | ||
140 | } | 148 | } | ||
141 | return job; | 149 | return job; | ||
142 | } | 150 | } | ||
143 | 151 | | |||
144 | KJob *AkonadiFakeStorage::removeItem(Akonadi::Item item) | 152 | KJob *AkonadiFakeStorage::removeItem(Akonadi::Item item) | ||
145 | { | 153 | { | ||
146 | auto job = new FakeJob; | 154 | auto job = new FakeJob; | ||
147 | if (m_data->item(item.id()).isValid()) { | 155 | if (m_data->item(item.id()).isValid()) { | ||
148 | Utils::JobHandler::install(job, [=] { | 156 | Utils::JobHandler::install(job, [=] { | ||
157 | if (!job->error()) { | ||||
149 | m_data->removeItem(item); | 158 | m_data->removeItem(item); | ||
159 | } | ||||
150 | }); | 160 | }); | ||
151 | } else { | 161 | } else { | ||
152 | job->setExpectedError(1, QStringLiteral("Item doesn't exist")); | 162 | job->setExpectedError(1, QStringLiteral("Item doesn't exist")); | ||
153 | Utils::JobHandler::install(job, noop); | 163 | Utils::JobHandler::install(job, noop); | ||
154 | } | 164 | } | ||
155 | return job; | 165 | return job; | ||
156 | } | 166 | } | ||
157 | 167 | | |||
158 | KJob *AkonadiFakeStorage::removeItems(Akonadi::Item::List items, QObject *parent) | 168 | KJob *AkonadiFakeStorage::removeItems(Akonadi::Item::List items, QObject *parent) | ||
159 | { | 169 | { | ||
160 | auto job = new FakeJob; | 170 | auto job = new FakeJob; | ||
161 | auto startMode = startModeForParent(parent); | 171 | auto startMode = startModeForParent(parent); | ||
162 | bool allItemsExist = std::all_of(items.constBegin(), items.constEnd(), | 172 | bool allItemsExist = std::all_of(items.constBegin(), items.constEnd(), | ||
163 | [=] (const Akonadi::Item &item) { | 173 | [=] (const Akonadi::Item &item) { | ||
164 | return m_data->item(item.id()).isValid(); | 174 | return m_data->item(item.id()).isValid(); | ||
165 | }); | 175 | }); | ||
166 | 176 | | |||
167 | if (allItemsExist) { | 177 | if (allItemsExist) { | ||
178 | job->setExpectedError(m_data->storageBehavior().deleteNextItemErrorCode(), | ||||
179 | m_data->storageBehavior().deleteNextItemErrorText()); | ||||
168 | Utils::JobHandler::install(job, [=] { | 180 | Utils::JobHandler::install(job, [=] { | ||
181 | if (!job->error()) { | ||||
169 | foreach (const Akonadi::Item &item, items) { | 182 | foreach (const Akonadi::Item &item, items) { | ||
170 | m_data->removeItem(item); | 183 | m_data->removeItem(item); | ||
171 | } | 184 | } | ||
185 | } | ||||
172 | }, startMode); | 186 | }, startMode); | ||
173 | } else { | 187 | } else { | ||
174 | job->setExpectedError(1, QStringLiteral("At least one item doesn't exist")); | 188 | job->setExpectedError(1, QStringLiteral("At least one item doesn't exist")); | ||
175 | Utils::JobHandler::install(job, noop, startMode); | 189 | Utils::JobHandler::install(job, noop, startMode); | ||
176 | } | 190 | } | ||
177 | return job; | 191 | return job; | ||
178 | } | 192 | } | ||
179 | 193 | | |||
180 | KJob *AkonadiFakeStorage::moveItem(Akonadi::Item item, Akonadi::Collection collection, QObject *parent) | 194 | KJob *AkonadiFakeStorage::moveItem(Akonadi::Item item, Akonadi::Collection collection, QObject *parent) | ||
181 | { | 195 | { | ||
182 | auto job = new FakeJob(parent); | 196 | auto job = new FakeJob(parent); | ||
183 | auto startMode = startModeForParent(parent); | 197 | auto startMode = startModeForParent(parent); | ||
184 | if (m_data->item(item.id()).isValid() | 198 | if (m_data->item(item.id()).isValid() | ||
185 | && m_data->collection(collection.id()).isValid()) { | 199 | && m_data->collection(collection.id()).isValid()) { | ||
186 | Utils::JobHandler::install(job, [=] () mutable { | 200 | Utils::JobHandler::install(job, [=] () mutable { | ||
201 | if (!job->error()) { | ||||
187 | item.setParentCollection(collection); | 202 | item.setParentCollection(collection); | ||
188 | // Force payload detach | 203 | // Force payload detach | ||
189 | item.setPayloadFromData(item.payloadData()); | 204 | item.setPayloadFromData(item.payloadData()); | ||
190 | m_data->modifyItem(item); | 205 | m_data->modifyItem(item); | ||
206 | } | ||||
191 | }, startMode); | 207 | }, startMode); | ||
192 | } else { | 208 | } else { | ||
193 | job->setExpectedError(1, QStringLiteral("The item or the collection doesn't exist")); | 209 | job->setExpectedError(1, QStringLiteral("The item or the collection doesn't exist")); | ||
194 | Utils::JobHandler::install(job, noop, startMode); | 210 | Utils::JobHandler::install(job, noop, startMode); | ||
195 | } | 211 | } | ||
196 | return job; | 212 | return job; | ||
197 | } | 213 | } | ||
198 | 214 | | |||
199 | KJob *AkonadiFakeStorage::moveItems(Akonadi::Item::List items, Akonadi::Collection collection, QObject *parent) | 215 | KJob *AkonadiFakeStorage::moveItems(Akonadi::Item::List items, Akonadi::Collection collection, QObject *parent) | ||
200 | { | 216 | { | ||
201 | using namespace std::placeholders; | 217 | using namespace std::placeholders; | ||
202 | 218 | | |||
203 | auto job = new FakeJob(parent); | 219 | auto job = new FakeJob(parent); | ||
204 | auto startMode = startModeForParent(parent); | 220 | auto startMode = startModeForParent(parent); | ||
205 | bool allItemsExist = std::all_of(items.constBegin(), items.constEnd(), | 221 | bool allItemsExist = std::all_of(items.constBegin(), items.constEnd(), | ||
206 | [=] (const Akonadi::Item &item) { | 222 | [=] (const Akonadi::Item &item) { | ||
207 | return m_data->item(item.id()).isValid(); | 223 | return m_data->item(item.id()).isValid(); | ||
208 | }); | 224 | }); | ||
209 | 225 | | |||
210 | if (allItemsExist | 226 | if (allItemsExist | ||
211 | && m_data->collection(collection.id()).isValid()) { | 227 | && m_data->collection(collection.id()).isValid()) { | ||
212 | Utils::JobHandler::install(job, [=] () mutable { | 228 | Utils::JobHandler::install(job, [=] () mutable { | ||
229 | if (!job->error()) { | ||||
213 | std::transform(items.constBegin(), items.constEnd(), | 230 | std::transform(items.constBegin(), items.constEnd(), | ||
214 | items.begin(), | 231 | items.begin(), | ||
215 | [=] (const Akonadi::Item &item) { | 232 | [=] (const Akonadi::Item &item) { | ||
216 | auto result = item; | 233 | auto result = item; | ||
217 | result.setParentCollection(collection); | 234 | result.setParentCollection(collection); | ||
218 | // Force payload detach | 235 | // Force payload detach | ||
219 | result.setPayloadFromData(result.payloadData()); | 236 | result.setPayloadFromData(result.payloadData()); | ||
220 | return result; | 237 | return result; | ||
221 | }); | 238 | }); | ||
222 | 239 | | |||
223 | foreach (const Akonadi::Item &item, items) { | 240 | foreach (const Akonadi::Item &item, items) { | ||
224 | m_data->modifyItem(item); | 241 | m_data->modifyItem(item); | ||
225 | } | 242 | } | ||
243 | } | ||||
226 | }, startMode); | 244 | }, startMode); | ||
227 | } else { | 245 | } else { | ||
228 | job->setExpectedError(1, QStringLiteral("One of the items or the collection doesn't exist")); | 246 | job->setExpectedError(1, QStringLiteral("One of the items or the collection doesn't exist")); | ||
229 | Utils::JobHandler::install(job, noop, startMode); | 247 | Utils::JobHandler::install(job, noop, startMode); | ||
230 | } | 248 | } | ||
231 | return job; | 249 | return job; | ||
232 | } | 250 | } | ||
233 | 251 | | |||
234 | KJob *AkonadiFakeStorage::createCollection(Akonadi::Collection collection, QObject *parent) | 252 | KJob *AkonadiFakeStorage::createCollection(Akonadi::Collection collection, QObject *parent) | ||
235 | { | 253 | { | ||
236 | Q_ASSERT(!collection.isValid()); | 254 | Q_ASSERT(!collection.isValid()); | ||
237 | 255 | | |||
238 | auto job = new FakeJob(parent); | 256 | auto job = new FakeJob(parent); | ||
239 | auto startMode = startModeForParent(parent); | 257 | auto startMode = startModeForParent(parent); | ||
240 | if (!m_data->collection(collection.id()).isValid()) { | 258 | if (!m_data->collection(collection.id()).isValid()) { | ||
241 | Utils::JobHandler::install(job, [=] () mutable { | 259 | Utils::JobHandler::install(job, [=] () mutable { | ||
260 | if (!job->error()) { | ||||
242 | collection.setId(m_data->maxCollectionId() + 1); | 261 | collection.setId(m_data->maxCollectionId() + 1); | ||
243 | m_data->createCollection(collection); | 262 | m_data->createCollection(collection); | ||
263 | } | ||||
244 | }, startMode); | 264 | }, startMode); | ||
245 | } else { | 265 | } else { | ||
246 | job->setExpectedError(1, QStringLiteral("The collection already exists")); | 266 | job->setExpectedError(1, QStringLiteral("The collection already exists")); | ||
247 | Utils::JobHandler::install(job, noop, startMode); | 267 | Utils::JobHandler::install(job, noop, startMode); | ||
248 | } | 268 | } | ||
249 | return job; | 269 | return job; | ||
250 | } | 270 | } | ||
251 | 271 | | |||
252 | KJob *AkonadiFakeStorage::updateCollection(Akonadi::Collection collection, QObject *parent) | 272 | KJob *AkonadiFakeStorage::updateCollection(Akonadi::Collection collection, QObject *parent) | ||
253 | { | 273 | { | ||
254 | auto job = new FakeJob(parent); | 274 | auto job = new FakeJob(parent); | ||
255 | auto startMode = startModeForParent(parent); | 275 | auto startMode = startModeForParent(parent); | ||
256 | if (m_data->collection(collection.id()).isValid()) { | 276 | if (m_data->collection(collection.id()).isValid()) { | ||
257 | Utils::JobHandler::install(job, [=] { | 277 | Utils::JobHandler::install(job, [=] { | ||
278 | if (!job->error()) { | ||||
258 | m_data->modifyCollection(collection); | 279 | m_data->modifyCollection(collection); | ||
280 | } | ||||
259 | }, startMode); | 281 | }, startMode); | ||
260 | } else { | 282 | } else { | ||
261 | job->setExpectedError(1, QStringLiteral("The collection doesn't exist")); | 283 | job->setExpectedError(1, QStringLiteral("The collection doesn't exist")); | ||
262 | Utils::JobHandler::install(job, noop, startMode); | 284 | Utils::JobHandler::install(job, noop, startMode); | ||
263 | } | 285 | } | ||
264 | return job; | 286 | return job; | ||
265 | } | 287 | } | ||
266 | 288 | | |||
267 | KJob *AkonadiFakeStorage::removeCollection(Akonadi::Collection collection, QObject *parent) | 289 | KJob *AkonadiFakeStorage::removeCollection(Akonadi::Collection collection, QObject *parent) | ||
268 | { | 290 | { | ||
269 | auto job = new FakeJob(parent); | 291 | auto job = new FakeJob(parent); | ||
270 | auto startMode = startModeForParent(parent); | 292 | auto startMode = startModeForParent(parent); | ||
271 | if (m_data->collection(collection.id()).isValid()) { | 293 | if (m_data->collection(collection.id()).isValid()) { | ||
272 | Utils::JobHandler::install(job, [=] { | 294 | Utils::JobHandler::install(job, [=] { | ||
295 | if (!job->error()) { | ||||
273 | m_data->removeCollection(collection); | 296 | m_data->removeCollection(collection); | ||
297 | } | ||||
274 | }, startMode); | 298 | }, startMode); | ||
275 | } else { | 299 | } else { | ||
276 | job->setExpectedError(1, QStringLiteral("The collection doesn't exist")); | 300 | job->setExpectedError(1, QStringLiteral("The collection doesn't exist")); | ||
277 | Utils::JobHandler::install(job, noop, startMode); | 301 | Utils::JobHandler::install(job, noop, startMode); | ||
278 | } | 302 | } | ||
279 | return job; | 303 | return job; | ||
280 | } | 304 | } | ||
281 | 305 | | |||
▲ Show 20 Lines • Show All 125 Lines • Show Last 20 Lines |