From 88e6ece973804ac0ed2aae860d9aff30491efe28 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 7 Aug 2009 14:45:39 +0000 Subject: Test script. --- server/src/queryhandlerpracro.cc | 81 +++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 18 deletions(-) (limited to 'server/src') diff --git a/server/src/queryhandlerpracro.cc b/server/src/queryhandlerpracro.cc index e26de0c..ae2f0f6 100644 --- a/server/src/queryhandlerpracro.cc +++ b/server/src/queryhandlerpracro.cc @@ -30,6 +30,8 @@ #include +#include "configuration.h" + QueryHandlerPracro::QueryHandlerPracro(Database &_db, std::string cpr) : db(_db) { @@ -39,43 +41,86 @@ QueryHandlerPracro::QueryHandlerPracro(Database &_db, std::string cpr) QueryResult QueryHandlerPracro::exec(Query &query) { QueryResult result; + result.timestamp = 0; + result.source = ""; std::string field = query.attributes["class"]; Fieldnames fields; fields.push_back(field); - std::string ttl = query.attributes["ttl"]; - time_t oldest = time(NULL) - atol(ttl.c_str()); + time_t oldest; + if(query.attributes.find("ttl") != query.attributes.end()) { + std::string ttl = query.attributes["ttl"]; + oldest = time(NULL) - atol(ttl.c_str()); + } else { + oldest = time(NULL) - Conf::db_max_ttl; + } Values values = db.getValues(cpr, fields, oldest); - std::string value = values[field].value; - time_t timestamp = values[field].timestamp; + if(values.find(field) != values.end()) { + std::string value = values[field].value; + time_t timestamp = values[field].timestamp; - result.timestamp = timestamp; - result.values[field] = value; - result.source = "pracrodb"; + result.timestamp = timestamp; + result.values[field] = value; + result.source = "pracrodb"; - PRACRO_DEBUG(queryhandler,"%s => %s (%lu)\n", field.c_str(), value.c_str(), timestamp); + PRACRO_DEBUG(queryhandler,"%s => %s (%lu)\n", field.c_str(), value.c_str(), timestamp); + } return result; } #ifdef TEST_QUERYHANDLERPRACRO -#include "configuration.h" +#include + +#define PATIENTID "1234567890" int main() { - Database db("testdb", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); - - QueryHandlerPracro qh(db, "2003791613"); - - Query q1; - q1.attributes["device_id"] = "lensmeter"; - q1.attributes["device_type"] = "lensmeter"; - QueryResult res = qh.exec(q1); - res.print(); + time_t now = time(NULL); + Database db("testdb", "", "", "", "", ""); + Macro macro; + Fields fields; + QueryHandlerPracro qh(db, PATIENTID); + + Query query; + QueryResult res; + + fields["myfield"] = "myval"; + db.commitTransaction("testuser", PATIENTID, macro, fields, now - Conf::db_max_ttl - 1); + + query.attributes["class"] = "myfield"; + res = qh.exec(query); + if(res.timestamp != 0) return 1; + if(res.source != "") return 1; + if(res.values.size() != 0) return 1; + + fields["myfield"] = "myval"; + db.commitTransaction("testuser", PATIENTID, macro, fields, now - 100); + + query.attributes["class"] = "myfield"; + query.attributes["ttl"] = "99"; + res = qh.exec(query); + if(res.timestamp != 0) return 1; + if(res.source != "") return 1; + if(res.values.size() != 0) return 1; + + query.attributes["class"] = "nosuchfield"; + query.attributes["ttl"] = "10000"; + res = qh.exec(query); + if(res.timestamp != 0) return 1; + if(res.source != "") return 1; + if(res.values.size() != 0) return 1; + + query.attributes["class"] = "myfield"; + query.attributes["ttl"] = "100"; + res = qh.exec(query); + if(res.timestamp != now - 100) return 1; + if(res.source != "pracrodb") return 1; + if(res.values["myfield"] != "myval") return 1; return 0; } -- cgit v1.2.3