[glom] test_selfhosting_new_empty: Move some code into test_selfhosting_utils



commit 309dbbe1b51731812d076661cfc2564ef3cb590c
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Jan 30 21:27:30 2012 +0100

    test_selfhosting_new_empty: Move some code into test_selfhosting_utils
    
    * tests/test_selfhosting_utils.[h|cc]: Add functions for self-hosting
    of an empty new database, used by the existing code too.
    * Makefile_tests.am:
    * tests/test_selfhosting_new_empty.cc: Use test_selfhosting_utils to
    simplify this code, making it easier to create variations of this test.

 ChangeLog                                  |   10 ++
 Makefile_tests.am                          |    2 +-
 tests/test_selfhosting_new_empty.cc        |  115 +----------------------
 tests/test_selfhosting_new_from_example.cc |   16 +++
 tests/test_selfhosting_utils.cc            |  138 +++++++++++++++++++---------
 tests/test_selfhosting_utils.h             |   17 ++++
 6 files changed, 144 insertions(+), 154 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0b3e36c..2b28cde 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2012-01-30  Murray Cumming  <murrayc murrayc com>
 
+	test_selfhosting_new_empty: Move some code into test_selfhosting_utils
+
+	* tests/test_selfhosting_utils.[h|cc]: Add functions for self-hosting 
+	of an empty new database, used by the existing code too.
+	* Makefile_tests.am:
+	* tests/test_selfhosting_new_empty.cc: Use test_selfhosting_utils to 
+	simplify this code, making it easier to create variations of this test.
+
+2012-01-30  Murray Cumming  <murrayc murrayc com>
+
 	test_selfhosting_new_empty: Simplify the cleanup code.
 
 	* tests/test_selfhosting_new_empty.cc: Call cleanup if test() returns 
diff --git a/Makefile_tests.am b/Makefile_tests.am
index c6ec279..fabcfcb 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -212,7 +212,7 @@ tests_test_fake_connection_SOURCES = tests/test_fake_connection.cc
 tests_test_fake_connection_LDADD = $(tests_ldadd)
 tests_test_fake_connection_CPPFLAGS = $(tests_cppflags)
 
-tests_test_selfhosting_new_empty_SOURCES = tests/test_selfhosting_new_empty.cc
+tests_test_selfhosting_new_empty_SOURCES = tests/test_selfhosting_new_empty.cc $(sources_test_selfhosting_utils)
 tests_test_selfhosting_new_empty_LDADD = $(tests_ldadd)
 tests_test_selfhosting_new_empty_CPPFLAGS = $(tests_cppflags)
 
diff --git a/tests/test_selfhosting_new_empty.cc b/tests/test_selfhosting_new_empty.cc
index 3ef62dc..748e17c 100644
--- a/tests/test_selfhosting_new_empty.cc
+++ b/tests/test_selfhosting_new_empty.cc
@@ -18,125 +18,22 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#include <libglom/document/document.h>
-#include <libglom/connectionpool.h>
-#include <libglom/connectionpool_backends/postgres_self.h>
+#include "tests/test_selfhosting_utils.h"
 #include <libglom/init.h>
 #include <libglom/privs.h>
-#include <libglom/utils.h>
 #include <libglom/db_utils.h>
-#include <giomm/file.h>
-#include <glibmm/convert.h>
-#include <glibmm/miscutils.h>
 #include <iostream>
 
-static void on_initialize_progress()
-{
-  std::cout << "Database initialization progress" << std::endl;
-}
-
-static void on_startup_progress()
-{
-  std::cout << "Database startup progress" << std::endl;
-}
-
-static void on_cleanup_progress()
-{
-  std::cout << "Database cleanup progress" << std::endl;
-}
-
-static void on_db_creation_progress()
-{
-  std::cout << "Database creation progress" << std::endl;
-}
-
-static Glom::ConnectionPool* connection_pool = 0;
-static std::string temp_filepath_dir;
-
-static void cleanup()
-{
-  //Stop:
-  const bool stopped = connection_pool->cleanup( sigc::ptr_fun(&on_cleanup_progress) );
-  g_assert(stopped);
-
-  //Make sure the directory is removed at the end,
-  const Glib::ustring uri = Glib::filename_to_uri(temp_filepath_dir);
-  Glom::Utils::delete_directory(uri);
-}
-
 static bool test(Glom::Document::HostingMode hosting_mode)
 {
-  connection_pool = 0;
-  temp_filepath_dir.clear();
-
   // Create the document:
   Glom::Document document;
 
-  //Save a copy, specifying the path to file in a directory:
-  //For instance, /tmp/testfileglom/testfile.glom");
-  const std::string temp_filename = "testglom";
-  temp_filepath_dir = 
-    Glom::Utils::get_temp_directory_path(temp_filename);
-  const std::string temp_filepath = Glib::build_filename(temp_filepath_dir, temp_filename);
-
-  //Make sure that the file does not exist yet:
-  {
-    const Glib::ustring uri = Glib::filename_to_uri(temp_filepath_dir);
-    Glom::Utils::delete_directory(uri);
-  }
-
-  //Save the file. TODO: Do we need to do this for the test?
-  const Glib::ustring file_uri = Glib::filename_to_uri(temp_filepath);
-  document.set_allow_autosave(false); //To simplify things and to not depend implicitly on autosave.
-  document.set_file_uri(file_uri);
-
-  document.set_hosting_mode(hosting_mode);
-  document.set_is_example_file(false);
-  document.set_network_shared(false);
-  const bool saved = document.save();
-  g_assert(saved);
-
-  //Specify the backend and backend-specific details to be used by the connectionpool.
-  connection_pool = Glom::ConnectionPool::get_instance();
-  connection_pool->setup_from_document(&document);
-
-  //We must specify a default username and password:
-  Glib::ustring password;
-  const Glib::ustring user = Glom::Privs::get_default_developer_user_name(password);
-  connection_pool->set_user(user);
-  connection_pool->set_password(password);
-
-  //Create the self-hosting files:
-  const Glom::ConnectionPool::InitErrors initialized_errors =
-    connection_pool->initialize( sigc::ptr_fun(&on_initialize_progress) );
-  g_assert(initialized_errors == Glom::ConnectionPool::Backend::INITERROR_NONE);
-
-  //Start self-hosting:
-  //TODO: Let this happen automatically on first connection?
-  const Glom::ConnectionPool::StartupErrors started = connection_pool->startup( sigc::ptr_fun(&on_startup_progress) );
-  if(started != Glom::ConnectionPool::Backend::STARTUPERROR_NONE)
+  if(!(test_create_and_selfhost_new_database(document, hosting_mode, "test_db")))
   {
-    std::cerr << "connection_pool->startup(): result=" << started << std::endl;
-  }
-  g_assert(started == Glom::ConnectionPool::Backend::STARTUPERROR_NONE);
-
-  //Test this function a little:
-  const Glib::ustring db_name = Glom::DbUtils::get_unused_database_name("test_db");
-  if(db_name.empty())
-  {
-    std::cerr << "DbUtils::get_unused_database_name) failed." << std::endl;
-    return false;
-  }
-
-  //Create a database:
-  const bool created = Glom::DbUtils::create_database(&document, db_name, 
-    "test title", sigc::ptr_fun(&on_db_creation_progress));
-  if(!created)
-  {
-    std::cerr << "DbUtils::create_database() failed." << std::endl;
+    std::cerr << "test_create_and_selfhost_new_database() failed" << std::endl;
     return false;
   }
-
   
   //Test some simple changes to the database:
   try
@@ -152,7 +49,7 @@ static bool test(Glom::Document::HostingMode hosting_mode)
     return false;
   }
 
-  cleanup();
+  test_selfhosting_cleanup();
   return true;
 }
 
@@ -163,14 +60,14 @@ int main()
   if(!test(Glom::Document::HOSTING_MODE_POSTGRES_SELF))
   {
     std::cerr << "Failed with PostgreSQL" << std::endl;
-    cleanup();
+    test_selfhosting_cleanup();
     return EXIT_FAILURE;
   }
   
   if(!test(Glom::Document::HOSTING_MODE_SQLITE))
   {
     std::cerr << "Failed with SQLite" << std::endl;
-    cleanup();
+    test_selfhosting_cleanup();
     return EXIT_FAILURE;
   }
 
diff --git a/tests/test_selfhosting_new_from_example.cc b/tests/test_selfhosting_new_from_example.cc
index c607c88..998f0c3 100644
--- a/tests/test_selfhosting_new_from_example.cc
+++ b/tests/test_selfhosting_new_from_example.cc
@@ -53,6 +53,22 @@ static bool test(Glom::Document::HostingMode hosting_mode)
     return false;
   }
 
+  //Test the system preferences for the database:
+  //TODO: We should store this only in the document anyway,
+  //and make it translatable:
+  /* TODO: This is not stored in the examples. Should it be?
+  const Glom::SystemPrefs prefs = 
+    Glom::DbUtils::get_database_preferences(&document);
+  g_return_val_if_fail(prefs.m_name == "Music Collection", false);
+  g_return_val_if_fail(prefs.m_org_name == "SomeOrganization Incorporated", false);
+  g_return_val_if_fail(prefs.m_org_address_street == "Some House", false);
+  g_return_val_if_fail(prefs.m_org_address_street2 == "123 Some Street", false);
+  g_return_val_if_fail(prefs.m_org_address_town == "Some Town", false);
+  g_return_val_if_fail(prefs.m_org_address_county == "Some State", false);
+  g_return_val_if_fail(prefs.m_org_address_postcode == "12345", false);
+  g_return_val_if_fail(prefs.m_org_address_country == "USA", false);
+  */
+
   test_selfhosting_cleanup();
  
   return true; 
diff --git a/tests/test_selfhosting_utils.cc b/tests/test_selfhosting_utils.cc
index 056fbd9..2fcaab2 100644
--- a/tests/test_selfhosting_utils.cc
+++ b/tests/test_selfhosting_utils.cc
@@ -52,6 +52,11 @@ static void on_cleanup_progress()
   //std::cout << "Database cleanup progress" << std::endl;
 }
 
+static void on_db_creation_progress()
+{
+  //std::cout << "Database creation progress" << std::endl;
+}
+
 std::string temp_filepath_dir;
 
 static bool check_directory_exists()
@@ -138,28 +143,7 @@ void test_selfhosting_cleanup()
   temp_filepath_dir.clear();
 }
 
-bool test_create_and_selfhost_from_example(const std::string& example_filename, Glom::Document& document, Glom::Document::HostingMode hosting_mode, const std::string& subdirectory_path)
-{
-  Glib::ustring uri;
-  
-  // Get a URI for the example file:
-  try
-  {
-    const std::string path =
-       Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED,
-         example_filename);
-    uri = Glib::filename_to_uri(path);
-  }
-  catch(const Glib::ConvertError& ex)
-  {
-    std::cerr << G_STRFUNC << ": " << ex.what();
-    return false;
-  }
-  
-  return test_create_and_selfhost_from_uri(uri, document, hosting_mode, subdirectory_path);
-}
-
-bool test_create_and_selfhost_from_uri(const Glib::ustring& example_file_uri, Glom::Document& document, Glom::Document::HostingMode hosting_mode, const std::string& subdirectory_path)
+bool test_create_and_selfhost_new_empty(Glom::Document& document, Glom::Document::HostingMode hosting_mode, const std::string& subdirectory_path)
 {
   if( (hosting_mode != Glom::Document::HOSTING_MODE_POSTGRES_SELF) &&
     (hosting_mode != Glom::Document::HOSTING_MODE_SQLITE) )
@@ -168,27 +152,6 @@ bool test_create_and_selfhost_from_uri(const Glib::ustring& example_file_uri, Gl
     return false;
   }
 
-  // Load the document:
-  document.set_allow_autosave(false); //To simplify things and to not depend implicitly on autosave.
-  document.set_file_uri(example_file_uri);
-  int failure_code = 0;
-  const bool test = document.load(failure_code);
-  //std::cout << "Document load result=" << test << std::endl;
-
-  if(!test)
-  {
-    std::cerr << G_STRFUNC << ": Document::load() failed with failure_code=" << failure_code << std::endl;
-    return false;
-  }
-
-  if(!document.get_is_example_file() && !document.get_is_backup_file())
-  {
-    std::cerr << G_STRFUNC << ": The document is not an example or a backup." << std::endl;
-    return false;
-  }
-
-  Glom::ConnectionPool* connection_pool = Glom::ConnectionPool::get_instance();
-
   //Save a copy, specifying the path to file in a directory:
   //For instance, /tmp/testglom/testglom.glom");
   const std::string temp_filename = "testglom";
@@ -204,7 +167,7 @@ bool test_create_and_selfhost_from_uri(const Glib::ustring& example_file_uri, Gl
     delete_directory(uri);
   }
 
-  //Save the example as a real file:
+   //Save the example as a real file:
   const Glib::ustring file_uri = Glib::filename_to_uri(temp_filepath);
   document.set_allow_autosave(false); //To simplify things and to not depend implicitly on autosave.
   document.set_file_uri(file_uri);
@@ -216,6 +179,7 @@ bool test_create_and_selfhost_from_uri(const Glib::ustring& example_file_uri, Gl
   g_assert(saved);
 
   //Specify the backend and backend-specific details to be used by the connectionpool.
+  Glom::ConnectionPool* connection_pool = Glom::ConnectionPool::get_instance();
   connection_pool->setup_from_document(&document);
 
   //We must specify a default username and password:
@@ -244,6 +208,92 @@ bool test_create_and_selfhost_from_uri(const Glib::ustring& example_file_uri, Gl
   }
   g_assert(started == Glom::ConnectionPool::Backend::STARTUPERROR_NONE);
 
+  return true;
+}
+
+bool test_create_and_selfhost_new_database(Glom::Document& document, Glom::Document::HostingMode hosting_mode, const Glib::ustring& database_name, const std::string& subdirectory_path)
+{
+  if(!test_create_and_selfhost_new_empty(document, hosting_mode, subdirectory_path))
+  {
+    std::cerr << G_STRFUNC << ": test_create_and_selfhost_new_empty() failed." << std::endl;
+    return false;
+  } 
+
+  const Glib::ustring db_name = Glom::DbUtils::get_unused_database_name("test_db");
+  if(db_name.empty())
+  {
+    std::cerr << "DbUtils::get_unused_database_name) failed." << std::endl;
+    return false;
+  }
+
+  //Create a database:
+  const bool created = Glom::DbUtils::create_database(&document, db_name,
+    "test title", sigc::ptr_fun(&on_db_creation_progress));
+  if(!created)
+  {
+    std::cerr << "DbUtils::create_database() failed." << std::endl;
+    return false;
+  }
+  
+  return true;
+}
+
+
+bool test_create_and_selfhost_from_example(const std::string& example_filename, Glom::Document& document, Glom::Document::HostingMode hosting_mode, const std::string& subdirectory_path)
+{
+  Glib::ustring uri;
+  
+  // Get a URI for the example file:
+  try
+  {
+    const std::string path =
+       Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED,
+         example_filename);
+    uri = Glib::filename_to_uri(path);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << ": " << ex.what();
+    return false;
+  }
+  
+  return test_create_and_selfhost_from_uri(uri, document, hosting_mode, subdirectory_path);
+}
+
+bool test_create_and_selfhost_from_uri(const Glib::ustring& example_file_uri, Glom::Document& document, Glom::Document::HostingMode hosting_mode, const std::string& subdirectory_path)
+{
+  if( (hosting_mode != Glom::Document::HOSTING_MODE_POSTGRES_SELF) &&
+    (hosting_mode != Glom::Document::HOSTING_MODE_SQLITE) )
+  {
+    std::cerr << G_STRFUNC << ": This test function does not support the specified hosting_mode: " << hosting_mode << std::endl;
+    return false;
+  }
+
+  // Load the document:
+  document.set_allow_autosave(false); //To simplify things and to not depend implicitly on autosave.
+  document.set_file_uri(example_file_uri);
+  int failure_code = 0;
+  const bool test = document.load(failure_code);
+  //std::cout << "Document load result=" << test << std::endl;
+
+  if(!test)
+  {
+    std::cerr << G_STRFUNC << ": Document::load() failed with failure_code=" << failure_code << std::endl;
+    return false;
+  }
+
+  if(!document.get_is_example_file() && !document.get_is_backup_file())
+  {
+    std::cerr << G_STRFUNC << ": The document is not an example or a backup." << std::endl;
+    return false;
+  }
+
+  if(!test_create_and_selfhost_new_empty(document, hosting_mode, subdirectory_path))
+  {
+    std::cerr << G_STRFUNC << ": test_create_and_selfhost_new_empty() failed." << std::endl;
+    return false;
+  }
+
   const bool recreated = Glom::DbUtils::recreate_database_from_document(&document, sigc::ptr_fun(&on_recreate_progress) );
   if(!recreated)
     test_selfhosting_cleanup();
diff --git a/tests/test_selfhosting_utils.h b/tests/test_selfhosting_utils.h
index 355054f..a31c5f4 100644
--- a/tests/test_selfhosting_utils.h
+++ b/tests/test_selfhosting_utils.h
@@ -27,6 +27,23 @@
 
 /** Create a .glom file from an example, with database data, and start a PostgreSQL server if necessary.
  *
+ * @param document A new empty document that will be filled with hosting details.
+ * @param hosting_mode Either HOSTING_MODE_POSTGRES_SELF or HOSTING_MODE_SQLITE
+ * @param subdirectory_path: An additional directory path to use under the temporary directory that will be used to save the file.
+ */
+bool test_create_and_selfhost_new_empty(Glom::Document& document, Glom::Document::HostingMode hosting_mode, const std::string& subdirectory_path = std::string());
+
+/** Create a .glom file from an example, with database data, and start a PostgreSQL server if necessary.
+ *
+ * @param document A new empty document that will be filled with hosting details.
+ * @param hosting_mode Either HOSTING_MODE_POSTGRES_SELF or HOSTING_MODE_SQLITE
+ * @param database_name The name of the database to created.
+ * @param subdirectory_path: An additional directory path to use under the temporary directory that will be used to save the file.
+ */
+bool test_create_and_selfhost_new_database(Glom::Document& document, Glom::Document::HostingMode hosting_mode, const Glib::ustring& database_name,  const std::string& subdirectory_path = std::string());
+
+/** Create a .glom file from an example, with database data, and start a PostgreSQL server if necessary.
+ *
  * @param hosting_mode Either HOSTING_MODE_POSTGRES_SELF or HOSTING_MODE_SQLITE
  * @param subdirectory_path: An additional directory path to use under the temporary directory that will be used to save the file.
  */



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