[glom] Add a test for changing of a field name.



commit e798856848ee9ce30cea12a142b8558d64d05495
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Nov 29 09:55:57 2011 +0100

    Add a test for changing of a field name.
    
    * tests/Makefile.am:
    * tests/test_document_load_and_change.cc: Change a field name and
    make sure that it has updated throughout the document.
    However, we still need to check lookups and related fields.

 ChangeLog                                         |    9 ++
 Makefile_tests.am                                 |    6 +
 glom/libglom/connectionpool.h                     |   30 +++++
 tests/test_document_load_and_change.cc            |  142 +++++++++++++++++++++
 tests/test_selfhosting_new_then_change_columns.cc |    7 +-
 5 files changed, 193 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6efa502..e431d53 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2011-11-29  Murray Cumming  <murrayc murrayc com>
+
+	Add a test for changing of a field name. 
+
+	* tests/Makefile.am:
+	* tests/test_document_load_and_change.cc: Change a field name and 
+	make sure that it has updated throughout the document.
+	However, we still need to check lookups and related fields.
+
 2011-11-28  Murray Cumming  <murrayc murray com>
 
 	Field::set_field_info(): Avoid unnecessary checks (and warnings).
diff --git a/Makefile_tests.am b/Makefile_tests.am
index 1e9a59d..1516c55 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -23,6 +23,7 @@ check_PROGRAMS =						\
 	glom/libglom/example_document_load \
 	glom/libglom/test_sharedptr_layoutitem		\
 	tests/test_document_load			\
+	tests/test_document_load_and_change		\
 	tests/test_document_change			\
 	tests/test_document_autosave			\
 	tests/test_parsing_time			\
@@ -44,6 +45,7 @@ check_PROGRAMS =						\
 	tests/import/test_signals
 
 TESTS =	tests/test_document_load	\
+	tests/test_document_load_and_change	\
 	tests/test_document_change	\
 	tests/test_document_autosave	\
 	tests/test_parsing_time	\
@@ -98,6 +100,10 @@ tests_test_document_load_SOURCES = tests/test_document_load.cc
 tests_test_document_load_LDADD = $(tests_ldadd)
 tests_test_document_load_CPPFLAGS = $(tests_cppflags)
 
+tests_test_document_load_and_change_SOURCES = tests/test_document_load_and_change.cc
+tests_test_document_load_and_change_LDADD = $(tests_ldadd)
+tests_test_document_load_and_change_CPPFLAGS = $(tests_cppflags)
+
 tests_test_document_change_SOURCES = tests/test_document_change.cc
 tests_test_document_change_LDADD = $(tests_ldadd)
 tests_test_document_change_CPPFLAGS = $(tests_cppflags)
diff --git a/glom/libglom/connectionpool.h b/glom/libglom/connectionpool.h
index f9d2937..e0fd497 100644
--- a/glom/libglom/connectionpool.h
+++ b/glom/libglom/connectionpool.h
@@ -243,12 +243,42 @@ public:
    */
   virtual bool set_network_shared(const SlotProgress& slot_progress, bool network_shared = true);
 
+  /** Add a field to the database.
+   * The caller should then update the document's list of fields,
+   * for instance by calling Document::set_table_fields(). 
+   *
+   * @param table_name The parent table of the fields to be changed.
+   * @param field The field to be added.
+   */
   bool add_column(const Glib::ustring& table_name, const sharedptr<const Field>& field) throw();
 
+  /** Remove a field from the database.
+   * The caller should then update the document's list of fields,
+   * for instance by calling Document::set_table_fields(). 
+   *
+   * @param table_name The parent table of the fields to be changed.
+   * @param field_name The name of the field to be removed.
+   */
   bool drop_column(const Glib::ustring& table_name, const Glib::ustring& field_name) throw();
 
+  /** Change some detail about a field in the database.
+   * The caller should then update the document's list of fields,
+   * for instance by calling Document::set_table_fields(). 
+   *
+   * @param table_name The parent table of the field to be changed.
+   * @param field_old The old field information.
+   * @param field The new field information. 
+   */
   bool change_column(const Glib::ustring& table_name, const sharedptr<const Field>& field_old, const sharedptr<const Field>& field) throw();
 
+  /** Change some detail about some fields in the database.
+   * The caller should then update the document's list of fields,
+   * for instance by calling Document::set_table_fields(). 
+   *
+   * @param table_name The parent table of the fields to be changed.
+   * @param old_fields The old field information.
+   * @param fields The new field information. 
+   */
   bool change_columns(const Glib::ustring& table_name, const type_vec_const_fields& old_fields, const type_vec_const_fields& fields) throw();
 
   /** Specify a callback that the ConnectionPool can call to get a pointer to the document.
diff --git a/tests/test_document_load_and_change.cc b/tests/test_document_load_and_change.cc
new file mode 100644
index 0000000..932bb9e
--- /dev/null
+++ b/tests/test_document_load_and_change.cc
@@ -0,0 +1,142 @@
+/* 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
+71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <libglom/document/document.h>
+#include <libglom/init.h>
+#include <giomm/file.h>
+#include <glibmm/convert.h>
+#include <glibmm/miscutils.h>
+
+#include <iostream>
+
+template<typename T_Container>
+bool contains(const T_Container& container, const Glib::ustring& name)
+{
+  typename T_Container::const_iterator iter =
+    std::find(container.begin(), container.end(), name);
+  return iter != container.end();
+}
+
+template<typename T_Container>
+bool contains_named(const T_Container& container, const Glib::ustring& name)
+{
+  typedef typename T_Container::value_type::object_type type_item;
+  typename T_Container::const_iterator iter =
+    std::find_if(container.begin(), container.end(),
+      Glom::predicate_FieldHasName<type_item>(name));
+  return iter != container.end();
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  // Get a URI for a test file:
+  Glib::ustring uri;
+
+  try
+  {
+    const std::string path =
+       Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED,
+         "example_music_collection.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;
+  }
+
+  //Prevent these test changes from being saved back to the example file:
+  document.set_allow_autosave(false);
+
+  //Change a field name throughout the document:
+  const Glib::ustring table_name = "songs";
+  const Glib::ustring field_name_original = "song_id";
+  const Glib::ustring field_name_new = "newfieldname";
+  document.change_field_name(table_name, field_name_original, field_name_new);
+
+  //Check that the original field name is not known to the document:
+  if(document.get_field(table_name, field_name_original))
+  {
+    std::cerr << "Failure: The document should have forgotten about the original field name." << std::endl;
+    return false;
+  }
+
+  //Check that the new field name is known to the document:
+  if(!(document.get_field(table_name, field_name_new)))
+  {
+    std::cerr << "Failure: The document does not know about the new field name." << std::endl;
+    return false;
+  }
+
+  //Check that the original field name is no longer used in the relationship:
+  const Glom::sharedptr<const Glom::Relationship> relationship = document.get_relationship("songs", "album");
+  if(!relationship)
+  {
+    std::cerr << "Failure: The relationship could not be found in the document." << std::endl;
+    return false;
+  }
+
+  if(relationship->get_from_field() == field_name_original)
+  {
+    std::cerr << "Failure: The relationship still uses the original field name." << std::endl;
+    return false;
+  }
+
+  //Check that the original field name is no longer used on a layout:
+  const std::vector<Glib::ustring> table_names = document.get_table_names();
+  for(std::vector<Glib::ustring>::const_iterator iter = table_names.begin(); iter != table_names.end(); ++iter)
+  {
+    const Glib::ustring table_name = *iter;
+    const Glom::Document::type_list_layout_groups groups = 
+      document.get_data_layout_groups("details", table_name);
+
+    for(Glom::Document::type_list_layout_groups::const_iterator iter = groups.begin(); iter != groups.end(); ++iter)
+    {
+      const Glom::sharedptr<Glom::LayoutGroup> group = *iter;
+      if(group->has_field(field_name_original))
+      {
+        std::cerr << "Failure: The field is still used on a layout for table: " << table_name << std::endl;
+        return false;
+      }
+    }
+  }
+
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}
diff --git a/tests/test_selfhosting_new_then_change_columns.cc b/tests/test_selfhosting_new_then_change_columns.cc
index 3a7bd5f..357f205 100644
--- a/tests/test_selfhosting_new_then_change_columns.cc
+++ b/tests/test_selfhosting_new_then_change_columns.cc
@@ -43,7 +43,8 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   }
   
   const Glib::ustring table_name = "contacts";
-  const Glom::sharedptr<const Glom::Field> field = document.get_field(table_name, "date_of_birth");
+  const Glib::ustring field_name_original = "date_of_birth";
+  const Glom::sharedptr<const Glom::Field> field = document.get_field(table_name, field_name_original);
   if(!field)
   {
     std::cerr << "Failure: Could not get field." << std::endl;
@@ -117,6 +118,10 @@ static bool test(Glom::Document::HostingMode hosting_mode)
     return false;
   }
 
+  //Anything using this code would then update the Glom::Document,
+  //for instance by calling Document::set_table_fields(),
+  //but we are not testing that here.
+
   test_selfhosting_cleanup();
  
   return true; 



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