class ReallocateTest(unittest.TestCase): """Test the dojo module""" def setUp(self): self.dojo = Dojo() def test_check_which_room_person_is(self): """Tests the method that checks which room a person is""" dict_a = { 'Mandela': ['David', 'Samuel', 'Turi'], 'Machel': ['Anne', 'Linet', 'Turi'] } dict_b = { 'Nyerere': ['Myles', 'Reginald', 'Booker'], 'Obote': ['Memo', 'Liliosa'] } self.assertEqual(self.dojo.check_which_room_person_is('Liliosa',\ dict_b), 'Obote') self.assertEqual(self.dojo.check_which_room_person_is('Francis',\ dict_a), False) def test_reallocate_person_removes_person(self): """Tests that a person has indeed been reallocated a room""" self.dojo.create_room('office', 'Mandela') self.dojo.create_room('office', 'Madiba') self.dojo.add_person('Joseph Simiyu', 'staff') if self.dojo.room_is_empty('Mandela'): self.dojo.reallocate_person('Joseph Simiyu', 'Mandela') self.assertEqual(len(self.dojo.dict_offices['Mandela']), 1) else: self.dojo.reallocate_person('Joseph Simiyu', 'Madiba') self.assertEqual(len(self.dojo.dict_offices['Madiba']), 1) def test_room_is_empty(self): """Tests the room_is_empty function""" self.dojo.create_room('office', 'Sama') self.dojo.add_person('Linda Masero', 'staff') self.assertFalse(self.dojo.room_is_empty('Sama')) def test_allocate_room(self): """Tests the allocate_room method""" self.dojo.add_person('John Doe', 'staff') self.dojo.add_person('Jane Duh', 'fellow', 'Y') self.dojo.create_room('office', 'Mandela') inital_count = len(self.dojo.unallocated_people) self.dojo.allocate_room('John Doe', 'office') current_count = len(self.dojo.unallocated_people) self.assertEqual((inital_count - current_count), 1) def test_allocate_missing_person(self): """Tests that missing person cannot be allocated""" self.dojo.create_room('living', 'Suswa') self.dojo.allocate_room('Babu Brian', 'living') output = "Living space Suswa created successfully"\ +"Babu Brian does not exist among the unallocated people" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output)
def test_reallocate_if_person_had_no_living_space(self): """test """ dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("living_space", "gorrilla") dojo.reallocate_person(1, "gorrilla") target_room = dojo.find_room("gorrilla") person = dojo.find_person(1) self.assertIn(person, target_room.residents)
def test_reallocate_if_person_had_no_office(self): """test """ dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("office", "orange") dojo.reallocate_person(1, "orange") target_room = dojo.find_room("orange") person = dojo.find_person(1) self.assertIn(person, target_room.residents)
def test_reallocate_person_no_office(self): """Tests reallocate if person had no office""" dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("office", "orange") dojo.reallocate_person(1, "orange") target_room = find_room(dojo.rooms, "orange") person = find_person(dojo.people, 1) self.assertIn(person, target_room.residents)
class DojoTestCases(unittest.TestCase): def setUp(self): self.dojo = Dojo() self.args = {'<room_type>': 'office', '<room_name>': ['blue']} self.red_args = {'<room_type>': 'office', '<room_name>': ['red']} self.person_args = { '<wants_accomodation>': 'Y', '<person_type>': 'staff', '<F_name>': 'Eva', '<L_name>': 'Maina' } def test_create_room_successfully(self): initial_office_count = len(self.dojo.rooms["offices"]) self.dojo.create_room(self.args) new_office_count = len(self.dojo.rooms["offices"]) self.assertEqual(new_office_count - initial_office_count, 1) def test_add_person_successfully(self): """Test add person""" self.dojo.create_room(self.red_args) self.dojo.add_person(self.person_args) self.assertEqual(len(self.dojo.people["staff"]), 1) def test_allocated_room_successfully(self): """Test add person""" self.dojo.create_room(self.red_args) self.dojo.add_person(self.person_args) self.assertEqual(len(self.dojo.people["with_offices"]), 1) def test_unallocated_room_successfully(self): """Test add person""" self.dojo.add_person(self.person_args) self.assertEqual(len(self.dojo.people["without_offices"]), 1) def test_reallocate_person(self): self.dojo.create_room(self.args) self.dojo.add_person(self.person_args) self.assertEqual(self.dojo.rooms["offices"][0].room_name, 'blue') self.assertEqual(len(self.dojo.people["staff"]), 1) self.dojo.create_room(self.red_args) self.assertEqual(len(self.dojo.rooms), 2) person_id = self.dojo.people["staff"][0].person_id new_room = 'red' # Relocation self.dojo.reallocate_person(person_id, new_room) self.assertEqual(self.dojo.rooms["offices"][1].room_name, 'red')
def test_transfer_of_person_on_reallocate(self): """Tests that correct information is printed on print_allocations""" dojo = Dojo() test_office = dojo.create_room("office", "testoffice") another_test_office = dojo.create_room("office", "orange") dojo.add_person("Neil", "Armstrong", "Staff", "Y") person = dojo.all_people[0] old_office = [elem['office']\ for elem in person.rooms_occupied if 'office' in elem] result1 = dojo.print_room(old_office[0]) self.assertIn("Neil Armstrong", result1) un_occupied_room = test_office if not test_office.residents else another_test_office print(un_occupied_room.room_name) dojo.reallocate_person(1, un_occupied_room.room_name) result2 = dojo.print_room(old_office[0]) self.assertNotIn("Neil Armstrong", result2)
class TestSpaceAllocator(unittest.TestCase): """class """ def setUp(self): self.dojo = Dojo() self.testOffice = self.dojo.create_room("office", "testOffice") self.testLivingSpace = self.dojo.create_room("living_space", "testLivingSpace") def test_create_room_successfully(self): initial_room_count = len(self.dojo.all_rooms) blue_office = self.dojo.create_room("office", "Blue") self.assertTrue(blue_office) new_room_count = len(self.dojo.all_rooms) self.assertEqual(new_room_count - initial_room_count, 1) def test_create_rooms_successfully(self): initial_room_count = len(self.dojo.all_rooms) offices = self.dojo.create_room("office", "Blue", "Black", "Brown") self.assertTrue(offices) new_room_count = len(self.dojo.all_rooms) self.assertEqual(new_room_count - initial_room_count, 3) def test_addition_of_duplicate_room_names(self): initial_room_count = len(self.dojo.all_people) new_room_count = len(self.dojo.all_people) self.assertEqual(new_room_count - initial_room_count, 0) def test_person_added_to_system(self): initial_person_count = len(self.dojo.all_people) person = self.dojo.add_person("Neil", "Armstrong", "Staff") self.assertTrue(person) new_person_count = len(self.dojo.all_people) self.assertEqual(new_person_count - initial_person_count, 1) def test_person_has_been_assigned_office(self): person = self.dojo.add_person("Neil", "Armstrong", "Staff") self.assertTrue(person) self.assertTrue(self.dojo.all_people[-1].has_office) def test_person_has_been_assigned_living_space(self): person = self.dojo.add_person("Eden", "Hazard", "Fellow", "Y") self.assertTrue(person) self.assertTrue(self.dojo.all_people[-1].has_living_space) def test_return_type_of_add_person(self): person = self.dojo.add_person("Eden", "Hazard", "Fellow", "Y") self.assertEqual( {'Person': 'Eden Hazard', 'Rooms': [{'office': 'testOffice'}, \ {'living_space': 'testLivingSpace'}]}, person) def test_that_maximum_no_of_people_is_not_exceeded(self): self.dojo.add_person("Neil", "Armstrong", "Staff", "Y") self.dojo.add_person("Harry", "Kane", "Fellow", "Y") self.dojo.add_person("Eden", "Hazard", "Staff", "Y") self.dojo.add_person("Ngolo", "Kante", "Staff", "Y") self.dojo.add_person("Eric", "Dier", "Staff", "Y") self.dojo.add_person("Dele", "Ali", "Fellow", "Y") self.dojo.add_person("Diego", "Costa", "Fellow", "Y") self.dojo.add_person("Willian", "Borges", "Staff", "Y") self.dojo.add_person("Tibaut", "Courtois", "Fellow", "Y") self.assertEqual(len(self.testOffice.residents), 6) def test_output_of_print_room(self): self.dojo.add_person("Neil", "Armstrong", "Staff", "Y") self.dojo.add_person("Harry", "Kane", "Fellow", "Y") self.dojo.add_person("Eden", "Hazard", "Staff", "Y") self.dojo.add_person("Ngolo", "Kante", "Staff", "Y") self.dojo.add_person("Eric", "Dier", "Staff", "Y") self.dojo.add_person("Dele", "Ali", "Fellow", "Y") self.dojo.add_person("Diego", "Costa", "Fellow", "Y") self.dojo.add_person("Willian", "Borges", "Staff", "Y") self.dojo.add_person("Tibaut", "Courtois", "Fellow", "Y") result = self.dojo.print_room("testOffice") non_existent_room = self.dojo.print_room("test room") self.assertEqual( ['Neil Armstrong', 'Harry Kane', 'Eden Hazard', 'Ngolo Kante',\ 'Eric Dier', 'Dele Ali'], result) self.assertFalse(non_existent_room) def test_print_room_for_reallocated_people(self): self.dojo.create_room("office", "orange") self.dojo.add_person("Neil", "Armstrong", "Staff", "Y") result1 = self.dojo.print_room("testOffice") self.assertIn("Neil Armstrong", result1) self.dojo.reallocate_person(1, "orange") result2 = self.dojo.print_room("testOffice") self.assertNotIn("Neil Armstrong", result2) def test_correct_output_on_print_allocations(self): self.dojo.add_person("Dele", "Ali", "Fellow", "Y") result = self.dojo.print_allocations() print(result) self.assertEqual([{ 'testOffice': ['DELE ALI'] }, { 'testLivingSpace': ['DELE ALI'] }], result) def test_print_allocations_on_file(self): """Tests that correct output is written to the file """ self.dojo.add_person("Dele", "Ali", "Fellow", "Y") result = self.dojo.print_allocations("allocations.txt", "N") file = open("allocations.txt").read() self.assertTrue("Room: testOffice" in file) self.assertTrue("DELE ALI" in file) self.assertTrue("Room: testLivingSpace" in file) self.assertEqual([{ 'testOffice': ['DELE ALI'] }, { 'testLivingSpace': ['DELE ALI'] }], result) def test_tabular_output_on_print_allocations(self): """Tests that print_allocations output is tabular """ #Create StringIO object and redirect output self.dojo.add_person("Dele", "Ali", "Fellow", "Y") program_captured_output = io.StringIO() sys.stdout = program_captured_output self.dojo.print_allocations("", "Y") sys.stdout = sys.__stdout__ table = PrettyTable(['Name', 'Type', 'Office', 'Living Space']) table.add_row(["Dele Ali", "fellow", "testOffice", "testLivingSpace"]) captured_output = io.StringIO() sys.stdout = captured_output print( colorful.blue( "List showing people with space and their respective rooms")) print(colorful.blue(table)) sys.stdout = sys.__stdout__ print(program_captured_output.getvalue().strip()) print(captured_output.getvalue()) self.assertTrue(captured_output.getvalue().strip() in program_captured_output.getvalue().strip()) def test_correct_output_on_print_unallocated(self): """test """ dojo = Dojo() dojo.add_person("Kylian", "Mbappe", "Fellow", "Y") # dojo.create_room("living_space", "zebra") # dojo.add_person("Gonzalo", "Higuan", "Fellow", "Y") dojo.add_person("Gianluggi", "Buffon", "Fellow", "N") dojo.create_room("office", "red") dojo.add_person("Timoue", "Bakayoko", "Fellow", "Y") program_captured_output = io.StringIO() sys.stdout = program_captured_output dojo.print_unallocated() sys.stdout = sys.__stdout__ table = PrettyTable(['Name', 'Person id', 'Missing']) table.add_row(["Kylian Mbappe", "1", "Office and Living Space"]) table.add_row(["Gianluggi Buffon", "2", "Office"]) table.add_row(["Timoue Bakayoko", "3", "Living Space"]) captured_output = io.StringIO() sys.stdout = captured_output print(colorful.blue("Table showing people along with missing rooms")) print(colorful.blue(table)) sys.stdout = sys.__stdout__ print(program_captured_output.getvalue().strip()) print(captured_output.getvalue()) self.assertTrue(captured_output.getvalue().strip() in program_captured_output.getvalue().strip()) def test_reallocate_if_person_had_no_office(self): """test """ dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("office", "orange") dojo.reallocate_person(1, "orange") target_room = dojo.find_room("orange") person = dojo.find_person(1) self.assertIn(person, target_room.residents) def test_reallocate_if_person_had_no_living_space(self): """test """ dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("living_space", "gorrilla") dojo.reallocate_person(1, "gorrilla") target_room = dojo.find_room("gorrilla") person = dojo.find_person(1) self.assertIn(person, target_room.residents) def test_person_exists_after_load_people(self): """test """ self.dojo.load_people("people.txt") last_person = self.dojo.find_person(7) self.assertIn(last_person, self.dojo.all_people) def test_person_exists_in_target_room_after_reallocation(self): """test """ self.dojo.create_room("office", "orange") self.dojo.create_room("living_space", "lion") self.dojo.add_person("John", "Ashaba", "Fellow", "Y") result1 = self.dojo.print_room("testOffice") result2 = self.dojo.print_room("testLivingSpace") self.assertIn("John Ashaba", result1) self.assertIn("John Ashaba", result2) self.dojo.reallocate_person(1, "orange") self.dojo.reallocate_person(1, "lion") target_office_room = self.dojo.find_room("orange") target_living_room = self.dojo.find_room("orange") person = self.dojo.find_person(1) self.assertIn(person, target_office_room.residents) self.assertIn(person, target_living_room.residents) def test_persists_data(self): """test """ dojo1 = Dojo() dojo1.create_room("office", "orange") dojo1.add_person("John", "Ashaba", "Staff", "Y") dojo1.save_state("mydb.db") dojo2 = Dojo() dojo2.load_state("mydb.db") room = dojo2.find_room("orange") self.assertIn(room, dojo2.all_rooms)
class TestSpaceAllocator(unittest.TestCase): """ Tests""" def setUp(self): """ Initial test setup""" self.dojo = Dojo() self.testoffice = self.dojo.create_room("office", "testoffice") self.testlivingspace = self.dojo.create_room("living_space", "testlivingspace") def test_create_room_successfully(self): """Tests that a room is created successfully""" initial_room_count = len(self.dojo.all_rooms) blue_office = self.dojo.create_room("office", "Blue") self.assertTrue(blue_office) new_room_count = len(self.dojo.all_rooms) self.assertEqual(new_room_count - initial_room_count, 1) def test_create_rooms_successfully(self): """Tests that multiple rooms are created at a single time successfully""" initial_room_count = len(self.dojo.all_rooms) offices = self.dojo.create_room("office", "Blue", "Black", "Brown") self.assertTrue(offices) new_room_count = len(self.dojo.all_rooms) self.assertEqual(new_room_count - initial_room_count, 3) def test_create_duplicate_rooms(self): """Tests that duplicate rooms are not created""" initial_room_count = len(self.dojo.all_people) self.testoffice = self.dojo.create_room("office", "testoffice") new_room_count = len(self.dojo.all_people) self.assertEqual(new_room_count - initial_room_count, 0) def test_add_person_to_system(self): """Test that person is added to the system""" initial_person_count = len(self.dojo.all_people) person = self.dojo.add_person("Neil", "Armstrong", "Staff") self.assertTrue(person) new_person_count = len(self.dojo.all_people) self.assertEqual(new_person_count - initial_person_count, 1) def test_assign_office_to_person(self): """Test that a person is assigned an office""" person = self.dojo.add_person("Neil", "Armstrong", "Staff") self.assertTrue(person) self.assertTrue(self.dojo.all_people[-1].has_office) def test_assign_living_space_to_person(self): """Test that person is assigned a living space""" person = self.dojo.add_person("Eden", "Hazard", "Fellow", "Y") self.assertTrue(person) self.assertTrue(self.dojo.all_people[-1].has_living_space) def test_add_person_return_type(self): """Tests the return type of method add_person""" person = self.dojo.add_person("Eden", "Hazard", "Fellow", "Y") self.assertEqual( {'Person': 'Eden Hazard', 'Rooms': [{'office': 'testoffice'}, \ {'living_space': 'testlivingspace'}]}, person) def test_that_maximum_no_of_people_is_not_exceeded(self): """Tests that the maximum number of people is not exceeded""" self.dojo.add_person("Neil", "Armstrong", "Staff", "Y") self.dojo.add_person("Harry", "Kane", "Fellow", "Y") self.dojo.add_person("Eden", "Hazard", "Staff", "Y") self.dojo.add_person("Ngolo", "Kante", "Staff", "Y") self.dojo.add_person("Eric", "Dier", "Staff", "Y") self.dojo.add_person("Dele", "Ali", "Fellow", "Y") self.dojo.add_person("Diego", "Costa", "Fellow", "Y") self.dojo.add_person("Willian", "Borges", "Staff", "Y") self.dojo.add_person("Tibaut", "Courtois", "Fellow", "Y") self.assertEqual(len(self.testoffice.residents), 6) def test_print_room_output(self): """Tests the output of print_room""" self.dojo.add_person("Neil", "Armstrong", "Staff", "Y") self.dojo.add_person("Harry", "Kane", "Fellow", "Y") self.dojo.add_person("Eden", "Hazard", "Staff", "Y") self.dojo.add_person("Ngolo", "Kante", "Staff", "Y") self.dojo.add_person("Eric", "Dier", "Staff", "Y") self.dojo.add_person("Dele", "Ali", "Fellow", "Y") self.dojo.add_person("Diego", "Costa", "Fellow", "Y") self.dojo.add_person("Willian", "Borges", "Staff", "Y") self.dojo.add_person("Tibaut", "Courtois", "Fellow", "Y") result = self.dojo.print_room("testoffice") non_existent_room = self.dojo.print_room("test room") self.assertEqual( ['Neil Armstrong', 'Harry Kane', 'Eden Hazard', 'Ngolo Kante',\ 'Eric Dier', 'Dele Ali'], result) self.assertFalse(non_existent_room) def test_transfer_of_person_on_reallocate(self): """Tests that correct information is printed on print_allocations""" dojo = Dojo() test_office = dojo.create_room("office", "testoffice") another_test_office = dojo.create_room("office", "orange") dojo.add_person("Neil", "Armstrong", "Staff", "Y") person = dojo.all_people[0] old_office = [elem['office']\ for elem in person.rooms_occupied if 'office' in elem] result1 = dojo.print_room(old_office[0]) self.assertIn("Neil Armstrong", result1) un_occupied_room = test_office if not test_office.residents else another_test_office print(un_occupied_room.room_name) dojo.reallocate_person(1, un_occupied_room.room_name) result2 = dojo.print_room(old_office[0]) self.assertNotIn("Neil Armstrong", result2) def test_output_of_print_allocations(self): """Tests the output of print_allocations""" self.dojo.add_person("Dele", "Ali", "Fellow", "Y") result = self.dojo.print_allocations() print(result) self.assertEqual([{ 'testoffice': ['DELE ALI'] }, { 'testlivingspace': ['DELE ALI'] }], result) def test_print_allocations_on_file(self): """Tests that correct output is written to the file """ self.dojo.add_person("Dele", "Ali", "Fellow", "Y") result = self.dojo.print_allocations("allocations.txt", "N") file = open("allocations.txt").read() self.assertTrue("Room: testoffice" in file) self.assertTrue("DELE ALI" in file) self.assertTrue("Room: testlivingspace" in file) self.assertEqual([{ 'testoffice': ['DELE ALI'] }, { 'testlivingspace': ['DELE ALI'] }], result) def test_tabular_output_on_print_allocations(self): """Tests that tabular data is output on print_allocations""" #Create StringIO object and redirect output self.dojo.add_person("Dele", "Ali", "Fellow", "Y") program_captured_output = io.StringIO() sys.stdout = program_captured_output self.dojo.print_allocations("", "Y") sys.stdout = sys.__stdout__ table = PrettyTable(['Name', 'Type', 'Office', 'Living Space']) table.add_row(["Dele Ali", "fellow", "testoffice", "testlivingspace"]) captured_output = io.StringIO() sys.stdout = captured_output print( colorful.blue( "List showing people with space and their respective rooms")) print(colorful.blue(table)) sys.stdout = sys.__stdout__ print(program_captured_output.getvalue().strip()) print(captured_output.getvalue()) self.assertTrue(captured_output.getvalue().strip() in program_captured_output.getvalue().strip()) def test_tabular_output_on_print_unallocated(self): """Tests that tabular data is output on test_tabular_output_on_print_unallocated""" dojo = Dojo() dojo.add_person("Kylian", "Mbappe", "Fellow", "Y") dojo.add_person("Gianluggi", "Buffon", "Fellow", "N") dojo.create_room("office", "red") dojo.add_person("Timoue", "Bakayoko", "Fellow", "Y") program_captured_output = io.StringIO() sys.stdout = program_captured_output dojo.print_unallocated() sys.stdout = sys.__stdout__ table = PrettyTable(['Name', 'Person id', 'Missing']) table.add_row(["Kylian Mbappe", "1", "Office and Living Space"]) table.add_row(["Gianluggi Buffon", "2", "Office"]) table.add_row(["Timoue Bakayoko", "3", "Living Space"]) captured_output = io.StringIO() sys.stdout = captured_output print(colorful.blue("Table showing people along with missing rooms")) print(colorful.blue(table)) sys.stdout = sys.__stdout__ print(program_captured_output.getvalue().strip()) print(captured_output.getvalue()) self.assertTrue(captured_output.getvalue().strip() in program_captured_output.getvalue().strip()) def test_reallocate_if_person_had_no_office(self): """Tests reallocate if person had no office""" dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("office", "orange") dojo.reallocate_person(1, "orange") target_room = dojo.find_room("orange") person = dojo.find_person(1) self.assertIn(person, target_room.residents) def test_reallocate_if_person_had_no_living_space(self): """Tests reallocate if person had no living space""" dojo = Dojo() dojo.add_person("John", "Ashaba", "Staff", "Y") dojo.create_room("living_space", "gorrilla") dojo.reallocate_person(1, "gorrilla") target_room = dojo.find_room("gorrilla") person = dojo.find_person(1) self.assertIn(person, target_room.residents) def test_that_person_exists_after_load_people(self): """Tests that person exists after load_people""" self.dojo.load_people("people.txt") last_person = self.dojo.find_person(7) self.assertIn(last_person, self.dojo.all_people) def test_if_person_exists_in_target_room_after_reallocation(self): """Tests that person exists after reallocation""" self.dojo.create_room("office", "orange") self.dojo.create_room("living_space", "lion") self.dojo.add_person("John", "Ashaba", "Fellow", "Y") person = self.dojo.all_people[0] old_office = [elem['office']\ for elem in person.rooms_occupied if 'office' in elem] old_living_space = [ elem['living_space'] \ for elem in person.rooms_occupied if 'living_space' in elem] result1 = self.dojo.print_room(old_office[0]) result2 = self.dojo.print_room(old_living_space[0]) self.assertIn("John Ashaba", result1) self.assertIn("John Ashaba", result2) self.dojo.reallocate_person(1, "orange") self.dojo.reallocate_person(1, "lion") target_office_room = self.dojo.find_room("orange") target_living_room = self.dojo.find_room("orange") person = self.dojo.find_person(1) self.assertIn(person, target_office_room.residents) self.assertIn(person, target_living_room.residents) def test_persists_data(self): """Tests that the application persists data""" dojo1 = Dojo() dojo1.create_room("office", "orange") dojo1.add_person("John", "Ashaba", "Staff", "Y") dojo1.save_state("mydb.db") dojo2 = Dojo() dojo2.load_state("mydb.db") room = dojo2.find_room("orange") self.assertIn(room, dojo2.all_rooms)