def test_amity_list_rooms(self): amity = Amity('list_room') amity.rooms = [] self.assertEqual(amity.list_rooms({}), False) amity = self.create_room() self.assertEqual(amity.list_rooms({'-u': True, '-a': False}), None) self.assertEqual(amity.list_rooms({'-u': False, '-a': True}), None)
def test_amity_list_people(self): self.create_room() amity = Amity('list_people') amity.people = [] self.assertEqual(amity.list_people({}), False) self.add_person(False, False) amity = self.add_person(True, False) self.assertEqual(amity.list_people({'-u': True, '-a': False}), None) self.assertEqual(amity.list_people({'-u': False, '-a': True}), None)
def test_amity_load_person(self): self.create_room() amity = Amity('load_people') amity.people = [] args = { '<file_location>': 'data/people_test.txt', } amity.run_command(args) self.assertEqual(len(amity.people), 2)
def create_room(self): amity = Amity('create_room') args = { '<room_name>': ['testRoom 1', 'testRoom 2', 'testRoom 3', 'testRoom 1'], '<room_type>': ['living', 'office', 'living'], } amity.rooms = [] # make sure the rooms list is empty amity.run_command(args) return amity
def add_person(self, living_space=True, reset=True): amity = Amity('add_person') args = { '<firstname>': 'test', '<lastname>': 'user', '<person_type>': 'fellow', '-w': living_space, } if reset: amity.people = [] # make sure the people list is empty amity.run_command(args) return amity
def test_amity_allocate_person(self): self.create_room() self.add_person(False) amity = Amity('allocate_person') person = amity.people[-1] # Select the last added args = { '<person_id>': person.uid, '<new_room_name>': 'testRoom 1', '-w': True, } amity.run_command(args) self.assertEqual(amity.people[-1].assigned_room['LIVINGSPACE'], 'TESTROOM 1') self.assertEqual(amity.people[-1].is_allocated, True)
def test_amity_reallocate_person(self): self.create_room() self.add_person() amity = Amity('reallocate_person') person = amity.people[0] new_room = "TESTROOM 3" if person.assigned_room[ 'LIVINGSPACE'] == 'TESTROOM 1' else "TESTROOM 1" args = { '<person_id>': person.uid, '<new_room_name>': new_room, '-l': True, } amity.run_command(args) self.assertEqual(amity.people[0].assigned_room['LIVINGSPACE'], new_room)
def test_amity_remove_person(self): self.create_room() self.add_person() amity = Amity('remove_person') person = amity.people[-1] # Select the last added args = { '<person_id>': person.uid, '<current_room_name>': person.assigned_room['LIVINGSPACE'], } amity.run_command(args) room = [room for room in amity.rooms if room.name == 'TESTROOM 1'] if room: old_person = [ p for p in room[0].people if p.name() == person.name() ] self.assertEqual(old_person, [])
def test_amity_print_unallocated(self): amity = Amity('print_unallocated') self.assertEqual( amity.print_unallocated({ '-r': True, '-o': True, '<file_name>': 'tt.txt' }), None) self.assertEqual( amity.print_unallocated({ '-r': True, '-o': False, '<file_name>': 'tt.txt' }), None) self.assertEqual( amity.print_unallocated({ '-r': False, '-o': False, '<file_name>': 'tt.txt' }), None) self.assertEqual( amity.print_unallocated({ '-r': False, '-o': True, '<file_name>': 'tt.txt' }), None)
def test_amity_print_allocations(self): amity = Amity('print_allocations') amity.rooms = [] self.assertEqual(amity.print_allocations({}), False) self.create_room() amity = self.add_person() self.assertEqual(amity.print_allocations({'-o': False}), None) self.assertEqual( amity.print_allocations({ '-o': True, '<file_name>': 'tt.txt' }), None)
class AmityInteractive(cmd.Cmd): intro = 'Welcome to Amity Room allocation!' \ +'Type help for a list of commands' prompt = '|amity|> ' the_amity = Amity() @docopt_cmd def do_create_room(self, args): """ Usage: create_room <room_type> <room_name> ... """ self.the_amity.create_room(args) @docopt_cmd def do_add_person(self, args): """ Usage: \ add_person <first_name> <last_name> <person_type> \ [<wants_accommodation>] """ self.the_amity.add_person(args)
""" from docopt import docopt from src.amity import Amity #function mapping to avoid long if else chain func_map = [ 'create_room', 'add_person', 'reallocate_person', 'load_people', 'print_allocations', 'print_unallocated', 'print_room', 'save_state', 'load_state', 'list_people', 'list_rooms', 'allocate_person', 'remove_person', 'clear', ] if __name__ == '__main__': arguments = docopt(__doc__) for command in func_map: if arguments[command]: amity = Amity(command) amity.run_command(arguments) break
def setUp(self): self.amity = Amity() self.amity.del_me() self.previous_room_count = len(self.amity.all_rooms)
Options: -h, --help Show this screen and exit """ import cmd import shutil import click from docopt import docopt, DocoptExit from src.amity import Amity from ui_additions import playSpinner term_size = shutil.get_terminal_size((80, 20)) term_width = term_size[0] amity = Amity() def amity_docopt(func): """ This decorator is used to simplify the try/except block and pass the result of the docopt parsing to the called action. """ def fn(self, arg): try: opt = docopt(fn.__doc__, arg) except DocoptExit as e: # The DocoptExit is thrown when the args do not match. # We print a message to the user and the usage block.
def test_amity_allocate_returns_false_for_empty_room(self): amity = Amity('list_people') amity.rooms = [] person = Fellow('Sunday', 'nwuguru', True) self.assertEqual(amity.allocate(person), False)
class AmityTest(unittest.TestCase): def setUp(self): self.amity = Amity() self.amity.del_me() self.previous_room_count = len(self.amity.all_rooms) def test_create_office(self): self.assertFalse("Kingslanding" in self.amity.all_rooms.keys()) self.amity.create_room('Kingslanding', 'O') self.assertTrue("Kingslanding" in self.amity.all_rooms.keys()) new_room_count = len(self.amity.all_rooms) self.assertEqual(self.previous_room_count + 1, new_room_count) def test_create_office_error_if_exists(self): self.assertFalse("Kingslanding" in self.amity.all_rooms.keys()) self.amity.create_room('Kingslanding', 'O') self.assertTrue("Kingslanding" in self.amity.all_rooms.keys()) response = self.amity.create_room('Kingslanding', 'O') self.assertEqual(response, "Room could not be created: Room exists!") def test_creating_living_space(self): self.assertFalse("Winterfell" in self.amity.all_rooms.keys()) self.amity.create_room('Winterfell', 'L') self.assertTrue("Winterfell" in self.amity.all_rooms.keys()) # creating an already created room try_duplicate = self.amity.create_room('Winterfell', 'L') self.assertEqual(try_duplicate, "Room could not be created: Room exists!") new_room_count = len(self.amity.all_rooms) self.assertEqual(self.previous_room_count + 1, new_room_count) def test_creating_room_with_invalid_room_type(self): self.assertTrue("Mereen" not in self.amity.all_rooms.keys()) sample = self.amity.create_room("Mereen", "K") self.assertEqual(sample, -1) def test_adding_fellows(self): self.assertFalse("*****@*****.**" in self.amity.all_persons) person_fellow = self.amity.add_person("Tyrion", "*****@*****.**", "FELLOW") self.assertTrue("*****@*****.**" in self.amity.all_persons) add_existing = self.amity.add_person("Tyrion", "*****@*****.**", "FELLOW") self.assertEqual(add_existing, "Email already used!") def test_adding_staff(self): self.assertFalse("*****@*****.**" in self.amity.all_persons) self.amity.add_person("Tyrion", "*****@*****.**", "STAFF") self.assertTrue("*****@*****.**" in self.amity.all_persons) add_existing = self.amity.add_person("Tyrion", "*****@*****.**", "STAFF") self.assertEqual(add_existing, "Email already used!") def test_adding_person_with_invalid_person_type(self): self.assertFalse("*****@*****.**" in self.amity.all_persons) sample = self.amity.add_person("Missandei", "*****@*****.**", "Iii") self.assertEqual(sample, -1) def test_allocating_rooms(self): self.amity.add_person("Tywin", "*****@*****.**", "STAFF") r = self.amity.allocate_room("*****@*****.**") def test_reallocating_rooms(self): self.assertFalse("Kingslanding" in self.amity.all_rooms) self.amity.create_room("Kingslanding", "O") self.assertTrue("Kingslanding" in self.amity.all_rooms) self.amity.create_room("Castle Black", "O") self.assertTrue("Castle Black" in self.amity.all_rooms) self.amity.add_person("Jaimie Lanister", "*****@*****.**", "STAFF") self.amity.create_room("Casterly", "O") self.assertTrue("Casterly" in self.amity.all_rooms) self.amity.reallocate_person(self.amity.staff["*****@*****.**"], "Casterly") self.assertEqual(self.amity.staff["*****@*****.**"].office, "Casterly") # reallocating a living space self.amity.create_room("Iron Islands", "L") self.assertTrue("Iron Islands" in self.amity.all_rooms) self.amity.create_room("Mereen", "O") self.assertTrue("Mereen" in self.amity.all_rooms) self.amity.add_person("Theon Greyjoy", "*****@*****.**", "FELLOW", wants_accomodation='Y') self.amity.create_room("Winterfell", "L") self.assertTrue("Winterfell" in self.amity.all_rooms) self.amity.reallocate_person(self.amity.fellows["*****@*****.**"], "Winterfell") self.assertEqual(self.amity.fellows["*****@*****.**"].living_space, "Winterfell") def test_amity_edgy_cases(self): self.amity.add_person("Theon Greyjoy", "*****@*****.**", "FELLOW", wants_accomodation='Y') self.amity.create_room("Winterfell", "L") self.assertTrue("Winterfell" in self.amity.all_rooms) self.amity.reallocate_person(self.amity.fellows["*****@*****.**"], "Winterfell") self.amity.create_room("Mereen", "O") self.amity.reallocate_person(self.amity.fellows["*****@*****.**"], "Mereen") self.amity.del_me() self.amity.create_room("Winterfell", "O") self.amity.add_person("Theon Greyjoy", "*****@*****.**", "FELLOW", wants_accomodation='Y') def test_loading_persons_file(self): self.amity.load_pips_from_text_file( FakeFileWrapper(u"""OLUWAFEMI SULE [email protected] FELLOW Y DOMINIC WALTERS [email protected] STAFF SIMON PATTERSON [email protected] FELLOW Y MARI LAWRENCE [email protected] FELLOW Y LEIGH RILEY [email protected] STAFF LEILA RILEY [email protected] STAFF TANA LOPEZ [email protected] FELLOW Y TINA LOPELA [email protected] FELLOW N KELLY McGUIRE STAFF KELLY McGUIRE STAFF""" )) def test_get_print_data_methods(self): self.assertFalse("Kingslanding" in self.amity.all_rooms) self.amity.create_room("Kingslanding", "O") self.assertTrue("Kingslanding" in self.amity.all_rooms) self.assertFalse("Winterfell" in self.amity.all_rooms) self.amity.create_room("Winterfell", "L") self.assertTrue("Winterfell" in self.amity.all_rooms) self.amity.load_pips_from_text_file( FakeFileWrapper(u"""OLUWAFEMI SULE [email protected] FELLOW Y DOMINIC WALTERS [email protected] STAFF SIMON PATTERSON [email protected] FELLOW Y MARI LAWRENCE [email protected] FELLOW Y LEIGH RILEY [email protected] STAFF LEILA RILEY [email protected] STAFF TANA LOPEZ [email protected] FELLOW Y TINA LOPELA [email protected] FELLOW N KELLY McGUIRE STAFF KELLY McGUIRE STAFF""" )) data = self.amity.get_print_room_data("Kingslanding") self.assertEqual(data['room'], 'Kingslanding') self.assertEqual(len(data['names']), 6) data2 = self.amity.get_print_allocations_data() print(data2) self.assertEqual(len(data2['offices']), 1) self.assertEqual(len(data2['living']), 1) data3 = self.amity.get_print_unallocated_data() self.assertNotEqual(data3['living'], None) self.assertNotEqual(data3['offices'], None) self.amity.load_pips_from_text_file() def test_validation_methods(self): self.assertEqual(self.amity.validate_db_name("db_mine"), "Valid") self.assertEqual(self.amity.validate_db_name("db5"), "Valid") self.assertEqual(self.amity.validate_db_name("5db"), "Valid") self.assertEqual(self.amity.validate_db_name("db-5"), "Invalid") self.assertEqual(self.amity.validate_db_name("&db"), "Invalid") self.assertEqual(self.amity.validate_person_name("name"), "Valid") self.assertEqual(self.amity.validate_person_name("name1"), "Invalid") self.assertEqual(self.amity.validate_person_name("name_"), "Invalid") self.assertEqual(self.amity.validate_room_name("valhalla1"), "Valid") self.assertEqual(self.amity.validate_room_name("2valhall"), "Invalid") self.assertEqual(self.amity.validate_room_name("valhall_1"), "Invalid") self.assertEqual(self.amity.validate_room_type("o"), "Valid") self.assertEqual(self.amity.validate_room_type("l"), "Valid") self.assertEqual(self.amity.validate_room_type("O"), "Valid") self.assertEqual(self.amity.validate_room_type("L"), "Valid") self.assertEqual(self.amity.validate_room_type("living"), "Invalid") self.assertEqual(self.amity.validate_room_type("office"), "Invalid") self.assertEqual(self.amity.validate_room_type("jjj"), "Invalid") self.assertEqual(self.amity.validate_person_type("Fellow"), "Valid") self.assertEqual(self.amity.validate_person_type("staff"), "Valid") self.assertEqual(self.amity.validate_person_type("FelloW"), "Valid") self.assertEqual(self.amity.validate_person_type("anything"), "Invalid") self.assertEqual(self.amity.validate_person_type("elow"), "Invalid") self.assertEqual(self.amity.validate_person_type("staffk"), "Invalid") self.assertEqual(self.amity.validate_person_type("Fellow"), "Valid") self.assertEqual(self.amity.validate_email("*****@*****.**"), "Valid") self.assertEqual(self.amity.validate_email("*****@*****.**"), "Valid") self.assertEqual(self.amity.validate_email("[email protected]"), "Valid") self.assertEqual(self.amity.validate_email("clementm-916@gmail"), "Invalid") self.assertEqual(self.amity.validate_email("clementm-916gmail.3com"), "Invalid") self.assertEqual(self.amity.validate_email("clementm-916@gmail3com"), "Invalid") self.assertEqual(self.amity.validate_email("#[email protected]"), "Invalid") def tearDown(self): pass
def test_amity_init_sets_command(self): amity = Amity('list_people') self.assertEqual(amity.command, 'list_people')
def test_amity_save_people_state(self): amity = Amity('save_state') amity.people = [] self.assertEqual(amity.save_people_state(), False)
def test_amity_load_people_state(self): migrate = Migration() migrate.drop() migrate.install() amity = Amity('load_state') self.assertEqual(amity.load_people_state(), False)