コード例 #1
0
ファイル: main.py プロジェクト: mrmuli/amity-space-allocator
 def do_add_person(self, arg):
     """Usage: add_person <first_name> <last_name> <job_type> <accomodation>"""
     person = Person()
     first_name = arg['<first_name>']
     last_name = arg['<last_name>']
     job_type = arg['<job_type>']
     accomodation = arg['<accomodation>']
     person.add_person(first_name, last_name, job_type, accomodation)
コード例 #2
0
class TestPerson(unittest.TestCase):
    def setUp(self):
        self.person = Person()

    # test add fellow who doesn't need living space or office
    @patch('app.person.Person.add')
    @patch('app.fellow.Fellow.fellow_names')
    @patch('app.room.Room.allocate_space')
    def test_add_fellow_who_dont_need_room(self, mock_allocate_space,
                                           mock_fellow_names, mock_add):
        mock_fellow_names.__iter__.return_value = []
        mock_allocate_space.return_value = """Kimani Ndegwa has been allocated Office:"""
        mock_add.return_value = True
        pers = self.person.add_person(person_name='Kimani Ndegwa',
                                      type_person='fellow',
                                      want_accomodation='n')
        self.assertIn('Kimani Ndegwa has been allocated Office:', pers)

    @patch('app.room.Room.get_room')
    @patch('app.person.Person.add')
    @patch('app.room.Room.allocate_space')
    @patch('app.staff.Staff.staff_names')
    def test_add_staff(self, mock_staff_names, mock_allocate_space,
                       mock_add, mock_get_room):
        mock_staff_names.__iter__.return_value = []
        mock_allocate_space.return_value = True
        mock_add.return_value = True
        mock_get_room.return_value = ' HAIL CESEAR!!'
        pers = self.person.add_person(person_name='Garbriel Mwata',
                                      type_person='staff',
                                      want_accomodation='n')
        self.assertIn('Allocated Office: HAIL CESEAR!!', pers)

    # test add staff and assign them livingspace
    def test_add_staff_assign_livingspace(self):
        pers = self.person.add_person(person_name='Njira', type_person='staff',
                                      want_accomodation='y')
        self.assertEqual(pers, 'staff cannot have accomodation')

    @patch('app.staff.Staff.staff_names')
    def test_add_two_people_who_share_name(self, mock_staff_names):
        mock_staff_names.__iter__.return_value = ['Joshua']
        pers = self.person.add_person(
            person_name='Joshua', type_person='staff')
        self.assertEqual(pers, 'Joshua already exists')

    @patch.dict('app.office.Office.office_n_occupants', {'Rm': ['we', 'ty']})
    def test_get_room_members(self):
        rm = self.person.get_room_members(room_name='Rm')
        self.assertEqual(rm, ['we', 'ty'])
コード例 #3
0
class TestPerson(unittest.TestCase):
    def setUp(self):
        self.person = Person()
        sys.stdout = StringIO()

    def test_person_class_instance(self):
        self.assertIsInstance(self.person, Person)

    @patch.dict('app.room.Room.total_rooms', {'oculus': [], 'haskel': []})
    @patch.dict('app.person.Person.total_people', {1: 'MigwiNdungu'})
    @patch('app.room.Room.offices')
    @patch('app.room.Room.livingspaces')
    @patch('app.person.Person.fellows')
    @patch('app.person.Person.staff')
    def test_it_adds_a_person(self, mock_staff, mock_fellows,
                              mock_livingspaces, mock_offices):
        mock_livingspaces.__iter__.return_value = ['haskel']
        mock_offices.__iter__.return_value = ['oculus']
        mock_fellows.__iter__.return_value = []
        mock_staff.__iter__.return_value = []

        already_present = self.person.add_person('Migwi', 'Ndungu', 'Fellow',
                                                 'Y')
        self.assertEqual(
            already_present,
            'oops! Someone with the username MigwiNdungu already exists')

        no_accomodation = self.person.add_person('Ben', 'Kamau', 'Fellow', 'N')
        self.assertEqual(no_accomodation,
                         'BenKamau added to unallocated-people')

        staff_accomodation = self.person.add_person('Michelle', 'Korir',
                                                    'Staff', 'Y')
        self.assertEqual(
            staff_accomodation,
            'NOTE: Staff members are not allocated livings paces')

        successfull_staff_add = self.person.add_person('Buck', 'Speed',
                                                       'Staff', 'N')
        self.assertEqual(successfull_staff_add,
                         'Staff member added successfully')

    @patch.dict(
        'app.room.Room.total_rooms', {
            'oculus': [1, 2, 5, 6, 9, 10],
            'narnia': [3, 4, 7, 8, 11, 12],
            'haskel': [1, 2, 3, 4]
        })
    @patch.dict(
        'app.person.Person.total_people', {
            1: 'MigwiNdungu',
            2: 'JosephMuli',
            3: 'Josh',
            4: 'Njira',
            5: 'kevin',
            6: 'mwangi',
            7: 'john',
            8: 'milkah',
            9: 'noelah',
            10: 'serah',
            11: 'sila',
            12: 'mary'
        })
    @patch('app.room.Room.offices')
    @patch('app.room.Room.livingspaces')
    @patch('app.person.Person.fellows')
    @patch('app.person.Person.staff')
    def test_adding_to_fully_occupied_rooms(self, mock_staff, mock_fellows,
                                            mock_livingspaces, mock_offices):
        mock_livingspaces.__iter__.return_value = ['haskel']
        mock_offices.__iter__.return_value = ['oculus']
        mock_fellows.__iter__.return_value = []
        mock_staff.__iter__.return_value = []

        fully_occupied_office = self.person.add_person('Jimmy', 'Kamau',
                                                       'Staff', 'N')
        self.assertEqual(
            fully_occupied_office,
            'all offices are currently fully occupied, adding to staff-unallocated-without-offices...'
        )

        fully_occupied_ls = self.person.add_person('Alex', 'Magana', 'Fellow',
                                                   'Y')
        self.assertEqual(
            fully_occupied_ls,
            'sorry all living spaces are currently fully occupied, adding to unallocated...'
        )

    @patch.dict('app.room.Room.total_rooms', {'oculus': []})
    @patch.dict('app.person.Person.total_people', {1: 'Migwi'})
    @patch('app.room.Room.offices')
    @patch('app.room.Room.livingspaces')
    @patch('app.person.Person.fellows')
    @patch('app.person.Person.staff')
    def test_response_on_no_rooms(self, mock_staff, mock_fellows,
                                  mock_livingspaces, mock_offices):
        mock_offices.__iter__.return_value = ['oculus']
        mock_livingspaces.__iter__.return_value = []
        mock_fellows.__iter__.return_value = []
        mock_staff.__iter__.return_value = []

        msg = self.person.add_person('joan', 'ngugi', 'Fellow', 'Y')
        self.assertEqual(msg, "There are currently no livingspaces")

    @patch.dict(
        'app.room.Room.total_rooms', {
            'oculus': [1],
            'green': [],
            'mordor': [],
            'shire': [23, 43, 52, 12, 32, 32],
            'haskel': [],
            'python': [1],
            'ruby': [],
            'blue': [98, 75, 45, 24, 76, 99]
        })
    @patch.dict('app.person.Person.total_people', {1: 'Migwi', 2: 'jojo'})
    @patch('app.room.Room.offices')
    @patch('app.room.Room.livingspaces')
    @patch('app.person.Person.fellows')
    @patch('app.person.Person.staff')
    def test_reallocation(self, mock_staff, mock_fellows, mock_livingspaces,
                          mock_offices):
        mock_livingspaces.__iter__.return_value = [
            'haskel', 'python', 'ruby', 'blue'
        ]
        mock_offices.__iter__.return_value = ['oculus', 'mordor', 'shire']
        mock_fellows.__iter__.return_value = ['Migwi']
        mock_staff.__iter__.return_value = ['jojo']

        fully_occupied = self.person.reallocate_person(1, 'shire')
        self.assertEqual(fully_occupied, "Sorry the office is occupied fully")

        already_present = self.person.reallocate_person(1, 'oculus')
        self.assertEqual(
            already_present,
            "The Person is already allocated in the requested room")

        msg = self.person.reallocate_person(1, 'mordor')
        self.assertEqual(msg, "Allocation to New office successfull!")

        person_msg = self.person.reallocate_person(3, 'mordor')
        self.assertEqual(person_msg, "The person ID does not exist!")

        room_msg = self.person.reallocate_person(1, 'shell')
        self.assertEqual(room_msg, "The room doesn't exist!")

        fellow_reallocate_livingspace = self.person.reallocate_person(
            1, 'ruby')
        self.assertEqual(fellow_reallocate_livingspace,
                         "Allocation to New livingSpace successful!")

        fully_occupied_ls = self.person.reallocate_person(1, 'blue')
        self.assertEqual(fully_occupied_ls,
                         "Sorry the LivingSpace is currently fully occupied!")

        unallocated_room = self.person.reallocate_person(1, 'green')
        self.assertEqual(unallocated_room, "green  was not  Allocated")

    @patch('app.person.Person.add_person')
    def test_loads_people(self, mock_add_person):
        mock_add_person.return_value = ''
        sample_read_text = 'OLUWAFEMI SULE FELLOW Y'
        with patch("__builtin__.open",
                   mock_open(read_data=sample_read_text)) as mock_file:
            self.person.load_people_data('list.txt')
        # assert open("people.txt", 'r').readlines() == sample_read_text
        mock_file.assert_called_with("list.txt")

    @patch.dict('app.room.Room.total_rooms', {
        'oculus': [1],
        'mordor': [2],
        'python': [1]
    })
    @patch.dict('app.person.Person.total_people', {
        1: 'Migwi',
        2: 'jojo',
        3: 'jimbo'
    })
    @patch('app.room.Room.offices')
    @patch('app.room.Room.livingspaces')
    @patch('app.person.Person.fellows')
    @patch('app.person.Person.staff')
    @patch('app.person.Person.unallocated_people')
    @patch('app.person.Person.fellows_not_allocated_office')
    @patch('app.person.Person.staff_not_allocated_office')
    def test_commit_people(self, mock_staff_not_allocated_office,
                           mock_fellows_not_allocated_office,
                           mock_unallocated_people, mock_staff, mock_fellows,
                           mock_livingspaces, mock_offices):
        mock_livingspaces.__iter__.return_value = ['python']
        mock_offices.__iter__.return_value = ['oculus', 'mordor']
        mock_fellows.__iter__.return_value = ['Migwi']
        mock_staff.__iter__.return_value = ['jojo', 'jimbo']
        mock_staff_not_allocated_office.__iter__.return_value = ['jimbo']
        mock_fellows_not_allocated_office.__iter__.return_value = []
        mock_unallocated_people.__iter__.return_value = []

        create_db('elsis.db')
        msg = self.person.commit_people('elsis.db')
        self.assertEqual(msg, 'Person data commit successfull')

    @patch.dict('app.room.Room.total_rooms', {
        'oculus': [1],
        'mordor': [2],
        'python': [1]
    })
    @patch.dict('app.person.Person.total_people', {
        1: 'Migwi',
        2: 'jojo',
        3: 'jimbo'
    })
    @patch('app.room.Room.offices')
    @patch('app.room.Room.livingspaces')
    @patch('app.person.Person.fellows')
    @patch('app.person.Person.staff')
    @patch('app.person.Person.unallocated_people')
    @patch('app.person.Person.fellows_not_allocated_office')
    @patch('app.person.Person.staff_not_allocated_office')
    def test_load_people(self, mock_staff_not_allocated_office,
                         mock_fellows_not_allocated_office,
                         mock_unallocated_people, mock_staff, mock_fellows,
                         mock_livingspaces, mock_offices):
        mock_livingspaces.__iter__.return_value = ['python']
        mock_offices.__iter__.return_value = ['oculus', 'mordor']
        mock_fellows.__iter__.return_value = ['Migwi']
        mock_staff.__iter__.return_value = ['jojo', 'jimbo']
        mock_staff_not_allocated_office.__iter__.return_value = ['jimbo']
        mock_fellows_not_allocated_office.__iter__.return_value = []
        mock_unallocated_people.__iter__.return_value = []

        msg = self.person.load_people('elsis.db')
        self.assertEqual(msg, 'People loaded successfully')