class TestsForTask1PrintAllocations(unittest.TestCase): """Test cases for the print_allocations method and functionality """ def setUp(self): """Set up the test case class""" self.dojo = Dojo() def test_if_filename_is_too_short(self): """Tests to see if the file name passed to be written is to short to be a valid txt file """ file_name_entered = self.dojo.print_allocations("txt") self.assertEqual(file_name_entered, "short", msg="The file name entered is too short to be valid") def test_if_filename_does_not_end_with_txt(self): """ Tests if a file name entered is not a valid text file since it doesn't end with .txt :return: """ another_file_name = self.dojo.print_allocations("plain_text") self.assertEqual(another_file_name, "not txt file", msg="The file name entered doesn't end with .txt") def test_if_a_valid_file_name_is_used(self): """ Tests to see if a valid text file name is recognized and written to. check if the right assignments are returned :return: """ self.dojo.create_room("office", "red") self.dojo.add_person("allan", "muhwezi", "fellow", "y") valid_file = self.dojo.print_allocations("valid_file.txt") expected_string = "valid_file.txt" + ":" + "RED" + "\n" + "----------------------------------------------\n" + "allan muhwezi" + "\n\n" self.assertMultiLineEqual(valid_file, expected_string) def test_if_no_file_name_runs_successfully(self): """ Tests to see if when argument is provided, the function runs successfully but doesn't create a file. check to see if the correct assignments are retrieved :return: """ self.dojo.create_room("office", "red") self.dojo.add_person("allan", "muhwezi", "fellow", "y") valid_file = self.dojo.print_allocations() expected_string = "none:" + "RED" + "\n" + "----------------------------------------------\n" + "allan muhwezi" + "\n\n" self.assertMultiLineEqual(valid_file, expected_string) """TODO test like in print_room - same as - unallocated - load_people - add people to the text file. save_state: save and retrieve happen well.""" """Spread some functionality across the classes"""
class TestsForTask1PrintUnAllocated(unittest.TestCase): """Test cases for the print_allocations method and functionality """ def setUp(self): """Set up the test case class""" self.dojo = Dojo() def test_if_filename_is_too_short(self): """Tests to see if the file name passed to be written is to short to be a valid txt file """ file_name_entered = self.dojo.print_unallocated("txt") self.assertEqual(file_name_entered, "short", msg="The file name entered is too short to be valid") def test_if_filename_does_not_end_with_txt(self): """ Tests if a file name entered is not a valid text file since it doesn't end with .txt :return: """ another_file_name = self.dojo.print_unallocated("plain_text") self.assertEqual(another_file_name, "not txt file", msg="The file name entered doesn't end with .txt") def test_if_a_valid_file_name_is_used(self): """ Tests to see if a valid text file name is recognized and written to. check to see if the unallocated people are successfully retrieved :return: """ self.dojo.add_person("allan", "muhwezi", "fellow", "y") self.dojo.add_person("van", "bro", "fellow", "y") valid_file = self.dojo.print_unallocated("valid_file.txt") self.assertEqual(valid_file, "valid_file.txt" + ":" + "allan muhwezi" + "\n" + "van bro" + "\n") def test_if_no_file_name_runs_successfully(self): """ Tests to see if when argument is provided, the function runs successfully but doesn't create a file :return: """ self.dojo.add_person("allan", "muhwezi", "fellow", "y") self.dojo.add_person("van", "bro", "fellow", "y") no_file_name = self.dojo.print_unallocated() self.assertEqual(no_file_name, "none" + ":" + "allan muhwezi" + "\n" + "van bro" + "\n")
class TestsForTask2ReallocatePerson(unittest.TestCase): """ Test cases for the reallocate_room method and functionality """ def setUp(self): """Set up the test case class""" self.dojo = Dojo() def test_reallocate_to_no_non_existent_room(self): """" Test if reallocating to a room that doesn't exist fails with a message """ no_room = self.dojo.reallocate_person("one", "two", "non_existent") self.assertEqual(no_room, "no_room") def test_if_available_room_is_reallocated_to(self): """ Test if an available room to be reallocated to is actually found :return: """ self.dojo.create_room("livingspace", "my_room") self.dojo.reallocate_person("fName", "lName", "my_room") self.assertEqual(self.dojo.all_rooms[-1].room_name, "my_room") # the last entered room is found def test_reallocate_to_non_existent_person(self): """ Test if reallocating to a person who is not in the system fails :return: """ self.dojo.create_room("livingspace", "my_room") no_person = self.dojo.reallocate_person("romain", "guy", "my_room") self.assertEqual(no_person, "no_person", msg="Should return no_person") def test_reallocate_to_an_unallocated_person(self): """ Test if reallocating a person who has not been fully allocated before :return: """ self.dojo.create_room("livingspace", "my_space") self.dojo.add_person("welike", "elly", "fellow", "y") unallocated = self.dojo.reallocate_person("welike", "elly", "my_space") self.assertEqual(unallocated, "unallocated_person") def test_reallocate_an_allocated_person(self): """ Test if a fully allocated person is found and the room entered is also found :return: """ self.dojo.create_room("office", "my_office") self.dojo.add_person("john", "ike", "staff") self.dojo.reallocate_person("john", "ike", "my_office") self.assertEqual(self.dojo.all_people[-1].is_allocated, True) def test_cant_move_staff_to_living_space(self): """ Test if Staff can't be reallocated to a living space room :return: """ self.dojo.create_room("livingspace", "my_space") self.dojo.create_room("office", "my_office") self.dojo.add_person("john", "ike", "staff") no_living_space = self.dojo.reallocate_person("john", "ike", "my_space") self.assertEqual(no_living_space, "staff_no_living_space") def test_reallocate_to_the_same_room(self): """ Test if the room to reallocate to is the same as what the person is already assigned to :return: """ self.dojo.create_room("livingspace", "my_space") self.dojo.create_room("office", "my_office") self.dojo.add_person("john", "ike", "staff") same_room = self.dojo.reallocate_person("john", "ike", "my_office") self.assertEqual(same_room, "same_room") def test_if_person_has_been_moved_from_the_former_room(self): """ Test the number of occupants of the old room have been reduced by 1 :return: """ self.dojo.create_room("office", "my_first_office") self.dojo.add_person("john", "ike", "staff") first_office_occupants_number_before = len( self.dojo.all_rooms[0].occupants) self.dojo.create_room("office", "my_second_office") self.dojo.reallocate_person("john", "ike", "my_second_office") first_office_occupants_number_after = len( self.dojo.all_rooms[0].occupants) self.assertEqual( first_office_occupants_number_before - first_office_occupants_number_after, 1) def test_if_person_has_been_added_to_the_new_room(self): """ Test if the number of occupants of the room moved to has increased by 1 :return: """ self.dojo.create_room("office", "my_first_office") self.dojo.add_person("john", "ike", "staff") self.dojo.create_room("office", "my_second_office") second_office_occupants_number_before = len( self.dojo.all_rooms[1].occupants) self.dojo.reallocate_person("john", "ike", "my_second_office") second_office_occupants_number_after = len( self.dojo.all_rooms[1].occupants) self.assertEqual( second_office_occupants_number_after - second_office_occupants_number_before, 1) def test_if_function_runs_successfully_as_expected(self): """ Test if the expected strings are returned from each conditions :return: """ self.dojo.create_room("office", "my_first_office") self.dojo.add_person("john", "ike", "staff") self.dojo.create_room("office", "my_second_office") string_expected = self.dojo.reallocate_person("john", "ike", "my_second_office") self.assertEqual( string_expected, "my_second_office, john ike, staff, office, my_first_office")
class TestsForTask0AddPerson(unittest.TestCase): """Test cases for add_person functionalities """ def setUp(self): """Initialize the test case class""" self.dojo = Dojo() def test_add_person_successfully(self): """ Test if a person is added successfully using the add_person method in Dojo """ staff_one = self.dojo.add_person("Stanley", "Lee", "staff") self.assertTrue(staff_one) def test_new_person_added_to_list(self): """Test if the total people count increases after creating a person """ initial_people_count = len(self.dojo.all_people) self.dojo.add_person("Stan", "Lee", "staff") new_people_count = len(self.dojo.all_people) self.assertEqual(new_people_count - initial_people_count, 1) def test_duplicate_person_not_created(self): """Test that a person that has a name as an already created person is not created """ self.dojo.add_person("Stan", "Lee", "staff") staff_duplicate = self.dojo.add_person("Stan", "Lee", "staff") self.assertEqual(staff_duplicate, False) def test_if_person_is_assigned_an_office(self): """Test and see if a person is created when there is an available room, that person is assigned to the room """ self.dojo.create_room("livingspace", "sample") self.dojo.add_person("Jumped", "Out", "fellow", "Y") self.assertTrue(self.dojo.all_people[0].is_allocated) def test_person_with_unknown_person_type_not_created(self): """Test if a person with person type not fellow or staff is not added """ alien_person = self.dojo.add_person("Jim", "Kim", "star") self.assertFalse(alien_person) def test_staff_not_assigned_living_space(self): """Test if only a livingspace room is available and a staff is added, whether the staff will be assigned the livingspace room """ self.dojo.create_room("livingspace", "sample") self.dojo.add_person("Mr", "John", "staff") self.assertFalse(self.dojo.all_people[0].is_allocated)