summaryrefslogtreecommitdiff
path: root/server/src/pracrodaotest.cc
diff options
context:
space:
mode:
authordeva <deva>2011-02-17 12:35:46 +0000
committerdeva <deva>2011-02-17 12:35:46 +0000
commited24baca8576c0516db07dbe904f9567a65c37c3 (patch)
tree569e8c7944c5f9d7f513670af212dc75c110e771 /server/src/pracrodaotest.cc
parentdb144840aba95affb3a6b5b2536059f49f0677ff (diff)
'Renamed' active/setActive methods to idle/setIdle, since these names makes more sence.
Diffstat (limited to 'server/src/pracrodaotest.cc')
-rw-r--r--server/src/pracrodaotest.cc158
1 files changed, 144 insertions, 14 deletions
diff --git a/server/src/pracrodaotest.cc b/server/src/pracrodaotest.cc
index 51ee24f..efd94f6 100644
--- a/server/src/pracrodaotest.cc
+++ b/server/src/pracrodaotest.cc
@@ -68,6 +68,58 @@ void PracroDAOTest::commitTransaction(std::string sessionid,
std::string macro = _macro.attributes["name"];
std::stringstream timestamp; timestamp << now;
+ dbtable_t::iterator ci = data.commits.begin();
+ while(ci != data.commits.end()) {
+ dbrow_t &c = *ci;
+ if(c["uid"] == sessionid) {
+ break;
+ }
+ ci++;
+ }
+
+ if(ci == data.commits.end()) {
+ DEBUG(testdb, "Create new commit: %s", sessionid.c_str());
+ dbrow_t c;
+ c["patientid"] = transaction.cpr;
+ c["template"] = commit.templ;
+ c["version"] = "1.0";
+ c["timestamp"] = timestamp.str();
+ c["uid"] = sessionid;
+ c["status"] = "active";
+ data.commits.push_back(c);
+ } else {
+ dbrow_t &c = *ci;
+ if(c["status"] == "committed") {
+ ERR_LOG(db, "Attempt to add to committed session %s blocked!\n",
+ sessionid.c_str());
+ return;
+ }
+
+ DEBUG(testdb, "Working on old commit: %s", sessionid.c_str());
+ c["status"] = "active";
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
dbrow_t t;
t["uid"] = data.trseq.nextval();
t["patientid"] = transaction.cpr;
@@ -123,9 +175,6 @@ Values PracroDAOTest::getLatestValues(std::string sessionid,
oldest);
Values values;
- // TODO: Take Macro* into account. If supplied (not NULL) the macro name, and
- // optionally version number should match the transaction.
-
Fieldnames::iterator fi = fieldnames.begin();
while(fi != fieldnames.end()) {
std::string fieldname = *fi;
@@ -133,19 +182,21 @@ Values PracroDAOTest::getLatestValues(std::string sessionid,
// Find matching transactions
dbtable_t::iterator ti = data.transactions.begin();
while(ti != data.transactions.end()) {
- std::map<std::string, std::string> &transaction = *ti;
+ dbrow_t &transaction = *ti;
time_t timestamp = atol(transaction["timestamp"].c_str());
- if(transaction["patientid"] == patientid && timestamp >= oldest && (transaction["macro"] == macro_name || macro == NULL)) {
+ if(transaction["patientid"] == patientid && timestamp >= oldest &&
+ (transaction["macro"] == macro_name || macro == NULL)) {
std::string tid = transaction["uid"];
// Find transaction values
dbtable_t::iterator vi = data.fields.begin();
while(vi != data.fields.end()) {
- std::map<std::string, std::string> &field = *vi;
+ dbrow_t &field = *vi;
// Upon match, insert it into values
if(field["transaction"] == tid && field["name"] == fieldname) {
- if(values.find(fieldname) == values.end() || values[fieldname].timestamp < timestamp) {
+ if(values.find(fieldname) == values.end() ||
+ values[fieldname].timestamp < timestamp) {
values[fieldname].timestamp = timestamp;
values[fieldname].value = field["value"];
values[fieldname].source = "testdb";
@@ -175,7 +226,7 @@ unsigned PracroDAOTest::nrOfCommits(std::string sessionid,
// Find and count matching transactions
dbtable_t::iterator ti = data.transactions.begin();
while(ti != data.transactions.end()) {
- std::map<std::string, std::string> &transaction = *ti;
+ dbrow_t &transaction = *ti;
time_t timestamp = atol(transaction["timestamp"].c_str());
if(transaction["patientid"] == patientid &&
transaction["macro"] == macroname &&
@@ -230,13 +281,13 @@ std::vector<Fieldname> PracroDAOTest::getFieldnames()
return fieldnames;
}
-bool PracroDAOTest::active(std::string sessionid)
+bool PracroDAOTest::idle(std::string sessionid)
{
dbtable_t::iterator i = data.commits.begin();
while(i != data.commits.end()) {
dbrow_t &commit = *i;
- if(commit["sessionid"] == sessionid) {
- return commit["status"] != "idle";
+ if(commit["uid"] == sessionid) {
+ return commit["status"] == "idle";
}
i++;
}
@@ -244,13 +295,52 @@ bool PracroDAOTest::active(std::string sessionid)
return false;
}
-void PracroDAOTest::setActive(std::string sessionid, bool active)
+void PracroDAOTest::setIdle(std::string sessionid, bool idle)
{
dbtable_t::iterator i = data.commits.begin();
while(i != data.commits.end()) {
dbrow_t &commit = *i;
- if(commit["sessionid"] == sessionid && commit["status"] != "committed") {
- commit["status"] = active?"active":"idle";
+ if(commit["uid"] == sessionid && commit["status"] != "committed") {
+ commit["status"] = idle?"idle":"active";
+ }
+ i++;
+ }
+}
+
+void PracroDAOTest::commit(std::string sessionid)
+{
+ dbtable_t::iterator i = data.commits.begin();
+ while(i != data.commits.end()) {
+ dbrow_t &commit = *i;
+ if(commit["uid"] == sessionid && commit["status"] != "committed") {
+ commit["status"] = "committed";
+ return;
+ }
+ i++;
+ }
+}
+
+void PracroDAOTest::nocommit(std::string sessionid)
+{
+ dbtable_t::iterator i = data.commits.begin();
+ while(i != data.commits.end()) {
+ dbrow_t &commit = *i;
+ if(commit["uid"] == sessionid && commit["status"] != "committed") {
+ commit["status"] = "idle";
+ return;
+ }
+ i++;
+ }
+}
+
+void PracroDAOTest::discard(std::string sessionid)
+{
+ dbtable_t::iterator i = data.commits.begin();
+ while(i != data.commits.end()) {
+ dbrow_t &commit = *i;
+ if(commit["uid"] == sessionid && commit["status"] != "committed") {
+ data.commits.erase(i);
+ return;
}
i++;
}
@@ -282,6 +372,8 @@ static bool vectorFind(std::vector<Fieldname> fs,
TEST_BEGIN;
+debug_parse("+all");
+
Data data;
PracroDAOTest db(data);
@@ -328,6 +420,44 @@ TEST_EQUAL_INT(vals.size(), 1, "One value");
TEST_NOTEQUAL(vals.find("field1"), vals.end(), "find value");
+{
+ std::string sid = db.newSessionId();
+ db.commitTransaction(sid, transaction, commit, macro, now);
+ TEST_FALSE(db.idle(sid), "Session should not be idle.");
+
+ db.setIdle(sid, true);
+ TEST_TRUE(db.idle(sid), "Session should be idle.");
+
+ db.setIdle(sid, false);
+ TEST_FALSE(db.idle(sid), "Session1 should not be idle.");
+}
+
+{
+ std::string sid = db.newSessionId();
+ db.commitTransaction(sid, transaction, commit, macro, now);
+ TEST_FALSE(db.idle(sid), "Session should not be idle.");
+ db.commit(sid);
+ TEST_FALSE(db.idle(sid), "Session is not idle (since committed != idle).");
+}
+
+{
+ std::string sid = db.newSessionId();
+ db.commitTransaction(sid, transaction, commit, macro, now);
+ TEST_FALSE(db.idle(sid), "Session should not be idle.");
+ db.nocommit(sid);
+ TEST_TRUE(db.idle(sid), "Session is idle.");
+}
+
+{
+ std::string sid = db.newSessionId();
+ db.commitTransaction(sid, transaction, commit, macro, now);
+ TEST_FALSE(db.idle(sid), "Session should not be idle.");
+ db.discard(sid);
+ TEST_FALSE(db.idle(sid), "Session not idle (it doesn't exist).");
+}
+
+TEST_FALSE(db.idle("no such session"), "Missing session is not idle.");
+
/*
time_t now = time(NULL);