def test_load_state(self): """ Test that data can be loaded to the application from an existing database """ amity = self.load_data_into_amity(Amity()) test_db = Database(amity) test_db.save_state({"--db": "test.db"}) amity2 = Amity() test_db2 = Database(amity2) # Load data from previously created database test_db2.load_state({"<sqlite_database>": "test.db"}) # Data is entered into the application print('in test_load_state', amity.people) print('in test_load_state', amity.rooms) print(len(amity.rooms)) print(amity.livingspaces) self.assertEqual(2, len(amity.rooms)) self.assertEqual(1, len(amity.livingspaces)) self.assertEqual(1, len(amity.offices)) self.assertEqual(2, len(amity.people)) self.assertEqual(1, len(amity.fellows)) self.assertEqual(1, len(amity.staff)) os.remove("test.db")
def setUp(self): self.amity = Amity() self.office_1 = Office('PERL') self.office_2 = Office('OCCULUS') self.living_space_1 = LivingSpace('DOJO') self.living_space_2 = LivingSpace('NODE') self.staff_1 = Staff("BOB", "WACHIRA") self.staff_2 = Staff("BOB", "ODHIAMBO") self.fellow_1 = Fellow("LAWRENCE", "WACHIRA", "N") self.fellow_2 = Fellow("LAWRENCE", "NYAMBURA", "Y") self.fellow_3 = Fellow("MARTIN", "MUNGAI", "Y") self.staff_1.employee_id, self.staff_1.office_allocated = 1, "PERL" self.staff_2.employee_id = 2 self.fellow_1.employee_id = 3 self.fellow_2.employee_id, self.fellow_2.office_allocated = 4, \ "OCCULUS" self.fellow_2.living_space_allocated = "DOJO" self.fellow_3.employee_id = 5 self.office_1.current_occupancy = 1 self.office_2.current_occupancy = 1 self.living_space_1.current_occupancy = 1 self.amity.rooms = [self.office_1, self.office_2, self.living_space_1, self.living_space_2] self.amity.offices = [self.office_1, self.office_2] self.amity.living_spaces = [self.living_space_2, self.living_space_1] self.amity.persons = [self.staff_1, self.staff_2, self.fellow_1, self.fellow_2, self.fellow_3] self.amity.staff = [self.staff_2, self.staff_1] self.amity.fellows = [self.fellow_2, self.fellow_1, self.fellow_3] self.initial_room_count = len(self.amity.rooms) self.initial_office_count = len(self.amity.offices) self.initial_living_space_count = len(self.amity.living_spaces) self.initial_persons_count = len(self.amity.persons) self.initial_staff_count = len(self.amity.staff) self.initial_fellow_count = len(self.amity.fellows) self.amity.create_files_directory() self.amity.create_databases_directory() self.saved_id = None if Path('./models/.id.txt').is_file(): with open('./models/.id.txt', "r") as current_id: self.saved_id = current_id.read() else: with open('./models/.id.txt', "w+") as person_id: person_id.write("5")
def setUp(self): self.amity = Amity() self.amity.room_types = { "office": {Office('MOMBASA'): ['Lavender Ayodi'], Office('HOGWARTS'): [], Office('LAGOS'): []}, "livingspace": {LivingSpace('KENYA'): [], LivingSpace('PLATFORM'): [], LivingSpace('VALHALLA'): []} } self.amity.persons = { 'fellow': [Fellow('Lavender Ayodi')], 'staff': [Staff('John Doe')] }
def test_save_state(self): """Test that application data can be saved to user-defined database""" amity = self.load_data_into_amity(Amity()) test_db = Database(amity) test_db.save_state({"--db": "test.db"}) # File is created self.assertTrue(os.path.exists("test.db")) os.remove("test.db")
def setUp(self): self.test_amity = Amity() """Create living space""" self.test_amity.create_room({ "<room_name>": ["LivingA"], "Living": True, "Office": False }) """Create office""" self.test_amity.create_room({ "<room_name>": ["OfficeA"], "Living": False, "Office": True }) # Assign rooms to variables self.livinga = self.test_amity.livingspaces[0] self.officea = self.test_amity.offices[0] """Add fellow that wants space""" self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Fellow", "<wants_space>": "Y", "Fellow": True, "Staff": False }) """Add staff member that wants space""" self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Staff", "<wants_space>": "Y", "Fellow": False, "Staff": True }) # Assign people to variables self.testfellow = self.test_amity.people[0] self.teststaff = self.test_amity.people[1]
def test_generate_id(self): self.assertIsInstance(Amity.generate_id(), int)
class TestAmity(unittest.TestCase): def setUp(self): self.amity = Amity() self.office_1 = Office('PERL') self.office_2 = Office('OCCULUS') self.living_space_1 = LivingSpace('DOJO') self.living_space_2 = LivingSpace('NODE') self.staff_1 = Staff("BOB", "WACHIRA") self.staff_2 = Staff("BOB", "ODHIAMBO") self.fellow_1 = Fellow("LAWRENCE", "WACHIRA", "N") self.fellow_2 = Fellow("LAWRENCE", "NYAMBURA", "Y") self.fellow_3 = Fellow("MARTIN", "MUNGAI", "Y") self.staff_1.employee_id, self.staff_1.office_allocated = 1, "PERL" self.staff_2.employee_id = 2 self.fellow_1.employee_id = 3 self.fellow_2.employee_id, self.fellow_2.office_allocated = 4, \ "OCCULUS" self.fellow_2.living_space_allocated = "DOJO" self.fellow_3.employee_id = 5 self.office_1.current_occupancy = 1 self.office_2.current_occupancy = 1 self.living_space_1.current_occupancy = 1 self.amity.rooms = [self.office_1, self.office_2, self.living_space_1, self.living_space_2] self.amity.offices = [self.office_1, self.office_2] self.amity.living_spaces = [self.living_space_2, self.living_space_1] self.amity.persons = [self.staff_1, self.staff_2, self.fellow_1, self.fellow_2, self.fellow_3] self.amity.staff = [self.staff_2, self.staff_1] self.amity.fellows = [self.fellow_2, self.fellow_1, self.fellow_3] self.initial_room_count = len(self.amity.rooms) self.initial_office_count = len(self.amity.offices) self.initial_living_space_count = len(self.amity.living_spaces) self.initial_persons_count = len(self.amity.persons) self.initial_staff_count = len(self.amity.staff) self.initial_fellow_count = len(self.amity.fellows) self.amity.create_files_directory() self.amity.create_databases_directory() self.saved_id = None if Path('./models/.id.txt').is_file(): with open('./models/.id.txt', "r") as current_id: self.saved_id = current_id.read() else: with open('./models/.id.txt', "w+") as person_id: person_id.write("5") def test_create_room(self): self.amity.create_room('php', 'office') self.amity.create_room('go', 'o') self.amity.create_room('scala', 'l') self.amity.create_room('shell', 'livingspace') self.assertEqual(len(self.amity.rooms), self.initial_room_count + 4) self.assertEqual(len(self.amity.offices), self.initial_office_count + 2) self.assertEqual(len(self.amity.living_spaces), self.initial_living_space_count + 2) self.assertEqual(self.amity.create_room('shell', 'livingspace'), "Duplicate entry") self.assertEqual(self.amity.create_room('hello', 'livispace'), "Invalid room type") self.assertEqual(self.amity.create_room('ag@in', 'livingspace'), "Invalid room name") self.assertIn('GO', [room.name for room in self.amity.offices]) def test_add_staff(self): self.amity.add_staff('Lawrence', 'Otieno') self.amity.add_staff('Lawrence', 'Muchiri') self.amity.add_staff('Lawrence', 'Kilonzo') self.amity.add_staff('Lawrence', 'Mutiga') self.assertEqual(len(self.amity.persons), self.initial_persons_count + 4) self.assertEqual(len(self.amity.staff), self.initial_staff_count + 4) self.assertEqual(self.amity.add_staff('Lawrence', 'Muchiri'), "Duplicate entry") self.assertIn('ROBERT OPIYO has been allocated the office', self.amity.add_staff('Robert', 'Opiyo')) self.amity.offices = [] self.assertEqual(self.amity.add_staff('Lawrence', 'Ndegwa'), "No empty offices available. Staff LAWRENCE NDEGWA " "has been added but has not been allocated an office") def test_add_fellow(self): self.amity.add_fellow('Mercy', 'Wachira', 'Y') self.amity.add_fellow('Mercy', 'Muchiri') self.amity.add_fellow('Mercy', 'Nyambura', 'y') self.amity.add_fellow('Mercy', 'Mutiga') self.assertEqual(self.amity.add_fellow('Mercy', 'Muchiri'), "Duplicate entry") self.assertEqual(self.amity.add_fellow('M0rcy', 'Muchiri'), "Invalid") self.assertEqual(len(self.amity.persons), self.initial_persons_count + 4) self.assertEqual(len(self.amity.fellows), self.initial_fellow_count + 4) self.assertIn("Fellow MERCY KIBOI has been allocated the living " "space", self.amity.add_fellow('Mercy', 'Kiboi', 'y')) self.amity.living_spaces = [] self.assertEqual(self.amity.add_fellow('Lawrence', 'Ndegwa', 'y'), "No empty living spaces available. Fellow LAWRENCE " "NDEGWA has not been allocated a living space") def test_load_people(self): with open('./files/test_sample.txt', "w+") as people: sample_list = ["OLUWAFEMI SULE FELLOW Y\n", "DOMINIC WALTERS STAFF\n", "SIMON PATTERSON FELLOW Y\n", "MARI LAWRENCE FELLOW Y\n", "LEIGH RILEY STAFF\n", "TANA LOPEZ FELLOW Y\n", "KELLY McGUIRE STAFF"] for person in sample_list: people.write(person) message = "People loaded from file successfully" self.assertEqual(message, self.amity.load_people('test_sample.txt')) self.assertEqual(len(self.amity.persons), self.initial_persons_count + 7) self.assertEqual(len(self.amity.fellows), self.initial_fellow_count + 4) self.assertEqual(len(self.amity.staff), self.initial_staff_count + 3) self.assertEqual('Non-existent file', self.amity.load_people( 'test_load_people.txt')) self.assertEqual("Invalid file", self.amity.load_people('people.mp3')) remove(files_directory_path + 'test_sample.txt') def test_reallocate_person(self): message = "Employee ID does not exist" self.assertEqual(self.amity.reallocate_person('100', "go"), message) message = "New Room has not been created" self.assertEqual(self.amity.reallocate_person('1', "go"), message) self.assertEqual(self.amity.reallocate_person('5a', "go"), 'Invalid ' 'ID') self.staff_1.office_allocated = None self.assertEqual(self.amity.reallocate_person('1', "occulus"), "Person allocated") self.assertEqual(self.amity.reallocate_person('1', "Perl"), "Person reallocated") self.assertEqual(self.amity.reallocate_person('1', "Perl"), "Already in office") self.assertEqual(self.amity.reallocate_person('3', "occulus"), "Person allocated") self.assertEqual(self.amity.reallocate_person('3', "dojo"), "Did not want accommodation") self.assertEqual(self.amity.reallocate_person('4', "node"), "Fellow reallocated") self.assertEqual(self.amity.reallocate_person('5', "node"), "Fellow allocated") self.assertEqual(self.amity.reallocate_person('5', "node"), "Already in living space") self.living_space_1.current_occupancy = 4 self.assertEqual(self.amity.reallocate_person('4', "dojo"), "New living space full") self.office_2.current_occupancy = 6 self.assertEqual(self.amity.reallocate_person('1', "occulus"), "New office full") message = 'Cannot reallocate Staff to a living space' self.assertEqual(self.amity.reallocate_person('1', "dojo"), message) def test_print_allocations(self): self.assertEqual(self.amity.print_allocations(), 'Finished') message = 'Allocations printed and saved to file successfully' self.assertEqual(self.amity.print_allocations('test_allocated.txt'), message) remove(files_directory_path + "test_allocated.txt") self.assertEqual(self.amity.print_allocations('test_allocated.mp3'), "Invalid filename") self.amity.rooms = [] self.assertEqual(self.amity.print_allocations(), "No rooms have been " "created") self.amity.persons = [] self.assertEqual(self.amity.print_allocations(), "No Employees have " "been added") def test_print_unallocated(self): self.assertEqual(self.amity.print_unallocated(), "Finished") message = 'Write to file complete' self.assertEqual(self.amity.print_unallocated('test_unallocated.txt'), message) remove(files_directory_path + "test_unallocated.txt") self.assertEqual(self.amity.print_unallocated('test_unallocated.mp3'), "Invalid filename") self.staff_2.office_allocated = "OCCULUS" self.fellow_1.office_allocated = "OCCULUS" self.fellow_3.office_allocated = "PERL" self.fellow_3.living_space_allocated = "NODE" self.assertEqual(self.amity.print_unallocated('unallocated.txt'), 'Did not output to file. All allocated') self.assertEqual(self.amity.print_unallocated(), 'All allocated') self.amity.persons = [] self.assertEqual(self.amity.print_unallocated(), 'No employees') def test_print_room(self): self.assertEqual(self.amity.print_room("go"), "Didn't print") self.assertEqual(self.amity.print_room("perl"), "Room printed " "successfully") self.office_1.current_occupancy = 0 self.living_space_1.current_occupancy = 0 self.assertEqual(self.amity.print_room("perl"), "None allocated") self.assertEqual(self.amity.print_room("dojo"), "None allocated") def test_save_state(self): if Path(databases_directory_path + 'Amity.sqlite3').is_file(): rename(databases_directory_path + 'Amity.sqlite3', databases_directory_path + 'Amity_backup.sqlite3') self.assertEqual(self.amity.save_state(), "State saved to " "Amity.sqlite3 " "successfully") remove(databases_directory_path + 'Amity.sqlite3') if Path(databases_directory_path + 'Amity_backup.sqlite3').is_file(): rename(databases_directory_path + 'Amity_backup.sqlite3', databases_directory_path + 'Amity.sqlite3') self.assertEqual(self.amity.save_state("February"), "State saved to " "February.sqlite3 " "successfully") remove("./databases/February.sqlite3") self.amity.rooms = [] self.amity.persons = [] self.assertEqual(self.amity.save_state(), "No data") def test_load_state(self): if not Path(databases_directory_path + 'Amity.sqlite3').is_file(): self.assertEqual(self.amity.load_state(), "Database does not " "exist") else: self.assertIn(self.amity.load_state(), ["State loaded " "from Amity.sqlite3 " "successfully", "No data to load"]) self.assertEqual(self.amity.load_state("March"), "Database does not " "exist") conn = sqlite3.connect(databases_directory_path + 'test_load_state_db.sqlite3') cur = conn.cursor() cur.execute('''CREATE TABLE IF NOT EXISTS Amity_employees (ID INTEGER PRIMARY KEY NOT NULL , Employee_ID INTEGER NOT NULL UNIQUE, First_name TEXT NOT NULL, Second_name TEXT NOT NULL, Designation TEXT NOT NULL, Office_allocated TEXT, Wants_accommodation TEXT, Living_space_allocated TEXT)''') cur.execute('''CREATE TABLE IF NOT EXISTS Amity_rooms (Id INTEGER PRIMARY KEY, Room_name TEXT NOT NULL UNIQUE, Room_type TEXT NOT NULL, Current_occupancy INTEGER NOT NULL)''') conn.commit() conn.close() self.assertEqual(self.amity.load_state("test_load_state_db"), "No data to load") remove(databases_directory_path + 'test_load_state_db.sqlite3') def test_generate_id(self): self.assertIsInstance(Amity.generate_id(), int) def test_verify_id_and_new_room_values(self): self.assertEqual(Amity.verify_id_and_new_room_values('a', 'python'), "Invalid ID") self.assertEqual(Amity.verify_id_and_new_room_values('1', 'pyth0n'), "Invalid new room name") def tearDown(self): if self.saved_id: with open('./models/.id.txt', "w+") as restore_id: restore_id.write(self.saved_id) else: remove('./models/.id.txt')
else: pretty_print_data(rooms) @docopt_cmd def do_allocate_unallocated(self, args): """ Randomly allocate rooms to unallocated people Usage: allocate_unallocated [-f|-s] """ result = amity.randomly_allocate_unallocated() if result: allocated_staff = amity.translate_staff_data_to_dict( result['staff']) allocated_fellows = amity.translate_fellow_data_to_dict( result['fellows']) print_subtitle("Newly Allocated") if args['-s']: pretty_print_data(allocated_staff) elif args['-f']: pretty_print_data(allocated_fellows) else: pretty_print_data(allocated_staff + allocated_fellows) opt = docopt(__doc__, sys.argv[1:], True, 2.0) if opt['--interactive']: print_header() amity = Amity() AmityInteractive().cmdloop()
class TestAmity(unittest.TestCase): def setUp(self): self.test_amity = Amity() """Create living space""" self.test_amity.create_room({ "<room_name>": ["LivingA"], "Living": True, "Office": False }) """Create office""" self.test_amity.create_room({ "<room_name>": ["OfficeA"], "Living": False, "Office": True }) # Assign rooms to variables self.livinga = self.test_amity.livingspaces[0] self.officea = self.test_amity.offices[0] """Add fellow that wants space""" self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Fellow", "<wants_space>": "Y", "Fellow": True, "Staff": False }) """Add staff member that wants space""" self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Staff", "<wants_space>": "Y", "Fellow": False, "Staff": True }) # Assign people to variables self.testfellow = self.test_amity.people[0] self.teststaff = self.test_amity.people[1] def test_create_room(self): """Test creation of rooms""" # LivingA and OfficeA from setup() added to relevant lists self.assertEqual(2, len(self.test_amity.rooms)) self.assertEqual(1, len(self.test_amity.livingspaces)) self.assertEqual(1, len(self.test_amity.offices)) """Test creation of multiple living spaces""" self.test_amity.create_room({ "<room_name>": ["LivingB", "LivingC"], "Living": True, "Office": False }) # Two living spaces added to list of rooms and list of living spaces self.assertEqual(4, len(self.test_amity.rooms)) self.assertEqual(3, len(self.test_amity.livingspaces)) """Test creation of multiple offices""" self.test_amity.create_room({ "<room_name>": ["OfficeB", "OfficeC"], "Living": False, "Office": True }) # Two offices added to list of rooms and list of offices self.assertEqual(6, len(self.test_amity.rooms)) self.assertEqual(3, len(self.test_amity.offices)) """Test that duplicate rooms are not added""" self.test_amity.create_room({ "<room_name>": ["OfficeA", "OfficeB"], "Living": False, "Office": True }) # Duplicate rooms not added to any list self.assertEqual(6, len(self.test_amity.rooms)) self.assertEqual(3, len(self.test_amity.offices)) def test_add_person(self): """Test addition of people""" # Test Fellow and Test Staff from setup() added to relevant lists self.assertEqual(2, len(self.test_amity.people)) self.assertEqual(1, len(self.test_amity.fellows)) self.assertEqual(1, len(self.test_amity.staff)) """Test allocation of those who want space""" # Check that people have been appended to rooms' lists of occupants self.assertEqual(2, len(self.officea.occupants)) def test_vacant_offices(self): """Test that vacant rooms are added to relevant list""" # Add office self.test_amity.create_room({ "<room_name>": ["OfficeB"], "Living": False, "Office": True }) self.test_amity.check_vacant_rooms() # Check if OfficeB has been appended to relevant lists self.assertEqual(2, len(self.test_amity.vacant_offices)) self.assertEqual(3, len(self.test_amity.vacant_rooms)) def test_reallocate_person(self): """Add another office""" self.test_amity.create_room({ "<room_name>": ["OfficeB"], "Living": False, "Office": True }) # Assign OfficeB to variable officeb = self.test_amity.offices[1] """Test reallocation of Test Staff from OfficeA to OfficeB""" self.test_amity.reallocate_person({ "<employee_id>": int(self.teststaff.emp_id), "<new_room_name>": "OfficeB" }) # Staff member no longer in OfficeA's list of occupants self.assertEqual(1, len(self.officea.occupants)) # Staff member now in OfficeB's list of occupants self.assertEqual(1, len(officeb.occupants)) """Add another living space""" self.test_amity.create_room({ "<room_name>": ["LivingB"], "Living": True, "Office": False }) # Assign LivingB to variable livingb = self.test_amity.livingspaces[1] """Test allocation of Test Fellow to LivingB""" self.test_amity.reallocate_person({ "<employee_id>": int(self.testfellow.emp_id), "<new_room_name>": "LivingB" }) # Fellow now in LivingB's list of occupants self.assertEqual(1, len(livingb.occupants)) """Test that staff cannot be allocated to living space""" self.test_amity.reallocate_person({ "<employee_id>": int(self.teststaff.emp_id), "<new_room_name>": "LivingB" }) # Staff not added to LivingB's list of occupants self.assertEqual(1, len(livingb.occupants)) def test_load_people(self): """ Test that people can be added to the app from a user-defined text file """ """Add office to have more vacant offices for people from text file """ self.test_amity.create_room({ "<room_name>": ["OfficeC"], "Living": False, "Office": True }) self.test_amity.load_people({"<filename>": "people.txt"}) # People from file are added to application self.assertEqual(12, len(self.test_amity.people)) self.assertEqual(7, len(self.test_amity.fellows)) self.assertEqual(5, len(self.test_amity.staff)) def test_print_allocations(self): """ Test that allocations are displayed on screen and printed to a text file if specified """ self.test_amity.print_allocations({"--o": "myfile.txt"}) # File is created self.assertTrue(os.path.exists("myfile.txt")) # Data is entered with open("myfile.txt") as myfile: lines = myfile.readlines() self.assertTrue("LivingA\n" in lines) self.assertTrue("OfficeA\n" in lines) self.assertTrue("Test Fellow, Test Staff\n" in lines) os.remove("myfile.txt") def test_print_unallocated(self): """ Test that unallocated people are displayed on screen and printed to a text file if specified """ # Add fellow that does not want space self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Fellow2", "<wants_space>": "N", "Fellow": True, "Staff": False }) self.test_amity.print_unallocated({"--o": "myfile2.txt"}) # File is created self.assertTrue(os.path.exists("myfile2.txt")) # Data is entered with open("myfile2.txt") as myfile: lines = myfile.readlines() self.assertTrue("Unallocated People\n" in lines) self.assertTrue("Test Fellow2\n" in lines) os.remove("myfile2.txt")
class TestAmity(unittest.TestCase): def setUp(self): self.test_amity = Amity() """Create living space""" self.test_amity.create_room({ "<room_name>": ["LivingA"], "Living": True, "Office": False }) """Create office""" self.test_amity.create_room({ "<room_name>": ["OfficeA"], "Living": False, "Office": True }) # Assign rooms to variables self.livinga = self.test_amity.livingspaces[0] self.officea = self.test_amity.offices[0] """Add fellow that wants space""" self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Fellow", "<wants_space>": "Y", "Fellow": True, "Staff": False }) """Add staff member that wants space""" self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Staff", "<wants_space>": "Y", "Fellow": False, "Staff": True }) # Assign people to variables self.testfellow = self.test_amity.people[0] self.teststaff = self.test_amity.people[1] def test_create_room(self): """Test creation of rooms""" # LivingA and OfficeA from setup() added to relevant lists self.assertEqual(2, len(self.test_amity.rooms)) self.assertEqual(1, len(self.test_amity.livingspaces)) self.assertEqual(1, len(self.test_amity.offices)) """Test creation of multiple living spaces""" self.test_amity.create_room({ "<room_name>": ["LivingB", "LivingC"], "Living": True, "Office": False }) # Two living spaces added to list of rooms and list of living spaces self.assertEqual(4, len(self.test_amity.rooms)) self.assertEqual(3, len(self.test_amity.livingspaces)) """Test creation of multiple offices""" self.test_amity.create_room({ "<room_name>": ["OfficeB", "OfficeC"], "Living": False, "Office": True }) # Two offices added to list of rooms and list of offices self.assertEqual(6, len(self.test_amity.rooms)) self.assertEqual(3, len(self.test_amity.offices)) """Test that duplicate rooms are not added""" self.test_amity.create_room({ "<room_name>": ["OfficeA", "OfficeB"], "Living": False, "Office": True }) # Duplicate rooms not added to any list self.assertEqual(6, len(self.test_amity.rooms)) self.assertEqual(3, len(self.test_amity.offices)) def test_add_person(self): """Test addition of people""" # Test Fellow and Test Staff from setup() added to relevant lists self.assertEqual(2, len(self.test_amity.people)) self.assertEqual(1, len(self.test_amity.fellows)) self.assertEqual(1, len(self.test_amity.staff)) """Test allocation of those who want space""" # Check that people have been appended to rooms' lists of occupants self.assertEqual(2, len(self.officea.occupants)) def test_vacant_offices(self): """Test that vacant rooms are added to relevant list""" # Add office self.test_amity.create_room({ "<room_name>": ["OfficeB"], "Living": False, "Office": True }) self.test_amity.check_vacant_rooms() # Check if OfficeB has been appended to relevant lists self.assertEqual(2, len(self.test_amity.vacant_offices)) self.assertEqual(3, len(self.test_amity.vacant_rooms)) def test_reallocate_person(self): """Add another office""" self.test_amity.create_room({ "<room_name>": ["OfficeB"], "Living": False, "Office": True }) # Assign OfficeB to variable officeb = self.test_amity.offices[1] """Test reallocation of Test Staff from OfficeA to OfficeB""" self.test_amity.reallocate_person({ "<employee_id>": int(self.teststaff.emp_id), "<new_room_name>": "OfficeB" }) # Staff member no longer in OfficeA's list of occupants self.assertEqual(1, len(self.officea.occupants)) # Staff member now in OfficeB's list of occupants self.assertEqual(1, len(officeb.occupants)) """Add another living space""" self.test_amity.create_room({ "<room_name>": ["LivingB"], "Living": True, "Office": False }) # Assign LivingB to variable livingb = self.test_amity.livingspaces[1] """Test allocation of Test Fellow to LivingB""" self.test_amity.reallocate_person({ "<employee_id>": int(self.testfellow.emp_id), "<new_room_name>": "LivingB" }) # Fellow now in LivingB's list of occupants self.assertEqual(1, len(livingb.occupants)) """Test that staff cannot be allocated to living space""" self.test_amity.reallocate_person({ "<employee_id>": int(self.teststaff.emp_id), "<new_room_name>": "LivingB" }) # Staff not added to LivingB's list of occupants self.assertEqual(1, len(livingb.occupants)) def test_load_people(self): """ Test that people can be added to the app from a user-defined text file """ """Add office to have more vacant offices for people from text file """ self.test_amity.create_room({ "<room_name>": ["OfficeC"], "Living": False, "Office": True }) self.test_amity.load_people({"<filename>": "people.txt"}) # People from file are added to application self.assertEqual(12, len(self.test_amity.people)) self.assertEqual(7, len(self.test_amity.fellows)) self.assertEqual(5, len(self.test_amity.staff)) def test_print_allocations(self): """ Test that allocations are displayed on screen and printed to a text file if specified """ self.test_amity.print_allocations({ "--o": "myfile.txt" }) # File is created self.assertTrue(os.path.exists("myfile.txt")) # Data is entered with open("myfile.txt") as myfile: lines = myfile.readlines() self.assertTrue("LivingA\n" in lines) self.assertTrue("OfficeA\n" in lines) self.assertTrue("Test Fellow, Test Staff\n" in lines) os.remove("myfile.txt") def test_print_unallocated(self): """ Test that unallocated people are displayed on screen and printed to a text file if specified """ # Add fellow that does not want space self.test_amity.add_person({ "<first_name>": "Test", "<last_name>": "Fellow2", "<wants_space>": "N", "Fellow": True, "Staff": False }) self.test_amity.print_unallocated({ "--o": "myfile2.txt" }) # File is created self.assertTrue(os.path.exists("myfile2.txt")) # Data is entered with open("myfile2.txt") as myfile: lines = myfile.readlines() self.assertTrue("Unallocated People\n" in lines) self.assertTrue("Test Fellow2\n" in lines) os.remove("myfile2.txt")
def setUp(self): self.amityville = Amity()
class TestAmityville(unittest.TestCase): """ Class test Amity """ def setUp(self): self.amityville = Amity() def tearDown(self): """ provide cleanup for all clear list data for each test. """ # remove all entries in dict self.amityville.office_allocations.clear() self.amityville.livingspace_allocations.clear() self.amityville.room_data.clear() self.amityville.person_data.clear() # remove all entries in list del self.amityville.rooms[:] del self.amityville.offices[:] del self.amityville.livingspaces[:] del self.amityville.people[:] del self.amityville.staff[:] del self.amityville.fellows[:] del self.amityville.unallocated_livingspace[:] del self.amityville.unallocated_office[:] def test_create_room_already_exists(self): """ test if a room being created already exists """ self.amityville.create_room('SUN', 'OFFICE') result = self.amityville.create_room('SUN', 'OFFICE') self.assertEqual(result, 'SUN already exists.') def test_create_room_livingspace(self): """ test if a livingspace is created """ result = self.amityville.create_room('MOON', 'LIVINGSPACE') self.assertEqual(result, 'MOON created.') def test_create_room_office(self): """ test if an office is created """ result = self.amityville.create_room('SUN', 'OFFICE') self.assertEqual(result, 'SUN created.') def test_create_room_invalid_room_type(self): """ Test for an invalid room type """ result = self.amityville.create_room('SUN', 'OFICE') self.assertEqual(result, 'Invalid room type.') def test_add_person_invalid_person_name(self): """ test if person name is valid """ result = self.amityville.add_person('asce1062', 'FELLOW', 'NO') self.assertEqual(result, 'Invalid person name.') def test_add_person_fellow(self): """ test if a fellow is added """ result = self.amityville.add_person('ALEX', 'FELLOW', 'NO') self.assertEqual( result, 'ALEX successfully added! \nAllocated to:\nOffice: No vaccant office.' ) def test_add_person_staff(self): """ test if a staff member is added """ result = self.amityville.add_person('ALEX', 'STAFF', 'NO') self.assertEqual( result, 'ALEX successfully added! \nAllocated to:\nOffice: No vaccant office.' ) def test_add_person_invalid_job_description(self): """ test if job description is valid """ result = self.amityville.add_person('ALEX', 'FELOW', 'NO') self.assertEqual(result, 'Invalid job description.') def test_add_person_invalid_accommodation_choice_fellow(self): """ test if accommodation input is valid """ result = self.amityville.add_person('ALEX', 'FELLOW', 'YEZ') self.assertEqual(result, 'Invalid accommodation input.') def test_add_person_invalid_accommodation_choice_staff(self): """ test if accommodation input is valid """ result = self.amityville.add_person('ALEX', 'STAFF', 'NEIN') self.assertEqual(result, 'Invalid accommodation input.') def test_add_person_staff_wants_accommodation(self): """ test if a staff request for accommodation """ result = self.amityville.add_person('ALEX', 'STAFF', 'YES') self.assertEqual(result, 'Staff cannot be allocated a livingspace.') def test_allocate_livingspace_to_nonexistent_fellow(self): """ test allocating a livingspace to a non existant fellow """ result = self.amityville.allocate_livingspace('ALEX') self.assertEqual(result, 'ALEX does not exist.') def test_allocate_livingspace_already_allocated_livingspace(self): """ test if a fellow has already been allocated a livingspace """ self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') result = self.amityville.allocate_livingspace('ALEX') self.assertEqual(result, 'ALEX already allocated a livingspace.') def test_allocate_livingspace(self): """ test if a fellow is allocated a living space """ self.amityville.create_room('MOON', 'LIVINGSPACE') result = self.amityville.add_person('ALEX', 'FELLOW', 'YES') self.assertEqual( result, 'ALEX successfully added! \nAllocated to:\nLivingSpace:' ' ALEX allocated to MOON.\nOffice: No vaccant office.') def test_allocate_livingspace_no_vaccant_livingspace(self): """ test that there are no vaccant livingspaces """ self.amityville.add_person('ALEX', 'FELLOW', 'YES') self.amityville.add_person('JOE', 'FELLOW', 'YES') self.amityville.add_person('TINA', 'FELLOW', 'YES') self.amityville.add_person('IBRA', 'FELLOW', 'YES') self.amityville.add_person('MILLY', 'FELLOW', 'YES') self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.allocate_livingspace('MILLY') self.amityville.allocate_livingspace('IBRA') self.amityville.allocate_livingspace('TINA') self.amityville.allocate_livingspace('JOE') result = self.amityville.allocate_livingspace('ALEX') self.assertEqual(result, 'No vaccant livingspace.') def test_allocate_office_to_nonexistent_staff(self): """ test allocating a livingspace to a non existant fellow """ result = self.amityville.allocate_office('ALEX') self.assertEqual(result, 'ALEX does not exist.') def test_allocate_office_already_allocated_office(self): """ test if a staff has already been allocated an office """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.allocate_office('ALEX') self.assertEqual(result, 'ALEX already allocated to an office.') def test_allocate_office(self): """ test if a staff is allocated an office """ self.amityville.add_person('ALEX', 'STAFF', 'NO') self.amityville.create_room('SUN', 'OFFICE') result = self.amityville.allocate_office('ALEX') self.assertEqual(result, 'ALEX allocated to SUN.') def test_allocate_office_no_vaccant_office(self): """ test that there are no vaccant offices """ self.amityville.add_person('ALEX', 'STAFF', 'NO') self.amityville.add_person('JOE', 'STAFF', 'NO') self.amityville.add_person('TINA', 'STAFF', 'NO') self.amityville.add_person('IBRA', 'STAFF', 'NO') self.amityville.add_person('MILLY', 'STAFF', 'NO') self.amityville.add_person('PAU', 'STAFF', 'NO') self.amityville.add_person('JONA', 'STAFF', 'NO') self.amityville.create_room('SUN', 'OFFICE') self.amityville.allocate_office('JONA') self.amityville.allocate_office('PAU') self.amityville.allocate_office('MILLY') self.amityville.allocate_office('IBRA') self.amityville.allocate_office('TINA') self.amityville.allocate_office('JOE') result = self.amityville.allocate_office('ALEX') self.assertEqual(result, 'No vaccant office.') def test_reallocate_person_nonexistent_rooms(self): """ test there are no rooms added in order to reallocate """ # self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.reallocate_person('S1', 'SUN') self.assertEqual(result, 'SUN does not exist.') def test_reallocate_person_nonexistent_people(self): """ test there is no one to reallocate """ self.amityville.create_room('SUN', 'OFFICE') result = self.amityville.reallocate_person('S1', 'SUN') self.assertEqual(result, 'S1 does not exist.') def test_reallocate_person_livingspace(self): """ test if person is reallocated to a livingspace """ self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') self.amityville.create_room('LIGHT', 'LIVINGSPACE') result = self.amityville.reallocate_person('F1', 'LIGHT') self.assertEqual(result, 'ALEX has been reallocated to LIGHT.') def test_reallocate_person_office(self): """ test if person is reallocated to an office. """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.add_person('ALEX', 'STAFF', 'NO') self.amityville.create_room('SHINE', 'OFFICE') result = self.amityville.reallocate_person('S1', 'SHINE') self.assertEqual(result, 'ALEX has been reallocated to SHINE.') def test_reallocate_person_staff_to_livingspace(self): """ test if a staff is reallocated to a living space """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.reallocate_person('S1', 'MOON') self.assertEqual(result, 'Cannot reallocate staff member to a livingspace.') def test_reallocate_person_to_full_livingspace(self): """ test if room being reallocated to is already full """ self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') self.amityville.add_person('JOE', 'FELLOW', 'YES') self.amityville.add_person('TINA', 'FELLOW', 'YES') self.amityville.add_person('IBRA', 'FELLOW', 'YES') self.amityville.create_room('LIGHT', 'LIVINGSPACE') self.amityville.add_person('MILLY', 'FELLOW', 'YES') result = self.amityville.reallocate_person('F5', 'MOON') self.assertEqual(result, 'MOON is already full.') def test_reallocate_person_to_full_office(self): """ test if room being reallocated to is already full """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.add_person('ALEX', 'STAFF', 'NO') self.amityville.add_person('JOE', 'STAFF', 'NO') self.amityville.add_person('TINA', 'STAFF', 'NO') self.amityville.add_person('IBRA', 'STAFF', 'NO') self.amityville.add_person('MILLY', 'STAFF', 'NO') self.amityville.add_person('PAU', 'STAFF', 'NO') self.amityville.create_room('SHINE', 'OFFICE') self.amityville.add_person('JONA', 'STAFF', 'NO') result = self.amityville.reallocate_person('s7', 'SUN') self.assertEqual(result, 'SUN is already full.') def test_reallocate_person_to_currently_occupied_livingspace(self): """ test if the room being reallocated to is the one they are currently residing in """ self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') result = self.amityville.reallocate_person('F1', 'MOON') self.assertEqual(result, 'ALEX already allocated to MOON.') def test_reallocate_person_to_currently_occupied_office(self): """ test if the room being reallocated to is the one they are currently residing in """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.reallocate_person('S1', 'SUN') self.assertEqual(result, 'ALEX already allocated to SUN.') def test_reallocate_person_to_a_livingspace_and_not_yet_allocated_a_livingspace( self): """ test reallocating a person who is not yet allocated. """ self.amityville.add_person('ALEX', 'FELLOW', 'YES') self.amityville.create_room('MOON', 'LIVINGSPACE') result = self.amityville.reallocate_person('F1', 'MOON') self.assertEqual(result, 'ALEX not yet allocated to any livingspace.') def test_reallocate_person_to_an_office_and_not_yet_allocated_an_office( self): """ test reallocating a person who is not yet allocated. """ self.amityville.add_person('ALEX', 'STAFF', 'NO') self.amityville.create_room('SHINE', 'OFFICE') result = self.amityville.reallocate_person('S1', 'SHINE') self.assertEqual(result, 'ALEX not yet allocated to any office.') def test_print_people_details_no_data_yet(self): """ test print people details and no people added to the system yet. """ result = self.amityville.print_people_details() self.assertEqual(result, 'No one exists in the system yet.') def test_load_people_wrong_file_name(self): """ test if people are loaded from wrong file. """ filename = 'peoplee' result = self.amityville.load_people(filename) self.assertEqual(result, 'File does not exist.') def test_load_people(self): """ test if people are loaded from a file. """ filename = 'people' result = self.amityville.load_people(filename) self.assertEqual(result, 'People successfully loaded.') def test_load_people_no_filename(self): """ test if no file name is provided. """ result = self.amityville.load_people('') self.assertEqual(result, 'Filename must be provided.') def test_load_people_invalid_accommodation(self): """ Test if invalid accommodation input is handled. """ filename = 'invalid_accommodation' result = self.amityville.load_people(filename) self.assertEqual(result, 'Invalid accommodation input.') def test_load_people_invalid_number_of_arguments_less(self): """ test if arguments provided are less than or equal to 2. """ filename = 'invalid_number_of_arguments_less' result = self.amityville.load_people(filename) self.assertEqual(result, 'Invalid number of arguments input.') def test_load_people_invalid_number_of_arguments_more(self): """ test if arguments provided are more than 4. """ filename = 'invalid_number_of_arguments_more' result = self.amityville.load_people(filename) self.assertEqual(result, 'Invalid number of arguments input.') def test_load_people_invalid_job_description(self): """ test for invalid job description. """ filename = 'invalid_job_description' result = self.amityville.load_people(filename) self.assertEqual(result, 'Invalid job description.') def test_load_rooms_from_file_wrong_file_name(self): """ test if rooms are loaded from a wrong file. """ filename = 'room' result = self.amityville.load_rooms(filename) self.assertEqual(result, 'File does not exist.') def test_load_rooms_from_file(self): """ test if rooms are loaded from file. """ filename = 'rooms' result = self.amityville.load_rooms(filename) self.assertEqual(result, 'Rooms successfully loaded from file.') def test_load_rooms_from_file_no_filename(self): """ test if no file name is provided. """ filename = '' result = self.amityville.load_rooms(filename) self.assertEqual(result, 'Filename must be provided.') def test_load_rooms_from_file_invalid_input(self): """ test if there are more than 2 arguments. """ filename = 'rooms_invalid_input' result = self.amityville.load_rooms(filename) self.assertEqual(result, 'Invalid number of arguments input.') def test_print_allocations_to_screen(self): """ test if allocations are successfully printed to the screen. """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') filename = '' result = self.amityville.print_allocations(filename) self.assertEqual(result, 'Done.') def test_print_allocations_to_file(self): """ test if allocations are successfully printed out to a file. """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') filename = 'allocations' result = self.amityville.print_allocations(filename) self.assertEqual(result, 'Done.') def test_print_specific_room_allocations(self): """ test if specific room allocations are printed to the screen. """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.print_specific_room_allocations('SUN') self.assertEqual(result, 'Done.') def test_print_specific_room_allocations_invalid_input(self): """ test if invalid input is handled. """ result = \ self.amityville.print_specific_room_allocations('SUN(1)') self.assertEqual(result, 'Invalid input.') def test_print_unallocated_to_screen(self): """ test if un-allocations are printed to the screen. """ filename = '' result = self.amityville.print_unallocated(filename) self.assertEqual(result, 'Done.') def test_print_unallocated_to_file(self): """ test if un-allocations are printed into a file. """ filename = 'unallocated' result = self.amityville.print_unallocated(filename) self.assertEqual(result, 'Done.') def test_print_rooms(self): """ test if existing rooms are printed to the screen. """ result = self.amityville.print_rooms() self.assertEqual(result, 'Done.') def test_print_fellows(self): """ test if existing fellows are printed to the screen. """ result = self.amityville.print_fellows() self.assertEqual(result, 'Done.') def test_print_staff(self): """ test if existing staff are printed to the screen. """ result = self.amityville.print_staff() self.assertEqual(result, 'Done.') def test_print_all_people(self): """ test if all existing people are printed to the screen. """ result = self.amityville.print_all_people() self.assertEqual(result, 'Done.') def test_print_people_details(self): """ test print people details. """ self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.print_people_details() self.assertEqual(result, 'Done.') def test_save_state(self): """ test if program state is saved to db """ rooms = 'rooms' self.amityville.load_rooms(rooms) people = 'people' self.amityville.load_people(people) dbname = 'amity' self.amityville.clear_db(dbname) result = self.amityville.save_state(dbname) self.assertEqual(result, 'Data saved successfully.') def test_save_state_no_dbname_provided(self): """ test if program state is saved to db """ rooms = 'rooms' self.amityville.load_rooms(rooms) people = 'people' self.amityville.load_people(people) dbname = '' self.amityville.clear_db(dbname) result = self.amityville.save_state() self.assertEqual(result, 'Data saved successfully.') def test_save_state_no_rooms_exist(self): """ test if program state is saved to db """ people = 'people' self.amityville.load_people(people) dbname = 'amity' self.amityville.clear_db(dbname) result = self.amityville.save_state(dbname) self.assertEqual(result, 'Data saved successfully.') def test_load_state(self): """ test if program state saved to db is loaded to program. """ dbname = 'amity' people = 'people' rooms = 'rooms' self.amityville.clear_db(dbname) self.amityville.load_rooms(rooms) self.amityville.load_people(people) self.amityville.save_state(dbname) result = self.amityville.load_state(dbname) self.assertEqual(result, 'Data has been loaded into the system successfully.') def test_load_state_no_db_name_provided(self): """ test if program state saved to db is loaded to program. """ dbname = '' people = 'people' rooms = 'rooms' self.amityville.clear_db(dbname) self.amityville.load_rooms(rooms) self.amityville.load_people(people) self.amityville.save_state(dbname) result = self.amityville.load_state(dbname) self.assertEqual(result, 'Data has been loaded into the system successfully.') def test_load_state_no_rooms_exist(self): """ test if program state saved to db is loaded to program. """ dbname = 'amity' people = 'people' self.amityville.clear_db(dbname) self.amityville.load_people(people) self.amityville.save_state(dbname) result = self.amityville.load_state(dbname) self.assertEqual(result, 'Data has been loaded into the system successfully.') def test_clear_db(self): """ test if db is cleared """ dbname = 'amity' result = self.amityville.clear_db(dbname) self.assertEqual(result, 'Database cleared successfully.') def test_clear_db_no_name(self): """ test if db is cleared """ dbname = '' result = self.amityville.clear_db(dbname) self.assertEqual(result, 'Database cleared successfully.') def test_delete_person_person_id_does_not_exist(self): """ test if removing a person who does not exist in the system. """ result = self.amityville.delete_person('F1') self.assertEqual(result, 'F1 does not exist.') def test_delete_person_fellow(self): """ test deleting a fellow from amity """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.create_room('MOON', 'LIVINGSPACE') self.amityville.add_person('ALEX', 'FELLOW', 'YES') result = self.amityville.delete_person('F1') self.assertEqual( result, 'F1: ALEX who is a FELLOW has been removed from amity.') def test_delete_person_fellow_not_yet_allocated(self): """ test deleting a fellow from amity who has not yet been allocated """ self.amityville.add_person('ALEX', 'FELLOW', 'YES') result = self.amityville.delete_person('F1') self.assertEqual( result, 'F1: ALEX who is a FELLOW has been removed from amity.') def test_delete_person_staff(self): """ test deleting a staff from amity """ self.amityville.create_room('SUN', 'OFFICE') self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.delete_person('S1') self.assertEqual( result, 'S1: ALEX who is a STAFF has been removed from amity.') def test_delete_person_staff_not_yet_allocated(self): """ test deleting a staff from amity who has not yet been allocated """ self.amityville.add_person('ALEX', 'STAFF', 'NO') result = self.amityville.delete_person('S1') self.assertEqual( result, 'S1: ALEX who is a STAFF has been removed from amity.') def test_delete_room_non_existant(self): """ test deleting a room that is not yet created """ result = self.amityville.delete_room('MOON') self.assertEqual(result, 'MOON does not exist.') def test_delete_room_livingspace(self): """ test deleting a livingspace from amity """ self.amityville.create_room('MOON', 'LIVINGSPACE') result = self.amityville.delete_room('MOON') self.assertEqual( result, 'Room MOON which is a LIVINGSPACE has been removed from amity.') def test_delete_room_office(self): """ test deleting an office from amity """ self.amityville.create_room('SUN', 'OFFICE') result = self.amityville.delete_room('SUN') self.assertEqual( result, 'Room SUN which is an OFFICE has been removed from amity.')
class AmityApp(cmd.Cmd): intro = colored( '\n\t\t\t\tWelcome to Amity!\n\t -Type help for a ' 'list of instructions on how to use the app-', 'green') prompt = colored('\n\nAmity >> ', 'magenta') amity = Amity() @docopt_cmd def do_create_room(self, args): """Usage: create_room <room_type> <room_name>...""" room_type = args['<room_type>'] room_list = args['<room_name>'] for name in room_list: self.amity.create_room(name, room_type) sleep(0.2) @docopt_cmd def do_add_person(self, args): """ Usage: add_person <designation> <first_name> <second_name> \ [-a <wants_accommodation>] Options: -a <wants_accommodation> Whether person wants accommodation [default: N] """ designation = args['<designation>'] first_name = args['<first_name>'] second_name = args['<second_name>'] wants_accommodation = args['-a'] if wants_accommodation.upper() not in ["Y", "N"]: print("\n<wants_accommodation> should either be 'Y' or 'N' and is " "not an option for STAFF persons") elif designation.upper() in ["FELLOW", "F"]: if wants_accommodation.upper() == 'Y': self.amity.add_fellow(first_name, second_name, "Y") else: self.amity.add_fellow(first_name, second_name) elif designation.upper() in ["STAFF", "S"]: if wants_accommodation != 'N': print("\n STAFF persons cannot be allocated living spaces") else: self.amity.add_staff(first_name, second_name) else: print("\n Invalid Employee designation. Designation should be " "either 'Staff' or 'Fellow'") @docopt_cmd def do_reallocate_person(self, args): """Usage: reallocate_person <employee_id> <new_room_name>""" employee_id = args['<employee_id>'] new_room_name = args['<new_room_name>'] self.amity.reallocate_person(employee_id, new_room_name) @docopt_cmd def do_load_people(self, arg): """Usage: load_people <file_name>""" self.amity.load_people(arg["<file_name>"]) @docopt_cmd def do_print_allocations(self, arg): """ Usage: print_allocations [-f <file_name>] Options: -f <file_name> Output to file """ if arg['-f']: self.amity.print_allocations(arg['-f']) else: self.amity.print_allocations() @docopt_cmd def do_print_unallocated(self, arg): """ Usage: print_unallocated [-f <file_name>] Options: -f <unallocated.txt> Output to file """ if arg['-f']: self.amity.print_unallocated(arg['-f']) else: self.amity.print_unallocated() @docopt_cmd def do_print_room(self, arg): """Usage: print_room <room_name>""" room_name = arg['<room_name>'] if room_name.isalpha(): self.amity.print_room(room_name) else: print("\n Invalid room name. Room name should consist of " "alphabetical characters only") @docopt_cmd def do_save_state(self, arg): """Usage: save_state [--db <database_name>] Options: --db <database_name> Save to specified database(default is set to 'Amity') """ if arg['--db']: self.amity.save_state(arg['--db']) else: self.amity.save_state() @docopt_cmd def do_load_state(self, arg): """Usage: load_state [--db <sqlite_database>] Options: --db <database_name> Load from specified database(default is set to 'Amity') """ if arg['--db']: self.amity.load_state(arg['--db']) else: self.amity.load_state() @docopt_cmd def do_help(self, arg): """Usage: help""" print(''' \t\t\t\t Commands: create room <room_type> <room_name> ... add person <first_name> <second_name> <designation> [wants_accommodation] reallocate person <employee_id> <new_room_name> print_allocations [-o=allocations.txt] print_unallocated [-o=unallocated.txt] print room <room_name> load_people <file_name> save_state [--db=sqlite_database] load_state <sqlite_database> help -Words enclosed in angle brackets '< >' should guide you on the required number of arguments, except when they appear like this: '< >...' when any number of arguments is allowed. -Square brackets '[]' denote optional arguments. -Separate different arguments with a space. \n\t\t\t ||Type exit to close the app|| ''') @docopt_cmd def do_exit(self, arg): """Usage: exit""" print('\n\n' + '*' * 60 + '\n') cprint('\t\tThank you for using Amity!\n', 'green') print('*' * 60) style = Figlet(font='pebbles') cprint(style.renderText('Good Bye!'), 'magenta') exit()
def test_verify_id_and_new_room_values(self): self.assertEqual(Amity.verify_id_and_new_room_values('a', 'python'), "Invalid ID") self.assertEqual(Amity.verify_id_and_new_room_values('1', 'pyth0n'), "Invalid new room name")
class AmityApp(cmd.Cmd): intro = cprint(figlet_format("Amity Room App", font="cosmic"), "white") prompt = 'Amity>>> ' # file = None amity = Amity() @docopt_cmd def do_create_room(self, arg): """ Creates a new room in the system. Usage: create_room <room_name> <room_type> """ room_name = arg["<room_name>"] room_type = arg["<room_type>"] print(self.amity.create_room(room_name, room_type)) @docopt_cmd def do_add_person(self, arg): """ Creates a new person and assigns them to a random room in Amity. Usage: add_person <first_name> <second_name> <person_type> [--Y] """ person_name = arg["<first_name>"] + " " + arg["<second_name>"] person_type = arg["<person_type>"] wants_accomodation = arg["--Y"] if wants_accomodation is False: wants_accomodation = "N" print( self.amity.add_person(person_name, person_type, wants_accomodation)) @docopt_cmd def do_reallocate_person(self, arg): """ Reallocates a person to a new room of their choice. Usage: reallocate_person <first_name> <second_name> <new_room> """ new_room_name = arg["<new_room>"] person_name = arg["<first_name>"] + " " + arg["<second_name>"] print(self.amity.reallocate_person(person_name, new_room_name)) @docopt_cmd def do_load_people(self, arg): """ Loads people into the Amity system from a text file. Usage: load_people <filename> """ print(self.amity.load_people(arg["<filename>"])) @docopt_cmd def do_print_allocations(self, arg): """ Prints and outputs the people who have been successfully allocated a space per room. Usage: print_allocations <filename> """ filename = arg['<filename>'] print(self.amity.print_allocations(filename)) @docopt_cmd def do_print_unallocated(self, arg): """ Prints and outputs the people who have been not yet been allocated a room. Usage: print_unallocated <filename> """ filename = arg['<filename>'] print(self.amity.print_unallocated(filename)) @docopt_cmd def do_print_room(self, arg): """ Prints the people who have been allocated the specified room. Usage: print_room <room_name> """ room_name = arg['<room_name>'] print(self.amity.print_room(room_name)) @docopt_cmd def do_save_state(self, arg): """ Saves the data to a SQLite database Usage: save_state <db_name> """ db_name = arg["<db_name>"] print(self.amity.save_state(db_name)) @docopt_cmd def do_load_state(self, arg): """ Loads the data to a SQLite database Usage: load_state <db_name> """ db_name = arg["<db_name>"] print(self.amity.load_state(db_name)) def do_quit(self, arg): """Quits out of Interactive Mode.""" print('Till next time, Good Bye!') exit()
class AmityTest(unittest.TestCase): def setUp(self): self.amity = Amity() self.amity.room_types = { "office": {Office('MOMBASA'): ['Lavender Ayodi'], Office('HOGWARTS'): [], Office('LAGOS'): []}, "livingspace": {LivingSpace('KENYA'): [], LivingSpace('PLATFORM'): [], LivingSpace('VALHALLA'): []} } self.amity.persons = { 'fellow': [Fellow('Lavender Ayodi')], 'staff': [Staff('John Doe')] } def _create_rooms(self): room_types = { "office": {Office('MOMBASA'), Office('HOGWARTS'), Office('LAGOS')}, "livingspace": {LivingSpace('KENYA'), LivingSpace('PLATFORM'), LivingSpace('VALHALLA')} } keys = room_types.keys() for room in keys: for names in room_types.get(room): self.amity._assign_room_name(room, names) def test_create_room_without_name(self): room_created = self.amity.create_room("", 'livingspace') self.assertIn("Error", room_created) def test_create_room_without_type(self): room_created = self.amity.create_room("Platform", "") self.assertIn("Error", room_created) def test_room_created_exists(self): self.amity.create_room('Lagos', 'office') room_created = self.amity.create_room('Lagos', 'office') print (self.amity.rooms) self.assertIn("Error", room_created) def test_amity_room_object(self): rooms = self.amity.rooms.keys() self.assertEqual(len(rooms), 2) self.assertIn("office", rooms) self.assertIn("livingspace", rooms) def test_create_office(self): office = self.amity.create_room("Platform", "office") self.assertIn("Success", office) def test_create_livingspace(self): livingspace = self.amity.create_room("Rongai", "livingspace") print (livingspace) self.assertIn("Success", livingspace) def test_create_invalid(self): livingspace = self.amity.create_room("Rongai", "school") self.assertIn("Error", livingspace) def test_add_person_without_name(self): person_added = self.amity.add_person('', 'fellow', 'N') self.assertIn('Error', person_added) def test_add_person_exists(self): self._create_rooms() self.amity.add_person('LESLEY AYODI', 'staff', 'Y') person_added = self.amity.add_person('LESLEY AYODI', 'staff', 'Y') self.assertIn('Error', person_added) def test_add_person_to_room(self): self.amity.create_room('MOMBASA', 'office') room_allocated = self.amity.add_person('EMILY MBELENGA', 'fellow', 'N') self.assertIn("Done", room_allocated) def test_add_invalid_person_type(self): self.amity.create_room('MOMBASA', 'office') type_added = self.amity.add_person('LESLEY AYODI', 'cook', 'N') self.assertIn('Error', type_added) def test_invalid_room_type(self): self.amity.create_room('MOMBASA', 'office') self.amity.add_person('LESLEY AYODI', 'staff', 'N') room_occupants = self.amity._find_room_occupant('offic', 'MOMBASA') self.assertIn("Error", room_occupants) def test_invalid_room_name(self): self.amity.create_room('MOMBASA', 'office') self.amity.add_person('LESLEY AYODI', 'staff', 'N') room_occupants = self.amity._find_room_occupant('office', 'MOMBA') self.assertIn("Error", room_occupants) def test_reallocate_person(self): reallocate_person = self.amity.reallocate_person( "office", 'MOMBASA', 'HOGWARTS', 'Lavender Ayodi') self.assertIn("Success", reallocate_person) def test_reallocate_invalid_person(self): reallocate_person = self.amity.reallocate_person( "office", 'MOMBASA', 'LAGOS', 'ISABEL AYODI') self.assertIn('Error', reallocate_person) def test_reallocate_invalid_room(self): invalid_room = self.amity.reallocate_person( "office", 'MOMBASA', 'LAGOS', 'ISABEL AYODI') self.assertIn('Error', invalid_room) def test_full_room(self): self.amity.create_room('Lagos', 'office') self.amity.add_person('Lesley Ayodi', 'staff', 'N') self.amity.create_room('Mombasa', 'office') occupants = ['A', 'B', 'C', 'D', 'E', 'F'] office = self.amity.rooms.get('office') office['Mombasa'] = occupants reallocate_person = self.amity.reallocate_person( "office", 'Lagos', 'Mombasa', 'Lesley Ayodi') self.assertEqual(False, reallocate_person) def test_print_allocations(self): self.amity.add_person('Lesley Ayodi', 'staff', 'N') allocated_room = self.amity.print_allocations('room.txt') self.assertEqual(True, allocated_room) def test_print_allocations_terminal(self): self.amity.add_person('Lesley Ayodi', 'staff', 'N') allocated_room = self.amity.print_allocations('room.txt') self.assertEqual(True, allocated_room) def test_print_unallocated_persons(self): self.amity.add_person('LESLEY AYODI', 'fellow', 'Y') self.amity.add_person('LAVENDER AYODI', 'fellow', 'Y') self.amity.add_person('BRIAN MUTHUI', 'fellow', 'Y') self.amity.add_person('DENNIS MWANGI', 'fellow', 'Y') self.amity.add_person('MBARAK MBIGO', 'fellow', 'Y') self.amity.add_person('DENNIS YESWA', 'fellow', 'Y') self.amity.add_person('CYNTHIA ABURA', 'fellow', 'Y') unallocated = self.amity.print_unallocated('unallocated.txt') self.assertEqual(len(unallocated), 1) def test_print_room_occupants(self): self.amity.create_room('MOMBASA', 'office') self.amity.add_person('LESLEY AYODI', 'staff', 'N') self.amity.add_person('LAVENDER AYODI', 'fellow', 'N') self.amity.add_person('PATIENCE AYODI', 'staff', 'N') available_occupants = self.amity.print_room('office', 'MOMBASA') self.assertIn('Success', available_occupants) def test_load_people_from_file(self): persons_list = self.amity.persons.get('fellow') self.assertEqual(len(persons_list), 1) self.amity.load_people("people.txt") self.assertEqual(len(persons_list), 5) def test_save_state_works(self): saved_state = self.amity.save_state('test_amity.db') self.assertEqual(True, saved_state) def test_load_state_works(self): loaded_state = self.amity.load_state('test_amity.db') self.assertEqual(True, loaded_state)