Browse Source

test_listmanager_dummy - Add Error conditions / add list_exists(), member_exists()

LIB-12
heck 4 years ago
parent
commit
943f07c469
  1. 218
      test/test_listmanager_dummy.cc

218
test/test_listmanager_dummy.cc

@ -4,6 +4,18 @@
#include <exception> #include <exception>
#include <map> #include <map>
#ifndef ASSERT_EXCEPT
#define ASSERT_EXCEPT(func) \
do { \
try { \
(func); \
assert(false); \
} catch (const exception& e) { \
Test::Utils::print_exception(e); \
} \
} while (0)
#endif
using namespace std; using namespace std;
using namespace pEp; using namespace pEp;
using namespace Test::Log; using namespace Test::Log;
@ -22,23 +34,19 @@ struct model_test_lmd {
string joe; string joe;
string db_path; string db_path;
vector<lm_list> lists; vector<lm_list> lists;
}; vector<string> lists_addr() const
{
model_test_lmd model;
vector<string> list_of_lists(const vector<lm_list>& lists)
{
vector<string> ret; vector<string> ret;
for (const lm_list& l : lists) { for (const lm_list& l : this->lists) {
ret.push_back(l.addr); ret.push_back(l.addr);
} }
return ret; return ret;
} }
};
void apply_model(ListManagerDummy& lmd) void apply_model(ListManagerDummy& lmd, const model_test_lmd& model)
{ {
logH2("Adding model"); cout << "apply_model()" << endl;
for (const lm_list& l : model.lists) { for (const lm_list& l : model.lists) {
lmd.list_add(l.addr, l.mod); lmd.list_add(l.addr, l.mod);
for (const string& m : l.members) { for (const string& m : l.members) {
@ -47,40 +55,32 @@ void apply_model(ListManagerDummy& lmd)
} }
} }
void verify_model(ListManagerDummy& lmd) void verify_model(ListManagerDummy& lmd, const model_test_lmd& model)
{ {
logH2("Verifying model"); cout << "verify_model()" << endl;
cout << "Verifying lists" << endl; assert((model.lists_addr()) == lmd.lists());
assert(list_of_lists(model.lists) == lmd.lists());
cout << "Verifying members" << endl;
for (const lm_list& l : model.lists) { for (const lm_list& l : model.lists) {
assert(l.members == lmd.members(l.addr)); assert(l.members == lmd.members(l.addr));
} }
cout << "Verifying moderators" << endl;
for (const lm_list& l : model.lists) { for (const lm_list& l : model.lists) {
assert(l.mod == lmd.moderator(l.addr)); assert(l.mod == lmd.moderator(l.addr));
} }
} }
void recreate_apply_and_verify_model(ListManagerDummy& lmd) void recreate_apply_and_verify_model(ListManagerDummy& lmd, const model_test_lmd& model)
{ {
logH2("test delete_db()");
try { try {
lmd.delete_db(); lmd.delete_db();
} catch (const exception& e) { } catch (const exception& e) {
print_exception(e);
} }
assert(!file_exists(model.db_path)); assert(!file_exists(model.db_path));
apply_model(lmd); apply_model(lmd, model);
verify_model(lmd); verify_model(lmd, model);
} }
int main(int argc, char* argv[]) model_test_lmd create_default_model()
{ {
try { model_test_lmd model;
ListManagerDummy::log_enabled = true;
{
model.db_path = "test_lmd.db"; model.db_path = "test_lmd.db";
model.alice = "alice@peptest.org"; model.alice = "alice@peptest.org";
model.bob = "bob@peptest.org"; model.bob = "bob@peptest.org";
@ -109,52 +109,168 @@ int main(int argc, char* argv[])
model.lists.push_back(l1); model.lists.push_back(l1);
model.lists.push_back(l2); model.lists.push_back(l2);
model.lists.push_back(l3); model.lists.push_back(l3);
} return model;
}
model_test_lmd create_model_bad_path()
{
model_test_lmd model = create_default_model();
model.db_path = "/wont_create_dirs/bad.db";
return model;
}
int main(int argc, char* argv[])
{
// pEpSQLite::log_enabled = true;
ListManagerDummy::log_enabled = false;
logH1("Testing SUCCESS conditions");
{ {
logH1("Test create new db"); logH2("Test create new db");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path); ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd); recreate_apply_and_verify_model(lmd, model);
} }
{ {
logH1("Test re-open db"); logH2("Test re-open db");
model_test_lmd model = create_default_model();
assert(file_exists(model.db_path)); assert(file_exists(model.db_path));
ListManagerDummy lmd(model.db_path); ListManagerDummy lmd(model.db_path);
verify_model(lmd); verify_model(lmd, model);
logH1("Test list_delete"); logH2("Test list_delete");
lmd.list_delete(model.lists.at(2).addr); lmd.list_delete(model.lists.at(2).addr);
model.lists.pop_back(); model.lists.pop_back();
verify_model(lmd); verify_model(lmd, model);
logH1("Test auto reopen after close()"); logH2("Test auto reopen after close()");
lmd.close_db(); lmd.close_db();
logH1("Test member_remove"); logH2("Test member_remove");
lmd.member_remove(model.lists.at(0).addr, model.lists.at(0).members.at(1)); lmd.member_remove(model.lists.at(0).addr, model.lists.at(0).members.at(1));
model.lists.at(0).members.pop_back(); model.lists.at(0).members.pop_back();
verify_model(lmd); verify_model(lmd, model);
logH2("Test list_exists() - true");
assert(lmd.list_exists(model.lists.at(0).addr));
logH2("Test list_exists() - false");
assert(!lmd.list_exists("does_not_exist_for_sure"));
logH2("Test member_exists() - true");
assert(lmd.member_exists(model.lists.at(0).addr, model.lists.at(0).members.at(0)));
logH2("Test member_exists() - false");
assert(!lmd.member_exists(model.lists.at(0).addr, "does_not_exist_for_sure"));
logH1("Test delete_db"); logH2("Test delete_db");
lmd.delete_db(); lmd.delete_db();
assert(!file_exists(model.db_path)); assert(!file_exists(model.db_path));
} }
logH1("Testing ERROR conditions");
{ {
logH1("Testing error conditions"); logH2("Testing success on close_db() on model_bad_path");
model_test_lmd model = create_model_bad_path();
ListManagerDummy lmd(model.db_path); ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd); lmd.close_db();
//TODO: Continue with error conditions
// * bad path
// * list_add - list already exists
// * list_delete - no such list
// * member_add - member already exists
// * member_remove - no such member
// * moderator - no such list
// * members - no such list
} }
{
} catch (const exception& e) { logH2("Testing exception on delete_db() on model_bad_path");
model_test_lmd model = create_model_bad_path();
ListManagerDummy lmd(model.db_path);
ASSERT_EXCEPT(lmd.delete_db());
}
{
logH2("Testing exception on lists() on: on model_bad_path");
model_test_lmd model = create_default_model();
model.db_path = "/wont_create_dirs/bad.db";
ListManagerDummy lmd(model.db_path);
ASSERT_EXCEPT(lmd.lists());
}
{
logH2("Testing list_add() AlreadyExistsException");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd, model);
try {
lmd.list_add(model.lists.at(0).addr, "any");
assert(false);
} catch (const AlreadyExistsException& e) {
print_exception(e);
} catch (...) {
cout << "NEVER SEEEE" << endl;
assert(false);
}
}
{
logH2("Testing list_delete() DoesNotExistException");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd, model);
try {
lmd.list_delete("does_not_exist_for_sure");
assert(false);
} catch (const DoesNotExistException& e) {
print_exception(e);
} catch (...) {
assert(false);
}
}
{
logH2("Testing member_add() AlreadyExistsException");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd, model);
try {
lmd.member_add(model.lists.at(0).addr, model.lists.at(0).members.at(0));
assert(false);
} catch (const AlreadyExistsException& e) {
print_exception(e);
} catch (...) {
assert(false);
}
}
{
logH2("Testing member_remove() DoesNotExistException");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd, model);
try {
lmd.member_remove(model.lists.at(0).addr, "does_not_exist_for_sure");
assert(false);
} catch (const DoesNotExistException& e) {
print_exception(e);
} catch (...) {
assert(false);
}
}
{
logH2("Testing moderator() DoesNotExistException");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd, model);
try {
lmd.moderator("does_not_exist_for_sure");
assert(false);
} catch (const DoesNotExistException& e) {
print_exception(e); print_exception(e);
} catch (...) {
assert(false);
}
}
{
logH2("Testing members() DoesNotExistException");
model_test_lmd model = create_default_model();
ListManagerDummy lmd(model.db_path);
recreate_apply_and_verify_model(lmd, model);
try {
lmd.members("does_not_exist_for_sure");
assert(false);
} catch (const DoesNotExistException& e) {
print_exception(e);
} catch (...) {
assert(false);
}
} }
} }
Loading…
Cancel
Save