class AmityTest(unittest.TestCase): def setUp(self): self.building = Amity() def tearDown(self): self.building = None def test_populate_offices(self): ''' Test whether the populate method populates the building with office rooms ''' self.assertTrue(len(self.building.office_rooms), 10) def test_populate_living(self): ''' Test whether the populate method populates the building with living rooms ''' self.assertEqual(len(self.building.living_rooms), 10) def test_create_staff(self): ''' Test create_occupants method ''' self.assertEqual(len(self.building.staff), 32) def test_create_fellows(self): ''' Test create_occupants method ''' fellows = len(self.building.boarding_fellows + self.building.non_boarding_fellows) self.assertEqual(fellows, 82) def test_allocate(self): ''' Tests allocate method ''' self.assertEqual(len(self.building.get_allocations()), 20) def test_get_available_room(self): ''' Tests getting available rooms ''' self.assertEqual(len(self.building.get_available_rooms('LIVING')), 0) def test_get_unallocated(self): ''' Tests getting unallocated staff ''' self.assertTrue( len(self.building.get_unallocated_occupants()), 38) def test_print_allocated(self): self.assertEqual(len(self.building.print_allocated()), 100) def test_analyse_allocations(self): result = self.building.analyze_allocations() self.assertTrue('total rooms' in result) self.assertTrue('allocated rooms' in result) self.assertTrue('total occupants' in result) self.assertEqual(result['total rooms'], 20) self.assertEqual(result['total occupants'], 114) self.assertEqual(result['allocated occupants'], 100)
class AllocationTestCase(unittest.TestCase): """ tests the allocation of rooms to persons """ def test_allocation_to_rooms(self): """ tests the allocation of persons to rooms """ self.fellow = Person.create( 'Jee Gikera', 'fellow', wants_accomodation='Y') self.staff = Person.create('Chidi Nnadi', 'staff') office_room = Office('valhalla') living_room = LivingSpace('blue') # store person instances for testing fellow_only.append(self.fellow) persons.append(self.staff) persons.append(self.fellow) office_results = self.staff.assign_office_space(office_room) living_results = self.fellow.assign_living_space(living_room) office_room.assign_person(self.staff) living_room.assign_person(self.fellow) self.assertTrue(self.staff.has_office()) self.assertTrue(self.fellow.has_living_space()) self.assertIsInstance(office_results, Office) self.assertIsInstance(living_results, LivingSpace) self.assertIsNotNone(living_room) self.assertIsNotNone(office_room) self.assertFalse(living_room.is_occupied()) self.assertFalse(office_room.is_occupied()) self.office = Office('GreenHouse') self.living = LivingSpace('BlueMoon') self.amity = Amity() ospace = self.amity.allocate_office_space(self.fellow) lspace = self.amity.allocate_living_space(self.fellow) allocated = self.office.get_occupants() self.assertEquals(self.staff.has_living_space(), False) self.assertEquals(self.fellow.has_living_space(), True) self.assertIsNotNone(allocated) self.assertIsNotNone(ospace) self.assertIsNotNone(lspace) def test_getting_room_occupants(self): """ tests getting a given room's occupants """ self.amity = Amity() office_results = self.amity.allocate_office_space( file_path, is_a_file=True) living_results = self.amity.allocate_living_space( file_path, is_a_file=True) office_roomies = office_results[0].get_occupants() living_roomies = living_results[0].get_occupants() self.assertIsNotNone(office_roomies) self.assertIsNotNone(living_roomies) def test_getting_all_allocations(self): """ tests getting all allocations to the building """ self.amity = Amity() self.amity.allocate_living_space(file_path, is_a_file=True) self.amity.allocate_office_space(file_path, is_a_file=True) allocations = self.amity.get_allocations() print_allocation = self.amity.print_allocations() self.assertIsNotNone(allocations) self.assertTrue(print_allocation) def test_unallocated(self): """ test getting unallocated people if any """ amity = Amity() amity.allocate_office_space(file_path, is_a_file=True) unalloc = amity.get_unallocated() unallocated = amity.unallocated # the rooms are currently 10, each taking 4 occupants, # therefore we don't have unallocated persons self.assertIsNotNone(unalloc) self.assertIsNotNone(unallocated)
class TestAmity(unittest.TestCase): def setUp(self): self.engine = create_engine('sqlite:///:memory:') session = sessionmaker(self.engine) session.configure(bind=self.engine) self.session = session() sys.stdout = StringIO() self.amity = Amity() self.Base = declarative_base() # Base.metadata.create_all(self.engine) # self.panel = Panel(1, 'ion torrent', 'start') # self.session.add(self.panel) # self.session.commit() def test_type_of_amity(self): self.assertIsInstance(Amity(), object) @patch('amity.Amity.connect_db') def test_create_tables(self, mock_connect_db): mock_connect_db.return_value = self.engine self.amity.create_tables() # assert table user exists self.assertTrue(self.engine.dialect.has_table( self.engine.connect(), "user")) # assert table room exists self.assertTrue(self.engine.dialect.has_table( self.engine.connect(), "room")) @patch('amity.Amity.connect_db') @patch('amity.Amity.retrieve_data_from_db') def test_load_state(self, mock_connect_db, mock_retrieve_data_from_db): mock_connect_db.return_value = self.engine mock_retrieve_data_from_db.return_value = True self.amity.load_state() printed = sys.stdout.getvalue() self.assertIn( "Please Wait... This may take some few minutes.", printed) self.assertIn("\tReading data from the database", printed) self.assertIn("Load State successfully completed", printed) self.assertIn('', printed) def test_retrieve_data_from_db(self): class User(self.Base): '''class mapper for table user''' __tablename__ = 'user' id = Column(Integer, Sequence('user_id_seq'), primary_key=True) person_name = Column(String(30)) type_person = Column(String(30)) class Room(self.Base): '''class mapper for table room''' __tablename__ = 'room' id = Column(Integer, Sequence('room_id_seq'), primary_key=True) room_type = Column(String(20)) room_name = Column(String(30)) occupant1 = Column(String(30), ForeignKey('user.id')) occupant2 = Column(String(30), ForeignKey('user.id')) occupant3 = Column(String(30), ForeignKey('user.id')) occupant4 = Column(String(30), ForeignKey('user.id')) occupant5 = Column(String(30), ForeignKey('user.id')) occupant6 = Column(String(30), ForeignKey('user.id')) self.Base.metadata.create_all(self.engine) if self.engine.dialect.has_table(self.engine.connect(), "user"): print('user') test_User = Table("user", MetaData(self.engine), autoload=True) sample_data = {'person_name': 'andela-jkariuki', 'type_person': 'fellow'} test_User.insert().execute(sample_data) elif self.engine.dialect.has_table(self.engine.connect(), "room"): test_Room = Table("room", MetaData(self.engine), autoload=True) sample_data = {'room_name': 'php', 'room_type': 'livingspace', 'occupant1': 'Morris', 'occupant2': 'Migwi'} test_Room.insert().execute(sample_data) self.amity.retrieve_data_from_db(self.session) self.assertIn('andela-jkariuki', Fellow.fellow_names) self.assertNotIn('php', list(LivingSpace.room_n_occupants.keys())) @patch('amity.Amity.connect_db') @patch('amity.Amity.create_tables') @patch('amity.Amity.order_data_ready_for_saving') def test_save_state(self, mock_connect_db, mock_create_tables, mock_order_data_ready_for_saving): mock_connect_db.return_value = self.engine mock_create_tables.return_value = True mock_order_data_ready_for_saving.return_value = True sys.stdout = StringIO() self.amity.save_state() printed = sys.stdout.getvalue() self.assertIn( "Please Wait... This may take some few minutes.", printed) self.assertIn("\tInitiating database population....", printed) self.assertIn("\tFinalizing database population....", printed) self.assertIn("Save State successfully completed.", printed) @patch('app.fellow.Fellow.fellow_names') @patch('app.staff.Staff.staff_names') @patch.dict('app.office.Office.office_n_occupants', {'Round_Table': ['Sass', 'Joshua', 'Jeremy'], 'Krypton': ['Percila', 'Kimani', 'Whitney'], 'Valhala': ['Migwi'] }) @patch.dict('app.livingspace.LivingSpace.room_n_occupants', {'php': ['Andela1', 'andela2', 'Mr know it all'], 'amity': ['chiemeka', 'mayowa', 'chioma'], 'm55': ['adeleke'] }) def test_order_data_ready_for_saving(self, mock_fellow_names, mock_staff_names): class User(self.Base): '''class mapper for table user''' __tablename__ = 'user' id = Column(Integer, Sequence('user_id_seq'), primary_key=True) person_name = Column(String(30)) type_person = Column(String(30)) class Room(self.Base): '''class mapper for table room''' __tablename__ = 'room' id = Column(Integer, Sequence('room_id_seq'), primary_key=True) room_type = Column(String(20)) room_name = Column(String(30)) occupant1 = Column(String(30), ForeignKey('user.id')) occupant2 = Column(String(30), ForeignKey('user.id')) occupant3 = Column(String(30), ForeignKey('user.id')) occupant4 = Column(String(30), ForeignKey('user.id')) occupant5 = Column(String(30), ForeignKey('user.id')) occupant6 = Column(String(30), ForeignKey('user.id')) self.Base.metadata.create_all(self.engine) mock_fellow_names.__iter__.return_value = ['chiemeka', 'mayowa', 'Andela1', 'andela2', 'adeleke'] mock_staff_names.__iter__.return_value = ['Sass', 'Joshua', 'Jeremy', 'chioma', 'Mr know it all'] self.amity.order_data_ready_for_saving(self.session) self.session.commit() self.session.flush() # check if table user exists all_rooms = {} all_users = [] if self.engine.dialect.has_table(self.engine.connect(), "user"): test_User = Table("user", MetaData(self.engine), autoload=True) all_users = test_User.select().execute() print(all_rooms) elif self.engine.dialect.has_table(self.engine.connect(), "room"): test_Room = Table("room", MetaData(self.engine), autoload=True) all_rooms = test_Room.select().execute() print(all_users) self.assertNotIn(frozenset({'Valhala': ['Migwi']}), all_rooms) self.assertNotIn(['Mr know it all', 'adeleke'], all_users) def test_print_file(self): data = {'The_Key': 'The value'} sample_text = 'Room Name: The_Key Occupants:The value \n\r' with patch("builtins.open", mock_open(read_data=sample_text)) as mock_file: self.amity.print_file('sample', 'allocated', data) mock_file.assert_called_with('sample-allocated.txt', 'w') # mock_file.write.assert_called_once_with(sample_text, 'w') assert open('sample-allocated.txt', 'w').read() == sample_text @patch('app.person.Person.add_person') def test_load_people(self, mock_add_person): sample_read_text = 'andela-dmigwi FELLOW Y' mock_add_person.return_value = 'Successful' with patch("builtins.open", mock_open(read_data=sample_read_text)) as mock_file: self.amity.load_people() # assert open("people.txt", 'r').readlines() == sample_read_text mock_file.assert_called_with("people.txt", 'r') printed = sys.stdout.getvalue() self.assertIn('Successful', printed) @patch('amity.Amity.compute_rooms') @patch('amity.Amity.print_file') def test_get_allocations(self, mock_print_file, mock_compute_rooms): mock_compute_rooms.return_value = {'Amity': ['Eston Mwaura']} mock_print_file.return_value = 'Successful' room_details = self.amity.get_allocations('test.txt') office_details = {'Amity': ['Eston Mwaura']} livingspace_details = 'Successful' self.assertIn(office_details, room_details) self.assertIn(livingspace_details, room_details) @patch('amity.Amity.compute_rooms') @patch('amity.Amity.print_file') def test_get_unallocated(self, mock_print_file, mock_compute_rooms): mock_compute_rooms.return_value = ['Amity'] mock_print_file.return_value = 'Successful' room_details = self.amity.get_unallocated('sample.txt') response_office = ['Amity', 'Amity'] response_livingspace = 'Successful' self.assertIn(response_office, room_details) self.assertIn(response_livingspace, room_details) def test_compute(self): rooms = {'m55': ['Aubrey, Chiemeka', 'Mayowa'], 'Amity': [], 'Dojo': ['Migwi', 'Elsis']} computed = self.amity.compute_rooms(rooms, 'allocated') rooms_1 = {'m55': ['Aubrey, Chiemeka', 'Mayowa'], 'Dojo': ['Migwi', 'Elsis']} self.assertEqual(computed, rooms_1) computed = self.amity.compute_rooms(rooms, 'unallocated') self.assertEqual(computed, ['Amity']) @patch.dict('app.office.Office.office_n_occupants', {'Dojo': ['Njira']}) @patch.dict('app.livingspace.LivingSpace.room_n_occupants', {'Valhala': ['Edwin']}) @patch('app.staff.Staff.staff_names') @patch('app.fellow.Fellow.fellow_names') def test_get_all_unallocated_people(self, mock_fellow_names, mock_staff_names): mock_fellow_names.__iter__.return_value = ['Lolo', 'Edwin'] mock_staff_names.__iter__.return_value = ['Eston', 'Njira'] check = self.amity.get_all_unallocated_people() self.assertIn('Lolo', check[0]) self.assertIn('Eston', check[2])
class AllocationTestCase(unittest.TestCase): """ tests the allocation of rooms to persons """ def test_allocation_to_rooms(self): """ tests the allocation of persons to rooms """ self.fellow = Person.create('Jee Gikera', 'fellow', wants_accomodation='Y') self.staff = Person.create('Chidi Nnadi', 'staff') office_room = Office('valhalla') living_room = LivingSpace('blue') # store person instances for testing fellow_only.append(self.fellow) persons.append(self.staff) persons.append(self.fellow) office_results = self.staff.assign_office_space(office_room) living_results = self.fellow.assign_living_space(living_room) office_room.assign_person(self.staff) living_room.assign_person(self.fellow) self.assertTrue(self.staff.has_office()) self.assertTrue(self.fellow.has_living_space()) self.assertIsInstance(office_results, Office) self.assertIsInstance(living_results, LivingSpace) self.assertIsNotNone(living_room) self.assertIsNotNone(office_room) self.assertFalse(living_room.is_occupied()) self.assertFalse(office_room.is_occupied()) self.office = Office('GreenHouse') self.living = LivingSpace('BlueMoon') self.amity = Amity() ospace = self.amity.allocate_office_space(self.fellow) lspace = self.amity.allocate_living_space(self.fellow) allocated = self.office.get_occupants() self.assertEquals(self.staff.has_living_space(), False) self.assertEquals(self.fellow.has_living_space(), True) self.assertIsNotNone(allocated) self.assertIsNotNone(ospace) self.assertIsNotNone(lspace) def test_getting_room_occupants(self): """ tests getting a given room's occupants """ self.amity = Amity() office_results = self.amity.allocate_office_space(file_path, is_a_file=True) living_results = self.amity.allocate_living_space(file_path, is_a_file=True) office_roomies = office_results[0].get_occupants() living_roomies = living_results[0].get_occupants() self.assertIsNotNone(office_roomies) self.assertIsNotNone(living_roomies) def test_getting_all_allocations(self): """ tests getting all allocations to the building """ self.amity = Amity() self.amity.allocate_living_space(file_path, is_a_file=True) self.amity.allocate_office_space(file_path, is_a_file=True) allocations = self.amity.get_allocations() print_allocation = self.amity.print_allocations() self.assertIsNotNone(allocations) self.assertTrue(print_allocation) def test_unallocated(self): """ test getting unallocated people if any """ amity = Amity() amity.allocate_office_space(file_path, is_a_file=True) unalloc = amity.get_unallocated() unallocated = amity.unallocated # the rooms are currently 10, each taking 4 occupants, # therefore we don't have unallocated persons self.assertIsNotNone(unalloc) self.assertIsNotNone(unallocated)
import os import sys import inspect currentdir = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) sys.path.insert(0, parentdir) from amity import Amity # exit gracefully when the text file to read is missing if __name__ == "__main__": try: arg1 = sys.argv[1] except IndexError: print("Usage: python sample.py <text file>") sys.exit(1) amity = Amity() amity.allocate_living_space(sys.argv[1], is_a_file=True) amity.allocate_office_space(sys.argv[1], is_a_file=True) amity.get_allocations() amity.print_allocations() print amity.get_unallocated()
import os import sys import inspect currentdir = os.path.dirname(os.path.abspath( inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) sys.path.insert(0, parentdir) from amity import Amity # exit gracefully when the text file to read is missing if __name__ == "__main__": try: arg1 = sys.argv[1] except IndexError: print ("Usage: python sample.py <text file>") sys.exit(1) amity = Amity() amity.allocate_living_space(sys.argv[1], is_a_file=True) amity.allocate_office_space(sys.argv[1], is_a_file=True) amity.get_allocations() amity.print_allocations() print amity.get_unallocated()