class Person(Staff, Fellow): '''Validate input data and undertake basic operaions ''' def __init__(self): self.room = Room() def add_person(self, person_name, type_person, want_accomodation='n'): '''By default someone needs an Office ''' staf = list(Staff.staff_names) felo = list(Fellow.fellow_names) if person_name in staf or person_name in felo: return person_name + ' already exists' elif type_person == 'staff': if 'y' == want_accomodation: return 'staff cannot have accomodation' create_staff = self.add(person_name, type_person) if create_staff is not True: return create_staff # By default the staff should never be allocated living space allocate_office = self.room.allocate_space(person_name) if allocate_office is not True: return allocate_office return 'Allocated Office:' + str(self.room.get_room(person_name)) create_fellow = self.add(person_name, type_person) if create_fellow is not True: return create_fellow # A fellow may or not want accomodation return self.room.allocate_space(person_name, want_accomodation) def add(self, person_name, type_person): if person_name == '' or type(person_name) != str: return 'Invalid person name type used' elif len(person_name) > 21: return 'Too long person name' if type_person == 'fellow': return self.add_one_fellow(person_name) elif type_person == 'staff': return self.add_one_staff(person_name) else: return " '" + type_person + "' type person is unknown" def get_room_members(self, room_name): if room_name in list(Office.office_n_occupants.keys()): return Office.office_n_occupants[room_name] elif room_name in list(LivingSpace.room_n_occupants.keys()): return LivingSpace.room_n_occupants[room_name] return 'Room not Found'
class TestRoom(unittest.TestCase): def setUp(self): self.room = Room() @patch.dict('app.office.Office.office_n_occupants', {"Dojo": []}) def test_create_room(self): res = self.room.create_room(room_name='Valhala', room_type='office') self.assertEqual(res, 'Valhala was created as an Office') # create duplicate room (With same name) @patch.dict('app.office.Office.office_n_occupants', {"Dojo": []}) @patch.dict('app.livingspace.LivingSpace.room_n_occupants', {"Valhala": []}) def test_create_duplicate_room(self): res = self.room.create_room( room_name='Valhala', room_type='LivingSpace') self.assertEqual(res, 'Valhala already exists ') # create a room without a name def test_create_room_without_name(self): res = self.room.create_room(room_name='', room_type='Office') self.assertEqual(res, 'Room name used is invalid') # create type of room not available def test_create_room_type_not_available(self): res = self.room.create_room(room_name='Valhala', room_type='game room') self.assertEqual( res, "Create a room of type 'LivingSpace' or 'Office' ONLY!") @patch('app.room.Room.randomly_allocate_rooms') @patch('app.room.Room.get_room') @patch('app.fellow.Fellow.fellow_names') def test_allocate_space(self, mock_fellow_names, mock_get_room, mock_randomly_allocate_rooms): mock_fellow_names.__iter__.return_value = ['Gaamuwa'] mock_randomly_allocate_rooms.return_value = True mock_get_room.return_value = 'None' value = self.room.allocate_space('Gaamuwa', 'y') mock_get_room.return_value = 'None' self.assertEqual( 'Allocated Office Space : None \nAllocated Living Space : None', value) def test_randomly_allocate(self): capacity = 1 data = {'Krypton': []} person_name = 'MIgwi' ret_val = self.room.randomly_allocate_rooms( capacity, data, person_name) self.assertTrue(ret_val) person_name = 'Zainab' ret_val = self.room.randomly_allocate_rooms( capacity, data, person_name) self.assertEqual('All rooms are full to capacity', ret_val) @patch('app.staff.Staff.staff_names') @patch.dict('app.office.Office.office_n_occupants', {'Narnia': []}) @patch('app.fellow.Fellow.fellow_names') @patch.dict('app.livingspace.LivingSpace.room_n_occupants', {'Dojo': []}) @patch('app.room.Room.reallocate') def test_reallocate_room(self, mock_reallocate, mock_fellow_names, mock_staff_names): mock_reallocate.return_value = True mock_fellow_names.__iter__.return_value = ['njoroge'] mock_staff_names.__iter__.return_value = ['James_Ndiga'] person_name, room_name = 'James_Ndiga', 'Narnia' ret_val = self.room.reallocate_room(person_name, room_name) self.assertIn(person_name, ret_val) self.assertIn(room_name, ret_val) self.assertIn('was allocated an Office', ret_val) person_name, room_name = 'James_Ndiga', 'Dojo' ret_val = self.room.reallocate_room(person_name, room_name) self.assertEqual( 'Living Spaces are allocated to fellows ONLY', ret_val) person_name, room_name = 'njoroge', 'Php' ret_val = self.room.reallocate_room(person_name, room_name) self.assertEqual('Room Not Found in the system', ret_val) person_name, room_name = 'njoroge', 'Dojo' ret_val = self.room.reallocate_room(person_name, room_name) self.assertIn(person_name, ret_val) self.assertIn(room_name, ret_val) self.assertIn('was allocated a Living Space', ret_val) @patch.dict('app.livingspace.LivingSpace.room_n_occupants', {'Mars': []}) @patch('app.livingspace.LivingSpace.living_capacity') @patch('app.room.Room.get_room') def test_reallocate(self, mock_get_room, mock_living_capacity): mock_get_room.return_value = 'None' mock_living_capacity.return_value = 1 value = self.room.reallocate('david', 'Mars', 'livingspace') self.assertTrue(value) value = self.room.reallocate('Stan', 'Mars', 'livingspace') self.assertEqual('Mars has a max of 1 person(s) currently', value) @patch.dict('app.office.Office.office_n_occupants', {'Alien-Planet': ['Sam', 'Edwin', 'Steve']}) def test_get_room(self): assign = self.room.get_room(person_name='Steve', type_space='office') self.assertEqual('Alien-Planet', assign)