summaryrefslogtreecommitdiff
path: root/server/src/widgetgenerator.cc
diff options
context:
space:
mode:
authordeva <deva>2009-08-07 13:00:10 +0000
committerdeva <deva>2009-08-07 13:00:10 +0000
commit1e1217e49bcac9228e37c7b6868d63da900ab07f (patch)
treef963957dcbc8ffe313e6eb41fc6fbb8f939b2993 /server/src/widgetgenerator.cc
parent83722ef5a1c46ee3e9a15d9c16daf227f06e56cb (diff)
Unit test and header documentation.
Diffstat (limited to 'server/src/widgetgenerator.cc')
-rw-r--r--server/src/widgetgenerator.cc121
1 files changed, 121 insertions, 0 deletions
diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc
index f8f3790..1e60479 100644
--- a/server/src/widgetgenerator.cc
+++ b/server/src/widgetgenerator.cc
@@ -198,3 +198,124 @@ std::string widgetgenerator(std::string cpr, Macro &macro, LUAQueryMapper &mappe
return send_macro_widget(macro, macro.widgets, " ", mapper, values);
}
+
+#ifdef TEST_WIDGETGENERATOR
+
+#include <time.h>
+
+#define PATIENTID "1234567890"
+
+int main()
+{
+ time_t now = time(NULL);
+
+ printf("Test pretty printer:\n");
+ {
+ Macro macro;
+ macro.widgets.attributes["tagname"] = "lineedit";
+ macro.widgets.attributes["name"] = "mywidget";
+ macro.widgets.attributes["value"] = "myvalue";
+ macro.widgets.attributes["map"] = "mapvalue";
+ macro.widgets.widgets.push_back(macro.widgets);
+
+ Database db("testdb", "", "", "", "", "");
+ LUAQueryMapper mapper;
+ std::string result;
+ Fields fields;
+ QueryResult queryresult;
+
+ // Test simple
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result != " <lineedit name=\"mywidget\" value=\"myvalue\">\n"
+ " <lineedit name=\"mywidget\" value=\"myvalue\"/>\n"
+ " </lineedit>\n") return 1;
+ }
+
+ printf("Positive tests:\n");
+ {
+ Macro macro;
+ macro.widgets.attributes["tagname"] = "lineedit";
+ macro.widgets.attributes["name"] = "mywidget";
+ macro.widgets.attributes["value"] = "myvalue";
+ macro.widgets.attributes["map"] = "mapvalue";
+
+ Database db("testdb", "", "", "", "", "");
+ LUAQueryMapper mapper;
+ std::string result;
+ Fields fields;
+ QueryResult queryresult;
+
+ // Test simple
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result != " <lineedit name=\"mywidget\" value=\"myvalue\"/>\n") return 1;
+
+ // Make a database commit and test if the value shows up
+ fields["mywidget"] = "testval";
+ db.commitTransaction("testuser", PATIENTID, macro, fields, now);
+
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result != " <lineedit name=\"mywidget\" value=\"testval\" prefilled=\"pracro\"/>\n") return 1;
+
+ // Make a query result (newer than the db value) and see if it shows up
+ queryresult.timestamp = now + 1;
+ queryresult.source = "testsource";
+ queryresult.values["mapvalue"] = "mymapvalue";
+ mapper.addQueryResult(queryresult);
+
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result != " <lineedit name=\"mywidget\" value=\"mymapvalue\" prefilled=\"testsource\"/>\n") return 1;
+
+ // Make another db value (newer than the query result) and see if it shows up.
+ fields["mywidget"] = "testval2";
+ db.commitTransaction("testuser", PATIENTID, macro, fields, now + 2);
+
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result != " <lineedit name=\"mywidget\" value=\"testval2\" prefilled=\"pracro\"/>\n") return 1;
+ }
+
+ printf("Negative tests:\n");
+ {
+ Macro macro;
+ macro.widgets.attributes["tagname"] = "lineedit";
+ macro.widgets.attributes["name"] = "mywidget";
+ macro.widgets.attributes["value"] = "myvalue";
+ macro.widgets.attributes["map"] = "mapvalue";
+
+ Database db("testdb", "", "", "", "", "");
+ LUAQueryMapper mapper;
+ std::string result;
+ Fields fields;
+ QueryResult queryresult;
+
+ // Test simple
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result != " <lineedit name=\"mywidget\" value=\"myvalue\"/>\n") return 1;
+
+ // Make a database commit too old, and test if the value shows up
+ fields["mywidget"] = "testval";
+ db.commitTransaction("testuser", PATIENTID, macro, fields, now - Conf::db_max_ttl - 1);
+
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result == " <lineedit name=\"mywidget\" value=\"testval\" prefilled=\"pracro\"/>\n") return 1;
+
+ // Make a too old query result (newer than the db value) and see if it shows up
+ queryresult.timestamp = now - Conf::pentominos_max_ttl - 1;
+ queryresult.source = "testsource";
+ queryresult.values["mapvalue"] = "mymapvalue";
+ mapper.addQueryResult(queryresult);
+
+ result = widgetgenerator(PATIENTID, macro, mapper, db);
+ printf("[%s]\n", result.c_str());
+ if(result == " <lineedit name=\"mywidget\" value=\"mymapvalue\" prefilled=\"testsource\"/>\n") return 1;
+ }
+ return 0;
+}
+
+#endif/*TEST_WIDGETGENERATOR*/