~hkdb/geary/disco-3.34.0

« back to all changes in this revision

Viewing changes to test/engine/imap-db/imap-db-account-test.vala

  • Committer: hkdb
  • Date: 2019-09-27 11:28:53 UTC
  • Revision ID: hkdb@3df.io-20190927112853-x02njxcww51q9jfp
Tags: upstream-3.34.0-disco
ImportĀ upstreamĀ versionĀ 3.34.0-disco

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2019 Michael Gratton <mike@vee.net>
 
3
 *
 
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.
 
6
 */
 
7
 
 
8
 
 
9
class Geary.ImapDB.AccountTest : TestCase {
 
10
 
 
11
 
 
12
    private GLib.File? tmp_dir = null;
 
13
    private Geary.AccountInformation? config = null;
 
14
    private Account? account = null;
 
15
    private FolderRoot? root = null;
 
16
 
 
17
 
 
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);
 
29
    }
 
30
 
 
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")
 
34
        );
 
35
 
 
36
        this.config = new Geary.AccountInformation(
 
37
            "test",
 
38
            ServiceProvider.OTHER,
 
39
            new MockCredentialsMediator(),
 
40
            new Geary.RFC822.MailboxAddress(null, "test@example.com")
 
41
        );
 
42
 
 
43
        this.account = new Account(
 
44
            config,
 
45
            this.tmp_dir,
 
46
            GLib.File.new_for_path(_SOURCE_ROOT_DIR).get_child("sql")
 
47
        );
 
48
        this.account.open_async.begin(
 
49
            null,
 
50
            (obj, ret) => { async_complete(ret); }
 
51
        );
 
52
        this.account.open_async.end(async_result());
 
53
 
 
54
        this.root = new FolderRoot("#test", false);
 
55
    }
 
56
 
 
57
    public override void tear_down() throws GLib.Error {
 
58
        this.root = null;
 
59
        this.account.close_async.begin(
 
60
            null,
 
61
            (obj, ret) => { async_complete(ret); }
 
62
        );
 
63
        this.account.close_async.end(async_result());
 
64
        this.account = null;
 
65
        this.config = null;
 
66
 
 
67
        delete_file(this.tmp_dir);
 
68
        this.tmp_dir = null;
 
69
    }
 
70
 
 
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>()
 
77
                ),
 
78
                new Imap.StatusData(
 
79
                    new Imap.MailboxSpecifier("test"),
 
80
                    10, // total
 
81
                    9, // recent
 
82
                    new Imap.UID(8),
 
83
                    new Imap.UIDValidity(7),
 
84
                    6 //unseen
 
85
                ),
 
86
                new Imap.Capabilities(1)
 
87
            )
 
88
        );
 
89
 
 
90
        this.account.clone_folder_async.begin(
 
91
            folder,
 
92
            null,
 
93
            (obj, ret) => { async_complete(ret); }
 
94
        );
 
95
        this.account.clone_folder_async.end(async_result());
 
96
 
 
97
        Geary.Db.Result result = this.account.db.query(
 
98
            "SELECT * FROM FolderTable;"
 
99
        );
 
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");
 
104
    }
 
105
 
 
106
    public void create_child_folder() throws GLib.Error {
 
107
        this.account.db.exec(
 
108
            "INSERT INTO FolderTable (id, name) VALUES (1, 'test');"
 
109
        );
 
110
 
 
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>()
 
116
                ),
 
117
                new Imap.StatusData(
 
118
                    new Imap.MailboxSpecifier("test>child"),
 
119
                    10, // total
 
120
                    9, // recent
 
121
                    new Imap.UID(8),
 
122
                    new Imap.UIDValidity(7),
 
123
                    6 //unseen
 
124
                ),
 
125
                new Imap.Capabilities(1)
 
126
            )
 
127
        );
 
128
 
 
129
        this.account.clone_folder_async.begin(
 
130
            folder,
 
131
            null,
 
132
            (obj, ret) => { async_complete(ret); }
 
133
        );
 
134
        this.account.clone_folder_async.end(async_result());
 
135
 
 
136
        Geary.Db.Result result = this.account.db.query(
 
137
            "SELECT * FROM FolderTable WHERE id != 1;"
 
138
        );
 
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");
 
143
    }
 
144
 
 
145
    public void list_folders() throws GLib.Error {
 
146
        this.account.db.exec("""
 
147
            INSERT INTO FolderTable (id, name, parent_id)
 
148
            VALUES
 
149
                (1, 'test1', null),
 
150
                (2, 'test2', 1),
 
151
                (3, 'test3', 2);
 
152
        """);
 
153
 
 
154
        this.account.list_folders_async.begin(
 
155
            this.account.imap_folder_root,
 
156
            null,
 
157
            (obj, ret) => { async_complete(ret); }
 
158
        );
 
159
        Gee.Collection<Geary.ImapDB.Folder> result =
 
160
            this.account.list_folders_async.end(async_result());
 
161
 
 
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");
 
165
 
 
166
        this.account.list_folders_async.begin(
 
167
            test1.get_path(),
 
168
            null,
 
169
            (obj, ret) => { async_complete(ret); }
 
170
        );
 
171
        result = this.account.list_folders_async.end(async_result());
 
172
 
 
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");
 
176
 
 
177
        this.account.list_folders_async.begin(
 
178
            test2.get_path(),
 
179
            null,
 
180
            (obj, ret) => { async_complete(ret); }
 
181
        );
 
182
        result = this.account.list_folders_async.end(async_result());
 
183
 
 
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");
 
187
    }
 
188
 
 
189
    public void delete_folder() throws GLib.Error {
 
190
        this.account.db.exec("""
 
191
            INSERT INTO FolderTable (id, name, parent_id)
 
192
            VALUES
 
193
                (1, 'test1', null),
 
194
                (2, 'test2', 1);
 
195
        """);
 
196
 
 
197
        this.account.delete_folder_async.begin(
 
198
            this.root.get_child("test1").get_child("test2"),
 
199
            null,
 
200
            (obj, ret) => { async_complete(ret); }
 
201
        );
 
202
        this.account.delete_folder_async.end(async_result());
 
203
 
 
204
        this.account.delete_folder_async.begin(
 
205
            this.root.get_child("test1"),
 
206
            null,
 
207
            (obj, ret) => { async_complete(ret); }
 
208
        );
 
209
        this.account.delete_folder_async.end(async_result());
 
210
    }
 
211
 
 
212
    public void delete_folder_with_child() throws GLib.Error {
 
213
        this.account.db.exec("""
 
214
            INSERT INTO FolderTable (id, name, parent_id)
 
215
            VALUES
 
216
                (1, 'test1', null),
 
217
                (2, 'test2', 1);
 
218
        """);
 
219
 
 
220
        this.account.delete_folder_async.begin(
 
221
            this.root.get_child("test1"),
 
222
            null,
 
223
            (obj, ret) => { async_complete(ret); }
 
224
        );
 
225
        try {
 
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);
 
230
        }
 
231
    }
 
232
 
 
233
    public void delete_nonexistent_folder() throws GLib.Error {
 
234
        this.account.db.exec("""
 
235
            INSERT INTO FolderTable (id, name, parent_id)
 
236
            VALUES
 
237
                (1, 'test1', null),
 
238
                (2, 'test2', 1);
 
239
        """);
 
240
 
 
241
        this.account.delete_folder_async.begin(
 
242
            this.root.get_child("test3"),
 
243
            null,
 
244
            (obj, ret) => { async_complete(ret); }
 
245
        );
 
246
        try {
 
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);
 
251
        }
 
252
    }
 
253
 
 
254
    public void fetch_base_folder() throws GLib.Error {
 
255
        this.account.db.exec("""
 
256
            INSERT INTO FolderTable (id, name, parent_id)
 
257
            VALUES
 
258
                (1, 'test1', null),
 
259
                (2, 'test2', 1);
 
260
        """);
 
261
 
 
262
        this.account.fetch_folder_async.begin(
 
263
            this.root.get_child("test1"),
 
264
            null,
 
265
            (obj, ret) => { async_complete(ret); }
 
266
        );
 
267
 
 
268
        Folder? result = this.account.fetch_folder_async.end(async_result());
 
269
        assert_non_null(result);
 
270
        assert_string("test1", result.get_path().name);
 
271
    }
 
272
 
 
273
    public void fetch_child_folder() throws GLib.Error {
 
274
        this.account.db.exec("""
 
275
            INSERT INTO FolderTable (id, name, parent_id)
 
276
            VALUES
 
277
                (1, 'test1', null),
 
278
                (2, 'test2', 1);
 
279
        """);
 
280
 
 
281
        this.account.fetch_folder_async.begin(
 
282
            this.root.get_child("test1").get_child("test2"),
 
283
            null,
 
284
            (obj, ret) => { async_complete(ret); }
 
285
        );
 
286
 
 
287
        Folder? result = this.account.fetch_folder_async.end(async_result());
 
288
        assert_non_null(result);
 
289
        assert_string("test2", result.get_path().name);
 
290
    }
 
291
 
 
292
    public void fetch_nonexistent_folder() throws GLib.Error {
 
293
        this.account.db.exec("""
 
294
            INSERT INTO FolderTable (id, name, parent_id)
 
295
            VALUES
 
296
                (1, 'test1', null),
 
297
                (2, 'test2', 1);
 
298
        """);
 
299
 
 
300
        this.account.fetch_folder_async.begin(
 
301
            this.root.get_child("test3"),
 
302
            null,
 
303
            (obj, ret) => { async_complete(ret); }
 
304
        );
 
305
        try {
 
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);
 
310
        }
 
311
    }
 
312
 
 
313
}