コード例 #1
0
class RoomClassTest(unittest.TestCase):
    def setUp(self):
        self.my_class_instance = Room("Blue", "office", 4)
        self.my_dojo_instance = Dojo()

    def test_add_occupant(self):
        self.my_dojo_instance.add_person("fellow", "Peter", "Musonye", "Y")
        new_fellow = self.my_dojo_instance.all_persons[-1]
        initial_persons_count = len(self.my_class_instance.persons)
        self.my_class_instance.add_occupant(new_fellow)
        new_persons_count = len(self.my_class_instance.persons)
        self.assertEqual(new_persons_count - initial_persons_count, 1)

    def test_cannot_add_more_than_max_occupants(self):
        self.my_dojo_instance.add_person("fellow", "Peter", "Musonye", "Y")
        fellow_1 = self.my_dojo_instance.all_persons[-1]
        self.my_dojo_instance.add_person("fellow", "Farhan", "Abdi")
        fellow_2 = self.my_dojo_instance.all_persons[-1]
        self.my_dojo_instance.add_person("fellow", "Rose", "Maina", "Y")
        fellow_3 = self.my_dojo_instance.all_persons[-1]
        self.my_dojo_instance.add_person("fellow", "Dennis", "Kola", "Y")
        fellow_4 = self.my_dojo_instance.all_persons[-1]
        self.my_dojo_instance.add_person("fellow", "Eddy", "Karanja", "Y")
        fellow_5 = self.my_dojo_instance.all_persons[-1]
        self.my_class_instance.add_occupant(fellow_1)
        self.my_class_instance.add_occupant(fellow_2)
        self.my_class_instance.add_occupant(fellow_3)
        self.my_class_instance.add_occupant(fellow_4)
        with self.assertRaises(Exception,
                               msg="Office Blue is at full capacity"):
            self.my_class_instance.add_occupant(fellow_5)
コード例 #2
0
 def setUp(self):
     self.my_class_instance = Room("Blue", "office", 4)
     self.my_dojo_instance = Dojo()
コード例 #3
0
 def setUp(self):
     self.dojo = Dojo()
コード例 #4
0
class TestClassDojo(unittest.TestCase):
    def setUp(self):
        self.dojo = Dojo()

    def test_create_offices_successfully(self):
        office_name_list = ['blue', 'red']
        self.dojo.create_room(office_name_list, 'office')
        size = len(self.dojo.all_offices)
        self.assertEqual(self.dojo.all_offices[size - 1].name, 'red')
        self.assertEqual(self.dojo.all_offices[size - 2].name, 'blue')

    def test_create_living_space_successfully(self):
        living_space_name_list = ['Apple', 'Mango']
        self.dojo.create_room(living_space_name_list, 'living_space')
        size = len(self.dojo.all_living_spaces)
        self.assertEqual(self.dojo.all_living_spaces[size - 1].name, 'Mango')
        self.assertEqual(self.dojo.all_living_spaces[size - 2].name, 'Apple')

    def tests_get_available_office(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.all_offices[0].spaces = 0
        self.assertFalse(self.dojo.get_available_office())

    def tests_get_available_office_with_no_space(self):
        office_name_list = ['blue', 'red']
        self.dojo.create_room(office_name_list, 'office')
        self.assertTrue(self.dojo.get_available_office(), True)

    def tests_add_person_to_room_type_living_space(self):
        room_name_list = ['blue']
        self.dojo.create_room(room_name_list, 'living_space')
        fellow = Fellow('Patrick')
        room = self.dojo.get_available_living_spaces()
        self.dojo.add_person_to_room(fellow, room)
        self.assertEqual(room.occupants[0].name, 'Patrick')

    def tests_add_person_to_room_type_office(self):
        room_name_list = ['blue']
        self.dojo.create_room(room_name_list, 'office')
        fellow = Fellow('Patrick')
        room = self.dojo.get_available_office()
        self.dojo.add_person_to_room(fellow, room)
        self.assertEqual(room.occupants[0].name, 'Patrick')

    def test_create_room_with_empty_string(self):
        self.assertRaises(ValueError, self.dojo.create_room, [], "")

    def test_create_room_with_wrong_argument_types(self):
        self.assertRaises(TypeError, self.dojo.create_room, 5, 9)

    def test_create_fellow_successfully_without_accomodation_specified(self):
        initial_fellow_count = len(self.dojo.all_fellows)
        self.dojo.add_fellow('name')
        new_fellow_count = len(self.dojo.all_fellows)
        self.assertEqual(new_fellow_count - initial_fellow_count, 1)

    def test_create_fellow_successfully_with_accomodation(self):
        initial_fellow_count = len(self.dojo.all_fellows)
        self.dojo.add_fellow('name', 'Y')
        new_fellow_count = len(self.dojo.all_fellows)
        self.assertEqual(new_fellow_count - initial_fellow_count, 1)

    def test_create_fellow_successfully_without_accomodation(self):
        initial_fellow_count = len(self.dojo.all_fellows)
        self.dojo.add_fellow('name', 'N')
        new_fellow_count = len(self.dojo.all_fellows)
        self.assertEqual(new_fellow_count - initial_fellow_count, 1)

    def test_create_staff_successfully_without_accomodation(self):
        initial_staff_count = len(self.dojo.all_staff)
        self.dojo.add_staff('name')
        new_staff_count = len(self.dojo.all_staff)
        self.assertEqual(new_staff_count - initial_staff_count, 1)

    def test_create_staff_with_accomodation(self):
        initial_staff_count = len(self.dojo.all_staff)
        self.assertEqual(self.dojo.add_staff('name', 'Y'),
                         'Staff can not have accomodation')

    def test_available_space_after_adding_fellow_to_office(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_fellow('name')
        self.dojo.add_fellow('name2')
        self.dojo.add_fellow('name3')
        self.dojo.add_fellow('name4')
        office = self.dojo.add_fellow('name5')
        self.assertTrue(office.contains_space())

    def test_space_not_available_after_adding_fellow_to_office(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_fellow('name')
        self.dojo.add_fellow('name2')
        self.dojo.add_fellow('name3')
        self.dojo.add_fellow('name4')
        self.dojo.add_fellow('name5')
        self.dojo.add_fellow('name5')
        office = self.dojo.add_fellow('name5')
        # self.assertEqual(office.spaces, 0)
        self.assertFalse(office.contains_space())

    def test_unallocated_fellows_if_office_space_runs_out(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_fellow('name')
        self.dojo.add_fellow('name2')
        self.dojo.add_fellow('name3')
        self.dojo.add_fellow('name4')
        self.dojo.add_fellow('name5')
        self.dojo.add_fellow('name6')
        self.dojo.add_fellow('name7')
        self.assertEqual(len(self.dojo.fellows_not_allocated_office), 1)
        self.dojo.add_fellow('name8')
        self.assertEqual(len(self.dojo.fellows_not_allocated_office), 2)

    def test_unallocated_fellows_if_living_space_runs_out(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'living_space')
        self.dojo.add_fellow('name1', 'Y')
        self.dojo.add_fellow('name2', 'Y')
        self.dojo.add_fellow('name3', 'Y')
        self.dojo.add_fellow('name4', 'Y')
        self.dojo.add_fellow('name5', 'Y')
        self.dojo.add_fellow('name6', 'Y')
        self.dojo.add_fellow('name7', 'Y')
        self.assertEqual(len(self.dojo.fellows_not_allocated_living_space), 3)
        self.dojo.add_fellow('name8', 'Y')
        self.assertEqual(len(self.dojo.fellows_not_allocated_living_space), 4)

    def test_re_allocate_person(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_fellow('Patrick')
        person = self.dojo.get_person('Patrick')
        self.assertEqual(person.office.name, 'blue')
        office_name_list = ['red']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.re_allocate_person('Patrick', 'red')
        person = self.dojo.get_person('Patrick')
        self.assertEqual(person.office.name, 'red')

    def test_check_occupants_in_room_when_person_reallocates(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_fellow('Patrick')
        person = self.dojo.get_person('Patrick')
        room = self.dojo.get_room('blue')
        self.assertEqual(room.occupants[0], person)
        office_name_list = ['red']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.re_allocate_person('Patrick', 'red')
        person = self.dojo.get_person('Patrick')
        room = self.dojo.get_room('red')
        self.assertEqual(room.occupants[0], person)

    def test_re_allocate_person_when_room_does_not_exist(self):
        office_name_list = ['blue', 'orange']
        self.dojo.create_room(office_name_list, 'office')
        self.assertEqual(self.dojo.re_allocate_person('Patrick', 'yellow'),
                         'Room with name yellow does not exist')

    def test_re_allocate_person_when_person_does_not_exist(self):
        office_name_list = ['blue', 'orange']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_fellow('Jim')
        self.assertEqual(self.dojo.re_allocate_person('Patrick', 'blue'),
                         'Person with name Patrick does not exist')

    def test_re_allocate_person_to_living_room(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'office')
        self.dojo.add_staff('Samuel')
        self.dojo.create_room(['orange'], 'living_space')
        self.assertEqual(self.dojo.re_allocate_person('Samuel', 'orange'),
                         'Cant Re-allocate staff to a living room')

    def test_re_allocate_fellow_who_doesnt_want_accomodation(self):
        office_name_list = ['blue']
        self.dojo.create_room(office_name_list, 'living_space')
        self.dojo.add_fellow('Samuel', 'N')
        self.dojo.create_room(['orange'], 'living_space')
        self.assertEqual(self.dojo.re_allocate_person('Samuel', 'orange'),
                         'Samuel does not want accommodation')

    def test_loads_people_from_file(self):
        init_number_of_fellows = len(self.dojo.all_fellows)
        init_number_of_staff = len(self.dojo.all_staff)
        self.dojo.load_people('../files/test.txt')
        final_number_of_fellows = len(self.dojo.all_fellows)
        final_number_of_staff = len(self.dojo.all_staff)
        self.assertEqual(4, final_number_of_fellows - init_number_of_fellows)
        self.assertEqual(3, final_number_of_staff - init_number_of_staff)
コード例 #5
0
ファイル: main.py プロジェクト: patlub/Dojo-Room-Allocation
        except SystemExit:
            # The SystemExit exception prints the usage for --help
            # We do not need to do the print here.

            return

        return func(self, opt)

    fn.__name__ = func.__name__
    fn.__doc__ = func.__doc__
    fn.__dict__.update(func.__dict__)
    return fn


dojo = Dojo()


class TheDojo(cmd.Cmd):
    intro = 'Welcome to THE DOJO OFFICE ALLOCATION PROGRAM!' \
            + ' (type help for a list of commands.)'
    prompt = '(The Dojo) '

    file = None

    @docopt_cmd
    def do_create_room(self, arg):
        """Usage: create_room <room_type> <room_name>..."""
        room_names = (arg['<room_name>'])
        room_type = arg['<room_type>']
        dojo.create_room(room_names, room_type)
コード例 #6
0
 def setUp(self):
     self.my_class_instance = Dojo()
     self.held, sys.stdout = sys.stdout, StringIO()
コード例 #7
0
class DojoClassTest(unittest.TestCase):
    def setUp(self):
        self.my_class_instance = Dojo()
        self.held, sys.stdout = sys.stdout, StringIO()

    def test_create_room_successfully(self):
        """Test that the list of all rooms increases by 1."""
        initial_room_count = len(self.my_class_instance.all_rooms)
        self.my_class_instance.create_room("office", "Blue")
        new_room_count = len(self.my_class_instance.all_rooms)
        self.assertEqual(new_room_count - initial_room_count, 1)

    def test_create_room_without_duplicates(self):
        """Test that a room cannot be created twice."""
        self.my_class_instance.create_room("office", "Blue")
        self.my_class_instance.create_room("office", "Blue")
        self.assertEqual(len(self.my_class_instance.all_rooms),
                         1,
                         msg="The same room can't be created twice")

    def test_add_person_successfully(self):
        """Test that the list of all persons increases by one."""
        initial_person_count = len(self.my_class_instance.all_persons)
        self.my_class_instance.add_person("staff", "Neil", "Armstrong", "Y")
        new_person_count = len(self.my_class_instance.all_persons)
        self.assertEqual(new_person_count - initial_person_count, 1)

    def test_wants_accommodation_default_is_N(self):
        """Test that the option for wants_accommodation has a default of N."""
        self.my_class_instance.add_person("Fellow", "Peter", "Musonye")
        result = self.my_class_instance.all_persons
        self.assertEqual(
            result[-1].wants_accommodation,
            'n',
            msg=
            "The default value of wants_accommodation is N if Y is not input")

    def test_person_type_either_fellow_or_staff(self):
        """Test that person_type is either 'fellow' or 'staff'."""
        self.my_class_instance.add_person("Fella", "Peter", "Musonye")
        message = sys.stdout.getvalue().strip()
        self.assertIn(
            "Invalid person_type: Your person type should be either 'fellow' or 'staff'",
            message)

    def test_wants_accommodation_is_Y_or_N(self):
        """Test that wants_accommodation is only 'Y' or 'N'."""
        self.my_class_instance.add_person("Fellow", "Peter", "Musonye", "Yes")
        message = sys.stdout.getvalue().strip()
        self.assertIn("Please input Y or N for wants_accommodation", message)

    def test_allocation(self):
        """
        Test that it actually allocates, by creating new room and fellow.

        Call allocate on them, then check if that room has the fellow in its persons list.
        Also check if self.allocated_persons' length has increased.
        """
        self.my_class_instance.create_room("office", "Blue")
        new_office = self.my_class_instance.all_rooms[-1]
        self.my_class_instance.add_person("Fellow", "Peter", "Musonye")
        new_fellow = self.my_class_instance.all_persons[-1]
        self.my_class_instance.allocate_rooms()
        self.assertEqual(new_office.persons[-1], new_fellow)
        self.assertEqual(len(self.my_class_instance.those_allocated_offices),
                         1)

    def test_that_rooms_must_exist_to_be_printed(self):
        """Test that error message is output if room does not exist."""
        self.my_class_instance.print_room("black")
        message = sys.stdout.getvalue().strip()
        self.assertIn("Black does not exist", message)

    def test_it_outputs_message_if_room_has_no_allocations(self):
        """Test that message is printed if a room has no occupants."""
        self.my_class_instance.create_room("office", "Black")
        new_office = self.my_class_instance.all_rooms[-1]
        self.my_class_instance.allocate_rooms()
        self.my_class_instance.print_room(new_office.room_name)
        message = sys.stdout.getvalue().strip()
        self.assertIn("Office Black has no occupants", message)

    def test_it_outputs_correct_occupants(self):
        """Test that actual occupants only are printed."""
        self.my_class_instance.add_person("fellow", "Peter", "Musonye")
        self.my_class_instance.add_person("staff", "Peter", "Muriuki")
        self.my_class_instance.create_room("office", "Black")
        new_office = self.my_class_instance.all_rooms[-1]
        self.my_class_instance.allocate_rooms()
        new_office_occupants = self.my_class_instance.print_room(
            new_office.room_name)
        allocations = self.my_class_instance.print_allocations()
        self.my_class_instance.print_unallocated()
        message = sys.stdout.getvalue().strip()
        self.assertEqual(
            new_office_occupants,
            ["Peter Musonye Fellow ID: 1", "Peter Muriuki Staff ID: 2"])
        self.assertEqual(
            allocations,
            ["Peter Musonye Fellow ID: 1", "Peter Muriuki Staff ID: 2"])
        self.assertIn("Everyone has been allocated a room", message)

    def test_print_allocations_to_file(self):
        """Test for correct allocations in file if optioned."""
        self.my_class_instance.add_person("fellow", "Peter", "Musonye")
        self.my_class_instance.add_person("staff", "Peter", "Muriuki")
        self.my_class_instance.create_room("office", "Black")
        self.my_class_instance.allocate_rooms()
        self.my_class_instance.print_allocations("test1")
        f = open("test1.txt").readlines()
        self.assertEqual(f[1], "Office Black:\n")
        self.assertEqual(f[3], "Peter Musonye Fellow ID: 1\n")
        self.assertEqual(f[4], "Peter Muriuki Staff ID: 2\n")
        os.remove("test1.txt")

    def test_print_unallocated_to_file(self):
        """Test for correct output in unallocated persons file if optioned."""
        self.my_class_instance.add_person("fellow", "Peter", "Musonye", "y")
        self.my_class_instance.add_person("fellow", "John", "Doe", "y")
        self.my_class_instance.add_person("fellow", "Barack", "Obama", "y")
        self.my_class_instance.add_person("fellow", "Hilary", "Clinton", "y")
        self.my_class_instance.add_person("fellow", "The", "Donald", "y")
        self.my_class_instance.create_room("living_space", "Buckingham")
        self.my_class_instance.allocate_rooms()
        self.my_class_instance.print_unallocated("test2")
        f = open("test2.txt").readlines()
        self.assertEqual(f[0], "The Donald Fellow ID: 5\n")
        os.remove("test2.txt")

    def test_it_reallocates_as_specified(self):
        """Test that once reallocated, the person is only in one list of occupants, that of the new room."""
        self.my_class_instance.add_person("fellow", "Peter", "Musonye")
        self.my_class_instance.create_room("office", "Black")
        black_office = self.my_class_instance.all_rooms[-1]
        self.my_class_instance.allocate_rooms()
        black_office_occupants = self.my_class_instance.print_room(
            black_office.room_name)
        self.assertEqual(black_office_occupants,
                         ["Peter Musonye Fellow ID: 1"])
        self.my_class_instance.create_room("office", "Blue")
        self.my_class_instance.reallocate_person(1, "office", "Blue")
        blue_office = self.my_class_instance.all_rooms[-1]
        black_office_occupants = self.my_class_instance.print_room(
            black_office.room_name)
        blue_office_occupants = self.my_class_instance.print_room(
            blue_office.room_name)
        self.assertEqual(black_office_occupants, [])
        self.assertEqual(blue_office_occupants, ["Peter Musonye Fellow ID: 1"])

    def test_cannot_reallocate_to_non_existent_room(self):
        """Test that reallocation only happens with rooms in the system."""
        self.my_class_instance.reallocate_person(5, "office", "Capitol")
        message = sys.stdout.getvalue().strip()
        self.assertIn("Office Capitol does not exist", message)

    def test_raises_error_if_reallocating_to_full_room(self):
        """Test that an error is raised when reallocating to a full room."""
        self.my_class_instance.add_person("fellow", "Peter", "Musonye", "y")
        self.my_class_instance.add_person("fellow", "John", "Doe", "y")
        self.my_class_instance.add_person("fellow", "Bar", "Obama", "y")
        self.my_class_instance.add_person("fellow", "Hilary", "Clinton", "y")
        self.my_class_instance.create_room("living_space", "Capitol")
        self.my_class_instance.allocate_rooms()
        self.my_class_instance.add_person("fellow", "The", "Donald", "y")
        self.my_class_instance.create_room("living_space", "TrumpTower")
        self.my_class_instance.allocate_rooms()
        self.my_class_instance.reallocate_person(5, "living_space", "Capitol")
        message = sys.stdout.getvalue().strip()
        self.assertIn("Living_Space Capitol is at full capacity", message)

    def test_cannot_reallocate_non_existent_person(self):
        """Test that reallocation only happens with people in the system."""
        self.my_class_instance.create_room("office", "Capitol")
        self.my_class_instance.reallocate_person(5, "office", "Capitol")
        message = sys.stdout.getvalue().strip()
        self.assertIn("ID 5 has not been allocated an office yet", message)

    def test_raises_error_for_wrong_room_type_or_name(self):
        """Test that room_type is only 'office' or 'living_space'."""
        self.my_class_instance.reallocate_person(5, "living", "Capitol")
        message = sys.stdout.getvalue().strip()
        self.assertIn(
            "Invalid room type: Your room type should be 'office' or 'living_space'",
            message)

    def test_load_people(self):
        """Test that people can be added from a txt file correctly."""
        f = open("test3.txt", "a")
        f.write("OLUWAFEMI SULE FELLOW Y" + "\n")
        f.write("DOMINIC WALTERS STAFF" + "\n")
        f.write("SIMON PATTERSON FELLOW Y" + "\n")
        f.write("MARI LAWRENCE FELLOW Y" + "\n")
        f.write("LEIGH RILEY STAFF" + "\n")
        f.write("TANA LOPEZ FELLOW Y" + "\n")
        f.write("KELLY McGUIRE STAFF" + "\n")
        f.close()
        self.my_class_instance.load_people("test3")
        people = self.my_class_instance.all_persons
        person_count = len(people)
        self.assertListEqual([people[0].person_name, people[0].person_surname],
                             ["oluwafemi", "sule"])
        self.assertListEqual([people[6].person_name, people[6].person_surname],
                             ["kelly", "mcguire"])
        self.assertEqual(person_count, 7)
        os.remove("test3.txt")

    def test_save_state(self):
        """Test that current app data is stored correctly in db specified."""
        pass