[glom] Add a test for correct default field values.



commit 93f654f5f20a7fb3ff2ad034491c1b58401470d8
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Nov 14 09:42:37 2013 +0100

    Add a test for correct default field values.
    
    * examples/example_smallbusiness.glom: Add a string default
      value, so we can test it. There are already some numeric
      default values.
    * Makefile_tests.am:
    * tests/test_selfhosting_new_from_example_defaultvalues.cc:
      Check some default values.

 Makefile_tests.am                                  |    6 +
 examples/example_smallbusiness.glom                |    2 +-
 glom/libglom/data_structure/field.cc               |    2 +-
 ...t_selfhosting_new_from_example_defaultvalues.cc |  121 ++++++++++++++++++++
 4 files changed, 129 insertions(+), 2 deletions(-)
---
diff --git a/Makefile_tests.am b/Makefile_tests.am
index 8e3e7af..67d2f2c 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -37,6 +37,7 @@ check_PROGRAMS =                                              \
        tests/test_selfhosting_new_empty_change_sysprefs \
        tests/test_selfhosting_new_empty_then_users \
        tests/test_selfhosting_new_from_example \
+       tests/test_selfhosting_new_from_example_defaultvalues \
        tests/test_selfhosting_new_from_example_float \
        tests/test_selfhosting_new_from_example_operator \
        tests/test_selfhosting_new_from_example_strangepath \
@@ -77,6 +78,7 @@ TESTS =       tests/test_document_load        \
        tests/test_selfhosting_new_empty_change_sysprefs \
        tests/test_selfhosting_new_empty_then_users \
        tests/test_selfhosting_new_from_example \
+       tests/test_selfhosting_new_from_example_defaultvalues \
        tests/test_selfhosting_new_from_example_float \
        tests/test_selfhosting_new_from_example_operator \
        tests/test_selfhosting_new_from_example_in_locales.sh \
@@ -263,6 +265,10 @@ tests_test_selfhosting_new_from_example_SOURCES = tests/test_selfhosting_new_fro
 tests_test_selfhosting_new_from_example_LDADD = $(tests_ldadd)
 tests_test_selfhosting_new_from_example_CPPFLAGS = $(tests_cppflags)
 
+tests_test_selfhosting_new_from_example_defaultvalues_SOURCES = 
tests/test_selfhosting_new_from_example_defaultvalues.cc $(sources_test_selfhosting_utils)
+tests_test_selfhosting_new_from_example_defaultvalues_LDADD = $(tests_ldadd)
+tests_test_selfhosting_new_from_example_defaultvalues_CPPFLAGS = $(tests_cppflags)
+
 tests_test_selfhosting_new_from_example_float_SOURCES = tests/test_selfhosting_new_from_example_float.cc 
$(sources_test_selfhosting_utils)
 tests_test_selfhosting_new_from_example_float_LDADD = $(tests_ldadd)
 tests_test_selfhosting_new_from_example_float_CPPFLAGS = $(tests_cppflags)
diff --git a/examples/example_smallbusiness.glom b/examples/example_smallbusiness.glom
index f94bba0..05fb311 100644
--- a/examples/example_smallbusiness.glom
+++ b/examples/example_smallbusiness.glom
@@ -608,7 +608,7 @@ http://www.flickr.com/photos/mape_s/350700095/</value>
           <trans loc="sr latin" val="Adresa el. pošte"/>
         </trans_set>
       </field>
-      <field name="website" type="Text" title="Web Site">
+      <field name="website" default_value="http://"; type="Text" title="Web Site">
         <formatting/>
         <trans_set>
           <trans loc="cs" val="Webové stránky"/>
diff --git a/glom/libglom/data_structure/field.cc b/glom/libglom/data_structure/field.cc
index f0e7984..b6722a0 100644
--- a/glom/libglom/data_structure/field.cc
+++ b/glom/libglom/data_structure/field.cc
@@ -591,7 +591,7 @@ bool Field::field_info_from_database_is_equal(const Glib::RefPtr<const Gnome::Gd
 
   temp->set_auto_increment( field->get_auto_increment() ); //Don't compare this, because the data is 
incorrect when libgda reads it from the database.
 
-  Gnome::Gda::Value value = field->get_default_value();
+  const Gnome::Gda::Value value = field->get_default_value();
   temp->set_default_value(value); //Don't compare this, because the data is incorrect when libgda reads it 
from the database.
 
   //TODO_gda: temp->set_primary_key( field->get_primary_key() ); //Don't compare this, because the data is 
incorrect when libgda reads it from the database.
diff --git a/tests/test_selfhosting_new_from_example_defaultvalues.cc 
b/tests/test_selfhosting_new_from_example_defaultvalues.cc
new file mode 100644
index 0000000..33350fc
--- /dev/null
+++ b/tests/test_selfhosting_new_from_example_defaultvalues.cc
@@ -0,0 +1,121 @@
+/* Glom
+ *
+ * Copyright (C) 2010-2013 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/data_structure/glomconversions.h>
+#include <libglom/db_utils.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;
+  }
+
+
+
+  //Check a numeric field:
+  Glib::ustring table_name = "invoice_lines";
+  if(!test_table_exists(table_name, document))
+  {
+    return false;
+  }
+
+  Glom::sharedptr<const Glom::Field> field = document.get_field(table_name, "count");
+  if(!field)
+  {
+    std::cerr << G_STRFUNC << ": Failure: Could not get field." << std::endl;
+    return false;
+  }
+
+  Gnome::Gda::Value default_value = field->get_default_value();
+  if(default_value.get_value_type() != GDA_TYPE_NUMERIC)
+  {
+    std::cerr << G_STRFUNC << ": Failure: Unexpected type of default value." << std::endl;
+    std::cerr << "  type=" << g_type_name(default_value.get_value_type()) << std::endl;
+    return false;
+  }
+
+  const double num = Glom::Conversions::get_double_for_gda_value_numeric(default_value);
+  if(num != 1)
+  {
+    std::cerr << G_STRFUNC << ": Failure: Unexpected value of default value:" << num << std::endl;
+    return false;
+  }
+
+
+  //Check a text field:
+  table_name = "contacts";
+  if(!test_table_exists(table_name, document))
+  {
+    return false;
+  }
+
+  field = document.get_field(table_name, "website");
+  if(!field)
+  {
+    std::cerr << G_STRFUNC << ": Failure: Could not get field." << std::endl;
+    return false;
+  }
+
+  default_value = field->get_default_value();
+  if(default_value.get_value_type() != G_TYPE_STRING)
+  {
+    std::cerr << G_STRFUNC << ": Failure: Unexpected type of default value." << std::endl;
+    std::cerr << "  type=" << g_type_name(default_value.get_value_type()) << std::endl;
+    return false;
+  }
+
+  const Glib::ustring str = default_value.get_string();
+  if(str != "http://";)
+  {
+    std::cerr << G_STRFUNC << ": Failure: Unexpected value of default value: " << str << std::endl;
+    return false;
+  }
+
+
+  test_selfhosting_cleanup();
+ 
+  return true; 
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  //We run this test in several locales via 
+  //test_selfhosting_new_from_example_in_locales.sh,
+  //so we do this so the locale will really be used:
+  setlocale(LC_ALL, "");
+  
+  const int result = test_all_hosting_modes(sigc::ptr_fun(&test));
+
+  Glom::libglom_deinit();
+
+  return result;
+}


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