1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
/* Glom
*
* Copyright (C) 2010 Openismus GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
#include "tests/test_selfhosting_utils.h"
#include <libglom/init.h>
#include <libglom/utils.h>
#include <libglom/db_utils.h>
#include <libglom/connectionpool.h>
#include <libglom/data_structure/glomconversions.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
static bool test(Glom::Document::HostingMode hosting_mode)
{
Glom::Document document;
const bool recreated =
test_create_and_selfhost_from_example("example_smallbusiness.glom", document, hosting_mode);
if(!recreated)
{
std::cerr << G_STRFUNC << ": Recreation failed." << std::endl;
return false;
}
const Glib::ustring table_name = "products";
std::shared_ptr<const Glom::Field> primary_key_field = document.get_field_primary_key(table_name);
if(!primary_key_field)
{
std::cerr << G_STRFUNC << ": Failure: primary_key_field is empty." << std::endl;
return false;
}
//Check that some data is as expected:
const auto pk_value = Gnome::Gda::Value::create_as_double(2.0l);
const Gnome::Gda::SqlExpr where_clause =
Glom::Utils::build_simple_where_expression(table_name, primary_key_field, pk_value);
Glom::Utils::type_vecLayoutFields fieldsToGet;
std::shared_ptr<const Glom::Field> field = document.get_field(table_name, "price");
std::shared_ptr<Glom::LayoutItem_Field> layoutitem = std::make_shared<Glom::LayoutItem_Field>();
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(table_name,
fieldsToGet, where_clause);
const Glib::RefPtr<const Gnome::Gda::DataModel> data_model =
Glom::DbUtils::query_execute_select(builder);
if(!test_model_expected_size(data_model, 1, 1))
{
std::cerr << G_STRFUNC << "Failure: Unexpected data model size with query: " <<
Glom::Utils::sqlbuilder_get_full_query(builder) << std::endl;
return false;
}
const auto count = Glom::DbUtils::count_rows_returned_by(builder);
if(count != 1 )
{
std::cerr << G_STRFUNC << "Failure: The COUNT query returned an unexpected value: " << count << std::endl;
return false;
}
//Get the value from the result:
const auto value = data_model->get_value_at(0, 0);
if(!test_check_numeric_value_type(hosting_mode, value))
{
std::cerr << G_STRFUNC << ": Failure: The value has an unexpected type: " <<
g_type_name(value.get_value_type()) << std::endl;
return false;
}
if(Glom::Conversions::get_double_for_gda_value_numeric(value) != 3.5l)
{
std::cerr << G_STRFUNC << ": Failure: The value has an unexpected value: " << value.to_string() << " instead of 3.5" << std::endl;
std::cerr << G_STRFUNC << ": value as string: " << value.to_string() << std::endl;
std::cerr << G_STRFUNC << ": value GType: " << g_type_name(value.get_value_type()) << std::endl;
return false;
}
test_selfhosting_cleanup();
return true;
}
int main()
{
Glom::libglom_init();
const auto result = test_all_hosting_modes(sigc::ptr_fun(&test));
Glom::libglom_deinit();
return result;
}
|