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)
def do_create_room(self, arg): """Usage: create_room <room_name>...""" room = Room() rooms = arg['<room_name>'] for rm in rooms: status = room.create_room(rm)
class TestRoom(unittest.TestCase): def setUp(self): self.room = Room() self.person = Person() def test_room_class_instance(self): self.assertIsInstance(self.room, Room) @patch.dict('app.room.Room.total_rooms', {'Michigan': []}) def test_duplicate_room_creation(self): res = self.room.create_room('Michigan') self.assertEqual(res, 'A room with that name already exists!') @patch.dict('app.room.Room.total_rooms', {'Michigan': []}) def test_it_creates_rooms(self): msg = self.room.create_room('narnia', 'whimm', 'kelly') self.assertEqual(msg, "Rooms have been successfully created") def test_it_creates_empty_room(self): with patch('app.room.Room'): mes = self.room.create_room() self.assertEqual(mes, "You can't create an empty room!") def test_create_room_input_string_type(self): with patch('app.room.Room'): self.room.create_room('vanish') self.assertEqual(type(self.room.room_name), str, msg="Room names should be Strings") @patch.dict('app.room.Room.total_rooms', { 'Michigan': [], 'galaxy': [], 'vanish': [] }) def test_it_allocates_room_type(self): ret_o_val = self.room.allocate_room_type('vanish', 'Office') ret_l_val = self.room.allocate_room_type('galaxy', 'LivingSpace') self.assertEqual(ret_o_val, "vanish has been added as an Office") self.assertEqual(ret_l_val, "galaxy has been added as a LivingSpace") res = self.room.allocate_room_type('samsung', 'Office') self.assertEqual(res, "The room doesn't exist") @patch.dict('app.room.Room.total_rooms', {'vanish': []}) def test_it_prints_an_existing_room(self): self.room.print_room('vanish') self.assertIn('vanish', Room.total_rooms.keys()) @patch.dict('app.room.Room.total_rooms', { 'Michigan': [], 'galaxy': [], 'vanish': [] }) def test_it_prints_room_occupants(self): msg = self.room.print_room('samsung') self.assertEqual(msg, "The room does not exist!") def test_it_prints_allocations(self): self.room.print_allocations('list.txt') self.assertIsNotNone('list.txt') pass @patch.dict('app.room.Room.total_rooms', {'Michigan': [], 'Camelot': []}) @patch('app.room.Room.offices') @patch('app.room.Room.livingspaces') def test_commit_rooms(self, mock_livingspaces, mock_offices): mock_livingspaces.__iter__.return_value = ['Michigan'] mock_offices.__iter__.return_value = ['Camelot'] create_db('test_amity.db') msg = self.room.commit_rooms('test_amity.db') self.assertEqual(msg, 'rooms committed to session') @patch.dict('app.room.Room.total_rooms', {'Michigan': [], 'Camelot': []}) @patch('app.room.Room.offices') @patch('app.room.Room.livingspaces') def test_load_rooms(self, mock_livingspaces, mock_offices): mock_livingspaces.__iter__.return_value = ['Michigan'] mock_offices.__iter__.return_value = ['Camelot'] msg = self.room.load_rooms('test_amity.db') self.assertEqual(msg, 'Room data added successfully')