[glom] Added a python function test that uses an actual database connection.



commit 3d55c2a5972e1b36754861d37581410a21377257
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Apr 7 10:29:34 2011 +0200

    Added a python function test that uses an actual database connection.
    
      * Makefile_tests.am: Mention the new test.
    * tests/python/test_python_execute_func_with_record.cc: This shows
    a crash in Glom::PyGlomRecord::get_connection().

 ChangeLog                                          |    8 +
 Makefile_tests.am                                  |    7 +
 .../python/test_python_execute_func_with_record.cc |  136 ++++++++++++++++++++
 3 files changed, 151 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 419c867..430841c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2011-04-07  Murray Cumming  <murrayc murrayc com>
 
+	Added a python function test that uses an actual database connection.
+
+  * Makefile_tests.am: Mention the new test.
+	* tests/python/test_python_execute_func_with_record.cc: This shows
+	a crash in Glom::PyGlomRecord::get_connection().
+
+2011-04-07  Murray Cumming  <murrayc murrayc com>
+
 	Moved the python tests to a sub-directory.
 
 	* tests/test_load_python_library.cc:
diff --git a/Makefile_tests.am b/Makefile_tests.am
index 035a29d..927e682 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -140,6 +140,7 @@ check_PROGRAMS += \
 	tests/python/test_python_execute_func_bad_syntax \
 	tests/python/test_python_execute_func_date \
 	tests/python/test_python_execute_func_change_result_type \
+	tests/python/test_python_execute_func_with_record \
 	tests/python/test_python_execute_script
 #	glom/mode_data/test_flowtablewithfields
 
@@ -150,6 +151,7 @@ TESTS += \
 	tests/python/test_python_execute_func_bad_syntax \
 	tests/python/test_python_execute_func_date \
 	tests/python/test_python_execute_func_change_result_type \
+	tests/python/test_python_execute_func_with_record \
 	tests/python/test_python_execute_script
 
 glom_utility_widgets_test_flowtable_SOURCES =	\
@@ -194,6 +196,11 @@ tests_python_test_python_execute_func_change_result_type_SOURCES = tests/python/
 tests_python_test_python_execute_func_change_result_type_LDADD = $(tests_ldadd) $(GLOM_LIBS) $(PYTHON_LIBS)
 tests_python_test_python_execute_func_change_result_type_CPPFLAGS = $(tests_cppflags_ui)
 
+tests_python_test_python_execute_func_with_record_SOURCES = tests/python/test_python_execute_func_with_record.cc \
+  glom/python_embed/glom_python.cc
+tests_python_test_python_execute_func_with_record_LDADD = $(tests_ldadd) $(GLOM_LIBS) $(PYTHON_LIBS)
+tests_python_test_python_execute_func_with_record_CPPFLAGS = $(tests_cppflags_ui)
+
 tests_python_test_python_execute_script_SOURCES = tests/python/test_python_execute_script.cc \
   glom/python_embed/glom_python.cc
 tests_python_test_python_execute_script_LDADD = $(tests_ldadd) $(GLOM_LIBS) $(PYTHON_LIBS)
diff --git a/tests/python/test_python_execute_func_with_record.cc b/tests/python/test_python_execute_func_with_record.cc
new file mode 100644
index 0000000..3ed3113
--- /dev/null
+++ b/tests/python/test_python_execute_func_with_record.cc
@@ -0,0 +1,136 @@
+#include <glom/libglom/init.h>
+#include <glom/libglom/connectionpool.h>
+#include <glom/python_embed/glom_python.h>
+#include <libglom/data_structure/glomconversions.h>
+#include <boost/python.hpp>
+#include <iostream>
+
+static void on_startup_progress()
+{
+  std::cout << "Database startup progress" << std::endl;
+}
+
+static void on_cleanup_progress()
+{
+  std::cout << "Database cleanup progress" << std::endl;
+}
+
+void cleanup()
+{
+  Glom::ConnectionPool* connection_pool = Glom::ConnectionPool::get_instance();
+
+  const bool stopped = connection_pool->cleanup( sigc::ptr_fun(&on_cleanup_progress) );
+  g_assert(stopped);
+}
+
+int main()
+{
+  Glom::libglom_init(); //Also initializes python.
+
+  //Connect to a Glom database
+  //A sqlite-based one, to simplify this test.
+  // Get a URI for a test file:
+  Glib::ustring uri;
+
+  try
+  {
+    const std::string path =
+       Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED,
+         "sqlite", "test_sqlite_music", "test_sqlite_music.glom");
+    uri = Glib::filename_to_uri(path);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << ": " << ex.what();
+    return EXIT_FAILURE;
+  }
+
+  //std::cout << "URI=" << uri << std::endl;
+
+
+  // Load the document:
+  Glom::Document document;
+  document.set_file_uri(uri);
+  int failure_code = 0;
+  const bool test = document.load(failure_code);
+  //std::cout << "Document load result=" << test << std::endl;
+
+  if(!test)
+  {
+    std::cerr << "Document::load() failed with failure_code=" << failure_code << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  g_assert(!document.get_is_example_file());;
+
+  Glom::ConnectionPool* connection_pool = Glom::ConnectionPool::get_instance();
+  connection_pool->setup_from_document(&document);
+
+  //This is not really necessary for sqlite-based databases.
+  const Glom::ConnectionPool::StartupErrors started =
+    connection_pool->startup( sigc::ptr_fun(&on_startup_progress) );
+  if(started != Glom::ConnectionPool::Backend::STARTUPERROR_NONE)
+  {
+    std::cerr << "connection_pool->startup(): result=" << started << std::endl;
+  }
+  g_assert(started == Glom::ConnectionPool::Backend::STARTUPERROR_NONE);
+
+  Glom::sharedptr<Glom::SharedConnection> connection = connection_pool->connect();
+  g_assert(connection);
+
+  Glib::RefPtr<Gnome::Gda::Connection> gda_connection = connection->get_gda_connection();
+  g_assert(connection->get_gda_connection());
+
+
+  //Some silly python code just to exercise our PyGlomRecord API:
+  const char* calculation = "connection = record.connection\nreturn connection.get_opened()";
+  Glom::type_map_fields field_values;
+
+  //TODO: Use this: const type_map_fields field_values = get_record_field_values_for_calculation(field_in_record.m_table_name, field_in_record.m_key, field_in_record.m_key_value);
+  //    if(!field_values.empty())
+
+  //Execute a python function:
+  Gnome::Gda::Value value;
+  Glib::ustring error_message;
+  try
+  {
+    value = Glom::glom_evaluate_python_function_implementation(
+      Glom::Field::TYPE_NUMERIC, calculation, field_values,
+      0 /* document */, "" /* table name */,
+      Glom::sharedptr<Glom::Field>(), Gnome::Gda::Value(), // primary key details. Not used in this test.
+      gda_connection,
+      error_message);
+  }
+  catch(const std::exception& ex)
+  {
+    std::cerr << "Exception: " << ex.what() << std::endl;
+    return EXIT_FAILURE;
+  }
+  catch(const boost::python::error_already_set& ex)
+  {
+    std::cerr << "Exception: boost::python::error_already_set" << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  //std::cout << "type=" << g_type_name(value.get_value_type()) << std::endl;
+
+  //Check that there was no python error:
+  if(!error_message.empty())
+  {
+    std::cerr << "Python error: " << error_message << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  //Check that the return value is of the expected type:
+  g_assert(value.get_value_type() == GDA_TYPE_NUMERIC);
+
+  //Check that the return value is of the expected value:
+  const double numeric = Glom::Conversions::get_double_for_gda_value_numeric(value);
+  g_assert(numeric == 4950.0);
+
+  //std::cout << "value=" << value.to_string() << std::endl;
+
+  cleanup();
+
+  return EXIT_SUCCESS;
+}



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