[geary] Add unit tests for adding accounts.



commit e8dcad9a4341a59652b19da9404f4118ab777cb9
Author: Michael James Gratton <mike vee net>
Date:   Fri Feb 17 17:41:16 2017 +1100

    Add unit tests for adding accounts.
    
    * src/engine/api/geary-engine.vala (Engine::create_orphan_account): Fix
      sense of test when determining the next account id to use. Add unit
      tests.
      (Engine::add_account): Made public so it can be used in public test.

 src/engine/api/geary-engine.vala       |    2 +-
 test/CMakeLists.txt                    |    1 +
 test/engine/api/geary-engine-test.vala |  143 ++++++++++++++++++++++++++++++++
 test/main.vala                         |    1 +
 4 files changed, 146 insertions(+), 1 deletions(-)
---
diff --git a/src/engine/api/geary-engine.vala b/src/engine/api/geary-engine.vala
index bd36ba2..54031d6 100644
--- a/src/engine/api/geary-engine.vala
+++ b/src/engine/api/geary-engine.vala
@@ -435,7 +435,7 @@ public class Geary.Engine : BaseObject {
      * Adds the account to be tracked by the engine.  Should only be called from
      * AccountInformation.store_async() and this class.
      */
-    internal void add_account(AccountInformation account, bool created = false) throws Error {
+    public void add_account(AccountInformation account, bool created = false) throws Error {
         check_opened();
 
         bool already_added = accounts.has_key(account.id);
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 68a437f..065435e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -7,6 +7,7 @@ set(TEST_SRC
   testcase.vala # Based on same file in libgee, courtesy Julien Peeters
 
   engine/api/geary-attachment-test.vala
+  engine/api/geary-engine-test.vala
   engine/mime-content-type-test.vala
   engine/rfc822-mailbox-address-test.vala
   engine/rfc822-message-test.vala
diff --git a/test/engine/api/geary-engine-test.vala b/test/engine/api/geary-engine-test.vala
new file mode 100644
index 0000000..c412f3b
--- /dev/null
+++ b/test/engine/api/geary-engine-test.vala
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2017 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.
+ */
+
+class Geary.EngineTest : Gee.TestCase {
+
+    private Engine? engine = null;
+    private File? tmp = null;
+    private File? config = null;
+    private File? data = null;
+    private File? res = null;
+
+    public EngineTest() {
+        base("Geary.EngineTest");
+        add_test("create_orphan_account", create_orphan_account);
+        add_test("create_orphan_account_with_legacy", create_orphan_account_with_legacy);
+    }
+
+    ~EngineTest() {
+        // Need this in addition to the code in tear_down in case a
+        // test fails
+        if (this.tmp != null) {
+            try {
+                @delete(this.tmp);
+            } catch (Error err) {
+                print("\nError removing tmp files: %s\n", err.message);
+            }
+        }
+    }
+
+    public override void set_up() {
+        // XXX this whole thing stinks. We need to be able to test the
+        // engine without creating all of these dirs.
+
+        try {
+            this.tmp = File.new_for_path(Environment.get_tmp_dir()).get_child("geary-test");
+            this.tmp.make_directory();
+
+            this.config = this.tmp.get_child("config");
+            this.config.make_directory();
+
+            this.data = this.tmp.get_child("data");
+            this.data.make_directory();
+
+            this.res = this.tmp.get_child("res");
+            this.res.make_directory();
+
+            this.engine = new Engine();
+            this.engine.open_async.begin(
+                config, data, res, null, null,
+                (obj, res) => {
+                    async_complete(res);
+                });
+            this.engine.open_async.end(async_result());
+        } catch (Error err) {
+            assert_not_reached();
+        }
+    }
+
+       public override void tear_down () {
+        try {
+            this.res.delete();
+            this.data.delete();
+            this.config.delete();
+            this.tmp.delete();
+            this.tmp = null;
+        } catch (Error err) {
+            assert_not_reached();
+        }
+       }
+
+    public void create_orphan_account() {
+        try {
+            AccountInformation info = this.engine.create_orphan_account();
+            assert(info.id == "account_01");
+            this.engine.add_account(info, true);
+
+            info = this.engine.create_orphan_account();
+            assert(info.id == "account_02");
+            this.engine.add_account(info, true);
+
+            info = this.engine.create_orphan_account();
+            assert(info.id == "account_03");
+            this.engine.add_account(info, true);
+
+            info = this.engine.create_orphan_account();
+            assert(info.id == "account_04");
+        } catch (Error err) {
+            print("\nerr: %s\n", err.message);
+            assert_not_reached();
+        }
+    }
+
+    public void create_orphan_account_with_legacy() {
+        try {
+            this.engine.add_account(
+                new AccountInformation("foo", this.config, this.data),
+                true
+            );
+
+             AccountInformation info = this.engine.create_orphan_account();
+            assert(info.id == "account_01");
+            this.engine.add_account(info, true);
+
+            assert(this.engine.create_orphan_account().id == "account_02");
+
+            this.engine.add_account(
+                new AccountInformation("bar", this.config, this.data),
+                true
+            );
+
+            assert(this.engine.create_orphan_account().id == "account_02");
+        } catch (Error err) {
+            print("\nerr: %s\n", err.message);
+            assert_not_reached();
+        }
+    }
+
+    private void delete(File parent) throws Error {
+        FileInfo info = parent.query_info(
+            "standard::*",
+            FileQueryInfoFlags.NOFOLLOW_SYMLINKS
+        );
+
+        if (info.get_file_type () == FileType.DIRECTORY) {
+            FileEnumerator enumerator = parent.enumerate_children(
+                "standard::*",
+                FileQueryInfoFlags.NOFOLLOW_SYMLINKS
+            );
+
+            info = null;
+            while (((info = enumerator.next_file()) != null)) {
+                @delete(parent.get_child(info.get_name()));
+            }
+        }
+
+        parent.delete();
+    }
+
+}
\ No newline at end of file
diff --git a/test/main.vala b/test/main.vala
index 14a972d..b959602 100644
--- a/test/main.vala
+++ b/test/main.vala
@@ -38,6 +38,7 @@ int main(string[] args) {
     TestSuite engine = new TestSuite("engine");
 
     engine.add_suite(new Geary.AttachmentTest().get_suite());
+    engine.add_suite(new Geary.EngineTest().get_suite());
     engine.add_suite(new Geary.HTML.UtilTest().get_suite());
     engine.add_suite(new Geary.IdleManagerTest().get_suite());
     engine.add_suite(new Geary.Inet.Test().get_suite());


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