[geary/mjog/invert-folder-class-hierarchy: 353/362] test: Add unit tests for new Geary.Folder email accessor methods




commit 97e7fd416ba7bedb2ca4d49c49fe8ba90ae2768e
Author: Michael Gratton <mike vee net>
Date:   Fri Feb 19 08:37:11 2021 +1100

    test: Add unit tests for new Geary.Folder email accessor methods

 .../imap-engine-account-based-test.vala            |  82 +++++
 .../imap-engine-generic-account-test.vala          |  67 +---
 .../imap-engine-minimal-folder-test.vala           | 357 +++++++++++++++++++++
 test/meson.build                                   |   2 +
 test/test-engine.vala                              |   1 +
 5 files changed, 444 insertions(+), 65 deletions(-)
---
diff --git a/test/engine/imap-engine/imap-engine-account-based-test.vala 
b/test/engine/imap-engine/imap-engine-account-based-test.vala
new file mode 100644
index 000000000..a8a902e96
--- /dev/null
+++ b/test/engine/imap-engine/imap-engine-account-based-test.vala
@@ -0,0 +1,82 @@
+/*
+ * Copyright © 2019-2021 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+internal abstract class Geary.ImapEngine.AccountBasedTest : TestCase {
+
+
+    internal class TestAccount : GenericAccount {
+
+        public TestAccount(AccountInformation config,
+                           ImapDB.Account local,
+                           Endpoint incoming_remote,
+                           Endpoint outgoing_remote) {
+            base(config, local, incoming_remote, outgoing_remote);
+        }
+
+        protected override MinimalFolder new_folder(ImapDB.Folder local_folder) {
+            return new MinimalFolder(
+                this,
+                local_folder,
+                NONE
+            );
+        }
+
+    }
+
+
+    protected GLib.File? tmp_dir = null;
+    protected Geary.AccountInformation? config = null;
+    protected ImapDB.Account? local_account = null;
+
+
+    protected AccountBasedTest(string name) {
+        base(name);
+    }
+
+    public override void set_up() throws GLib.Error {
+        this.tmp_dir = GLib.File.new_for_path(
+            GLib.DirUtils.make_tmp(
+                "geary-imap-engine-generic-account-test-XXXXXX"
+            )
+        );
+
+        this.config = new Geary.AccountInformation(
+            "test",
+            ServiceProvider.OTHER,
+            new Mock.CredentialsMediator(),
+            new Geary.RFC822.MailboxAddress(null, "test example com")
+        );
+
+        this.local_account = new ImapDB.Account(
+            config,
+            this.tmp_dir,
+            GLib.File.new_for_path(_SOURCE_ROOT_DIR).get_child("sql")
+        );
+        this.local_account.open_async.begin(null, this.async_completion);
+        this.local_account.open_async.end(async_result());
+    }
+
+    public override void tear_down() throws GLib.Error {
+        this.local_account.close_async.begin(null, this.async_completion);
+        this.local_account.close_async.end(async_result());
+        this.local_account = null;
+        this.config = null;
+
+        delete_file(this.tmp_dir);
+        this.tmp_dir = null;
+    }
+
+    protected TestAccount new_test_account() throws GLib.Error {
+        return new TestAccount(
+            this.config,
+            this.local_account,
+            new Endpoint(new GLib.NetworkAddress("localhost", 143), NONE, 0),
+            new Endpoint(new GLib.NetworkAddress("localhost", 25), NONE, 0)
+        );
+    }
+
+}
diff --git a/test/engine/imap-engine/imap-engine-generic-account-test.vala 
b/test/engine/imap-engine/imap-engine-generic-account-test.vala
index 6bc8246ae..a65ca2221 100644
--- a/test/engine/imap-engine/imap-engine-generic-account-test.vala
+++ b/test/engine/imap-engine/imap-engine-generic-account-test.vala
@@ -5,32 +5,7 @@
  * (version 2.1 or later). See the COPYING file in this distribution.
  */
 
-public class Geary.ImapEngine.GenericAccountTest : TestCase {
-
-
-    internal class TestAccount : GenericAccount {
-
-        public TestAccount(AccountInformation config,
-                           ImapDB.Account local,
-                           Endpoint incoming_remote,
-                           Endpoint outgoing_remote) {
-            base(config, local, incoming_remote, outgoing_remote);
-        }
-
-        protected override MinimalFolder new_folder(ImapDB.Folder local_folder) {
-            return new MinimalFolder(
-                this,
-                local_folder,
-                NONE
-            );
-        }
-
-    }
-
-
-    private GLib.File? tmp_dir = null;
-    private Geary.AccountInformation? config = null;
-    private ImapDB.Account? local_account = null;
+internal class Geary.ImapEngine.GenericAccountTest : AccountBasedTest {
 
 
     public GenericAccountTest() {
@@ -38,46 +13,8 @@ public class Geary.ImapEngine.GenericAccountTest : TestCase {
         add_test("to_email_identifier", to_email_identifier);
     }
 
-    public override void set_up() throws GLib.Error {
-        this.tmp_dir = GLib.File.new_for_path(
-            GLib.DirUtils.make_tmp(
-                "geary-imap-engine-generic-account-test-XXXXXX"
-            )
-        );
-
-        this.config = new Geary.AccountInformation(
-            "test",
-            ServiceProvider.OTHER,
-            new Mock.CredentialsMediator(),
-            new Geary.RFC822.MailboxAddress(null, "test example com")
-        );
-
-        this.local_account = new ImapDB.Account(
-            config,
-            this.tmp_dir,
-            GLib.File.new_for_path(_SOURCE_ROOT_DIR).get_child("sql")
-        );
-        this.local_account.open_async.begin(null, this.async_completion);
-        this.local_account.open_async.end(async_result());
-    }
-
-    public override void tear_down() throws GLib.Error {
-        this.local_account.close_async.begin(null, this.async_completion);
-        this.local_account.close_async.end(async_result());
-        this.local_account = null;
-        this.config = null;
-
-        delete_file(this.tmp_dir);
-        this.tmp_dir = null;
-    }
-
     public void to_email_identifier() throws GLib.Error {
-        TestAccount test_article = new TestAccount(
-            this.config,
-            this.local_account,
-            new Endpoint(new GLib.NetworkAddress("localhost", 143), NONE, 0),
-            new Endpoint(new GLib.NetworkAddress("localhost", 25), NONE, 0)
-        );
+        var test_article = new_test_account();
 
         assert_non_null(
             test_article.to_email_identifier(
diff --git a/test/engine/imap-engine/imap-engine-minimal-folder-test.vala 
b/test/engine/imap-engine/imap-engine-minimal-folder-test.vala
new file mode 100644
index 000000000..f27da215a
--- /dev/null
+++ b/test/engine/imap-engine/imap-engine-minimal-folder-test.vala
@@ -0,0 +1,357 @@
+/*
+ * Copyright © 2021 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+internal class Geary.ImapEngine.MinimalFolderTest : AccountBasedTest {
+
+
+    private GenericAccount? account = null;
+    private ImapDB.Folder? local_folder = null;
+
+
+    public MinimalFolderTest() {
+        base("Geary.ImapEngine.MinimalFolderTest");
+        add_test("get_email_by_id", get_email_by_id);
+        add_test("get_multiple_email_by_id", get_multiple_email_by_id);
+        add_test("list_email_range_by_null_id", list_email_range_by_null_id);
+        add_test(
+            "list_email_range_by_non_null_id_descending",
+            list_email_range_by_non_null_id_descending
+        );
+        add_test(
+            "list_email_range_by_non_null_id_ascending",
+            list_email_range_by_non_null_id_ascending
+        );
+        add_test(
+            "list_email_range_by_id_partial",
+            list_email_range_by_id_partial
+        );
+    }
+
+    public override void set_up() throws GLib.Error {
+        base.set_up();
+        this.account = new_test_account();
+
+        this.local_account.db.exec(
+            "INSERT INTO FolderTable (id, name) VALUES (1, 'test');"
+        );
+        this.local_account.db.exec(
+            "INSERT INTO MessageTable (id, fields, to_field, from_field) VALUES " +
+            "(1, %d, '%s', null),".printf(
+                Email.Field.RECEIVERS,
+                "test1 example com"
+            ) +
+            "(2, %d, '%s', '%s');".printf(
+                (Email.Field.RECEIVERS | Email.Field.ORIGINATORS),
+                "test2 example com",
+                "sender example com"
+            )
+        );
+        this.local_account.db.exec("""
+            INSERT INTO MessageLocationTable (id, message_id, folder_id, ordering)
+                VALUES (1, 1, 1, 1), (2, 2, 1, 2);
+        """);
+        this.local_account.list_folders_async.begin(
+            this.local_account.imap_folder_root,
+            null,
+            this.async_completion
+        );
+        this.local_folder = traverse<ImapDB.Folder>(
+            this.local_account.list_folders_async.end(async_result())
+        ).first();
+    }
+
+    public override void tear_down() throws GLib.Error {
+        this.account = null;
+        base.tear_down();
+    }
+
+    public void get_email_by_id() throws GLib.Error {
+        var folder = new MinimalFolder(
+            this.account,
+            this.local_folder,
+            NONE
+        );
+        var invalid_id = new ImapDB.EmailIdentifier(0, new Imap.UID(0));
+        var valid_id = new ImapDB.EmailIdentifier(1, new Imap.UID(1));
+
+        folder.get_email_by_id.begin(
+            invalid_id, ALL, null, this.async_completion
+        );
+        try {
+            folder.get_email_by_id.end(async_result());
+            assert_not_reached();
+        } catch (EngineError.NOT_FOUND err) {
+            // all good
+        }
+
+        folder.get_email_by_id.begin(
+            valid_id, RECEIVERS, null, this.async_completion
+        );
+        var email = folder.get_email_by_id.end(async_result());
+        assert_true(email.id.equal_to(valid_id));
+        assert_non_null(email.to);
+        assert_equal<int?>(email.to.size, 1);
+        assert_equal(email.to[0].address, "test1 example com");
+
+        folder.get_email_by_id.begin(
+            valid_id, ALL, null, this.async_completion
+        );
+        try {
+            folder.get_email_by_id.end(async_result());
+            assert_not_reached();
+        } catch (EngineError.INCOMPLETE_MESSAGE err) {
+            // all good
+        }
+    }
+
+    public void get_multiple_email_by_id() throws GLib.Error {
+        var folder = new MinimalFolder(
+            this.account,
+            this.local_folder,
+            NONE
+        );
+        var invalid_id = new ImapDB.EmailIdentifier(0, new Imap.UID(0));
+        var valid_id = new ImapDB.EmailIdentifier(1, new Imap.UID(1));
+
+        folder.get_multiple_email_by_id.begin(
+            Collection.single(invalid_id), ALL, null, this.async_completion
+        );
+        try {
+            folder.get_multiple_email_by_id.end(async_result());
+            assert_not_reached();
+        } catch (EngineError.NOT_FOUND err) {
+            // all good
+        }
+
+        folder.get_multiple_email_by_id.begin(
+            Collection.single(valid_id), RECEIVERS, null, this.async_completion
+        );
+        var email = assert_collection(
+            folder.get_multiple_email_by_id.end(async_result())
+        ).size(1)[0];
+        assert_true(email.id.equal_to(valid_id));
+        assert_non_null(email.to);
+        assert_equal<int?>(email.to.size, 1);
+        assert_equal(email.to[0].address, "test1 example com");
+
+        folder.get_multiple_email_by_id.begin(
+            Collection.single(valid_id), ALL, null, this.async_completion
+        );
+        try {
+            folder.get_multiple_email_by_id.end(async_result());
+            assert_not_reached();
+        } catch (EngineError.INCOMPLETE_MESSAGE err) {
+            // all good
+        }
+    }
+
+    public void list_email_range_by_null_id() throws GLib.Error {
+        var folder = new MinimalFolder(
+            this.account,
+            this.local_folder,
+            NONE
+        );
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, RECEIVERS, NONE, null,
+            this.async_completion
+        );
+        var list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+        assert_equal(list[0].to[0].address, "test2 example com");
+        assert_equal(list[1].to[0].address, "test1 example com");
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, RECEIVERS, OLDEST_TO_NEWEST, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+        assert_equal(list[0].to[0].address, "test1 example com");
+        assert_equal(list[1].to[0].address, "test2 example com");
+    }
+
+    public void list_email_range_by_non_null_id_descending() throws GLib.Error {
+        var folder = new MinimalFolder(
+            this.account,
+            this.local_folder,
+            NONE
+        );
+        var invalid_id = new ImapDB.EmailIdentifier(0, new Imap.UID(0));
+        var valid_id1 = new ImapDB.EmailIdentifier(1, new Imap.UID(1));
+        var valid_id2 = new ImapDB.EmailIdentifier(2, new Imap.UID(2));
+
+        folder.list_email_range_by_id.begin(
+            invalid_id, int.MAX, NONE, NONE, null, this.async_completion
+        );
+        try {
+            folder.list_email_range_by_id.end(async_result());
+            assert_not_reached();
+        } catch (EngineError.NOT_FOUND err) {
+            // all good
+        }
+
+        folder.list_email_range_by_id.begin(
+            valid_id1, int.MAX, RECEIVERS, NONE, null, this.async_completion
+        );
+        var list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).is_empty();
+
+        folder.list_email_range_by_id.begin(
+            valid_id1, int.MAX, RECEIVERS, INCLUDING_ID, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(1);
+        assert_equal(list[0].to[0].address, "test1 example com");
+
+        folder.list_email_range_by_id.begin(
+            valid_id2, int.MAX, RECEIVERS, INCLUDING_ID, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+        assert_equal(list[0].to[0].address, "test2 example com");
+        assert_equal(list[1].to[0].address, "test1 example com");
+    }
+
+    public void list_email_range_by_non_null_id_ascending() throws GLib.Error {
+        var folder = new MinimalFolder(
+            this.account,
+            this.local_folder,
+            NONE
+        );
+        var invalid_id = new ImapDB.EmailIdentifier(0, new Imap.UID(0));
+        var valid_id1 = new ImapDB.EmailIdentifier(1, new Imap.UID(1));
+        var valid_id2 = new ImapDB.EmailIdentifier(2, new Imap.UID(2));
+
+        folder.list_email_range_by_id.begin(
+            invalid_id, int.MAX, NONE, OLDEST_TO_NEWEST, null,
+            this.async_completion
+        );
+        try {
+            folder.list_email_range_by_id.end(async_result());
+            assert_not_reached();
+        } catch (EngineError.NOT_FOUND err) {
+            // all good
+        }
+
+        folder.list_email_range_by_id.begin(
+            valid_id2, int.MAX, RECEIVERS, OLDEST_TO_NEWEST, null,
+            this.async_completion
+        );
+        var list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).is_empty();
+
+        folder.list_email_range_by_id.begin(
+            valid_id2, int.MAX, RECEIVERS, INCLUDING_ID | OLDEST_TO_NEWEST, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(1);
+        assert_equal(list[0].to[0].address, "test2 example com");
+
+        folder.list_email_range_by_id.begin(
+            valid_id1, int.MAX, RECEIVERS, INCLUDING_ID | OLDEST_TO_NEWEST, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+        assert_equal(list[0].to[0].address, "test1 example com");
+        assert_equal(list[1].to[0].address, "test2 example com");
+
+        folder.list_email_range_by_id.begin(
+            valid_id1, int.MAX, RECEIVERS, OLDEST_TO_NEWEST, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(1);
+        assert_equal(list[1].to[0].address, "test2 example com");
+
+        folder.list_email_range_by_id.begin(
+            valid_id1, int.MAX, RECEIVERS, OLDEST_TO_NEWEST | INCLUDING_ID, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+        assert_equal(list[0].to[0].address, "test1 example com");
+        assert_equal(list[1].to[0].address, "test2 example com");
+    }
+
+    public void list_email_range_by_id_partial() throws GLib.Error {
+        var folder = new MinimalFolder(
+            this.account,
+            this.local_folder,
+            NONE
+        );
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, NONE, NONE, null,
+            this.async_completion
+        );
+        var list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, RECEIVERS, NONE, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, ORIGINATORS, NONE, null,
+            this.async_completion
+        );
+        try {
+            folder.list_email_range_by_id.end(async_result());
+            assert_not_reached("receivers-fields");
+        } catch (EngineError.INCOMPLETE_MESSAGE err) {
+            // all good
+        }
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, ORIGINATORS, INCLUDING_PARTIAL, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, ALL, NONE, null,
+            this.async_completion
+        );
+        try {
+            folder.list_email_range_by_id.end(async_result());
+            assert_not_reached("all-fields");
+        } catch (EngineError.INCOMPLETE_MESSAGE err) {
+            // all good
+        }
+
+        folder.list_email_range_by_id.begin(
+            null, int.MAX, ALL, INCLUDING_PARTIAL, null,
+            this.async_completion
+        );
+        list = assert_collection(
+            folder.list_email_range_by_id.end(async_result())
+        ).size(2);
+    }
+
+}
diff --git a/test/meson.build b/test/meson.build
index 973727996..a991ec579 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -55,7 +55,9 @@ test_engine_sources = [
   'engine/imap-db/imap-db-email-identifier-test.vala',
   'engine/imap-db/imap-db-folder-test.vala',
   'engine/imap-engine/account-processor-test.vala',
+  'engine/imap-engine/imap-engine-account-based-test.vala',
   'engine/imap-engine/imap-engine-generic-account-test.vala',
+  'engine/imap-engine/imap-engine-minimal-folder-test.vala',
   'engine/mime/mime-content-type-test.vala',
   'engine/outbox/outbox-email-identifier-test.vala',
   'engine/rfc822/rfc822-mailbox-address-test.vala',
diff --git a/test/test-engine.vala b/test/test-engine.vala
index e829b0bf5..8ef892db1 100644
--- a/test/test-engine.vala
+++ b/test/test-engine.vala
@@ -78,6 +78,7 @@ int main(string[] args) {
 
     engine.add_suite(new Geary.ImapEngine.AccountProcessorTest().suite);
     engine.add_suite(new Geary.ImapEngine.GenericAccountTest().suite);
+    engine.add_suite(new Geary.ImapEngine.MinimalFolderTest().suite);
 
     // Depends on ImapDb.Database working correctly
     engine.add_suite(new Geary.ContactStoreImplTest().suite);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]