diff options
Diffstat (limited to 'server/src/pracrodaotest.cc')
-rw-r--r-- | server/src/pracrodaotest.cc | 176 |
1 files changed, 117 insertions, 59 deletions
diff --git a/server/src/pracrodaotest.cc b/server/src/pracrodaotest.cc index ec9c293..51ee24f 100644 --- a/server/src/pracrodaotest.cc +++ b/server/src/pracrodaotest.cc @@ -190,86 +190,145 @@ unsigned PracroDAOTest::nrOfCommits(std::string sessionid, void PracroDAOTest::addFieldname(std::string name, std::string description) { - // TODO - - /* - std::stringstream timestamp; timestamp << time(NULL); - std::string ts; - try { - pqxx::work W(*conn); - ts = "INSERT INTO fieldnames (name, description, \"timestamp\") VALUES (" - " '" + W.esc(name) + "', " - " '" + W.esc(description) + "', " - " '" + W.esc(timestamp.str()) + "' " - ")" - ; - DEBUG(sql, "Query: %s\n", ts.c_str()); - pqxx::result R = W.exec(ts); - W.commit(); - } catch (std::exception &e) { - ERR_LOG(db, "Query failed: %s: %s\n", e.what(), ts.c_str()); - } - */ + dbrow_t fieldname; + fieldname["name"] = name; + fieldname["description"] = description; + char buf[256]; + sprintf(buf, "%lu", time(NULL)); + fieldname["timestamp"] = buf; + data.fieldnames.push_back(fieldname); } void PracroDAOTest::delFieldname(std::string name) { - // TODO - - /* - std::string ts; - try { - pqxx::work W(*conn); - ts = "DELETE FROM fieldnames WHERE name=" - "'" + W.esc(name) + "' "; - DEBUG(sql, "Query: %s\n", ts.c_str()); - pqxx::result R = W.exec(ts); - W.commit(); - } catch (std::exception &e) { - ERR_LOG(db, "Query failed: %s: %s\n", e.what(), ts.c_str()); + dbtable_t::iterator i = data.fieldnames.begin(); + while(i != data.fieldnames.end()) { + dbrow_t &row = *i; + if(row["name"] == name) { + data.fieldnames.erase(i); + return; + } + i++; } - */ } std::vector<Fieldname> PracroDAOTest::getFieldnames() { - // TODO - std::vector<Fieldname> fieldnames; - /* - std::string query; - try { - pqxx::work W(*conn); - query = "SELECT * FROM fieldnames"; - DEBUG(sql, "Query: %s\n", query.c_str()); - pqxx::result R = W.exec(query); - pqxx::result::const_iterator ri = R.begin(); - while(ri != R.end()) { - Fieldname f; - f.name = (*ri)[0].c_str(); - f.description = (*ri)[1].c_str(); - f.timestamp = atol((*ri)[2].c_str()); - fieldnames.push_back(f); - ri++; - } - } catch (std::exception &e) { - ERR_LOG(db, "Query failed: %s: %s\n", e.what(), query.c_str()); + + dbtable_t::iterator i = data.fieldnames.begin(); + while(i != data.fieldnames.end()) { + dbrow_t &row = *i; + Fieldname fn; + fn.name = row["name"]; + fn.description = row["description"]; + fn.timestamp = atoll(row["timestamp"].c_str()); + fieldnames.push_back(fn); + i++; } - */ + return fieldnames; } +bool PracroDAOTest::active(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"; + } + i++; + } + return false; +} +void PracroDAOTest::setActive(std::string sessionid, bool active) +{ + 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"; + } + i++; + } +} #ifdef TEST_PRACRODAOTEST +//deps: debug.cc log.cc configuration.cc exception.cc pracrodao.cc +//cflags: -I.. +//libs: +#include <test.h> #include <time.h> #define PATIENTID "1234567890" +#define MACRO "testmacro" -int main() +static bool vectorFind(std::vector<Fieldname> fs, + std::string name, std::string desc) { + std::vector<Fieldname>::iterator i = fs.begin(); + while(i != fs.end()) { + Fieldname &fn = *i; + if(fn.name == name && + (desc == "" || fn.description == desc)) return true; + i++; + } + return false; +} + +TEST_BEGIN; + +Data data; +PracroDAOTest db(data); + +db.addFieldname("field1", "desc1"); +db.addFieldname("field2", "desc2"); +db.addFieldname("field3", "desc3"); +db.delFieldname("field3"); + +std::vector<Fieldname> fs = db.getFieldnames(); +TEST_EQUAL_INT(fs.size(), 2, "Test fieldname size."); +TEST_TRUE(vectorFind(fs, "field1", "desc1"), "Test fieldname 'field1'."); +TEST_TRUE(vectorFind(fs, "field2", "desc2"), "Test fieldname 'field2'."); +TEST_FALSE(vectorFind(fs, "field3", ""), "Test fieldname 'field3'."); + +std::string sid1 = db.newSessionId(); +std::string sid2 = db.newSessionId(); + +TEST_NOTEQUAL_STR(sid1, sid2, "Do not produce the same uid each time."); + +Transaction transaction; +transaction.cpr = PATIENTID; +transaction.user = "me"; + +Commit commit; +commit.fields["field1"] = "hello"; +commit.fields["field2"] = "world"; +commit.templ = "tester"; + +Macro macro; +macro.attributes["version"] = "1.0"; +macro.attributes["name"] = MACRO; + +time_t now = time(NULL); + +db.commitTransaction(sid1, transaction, commit, macro, now); + +TEST_EQUAL_INT(db.nrOfCommits(sid1, PATIENTID, MACRO, now), 1, "How many?"); + +Fieldnames fieldnames; +fieldnames.push_back("field1"); +fieldnames.push_back("field_nop"); +Values vals = db.getLatestValues(sid1, PATIENTID, ¯o, fieldnames, 0); +TEST_EQUAL_INT(vals.size(), 1, "One value"); + +TEST_NOTEQUAL(vals.find("field1"), vals.end(), "find value"); + +/* time_t now = time(NULL); Data data; @@ -345,8 +404,7 @@ int main() // This value was not committed, since it wasn't in the fieldnames table. if(values.find("field4") != values.end()) return 1; - - return 0; -} +*/ +TEST_END; #endif/*TEST_PRACRODAOTEST*/ |