[glom] Image fields test: Get and set a value and check that it is the same.



commit 4c561b0719dd3de568812af943b8c6411804d16e
Author: Murray Cumming <murrayc murrayc com>
Date:   Sat Oct 29 22:30:12 2011 +0200

    Image fields test: Get and set a value and check that it is the same.
    
    * tests/test_image.glom: Added an image file to set and get in the
    database.
    * Makefile_tests.am: Disttribute the image file.
    * tests/test_selfhosting_new_then_image.cc: tests(): Read the data
    from the file, use it to set the data, and then read that data back.
    Correct the type check. However, the data equality test fails due to
    libgda bug #662922 .

 ChangeLog                                |   13 ++++++
 Makefile_tests.am                        |    3 +-
 tests/test_image.jpg                     |  Bin 0 -> 130687 bytes
 tests/test_selfhosting_new_then_image.cc |   68 +++++++++++++++++++++++++----
 4 files changed, 73 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c41144f..2bb6e54 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2011-10-29  Murray Cumming  <murrayc murrayc com>
 
+	Image fields test: Load data from a file and check setting and getting.
+
+	* tests/test_image.glom: Added an image file to set and get in the 
+	database.
+	* Makefile_tests.am: Distribute the image file.
+	* tests/test_selfhosting_new_then_image.cc: Load the data from the 
+	file, set it in the database, and check that the data read back is 
+	the same.
+	Correct the type check. However, the data equality test still fails
+	because of libgda bug #662922 .
+
+2011-10-29  Murray Cumming  <murrayc murrayc com>
+
 	libglom: Added build_sql_update_with_where_clause().
 
 	* glom/libglom/utils.[h|cc]: Added build_sql_update_with_where_clause().
diff --git a/Makefile_tests.am b/Makefile_tests.am
index 5ca7324..e55794c 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -105,6 +105,7 @@ dist_noinst_DATA = tests/import/data/albums.csv
 
 # Let the .cc source code know about this path: TODO: Actually use this.
 glom_test_import_defines = -DGLOM_TESTS_IMPORT_DATA_NOTINSTALLED=\""$(abs_top_srcdir)/tests/import/data/"\"
+glom_test_image_defines = -DGLOM_TESTS_IMAGE_DATA_NOTINSTALLED=\""$(abs_top_srcdir)/tests/"\"
 
 
 tests_import_test_parsing_SOURCES =	\
@@ -143,7 +144,7 @@ tests_test_selfhosting_new_then_report_CPPFLAGS = $(tests_cppflags)
 
 tests_test_selfhosting_new_then_image_SOURCES = tests/test_selfhosting_new_then_image.cc $(sources_test_selfhosting_utils)
 tests_test_selfhosting_new_then_image_LDADD = $(tests_ldadd)
-tests_test_selfhosting_new_then_image_CPPFLAGS = $(tests_cppflags)
+tests_test_selfhosting_new_then_image_CPPFLAGS = $(tests_cppflags) $(glom_test_image_defines)
 
 tests_test_selfhosting_sqlinjection_SOURCES = tests/test_selfhosting_sqlinjection.cc $(sources_test_selfhosting_utils)
 tests_test_selfhosting_sqlinjection_LDADD = $(tests_ldadd)
diff --git a/tests/test_image.jpg b/tests/test_image.jpg
new file mode 100644
index 0000000..0472ba1
Binary files /dev/null and b/tests/test_image.jpg differ
diff --git a/tests/test_selfhosting_new_then_image.cc b/tests/test_selfhosting_new_then_image.cc
index d02a701..5e0b515 100644
--- a/tests/test_selfhosting_new_then_image.cc
+++ b/tests/test_selfhosting_new_then_image.cc
@@ -22,6 +22,8 @@
 #include <libglom/init.h>
 #include <libglom/utils.h>
 #include <libglom/db_utils.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
 #include <glib.h> //For g_assert()
 #include <iostream>
 #include <cstdlib> //For EXIT_SUCCESS and EXIT_FAILURE
@@ -37,35 +39,74 @@ static bool test(Glom::Document::HostingMode hosting_mode)
     return false;
   }
   
+
+  //Where clause:
+
+  const Glib::ustring table_name = "contacts";
+  const Glom::sharedptr<const Glom::Field> field = document.get_field(table_name, "picture");
+
   //Where clause:
-  const Glom::sharedptr<const Glom::Field> key_field = document.get_field("contacts", "contact_id");
+  const Glom::sharedptr<const Glom::Field> key_field = document.get_field(table_name, "contact_id");
   if(!key_field)
   {
     std::cerr << "Failure: Could not get key field." << std::endl;
     return false;
   }
-  
+
   const Gnome::Gda::SqlExpr where_clause = 
-    Glom::Utils::build_simple_where_expression("contacts", key_field, Gnome::Gda::Value(1));
-  
+    Glom::Utils::build_simple_where_expression(table_name, key_field, Gnome::Gda::Value(1));
+
+  //Fill a value from a file:
+  const std::string filename =
+    Glib::build_filename(GLOM_TESTS_IMAGE_DATA_NOTINSTALLED, "test_image.jpg");
+  std::string data;
+  try
+  {
+    data = Glib::file_get_contents(filename);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << "Failed: file_get_contents() failed: " << ex.what() << std::endl;
+    return false; //Something went wrong. It does not exist.
+  }
+
+  if(data.empty())
+  {
+    std::cerr << "Failed: The data read from the file was empty. filepath=" << filename << std::endl;
+    return false;
+  }
+
+  //Set the value:
+  Gnome::Gda::Value value_set((const guchar*)data.c_str(), data.size());
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder> builder_set = 
+    Glom::Utils::build_sql_update_with_where_clause(table_name,
+      field, value_set, where_clause);
+  const int rows_affected = Glom::DbUtils::query_execute(builder_set);
+  if(rows_affected == -1)
+  {
+    std::cerr << "Failure: UPDATE failed." << std::endl;
+    return false;
+  }
+
+
+  //Get the value:
   Glom::Utils::type_vecLayoutFields fieldsToGet;
-  Glom::sharedptr<const Glom::Field> field = document.get_field("contacts", "picture");
   Glom::sharedptr<Glom::LayoutItem_Field> layoutitem = Glom::sharedptr<Glom::LayoutItem_Field>::create();
   layoutitem->set_full_field_details(field);
   fieldsToGet.push_back(layoutitem);
 
-  const Glib::RefPtr<const Gnome::Gda::SqlBuilder> builder = 
-    Glom::Utils::build_sql_select_with_where_clause("contacts",
+  const Glib::RefPtr<const Gnome::Gda::SqlBuilder> builder_get = 
+    Glom::Utils::build_sql_select_with_where_clause(table_name,
       fieldsToGet, where_clause);
   Glib::RefPtr<Gnome::Gda::DataModel> data_model = 
-    Glom::DbUtils::query_execute_select(builder);
+    Glom::DbUtils::query_execute_select(builder_get);
   if(!test_model_expected_size(data_model, 1, 1))
   {
     std::cerr << "Failure: Unexpected data model size for main query." << std::endl;
     return false;
   }
 
-  const int count = Glom::DbUtils::count_rows_returned_by(builder);
+  const int count = Glom::DbUtils::count_rows_returned_by(builder_get);
   if(count != 1 )
   {
     std::cerr << "Failure: The COUNT query returned an unexpected value: " << count << std::endl;
@@ -73,9 +114,16 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   }
   
   const Gnome::Gda::Value value_read = data_model->get_value_at(0, 0);
-  if(value_read.get_value_type() != GDA_TYPE_NUMERIC)
+  if(value_read.get_value_type() != GDA_TYPE_BINARY)
   {
     std::cerr << "Failure: The value read was not of the expected type: " << g_type_name( value_read.get_value_type() ) << std::endl;
+    return false;
+  }
+
+  if(value_set != value_read)
+  {
+    std::cerr << "Failure: The value read was not equal to the value set." << std::endl;
+    return false;
   }
 
   test_selfhosting_cleanup();



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