Commit 4c561b07 authored by Murray Cumming's avatar Murray Cumming
Browse files

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 .
parent e0e6e9b2
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().
......
......@@ -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)
......
......@@ -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();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment