[glom] test_document_load: Test some navigation utility functions.



commit e51bf19e8a595dbc4e34c390ae2194293deb0236
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Dec 2 12:57:54 2011 +0100

    test_document_load: Test some navigation utility functions.
    
    * glom/libglom/data_structure/layout/layoutgroup.cc: get_items_recursive():
      Return the child groups as well as their items.
    * tests/test_document_load.cc:
    Test DbUtils::layout_field_should_have_navigation() and
    LayoutItem_Portal::get_suitable_table_to_view_details().

 ChangeLog                                         |   10 ++
 glom/libglom/data_structure/layout/layoutgroup.cc |   11 ++-
 tests/test_document_load.cc                       |   99 +++++++++++++++++++++
 3 files changed, 118 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 52ce63c..f5f845b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2011-12-02  Murray Cumming  <murrayc murrayc com>
 
+	test_document_load: Test some navigation utility functions.
+
+	* glom/libglom/data_structure/layout/layoutgroup.cc: get_items_recursive():
+  Return the child groups as well as their items.
+	* tests/test_document_load.cc:
+	Test DbUtils::layout_field_should_have_navigation() and 
+	LayoutItem_Portal::get_suitable_table_to_view_details().
+	
+2011-12-02  Murray Cumming  <murrayc murrayc com>
+
 	test_selfhosting_new_empty(): Also test DbUtils::get_unused_database_name().
 
 	* glom/libglom/db_utils.cc: get_unused_database_name(): Remove debug output.
diff --git a/glom/libglom/data_structure/layout/layoutgroup.cc b/glom/libglom/data_structure/layout/layoutgroup.cc
index e91fbb0..7860b0d 100644
--- a/glom/libglom/data_structure/layout/layoutgroup.cc
+++ b/glom/libglom/data_structure/layout/layoutgroup.cc
@@ -22,6 +22,7 @@
 #include <libglom/data_structure/layout/layoutitem_field.h>
 #include <libglom/data_structure/layout/layoutitem_portal.h>
 #include <glibmm/i18n.h>
+#include <iostream> 
 
 namespace Glom
 {
@@ -215,14 +216,16 @@ LayoutGroup::type_list_const_items LayoutGroup::get_items_recursive() const
   for(type_list_items::const_iterator iter = m_list_items.begin(); iter != m_list_items.end(); ++iter)
   {
     const sharedptr<const LayoutItem> item = *iter;
+    
+    //Add the item itself:
+    result.push_back(item);
+    
     sharedptr<const LayoutGroup> group = sharedptr<const LayoutGroup>::cast_dynamic(item);
     if(group)
     {
       const type_list_const_items sub_result = group->get_items_recursive();
       result.insert(result.end(), sub_result.begin(), sub_result.end());
     }
-    else
-      result.push_back(item);
   }
 
   return result;
@@ -235,6 +238,10 @@ LayoutGroup::type_list_items LayoutGroup::get_items_recursive()
   for(type_list_items::const_iterator iter = m_list_items.begin(); iter != m_list_items.end(); ++iter)
   {
     const sharedptr<LayoutItem> item = *iter;
+    
+    //Add the item itself:
+    result.push_back(item);
+    
     sharedptr<LayoutGroup> group = sharedptr<LayoutGroup>::cast_dynamic(item);
     if(group)
     {
diff --git a/tests/test_document_load.cc b/tests/test_document_load.cc
index a8caa51..3f8d23a 100644
--- a/tests/test_document_load.cc
+++ b/tests/test_document_load.cc
@@ -20,6 +20,7 @@
 
 #include <libglom/document/document.h>
 #include <libglom/init.h>
+#include <libglom/db_utils.h>
 #include <giomm/file.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
@@ -75,6 +76,57 @@ static Glom::sharedptr<const Glom::LayoutItem_Field> get_field_on_layout(const G
   return Glom::sharedptr<const Glom::LayoutItem_Field>();
 }
 
+static bool needs_navigation(Glom::Document& document, const Glib::ustring& table_name, const Glib::ustring& field_name)
+{
+  Glom::sharedptr<Glom::LayoutItem_Field> layout_item = Glom::sharedptr<Glom::LayoutItem_Field>::create();
+  layout_item->set_name(field_name);
+  layout_item->set_full_field_details(
+    document.get_field(table_name, field_name));
+
+  Glom::sharedptr<Glom::Relationship> field_used_in_relationship_to_one;
+  return Glom::DbUtils::layout_field_should_have_navigation(table_name, 
+    layout_item, &document, field_used_in_relationship_to_one);
+}
+
+static Glom::sharedptr<const Glom::LayoutItem_Portal> get_portal_from_details_layout(const Glom::Document& document, const Glib::ustring& table_name, const Glib::ustring& relationship_name)
+{
+  const Glom::Document::type_list_layout_groups groups = 
+    document.get_data_layout_groups("details", table_name);
+  if(groups.empty())
+  {
+    std::cerr << G_STRFUNC << ": groups is empty." << std::endl;
+  }
+  
+  for(Glom::Document::type_list_layout_groups::const_iterator iter = groups.begin(); iter != groups.end(); ++iter)
+  {
+    const Glom::sharedptr<const Glom::LayoutGroup> group = *iter;
+
+    const Glom::LayoutGroup::type_list_const_items items = 
+      group->get_items_recursive();
+    for(Glom::LayoutGroup::type_list_const_items::const_iterator iter = items.begin(); 
+      iter != items.end(); ++iter)
+    {
+      const Glom::sharedptr<const Glom::LayoutItem> layout_item = *iter;
+ 
+      const Glom::sharedptr<const Glom::LayoutGroup> group =
+        Glom::sharedptr<const Glom::LayoutGroup>::cast_dynamic(layout_item);
+      if(!group)
+        continue;
+
+      const Glom::sharedptr<const Glom::LayoutItem_Portal> portal =
+        Glom::sharedptr<const Glom::LayoutItem_Portal>::cast_dynamic(layout_item);
+      if(!portal)
+        continue;
+      
+      if(portal->get_relationship_name() == relationship_name)
+        return portal;
+    }
+  }
+      
+  return Glom::sharedptr<Glom::LayoutItem_Portal>();
+}
+  
+ 
 int main()
 {
   Glom::libglom_init();
@@ -233,6 +285,53 @@ int main()
     return false;
   }
 
+  //Test navigation:
+  if(!needs_navigation(document, "scenes", "location_id"))
+  {
+    std::cerr << "Failure: DbUtils::layout_field_should_have_navigation() did not return the expected result." << std::endl;
+    return false;
+  }
+
+  if(needs_navigation(document, "scenes", "description"))
+  {
+    std::cerr << "Failure: DbUtils::layout_field_should_have_navigation() did not return the expected result." << std::endl;
+    return false;
+  }
+
+
+  //Test portal navigation.
+  //Note that related records portals don't have names.
+  //This example portal shows the scenes_cast table, but should navigate though that to the cast table.
+  const Glib::ustring portal_relationship_name = "scene_cast";
+  Glom::sharedptr<const Glom::LayoutItem_Portal> portal = 
+    get_portal_from_details_layout(document, "scenes", portal_relationship_name);
+  if(!portal)
+  {
+    std::cerr << "Failure: Could not get the portal from the layout." << std::endl;
+    return false;
+  }
+
+  Glib::ustring navigation_table_name;
+  Glom::sharedptr<const Glom::UsesRelationship> navigation_relationship;
+  portal->get_suitable_table_to_view_details(navigation_table_name, navigation_relationship, &document);
+
+  if(navigation_table_name != "characters")
+  {
+    std::cerr << "Failure: get_suitable_table_to_view_details() returned an unexpected table name: " << navigation_table_name << std::endl;
+    return false;
+  }
+
+  if(!navigation_relationship)
+  {
+    std::cerr << "Failure: get_suitable_table_to_view_details() returned an empty navigation_relationship." << std::endl;
+    return false;
+  }
+
+  if(navigation_relationship->get_relationship_name() != "cast")
+  {
+    std::cerr << "Failure: get_suitable_table_to_view_details() returned an unexpected navigation_relationship name: " << navigation_relationship->get_relationship_name() << std::endl;
+    return false;
+  }
 
   Glom::libglom_deinit();
 



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