class MailListFactoryTest(unittest.TestCase): def setUp(self): self.factory = MailListFactory() def test_next_id(self): self.assertEqual(1, self.factory.get_next_id()) def test_create(self): m = self.factory.create('asd') self.assertEqual('asd', m.get_name())
class MailListFactoryTest(unittest.TestCase): """docstring for MailListFactoryTest""" def setUp(self): self.factory = MailListFactory() def test_get_next_id(self): self.assertEqual(1, self.factory.next_id()) def test_create_new_mail_list(self): m = self.factory.create("Hack Bulgaria") self.assertEqual("Hack Bulgaria", m.get_name()) self.assertEqual(1, m.get_id())
class MailListFactoryTest(unittest.TestCase): """docstring for MailListFactoryTest""" def setUp(self): self.factory = MailListFactory() def test_get_next_id(self): self.assertEqual(1, self.factory.next_id()) def test_create_new_mail_list(self): m = self.factory.create("Hack Bulgaria") self.assertEqual("Hack Bulgaria", m.get_name())
class MailListProgram(): """docstring for MailListProgram""" def __init__(self): self.factory = MailListFactory() self.cp = CommandParser() self.lists = {} self.db_path = "lists/" self._load_initial_state() self._init_callbacks() self._loop() def create_list_callback(self, arguments): name = " ".join(arguments) maillist = self.factory.create(name) maillist_adapter = MailListFileAdapter(self.db_path, maillist) maillist_adapter.save() self.lists[maillist.get_id()] = (maillist, maillist_adapter) def add_subscriber_callback(self, arguments): list_id = int("".join(arguments)) name = input("name>") email = input("email>") self.lists[list_id][0].add_subscriber(name, email) self._notify_save(list_id) def show_lists_callback(self, arguments): for list_id in self.lists: current_list = self.lists[list_id][0] print("[{}] {}".format(list_id, current_list.get_name())) def show_list_callback(self, arguments): list_id = int("".join(arguments)) if list_id in self.lists: subscribers = self.lists[list_id][0].get_subscribers() for s in subscribers: print("{} - {}".format(s[0], s[1])) else: print("List with id <{}> was not found".format(list_id)) def exit_callback(self, arguments): sys.exit(0) def _load_initial_state(self): dir_lists = map(basename, glob(self.db_path + "*")) for list_file in dir_lists: adapter = MailListFileAdapter(self.db_path) parsed_list = adapter.load(list_file) maillist_adapter = MailListFileAdapter(self.db_path, parsed_list) self.lists[parsed_list.get_id()] = (parsed_list, maillist_adapter) def _init_callbacks(self): self.cp.on("create", self.create_list_callback) self.cp.on("add", self.add_subscriber_callback) self.cp.on("show_lists", self.show_lists_callback) self.cp.on("show_list", self.show_list_callback) self.cp.on("exit", self.exit_callback) # TODO - implement the rest of the callbacks def _notify_save(self, list_id): self.lists[list_id][1].save() def _loop(self): while True: command = input(">") self.cp.take_command(command)
class MailListProgram(): """docstring for MailListProgram""" def __init__(self): self.factory = MailListFactory() self.cp = CommandParser() self.lists = [] self._load_initial_state() self._init_callbacks() self._loop() def create_list_callback(self, arguments): name = " ".join(arguments) maillist = self.factory.create(name) maillist_adapter = MailListFileAdapter(self.db_path, maillist) maillist_adapter.save() self.lists.append(maillist.get_name()) print(self.lists) def add_subscriber_callback(self, arguments): list_id = int("".join(arguments)) name = input("name>") email = input("email>") subscriber = Subscriber(name, email) subscriber.save() self.db_path = sqlite3.connect("maillist.db") self.cursor = self.db_path.cursor() self.cursor.execute("""CREATE TABLE IF NOT EXISTS relations (list_id, subscriber_id)""") query = ("""INSERT INTO relations(list_id, subscriber_id) VALUES(?, ?)""") data = [list_id, subscriber.id] print(data) self.cursor.execute(query, data) self.db_path.commit() self.db_path.close() def show_lists_callback(self, arguments): for list_id in self.lists: current_list = self.lists[list_id][0] print("[{}] {}".format(list_id, current_list.get_name())) def show_list_callback(self, arguments): list_id = int("".join(arguments)) if list_id in self.lists: subscribers = self.lists[list_id][0].get_subscribers() for s in subscribers: print("{} - {}".format(s[0], s[1])) else: print("List with id <{}> was not found".format(list_id)) def exit_callback(self, arguments): sys.exit(0) def _load_initial_state(self): if os.path.isfile("maillist.db"): self.db_path = sqlite3.connect("maillist.db") self.cursor = self.db_path.cursor() lists_data = self.cursor.execute("""SELECT name FROM Lists""") for list in lists_data: self.lists.append(list[0]) def _init_callbacks(self): self.cp.on("create", self.create_list_callback) self.cp.on("add", self.add_subscriber_callback) self.cp.on("show_lists", self.show_lists_callback) self.cp.on("show_list", self.show_list_callback) self.cp.on("exit", self.exit_callback) # TODO - implement the rest of the callbacks def _notify_save(self, list_id): self.lists[list_id - 1].save() def _loop(self): while True: command = input(">") self.cp.take_command(command)
class MailListFileAdapterTest(unittest.TestCase): """docstring for MailListFileAdapterTest""" def setUp(self): self.factory = MailListFactory() self.m = self.factory.create("Hack Bulgaria") self.m.add_subscriber("Rado", "rado@rado") self.m.add_subscriber("Ivan", "ivan@ivan") self.db_path = "list_tests/" self.maillist_adapter = MailListFileAdapter(self.db_path, self.m) def test_get_file_name(self): self.assertEqual("Hack_Bulgaria", self.maillist_adapter.get_file_name()) def test_get_file_path(self): self.assertEqual(self.db_path + "Hack_Bulgaria", self.maillist_adapter.get_file_path()) def test_prepare_for_save(self): expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"]) self.assertEqual(expected, self.maillist_adapter.prepare_for_save()) def test_save_id_on_first_line(self): file_name = self.maillist_adapter.get_file_name() self.maillist_adapter.save() file = open(self.db_path + file_name, "r") contents = file.read() file.close() lines = contents.split("\n") self.assertEqual("1", lines[0]) def test_save_contents_format(self): file_name = self.maillist_adapter.get_file_name() self.maillist_adapter.save() file = open(self.db_path + file_name, "r") contents = file.read() lines = contents.split("\n") file.close() expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"]) expected = "\n".join(expected) lines.pop(0) # remove the id self.assertEqual(expected, "\n".join(lines)) os.remove(self.maillist_adapter.get_file_path()) def test_load_from_file(self): m = self.factory.create("Hack Bulgaria") m.add_subscriber("Ivo", "*****@*****.**") m.add_subscriber("Maria", "*****@*****.**") file_adapter = MailListFileAdapter(self.db_path, m) file_name = file_adapter.get_file_name() file_adapter.save() loaded_mail_list = file_adapter.load(file_name) self.assertEqual(m.get_id(), loaded_mail_list.get_id()) self.assertEqual(m.get_name(), loaded_mail_list.get_name()) self.assertEqual(m.get_subscribers(), loaded_mail_list.get_subscribers()) def test_load_from_file_without_giving_maillist(self): m = self.factory.create("Hack Bulgaria") m.add_subscriber("Ivo", "*****@*****.**") m.add_subscriber("Maria", "*****@*****.**") file_adapter = MailListFileAdapter(self.db_path, m) file_adapter.save() load_adapter = MailListFileAdapter(self.db_path) loaded_mail_list = load_adapter.load(file_adapter.get_file_name()) self.assertEqual(m.get_id(), loaded_mail_list.get_id()) self.assertEqual(m.get_name(), loaded_mail_list.get_name()) self.assertEqual(m.get_subscribers(), loaded_mail_list.get_subscribers()) def test_load_from_empty_list(self): #pass m = self.factory.create("Hack Bulgaria") file_adapter = MailListFileAdapter(self.db_path, m) file_adapter.save() load_adapter = MailListFileAdapter(self.db_path) self.assertEqual(1, 1) loaded_mail_list = load_adapter.load(file_adapter.get_file_name()) self.assertEqual(0, loaded_mail_list.count()) self.assertEqual([], loaded_mail_list.get_subscribers()) def tearDown(self): call("rm -rf {}".format(self.db_path), shell=True)
class MailTest(unittest.TestCase): """docstring for MailTest""" def setUp(self): self.factory = MailListFactory() self.lists = [self.factory.create('list1'), self.factory.create('list2')] def test_merge_empty_lists(self): mail.merge(self.lists, self.factory, 1, 2, 'list3') expected = '[1] - list1\n[2] - list2\n[3] - list3\n' self.assertEqual(expected, mail.show_lists(self.lists)) def test_merge_non_empty_lists(self): mail.add_new_user(self.lists, 1, 'asd', 'asd') mail.add_new_user(self.lists, 2, 'dsa', 'dsa') mail.merge(self.lists, self.factory, 1, 2, 'list3') expected = '[1] asd - asd\n[2] dsa - dsa' self.assertEqual(expected, mail.show_list(self.lists, 3)) def test_show_lists(self): expected = '[1] - list1\n[2] - list2\n' self.assertEqual(expected, mail.show_lists(self.lists)) def test_show_list(self): self.lists[0].add_user('ivan', 'dragan@petkan') self.assertEqual('[1] ivan - dragan@petkan', mail.show_list(self.lists, 1)) def test_show_list_with_bad_id(self): self.assertFalse(mail.show_list(self.lists, 3)) def test_create_list(self): lists = [] m = MailListFactory() mail.create_list(lists, m, 'list1') self.assertEqual('[1] - list1\n', mail.show_lists(lists)) def test_add_new_user(self): result = mail.add_new_user(self.lists, 1, 'ivan', 'dragan@petkan') self.assertTrue(result) print(mail.show_list(self.lists, 1)) self.assertEqual('[1] ivan - dragan@petkan', mail.show_list(self.lists, 1)) def test_add_new_user_in_non_existent_list(self): self.assertFalse(mail.add_new_user(self.lists, 4, 'a', 'b')) def test_search_email(self): mail.add_new_user(self.lists, 1, 'ivan', 'dragan@petkan') mail.add_new_user(self.lists, 2, 'ivan', 'dragan@petkan') expected = '<dragan@petkan> was found in:\n'\ '[1] - list1\n[2] - list2' self.assertEqual(expected, mail.search_email(self.lists, 'dragan@petkan')) def test_export_list_to_json(self): mail.export(self.lists, 1) expected = ['{"_MailList__id": 1, "_MailList__name": "list1", "users": []}', '{"_MailList__id": 1, "users": [], "_MailList__name": "list1"}', '{"_MailList__name": "list1", "_MailList__id": 1, "users": []}', '{"_MailList__name": "list1", "users": [], "_MailList__id": 1}', '{"users": [], "_MailList__name": "list1", "_MailList__id": 1}', '{"users": [], "_MailList__id": 1, "_MailList__name": "list1"}'] exp_file = open('list1.json', 'r') actual = exp_file.read() exp_file.close() self.assertIn(actual, expected) call('rm list1.json', shell=True) def test_import_list_from_json(self): call('echo \'{\"name\": \"list3\", \"users\":'\ '[{\"name\": \"asd\", \"email\": \"asd\"}]}\' > list3.json', shell=True) mail.import_json(self.lists, self.factory, 'list3.json') self.assertEqual('list3', self.lists[2].get_name()) call('rm list3.json', shell=True) def test_remove_subscriber(self): self.lists[0].add_user('ivan', 'dragan@petkan') self.lists[0].add_user('georgi', '*****@*****.**') mail.remove_subscriber(self.lists, 1, 2) self.assertEqual('[1] ivan - dragan@petkan', mail.show_list(self.lists, 1))
class MailListFileAdapterTest(unittest.TestCase): """docstring for MailListFileAdapterTest""" def setUp(self): self.factory = MailListFactory() self.m = self.factory.create("Hack Bulgaria") self.m.add_subscriber("Rado", "rado@rado") self.m.add_subscriber("Ivan", "ivan@ivan") self.db_path = "list_tests/" self.maillist_adapter = MailListFileAdapter(self.db_path, self.m) def test_get_file_name(self): self.assertEqual("Hack_Bulgaria", self.maillist_adapter.get_file_name()) def test_get_file_path(self): self.assertEqual(self.db_path + "Hack_Bulgaria", self.maillist_adapter.get_file_path()) def test_prepare_for_save(self): expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"]) self.assertEqual(expected, self.maillist_adapter.prepare_for_save()) def test_save_id_on_first_line(self): file_name = self.maillist_adapter.get_file_name() self.maillist_adapter.save() file = open(self.db_path + file_name, "r") contents = file.read() file.close() lines = contents.split("\n") self.assertEqual("1", lines[0]) def test_save_contents_format(self): file_name = self.maillist_adapter.get_file_name() self.maillist_adapter.save() file = open(self.db_path + file_name, "r") contents = file.read() lines = contents.split("\n") file.close() expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"]) expected = "\n".join(expected) lines.pop(0) # remove the id self.assertEqual(expected, "\n".join(lines)) os.remove(self.maillist_adapter.get_file_path()) def test_load_from_file(self): m = self.factory.create("Hack Bulgaria") m.add_subscriber("Ivo", "*****@*****.**") m.add_subscriber("Maria", "*****@*****.**") file_adapter = MailListFileAdapter(self.db_path, m) file_name = file_adapter.get_file_name() file_adapter.save() loaded_mail_list = file_adapter.load(file_name) self.assertEqual(m.get_id(), loaded_mail_list.get_id()) self.assertEqual(m.get_name(), loaded_mail_list.get_name()) self.assertEqual(m.get_subscribers(), loaded_mail_list.get_subscribers()) def test_load_from_file_without_giving_maillist(self): m = self.factory.create("Hack Bulgaria") m.add_subscriber("Ivo", "*****@*****.**") m.add_subscriber("Maria", "*****@*****.**") file_adapter = MailListFileAdapter(self.db_path, m) file_adapter.save() load_adapter = MailListFileAdapter(self.db_path) loaded_mail_list = load_adapter.load(file_adapter.get_file_name()) self.assertEqual(m.get_id(), loaded_mail_list.get_id()) self.assertEqual(m.get_name(), loaded_mail_list.get_name()) self.assertEqual(m.get_subscribers(), loaded_mail_list.get_subscribers()) def test_load_from_empty_list(self): m = self.factory.create("Hack Bulgaria") file_adapter = MailListFileAdapter(self.db_path, m) file_adapter.save() load_adapter = MailListFileAdapter(self.db_path) loaded_mail_list = load_adapter.load(file_adapter.get_file_name()) self.assertEqual(0, loaded_mail_list.count()) self.assertEqual([], loaded_mail_list.get_subscribers()) def tearDown(self): call("rm -rf {}".format(self.db_path), shell=True)