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 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_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_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_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 TestHello(TestCase): def setUp(self): self.dojo = Dojo() def test_instance(self): self.assertIsInstance(self.dojo, Dojo) def test_command(self): text = self.dojo.getText('some text') self.assertEqual(text, 'some text')
class PrintDojo(cmd.Cmd): prompt = "~>" # instead of printing '(Cmd)' it will print '~>' print(__doc__) # this will print the comments from line 1 to 9 above dojo_instance = Dojo() # creates an instance our our class Dojo() @app_exec # same decorator defined above ~ line 15 def do_dojo(self, arg): # the method has to start with 'do_' """Prints the command Usage: dojo <commands>... """ argument_entered = arg["<commands>"] # command entered after dojo """Our instance of class Dojo() is calling method getText""" result = self.dojo_instance.getText(argument_entered) print(result) @app_exec def do_q(self, arg): """Exits the app. Usage: q """ exit()
class DojoCli(cmd.Cmd): prompt = "DojoApp=>" dojo = Dojo() @app_exec def do_create_room(self, arg): """Creates a new room Usage: create_room <room_type> <room_name> ... """ self.dojo.create_room(arg) @app_exec def do_add_person(self, arg): """adds a new person Usage: add_person <F_name> <L_name> <person_type> [<wants_accomodation>] """ self.dojo.add_person(arg) @app_exec def do_print_room(self, args): """Print a room Usage: print_room <room_name>... """ self.dojo.print_room(args) @app_exec def do_print_allocations(self, arg): """Print a room Usage: print_allocation [<o>] """ self.dojo.print_allocations(arg) @app_exec def do_print_unallocated(self, args): """prints unallocted rooms Usage: print_unallocated [<o>] """ self.dojo.print_unallocated(args) @app_exec def do_reallocate_person(self, args): """ Usage: reallocate_person <person_id> <room_name> """ p_id = int(args["<person_id>"]) r_name = args["<room_name>"] self.dojo.reallocate_person(p_id, r_name) @app_exec def do_load_people(self, args): """ loads people Usage: load_people """ self.dojo.load_people(args) @app_exec def do_quit(self, arg): """usage: Exits the app"""
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)
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)
def test_example(self): dojo = Dojo() self.assertEqual(dojo.get_random_number(), 4)
class TheDojo(cmd.Cmd): # Print the_dojo graphic print(colored("****** " + " ** ** " + " ****** " + " " + "*** " + " **** " \ + " ** " + " **** ", 'blue')) print(colored("****** " + " ** ** " + " ****** " + " " + "***** " + " ****** " \ + " ** " + " ****** ", 'blue')) print(" ** " + " ** ** " + " ** " + " " + "** ** " + " ** ** " \ + " ** " + " ** ** ") print(colored(" ** " + " ****** " + " ***** " + " " + "** ** " + " ** ** " \ + " ** " + " ** ** ", 'red')) print(colored(" ** " + " ****** " + " ***** " + " " + "** ** " + " ** ** " \ + " ** ** " + " ** ** ", 'red')) print(" ** " + " ** ** " + " ** " + " " + "** ** " + " ** ** " \ + " ** ** " + " ** ** ") print(colored(" ** " + " ** ** " + " ****** " + " " + "***** " + " ****** " \ + " ****** " + " ****** ", 'green')) print(colored(" ** " + " ** ** " + " ****** " + " " + "*** " + " **** " \ + " **** " + " **** ", 'green')) prompt = '[dojo]>> ' dojo = Dojo() @the_dojo_docopt def do_add_person(self, args): """ Usage: add_person <first_name> <second_name> <person_type> [<wants_accommodation>] Options: first_name First name of the person to be added second_name Second name of the person to be added wants_accommodation Whether person wants accommodation or not. [default: N] """ if args['<first_name>'].isalpha() and args['<second_name>'].isalpha(): person_name = '{} {}'.format(args['<first_name>'], \ args['<second_name>']) if args['<person_type>'].lower() == 'staff' \ and args['<wants_accommodation>'] is not None: print(colored("Staff cannot be allocated living spaces", 'red')) elif args['<person_type>'].lower() == 'staff' \ and args['<wants_accommodation>'] is None: self.dojo.add_person(person_name, args['<person_type>']) elif args['<person_type>'].lower() == 'fellow' \ and args['<wants_accommodation>'] is None: self.dojo.add_person(person_name, args['<person_type>']) elif args['<person_type>'].lower() == 'fellow' \ and args['<wants_accommodation>'] == 'Y': self.dojo.add_person(person_name, args['<person_type>'], \ args['<wants_accommodation>']) elif args['<person_type>'].lower() == 'fellow' \ and args['<wants_accommodation>'] == 'y': self.dojo.add_person(person_name, args['<person_type>'], \ args['<wants_accommodation>']) elif args['<person_type>'].lower() == 'fellow' \ and args['<wants_accommodation>'] == 'N': self.dojo.add_person(person_name, args['<person_type>'], \ args['<wants_accommodation>']) elif args['<person_type>'].lower() == 'fellow' \ and args['<wants_accommodation>'] == 'n': self.dojo.add_person(person_name, args['<person_type>'], \ args['<wants_accommodation>']) else: print(colored("Wrong inputs entered." \ + "Type 'help add_person' for help", 'red')) else: print( colored( "Person name can only contain aplhabets. Please try again", 'red')) @the_dojo_docopt def do_create_room(self, args): """ Usage: create_room <room_type> <room_name> ... Options: person_name Name of the person to be created wants_accommodation Whether person wants accommodation or not. [default: N] """ if args['<room_type>'].lower() == 'office' or args['<room_type>']\ .lower() == 'living': for name in args['<room_name>']: if name.isalpha(): self.dojo.create_room(args['<room_type>'], name) else: print(colored("Room "+name+" not created. Ensure that "\ +"it only contains alphabets", 'red')) else: print(colored("Wrong room type entered", 'red')) @the_dojo_docopt def do_print_room(self, args): """ Usage: print_room <room_name> Options: room_name Name of the room whose occupants to list """ self.dojo.print_room(args['<room_name>']) @the_dojo_docopt def do_print_allocations(self, arg_o): """ Usage: print_allocations [-o] Options: -o Save to a file or not [default:filename] """ if arg_o["-o"]: self.dojo.print_allocations('-o') else: self.dojo.print_allocations() @the_dojo_docopt def do_print_unallocated(self, arg_o): """ Usage: print_unallocated [-o] Options: -o Save to a file or not [default:filename] """ if arg_o["-o"]: self.dojo.print_unallocated('-o') else: self.dojo.print_unallocated() @the_dojo_docopt def do_reallocate_person(self, args): """ Usage: reallocate_person <first_name> <second_name> <new_room_name> Options: new_room_name Name of the room to transfer person to. """ person_name = '{} {}'.format(args['<first_name>'], \ args['<second_name>']) self.dojo.reallocate_person(person_name, args['<new_room_name>']) @the_dojo_docopt def do_allocate_room(self, args): """ Usage: allocate_room <room_type> <first_name> <second_name> """ person_name = '{} {}'.format(args['<first_name>'], \ args['<second_name>']) self.dojo.allocate_room(person_name, args['<room_type>']) @the_dojo_docopt def do_load_people(self, args): """ Usage: load_people """ self.dojo.load_people() def do_quit(self, args): """Quits the_dojo""" print(colored("Thank you for using the_dojo. Goodbye!", 'yellow')) exit()
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)
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") if os.path.exists("resources/testdb.db"): os.remove("resources/testdb.db") dojo1.save_state("testdb.db") dojo2 = Dojo() dojo2.load_state("resources/testdb.db") room = find_room(dojo2.rooms, "orange") self.assertIn(room, dojo2.rooms)
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())
class DojoTest(unittest.TestCase): """Test the dojo module""" def setUp(self): self.dojo = Dojo() def test_default_want_accommodation_for_fellow(self): """Test default value for Fellow wants accommodation""" fellow2 = Fellow('Omingo') self.assertEqual('N', fellow2.wants_accommodation) def test_living_object_type(self): """Test the type of LivingSpace class object""" living3 = LivingSpace('room4') self.assertTrue(isinstance(living3, LivingSpace)) def test_office_object_type(self): """Test the type of Office class object""" office3 = LivingSpace('room4') self.assertTrue(isinstance(office3, LivingSpace)) def test_create_room_successfully(self): """Tests that a new room is successfully created""" initial_office_count = len(self.dojo.all_offices) self.dojo.create_room("office", "Turquoise") new_office_count = len(self.dojo.all_offices) self.assertEqual(new_office_count - initial_office_count, 1) def test_cannot_create_dup_office(self): """Tests that duplicate office cannot be added""" self.dojo.create_room("office", "Grey") self.dojo.create_room("office", "Grey") output = "Office Grey created successfullyOffice already exists."\ " Please try using a different name" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_cannot_create_dup_livingspace(self): """Tests that duplicate living space cannot be added""" self.dojo.create_room("living", "Grey") self.dojo.create_room("living", "Grey") output = "Living space Grey created successfullyLiving space already exists."\ " Please try using a different name" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_cannot_create_dup_fellow(self): """Tests that duplicate fellow cannot be added""" self.dojo.add_person("Peter Alask", "fellow") self.dojo.add_person("Peter Alask", "fellow") output = "Peter Alask has been added as a FellowThere is no available"\ +" office to add Peter Alask. Create one firstFellow already exists!" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_cannot_create_dup_staff(self): """Tests that duplicate staff cannot be added""" self.dojo.add_person("Pet Alaska", "staff") self.dojo.add_person("Pet Alaska", "staff") output = "Pet Alaska has been added as a StaffThere is no available "\ +"office to add Pet Alaska. Create one firstStaff already exists!" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_person_staff_or_fellow(self): """Tests that only staff or office is entered as the person type""" self.dojo.add_person('John Doe', 'officer') output = "Wrong person type entered. Please try again" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_add_staff_without_available_office(self): """Tests what is printed when staff is added with no office""" self.dojo.add_person('John Doe', 'staff') output = "John Doe has been added as a Staff"\ +"There is no available office to add John Doe. Create one first" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_add_fellow_without_available_office(self): """Tests what is printed when fellow is added with no office""" self.dojo.add_person('John Doe', 'fellow') output = "John Doe has been added as a Fellow"\ +"There is no available office to add John Doe. Create one first" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_add_fellow_without_available_living(self): """Tests what is printed when fellow is added with no living space""" self.dojo.add_person('Jane Doe', 'fellow', 'Y') output = "Jane Doe has been added as a FellowThere is no available"\ +" office to add Jane Doe. Create one firstThere is no available "\ +"living space to add Jane Doe. Create one first" self.assertEqual(re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~][\n]*', '', \ sys.stdout.getvalue()), output) def test_check_available_space(self): """Tests that any room has available space""" dict_a = { 'red': ['Joan', 'Anne', 'Beatrice'], 'blue': ['Joy', 'Irene'] } self.assertTrue(self.dojo.check_available_space(dict_a, 3))
def setUp(self): self.dojo = Dojo()
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)
def setUp(self): self.dojo = Dojo() self.testOffice = self.dojo.create_room("office", "testOffice") self.testLivingSpace = self.dojo.create_room("living_space", "testLivingSpace")
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')