[glom] Test report contents.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] Test report contents.
- Date: Tue, 18 Oct 2011 18:40:09 +0000 (UTC)
commit 1db3de454b2005ae905d161b8b1d90ba94d03a0a
Author: Murray Cumming <murrayc murrayc com>
Date: Mon Oct 17 22:24:38 2011 +0200
Test report contents.
* glom/libglom/xsl_utils.[h|cc]: transform(): Return the contents,
not a filepath.
* glom/libglom/report_builder.[h|cc]: report_build(): return the
contents, not the path. Add report_build_and_save() to get a temporary
filepath.
* glom/mode_data/box_data_manyrecords.cc:
* glom/frame_glom.cc: Adapted.
* tests/test_selfhosting_new_then_report.cc: Check that some expected
text is in the generated HTML.
ChangeLog | 14 +++++++
glom/frame_glom.cc | 2 +-
glom/libglom/report_builder.cc | 54 ++++++++++++++++++++++++++++-
glom/libglom/report_builder.h | 10 ++++-
glom/libglom/xsl_utils.cc | 50 +-------------------------
glom/libglom/xsl_utils.h | 4 +-
glom/mode_data/box_data_manyrecords.cc | 2 +-
tests/test_selfhosting_new_then_report.cc | 11 +++---
8 files changed, 87 insertions(+), 60 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2347875..78ecbf5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -52,6 +52,20 @@
2011-10-17 Murray Cumming <murrayc murrayc com>
+ Test report contents.
+
+ * glom/libglom/xsl_utils.[h|cc]: transform(): Return the contents,
+ not a filepath.
+ * glom/libglom/report_builder.[h|cc]: report_build(): return the
+ contents, not the path. Add report_build_and_save() to get a temporary
+ filepath.
+ * glom/mode_data/box_data_manyrecords.cc:
+ * glom/frame_glom.cc: Adapted.
+ * tests/test_selfhosting_new_then_report.cc: Check that some expected
+ text is in the generated HTML.
+
+2011-10-17 Murray Cumming <murrayc murrayc com>
+
Test report building.
* tests/test_selfhosting_new_from_example.cc: Move most code into
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index cdcc81e..1293924 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -2317,7 +2317,7 @@ void Frame_Glom::on_menu_report_selected(const Glib::ustring& report_name)
ReportBuilder report_builder;
report_builder.set_document(document);
const std::string filepath =
- report_builder.report_build(found_set, report); //TODO: Use found set's where_clause.
+ report_builder.report_build_and_save(found_set, report); //TODO: Use found set's where_clause.
Utils::show_report_in_browser(filepath, get_app_window());
}
diff --git a/glom/libglom/report_builder.cc b/glom/libglom/report_builder.cc
index 8d773f3..af4a2a6 100644
--- a/glom/libglom/report_builder.cc
+++ b/glom/libglom/report_builder.cc
@@ -23,6 +23,7 @@
#include <libglom/data_structure/glomconversions.h>
#include <libglom/db_utils.h>
#include <libglom/xsl_utils.h>
+#include <iostream>
#include <glibmm/i18n.h>
namespace Glom
@@ -478,8 +479,59 @@ void ReportBuilder::report_build_records_vertical_group(const FoundSet& found_se
}
}
+std::string ReportBuilder::report_build_and_save(const FoundSet& found_set, const sharedptr<const Report>& report)
+{
+ const Glib::ustring contents = report_build(found_set, report);
+
+ //Save it to a temporary file and show it in a browser:
+ const std::string temp_path = Glib::build_filename(
+ Glib::get_tmp_dir(), "glom_printout.html");
+ std::cout << G_STRFUNC << ": temp_path=" << temp_path << std::endl;
+
+ Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(temp_path);
+ Glib::RefPtr<Gio::FileOutputStream> stream;
+
+ //Create the file if it does not already exist:
+ try
+ {
+ if(file->query_exists())
+ {
+ stream = file->replace(); //Instead of append_to().
+ }
+ else
+ {
+ //By default files created are generally readable by everyone, but if we pass FILE_CREATE_PRIVATE in flags the file will be made readable only to the current user, to the level that is supported on the target filesystem.
+ //TODO: Do we want to specify 0660 exactly? (means "this user and his group can read and write this non-executable file".)
+ stream = file->create_file();
+ }
+ }
+ catch(const Gio::Error& ex)
+ {
+ // If the operation was not successful, print the error and abort
+ return std::string(); // print_error(ex, output_uri_string);
+ }
+
+ //Write the data to the output uri
+ gssize bytes_written = 0;
+ const Glib::ustring::size_type result_bytes = contents.bytes();
+ try
+ {
+ bytes_written = stream->write(contents.data(), result_bytes);
+ }
+ catch(const Gio::Error& ex)
+ {
+ // If the operation was not successful, print the error and abort
+ return std::string(); // false; //print_error(ex, output_uri_string);
+ }
+
+ if(bytes_written != (gssize)result_bytes)
+ return std::string(); //false
+
+ return file->get_path();
+}
+
-std::string ReportBuilder::report_build(const FoundSet& found_set, const sharedptr<const Report>& report)
+Glib::ustring ReportBuilder::report_build(const FoundSet& found_set, const sharedptr<const Report>& report)
{
//Create a DOM Document with the XML:
xmlpp::DomParser dom_parser;;
diff --git a/glom/libglom/report_builder.h b/glom/libglom/report_builder.h
index 253b5c4..641c7f6 100644
--- a/glom/libglom/report_builder.h
+++ b/glom/libglom/report_builder.h
@@ -46,9 +46,15 @@ public:
//sharedptr<Report> get_report();
/**
- * @result The filepath of the generated HTML file.
+ * @result The HTML of the generated report.
*/
- std::string report_build(const FoundSet& found_set, const sharedptr<const Report>& report);
+ Glib::ustring report_build(const FoundSet& found_set, const sharedptr<const Report>& report);
+
+ /**
+ * @result The filepath of a temporary file containing the generated HTML file.
+ */
+ std::string report_build_and_save(const FoundSet& found_set, const sharedptr<const Report>& report);
+
private:
diff --git a/glom/libglom/xsl_utils.cc b/glom/libglom/xsl_utils.cc
index 0249cc1..20c6db1 100644
--- a/glom/libglom/xsl_utils.cc
+++ b/glom/libglom/xsl_utils.cc
@@ -126,57 +126,11 @@ static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std
return result;
}
-std::string GlomXslUtils::transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path)
+Glib::ustring GlomXslUtils::transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path)
{
//Use libxslt to convert the XML to HTML:
- const Glib::ustring result = xslt_process(xml_document, get_xslt_file(xsl_file_path));
+ return xslt_process(xml_document, get_xslt_file(xsl_file_path));
//std::cout << "After xslt: " << result << std::endl;
-
- //Save it to a temporary file and show it in a browser:
- const std::string temp_path = Glib::build_filename(
- Glib::get_tmp_dir(), "glom_printout.html");
- std::cout << "temp_path=" << temp_path << std::endl;
-
- Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(temp_path);
- Glib::RefPtr<Gio::FileOutputStream> stream;
-
- //Create the file if it does not already exist:
- try
- {
- if(file->query_exists())
- {
- stream = file->replace(); //Instead of append_to().
- }
- else
- {
- //By default files created are generally readable by everyone, but if we pass FILE_CREATE_PRIVATE in flags the file will be made readable only to the current user, to the level that is supported on the target filesystem.
- //TODO: Do we want to specify 0660 exactly? (means "this user and his group can read and write this non-executable file".)
- stream = file->create_file();
- }
- }
- catch(const Gio::Error& ex)
- {
- // If the operation was not successful, print the error and abort
- return std::string(); // print_error(ex, output_uri_string);
- }
-
- //Write the data to the output uri
- gssize bytes_written = 0;
- const Glib::ustring::size_type result_bytes = result.bytes();
- try
- {
- bytes_written = stream->write(result.data(), result_bytes);
- }
- catch(const Gio::Error& ex)
- {
- // If the operation was not successful, print the error and abort
- return std::string(); // false; //print_error(ex, output_uri_string);
- }
-
- if(bytes_written != (gssize)result_bytes)
- return std::string(); //false
-
- return file->get_path();
}
} //namespace Glom
diff --git a/glom/libglom/xsl_utils.h b/glom/libglom/xsl_utils.h
index 2f7741d..2107e9e 100644
--- a/glom/libglom/xsl_utils.h
+++ b/glom/libglom/xsl_utils.h
@@ -39,9 +39,9 @@ namespace GlomXslUtils
{
/**
- * @result the filepath of the generated HTML file.
+ * @result the generated HTML.
*/
-std::string transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path);
+Glib::ustring transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path);
} //namespace GlomXslUtils
diff --git a/glom/mode_data/box_data_manyrecords.cc b/glom/mode_data/box_data_manyrecords.cc
index efc7177..6a83dad 100644
--- a/glom/mode_data/box_data_manyrecords.cc
+++ b/glom/mode_data/box_data_manyrecords.cc
@@ -85,7 +85,7 @@ void Box_Data_ManyRecords::print_layout()
ReportBuilder report_builder;
report_builder.set_document(document);
const std::string filepath =
- report_builder.report_build(m_found_set, report_temp);
+ report_builder.report_build_and_save(m_found_set, report_temp);
Utils::show_report_in_browser(filepath, get_app_window());
}
}
diff --git a/tests/test_selfhosting_new_then_report.cc b/tests/test_selfhosting_new_then_report.cc
index 398ef7c..9701111 100644
--- a/tests/test_selfhosting_new_then_report.cc
+++ b/tests/test_selfhosting_new_then_report.cc
@@ -41,21 +41,22 @@ int main()
found_set.m_table_name = "albums";
Glom::ReportBuilder report_builder;
report_builder.set_document(&document);
- const std::string filepath =
+ const Glib::ustring html =
report_builder.report_build(found_set, report_temp);
- if(filepath.empty())
+
+ if(html.empty())
{
test_selfhosting_cleanup();
+ std::cerr << "Failed: html was empty." << std::endl;
return EXIT_FAILURE;
}
- /*
- if(filecontents.find("Bruce Springsteen") == std::string::npos)
+ if(html.find("Bruce Springsteen") == std::string::npos)
{
+ std::cerr << "Failed: html did not contain the expected text." << std::endl;
test_selfhosting_cleanup();
return EXIT_FAILURE;
}
- */
test_selfhosting_cleanup();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]