2
* Copyright 2019 Michael Gratton <mike@vee.net>
4
* This software is licensed under the GNU Lesser General Public License
5
* (version 2.1 or later). See the COPYING file in this distribution.
9
class Geary.ImapDB.AccountTest : TestCase {
12
private GLib.File? tmp_dir = null;
13
private Geary.AccountInformation? config = null;
14
private Account? account = null;
15
private FolderRoot? root = null;
18
public AccountTest() {
19
base("Geary.ImapDB.AccountTest");
20
add_test("create_base_folder", create_base_folder);
21
add_test("create_child_folder", create_child_folder);
22
add_test("list_folders", list_folders);
23
add_test("delete_folder", delete_folder);
24
add_test("delete_folder_with_child", delete_folder_with_child);
25
add_test("delete_nonexistent_folder", delete_nonexistent_folder);
26
add_test("fetch_base_folder", fetch_base_folder);
27
add_test("fetch_child_folder", fetch_child_folder);
28
add_test("fetch_nonexistent_folder", fetch_nonexistent_folder);
31
public override void set_up() throws GLib.Error {
32
this.tmp_dir = GLib.File.new_for_path(
33
GLib.DirUtils.make_tmp("geary-imap-db-account-test-XXXXXX")
36
this.config = new Geary.AccountInformation(
38
ServiceProvider.OTHER,
39
new MockCredentialsMediator(),
40
new Geary.RFC822.MailboxAddress(null, "test@example.com")
43
this.account = new Account(
46
GLib.File.new_for_path(_SOURCE_ROOT_DIR).get_child("sql")
48
this.account.open_async.begin(
50
(obj, ret) => { async_complete(ret); }
52
this.account.open_async.end(async_result());
54
this.root = new FolderRoot("#test", false);
57
public override void tear_down() throws GLib.Error {
59
this.account.close_async.begin(
61
(obj, ret) => { async_complete(ret); }
63
this.account.close_async.end(async_result());
67
delete_file(this.tmp_dir);
71
public void create_base_folder() throws GLib.Error {
72
Imap.Folder folder = new Imap.Folder(
73
this.root.get_child("test"),
74
new Imap.FolderProperties.selectable(
75
new Imap.MailboxAttributes(
76
Gee.Collection.empty<Geary.Imap.MailboxAttribute>()
79
new Imap.MailboxSpecifier("test"),
83
new Imap.UIDValidity(7),
86
new Imap.Capabilities(1)
90
this.account.clone_folder_async.begin(
93
(obj, ret) => { async_complete(ret); }
95
this.account.clone_folder_async.end(async_result());
97
Geary.Db.Result result = this.account.db.query(
98
"SELECT * FROM FolderTable;"
100
assert_false(result.finished, "Folder not created");
101
assert_string("test", result.string_for("name"), "Folder name");
102
assert_true(result.is_null_for("parent_id"), "Folder parent");
103
assert_false(result.next(), "Multiple rows inserted");
106
public void create_child_folder() throws GLib.Error {
107
this.account.db.exec(
108
"INSERT INTO FolderTable (id, name) VALUES (1, 'test');"
111
Imap.Folder folder = new Imap.Folder(
112
this.root.get_child("test").get_child("child"),
113
new Imap.FolderProperties.selectable(
114
new Imap.MailboxAttributes(
115
Gee.Collection.empty<Geary.Imap.MailboxAttribute>()
118
new Imap.MailboxSpecifier("test>child"),
122
new Imap.UIDValidity(7),
125
new Imap.Capabilities(1)
129
this.account.clone_folder_async.begin(
132
(obj, ret) => { async_complete(ret); }
134
this.account.clone_folder_async.end(async_result());
136
Geary.Db.Result result = this.account.db.query(
137
"SELECT * FROM FolderTable WHERE id != 1;"
139
assert_false(result.finished, "Folder not created");
140
assert_string("child", result.string_for("name"), "Folder name");
141
assert_int(1, result.int_for("parent_id"), "Folder parent");
142
assert_false(result.next(), "Multiple rows inserted");
145
public void list_folders() throws GLib.Error {
146
this.account.db.exec("""
147
INSERT INTO FolderTable (id, name, parent_id)
154
this.account.list_folders_async.begin(
155
this.account.imap_folder_root,
157
(obj, ret) => { async_complete(ret); }
159
Gee.Collection<Geary.ImapDB.Folder> result =
160
this.account.list_folders_async.end(async_result());
162
Folder test1 = traverse(result).first();
163
assert_int(1, result.size, "Base folder not listed");
164
assert_string("test1", test1.get_path().name, "Base folder name");
166
this.account.list_folders_async.begin(
169
(obj, ret) => { async_complete(ret); }
171
result = this.account.list_folders_async.end(async_result());
173
Folder test2 = traverse(result).first();
174
assert_int(1, result.size, "Child folder not listed");
175
assert_string("test2", test2.get_path().name, "Child folder name");
177
this.account.list_folders_async.begin(
180
(obj, ret) => { async_complete(ret); }
182
result = this.account.list_folders_async.end(async_result());
184
Folder test3 = traverse(result).first();
185
assert_int(1, result.size, "Grandchild folder not listed");
186
assert_string("test3", test3.get_path().name, "Grandchild folder name");
189
public void delete_folder() throws GLib.Error {
190
this.account.db.exec("""
191
INSERT INTO FolderTable (id, name, parent_id)
197
this.account.delete_folder_async.begin(
198
this.root.get_child("test1").get_child("test2"),
200
(obj, ret) => { async_complete(ret); }
202
this.account.delete_folder_async.end(async_result());
204
this.account.delete_folder_async.begin(
205
this.root.get_child("test1"),
207
(obj, ret) => { async_complete(ret); }
209
this.account.delete_folder_async.end(async_result());
212
public void delete_folder_with_child() throws GLib.Error {
213
this.account.db.exec("""
214
INSERT INTO FolderTable (id, name, parent_id)
220
this.account.delete_folder_async.begin(
221
this.root.get_child("test1"),
223
(obj, ret) => { async_complete(ret); }
226
this.account.delete_folder_async.end(async_result());
227
assert_not_reached();
228
} catch (GLib.Error err) {
229
assert_error(new ImapError.NOT_SUPPORTED(""), err);
233
public void delete_nonexistent_folder() throws GLib.Error {
234
this.account.db.exec("""
235
INSERT INTO FolderTable (id, name, parent_id)
241
this.account.delete_folder_async.begin(
242
this.root.get_child("test3"),
244
(obj, ret) => { async_complete(ret); }
247
this.account.delete_folder_async.end(async_result());
248
assert_not_reached();
249
} catch (GLib.Error err) {
250
assert_error(new EngineError.NOT_FOUND(""), err);
254
public void fetch_base_folder() throws GLib.Error {
255
this.account.db.exec("""
256
INSERT INTO FolderTable (id, name, parent_id)
262
this.account.fetch_folder_async.begin(
263
this.root.get_child("test1"),
265
(obj, ret) => { async_complete(ret); }
268
Folder? result = this.account.fetch_folder_async.end(async_result());
269
assert_non_null(result);
270
assert_string("test1", result.get_path().name);
273
public void fetch_child_folder() throws GLib.Error {
274
this.account.db.exec("""
275
INSERT INTO FolderTable (id, name, parent_id)
281
this.account.fetch_folder_async.begin(
282
this.root.get_child("test1").get_child("test2"),
284
(obj, ret) => { async_complete(ret); }
287
Folder? result = this.account.fetch_folder_async.end(async_result());
288
assert_non_null(result);
289
assert_string("test2", result.get_path().name);
292
public void fetch_nonexistent_folder() throws GLib.Error {
293
this.account.db.exec("""
294
INSERT INTO FolderTable (id, name, parent_id)
300
this.account.fetch_folder_async.begin(
301
this.root.get_child("test3"),
303
(obj, ret) => { async_complete(ret); }
306
this.account.fetch_folder_async.end(async_result());
307
assert_not_reached();
308
} catch (GLib.Error err) {
309
assert_error(new EngineError.NOT_FOUND(""), err);