def create_room(room_name, room_type): try: room_input_index = 0 while room_input_index < len(room_name): room_type_curr = room_type[room_input_index].upper() room_name_curr = room_name[room_input_index].upper() if room_type_curr == "LIVINGSPACE": livingspace = LivingSpace(room_name_curr) LivingSpace.add(livingspace) print_pretty( " A living space called %s has been successfully created." % livingspace.name) elif room_type_curr == "OFFICE": office = Office(room_name_curr) Office.add(office) print_pretty( " An office called %s has been successfully created." % office.name) else: print_pretty( " The specified room type %s, is currently not supported." % room_type_curr) room_input_index += 1 except Exception as e: print_pretty(str(e))
def test_allocate_to_new_staff_space(self): office = Office("staff" + "Foin") staff = Staff("staff" + "Neritus", "staff" + "Otieno", "0784334537") result = len(allocations) office.allocate_to(staff) result_1 = len(allocations) self.assertEqual(result + 1, result_1)
def test_allocate_to_new_fellow_space(self): office = Office("staff" + "Foin") fellow = Fellow("staff" + "Neritus", "staff" + "Otieno", "0788934537", "Y") result = len(allocations) office.allocate_to(fellow) result_1 = len(allocations) self.assertEqual(result + 1, result_1)
def test_instantiate_room_from_name(self): livingspace = LivingSpace('MyLLLLL') LivingSpace.add(livingspace) office = Office('MyJJJSS') Office.add(office) livingspace1 = LivingSpace.from_name("MyLLLLL") office1 = Office.from_name("MyJJJSS") self.assertEqual(office.name, office1.name)
def test_add_office(self): office = Office('MyOffice78') initial_room_count = len(Dojo.rooms()) initial_office_count = len(Office.rooms()) Office.add(office) new_room_count = len(Dojo.rooms()) new_office_count = len(Office.rooms()) self.assertEqual([initial_room_count + 1, initial_office_count + 1], [new_room_count, new_office_count])
def add_person(first_name, last_name, phone, type_, opt_in="N"): try: type_ = type_.upper() if type_ == "FELLOW": fellow = Fellow(first_name, last_name, phone, opt_in) fellow.register() first_name = fellow.first_name last_name = fellow.last_name type_ = fellow.type_ available_offices = Office.available() if available_offices is False: print_pretty(" There are currently no available offices.") else: selection = random.choice(list(available_offices)) office = Office(selection) office.allocate_to(fellow) print_pretty( " The fellow: %s has been allocated to the office: %s." % (fellow.last_name, office.name)) if fellow.opt_in == "Y": available_livingspaces = LivingSpace.available() if available_livingspaces is False: print_pretty( " There are currently no available living spaces.") else: selection = random.choice(list(available_livingspaces)) livingspace = LivingSpace(selection) livingspace.allocate_to(fellow) print_pretty( " The fellow: %s has been allocated to the living space: %s." % (fellow.last_name, livingspace.name)) print_pretty(" A %s: %s %s has been successfully created." % (type_, first_name, last_name)) elif type_ == "STAFF": staff = Staff(first_name, last_name, phone, opt_in) staff.register() first_name = staff.first_name last_name = staff.last_name type_ = staff.type_ available_offices = Office.available() if available_offices is False: print_pretty(" There are currently no available offices.") else: selection = random.choice(list(available_offices)) office = Office(selection) office.allocate_to(staff) print_pretty( " The staff: %s has been allocated to the office: %s." % (staff.last_name, office.name)) print_pretty(" A %s: %s %s has been successfully created." % (type_, first_name, last_name)) else: print_pretty(" %s is currently not a supported role." % type_) #print(persons_detail) except Exception as e: print_pretty(str(e))
def test_print_populated_room(self): self.clear_stores() office = Office("NDO") Dojo.add_room(office) fellow = Fellow("Xone", "Ndobile", "0856443334", "y") fellow.register() office.allocate_to(fellow) allocations = office.allocations() output = Room.members(allocations) self.assertEqual(output, " 0856443334, NDOBILE, XONE, FELLOW\n")
def test_print_existing_allocations_to_screen(self): self.clear_stores() office = Office("NDO2") Dojo.add_room(office) fellow = Fellow("Xone2", "Ndobile2", "0856443324", "y") fellow.register() office.allocate_to(fellow) allocations_ = Room.all_allocations() output = Room.members(allocations_, room_tag=True) expected_output = " NDO2-OFFICE, 0856443324, NDOBILE2, XONE2, FELLOW\n" self.assertEqual(output, expected_output)
def test_allocate_to_staff_no_space(self): office = Office("staff" + 'Focusp') with self.assertRaises(ValueError): x = 0 while (x <= 5): suffix = str(x) staff = Staff("staff" + "Neris" + suffix, "staff" + "Oten" + suffix, "078433448" + suffix, "N") office.allocate_to(staff) x += 1
def setUp(self): self.tom_fellow = Fellow('Tom', 'Soyer', True) self.joe_staff = Staff('Joe', 'Swanson') self.becky_staff = Staff('Rebecca', 'Storm', 'The Office') self.occupants = { self.tom_fellow.first_name + ' ' + self.tom_fellow.last_name: self.tom_fellow, self.joe_staff.first_name + ' ' + self.joe_staff.last_name: self.joe_staff, self.becky_staff.first_name + ' ' + self.becky_staff.last_name: self.becky_staff } self.blue_office = Office('Blue Office', self.occupants)
def test_office_load_state(self): path = "db/" file_name = "mydb" file_ = file_name + ".db" #clean up to avoid conflict between tests os.remove(path + file_) self.clear_stores() #memory office1 = Office('ooskks9') Office.add(office1) Office.save_state(file_name) #db engine = create_engine("sqlite:///" + path + file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_office = session.query(Room).filter_by( roomname='ooskks9'.upper()).first() #clear memory stores self.clear_stores() #memory Office.load_state(file_name) office = Office.from_name('ooskks9') #compare full_office = [office.name, office.capacity, office.type_] full_db_office = [ db_office.roomname, db_office.roomcapacity, db_office.roomtype ] session.close() self.assertEqual(full_db_office, full_office)
def test_arrogate_from_existing_staff(self): office = Office("staff" + 'Focs') staff = Staff("staff" + "Erits", "staff" + "Teno", "0785534224", "Y") office.allocate_to(staff) allocated_1 = office.has_allocation(staff) office.arrogate_from(staff) allocated_2 = office.has_allocation(staff) self.assertEqual([allocated_1, allocated_2], [True, False])
def test_print_existing_allocations_to_default_file(self): self.clear_stores() office = Office("ND2") Dojo.add_room(office) fellow = Fellow("Xne2", "Ndoile2", "0868443324", "y") fellow.register() office.allocate_to(fellow) allocations_ = Room.all_allocations() expected_output = Room.members(allocations_, room_tag=True) Room.to_file(expected_output) path = "output/" f = open(path+"File.txt", "r") output = f.read() #"NDO2-Office, 0856443324, NDOBILE2, XONE2, FELLOW" f.close() self.assertEqual(expected_output, output)
def create_room(room_type, *room_name_arguments): output = '' if room_name_arguments and isinstance(room_name_arguments[0], tuple): room_name_arguments = room_name_arguments[0] if room_name_arguments and room_type: for room_name in room_name_arguments: if room_name not in the_dojo.rooms.keys(): new_room = Room(room_type, room_name) if room_type.lower() == 'office': new_room = Office(room_name, {}) elif room_type.replace(' ', '').lower() == 'livingspace': new_room = LivingSpace(room_name, {}) if len( the_dojo.rooms ) <= total_number_of_rooms and room_name not in the_dojo.rooms.keys( ) and (isinstance(new_room, Office) or isinstance(new_room, LivingSpace)): the_dojo.rooms[room_name] = new_room if room_name in the_dojo.rooms.keys(): prefix = 'A' if room_type.lower() == 'office': prefix = 'An' output += prefix + ' ' + room_type.lower( ) + ' called ' + room_name + " has been successfully created! \n" return output
def test_print_existing_allocations_to_specific_file(self): self.clear_stores() office = Office("ND88") Dojo.add_room(office) fellow = Fellow("Xne88", "Ndoile88", "086800000", "y") fellow.register() office.allocate_to(fellow) allocations_ = Room.all_allocations() expected_output = Room.members(allocations_, room_tag=True) file_name = office.name+"-Allocations" Room.to_file(expected_output, file_name) path = "output/" f = open(path+file_name+".txt", "r") output = f.read() f.close() self.assertEqual(expected_output, output)
def get_room(room_name): try: return LivingSpace.from_name(room_name) except ValueError: pass try: return Office.from_name(room_name) except ValueError: raise ValueError("specifed room is unknown")
def save_state(file_name="default"): file_name = str(file_name) path = "db/" file_ = file_name + ".db" try: if os.path.isfile(path + file_): os.remove(path + file_) Person.save_state(file_name) LivingSpace.save_state(file_name) Office.save_state(file_name) Allocation.save_state(file_name) print_pretty( "The current state of the application has successfully been saved in the db directory under the file: %s." % file_) except exc.DBAPIError: print_pretty( str("There is currently a problem with the specified file please try a different one." )) except Exception as e: print_pretty(str(e))
def test_add_existing_room(self): room = Office("yc") Dojo.add_room(room) initial_room_count = Dojo.room_count() initial_found_state = Dojo.has_room(room) with self.assertRaises(ValueError): Dojo.add_room(room) new_room_count = Dojo.room_count() new_found_state = Dojo.has_room(room) self.assertEqual([new_room_count, new_found_state], [initial_room_count, initial_found_state])
def test_remove_office(self): office = Office('MyOffice89') Office.add(office) initial_room_count = len(Dojo.rooms()) initial_office_count = len(Office.rooms()) Office.remove(office) new_room_count = len(Dojo.rooms()) new_office_count = len(Office.rooms()) self.assertEqual([initial_room_count - 1, initial_office_count - 1], [new_room_count, new_office_count])
def test_reallocate_existing_staff_to_office(self): office = Office('My9994') Office.add(office) staff = Staff("Ugeg", "Insdnis", "073437") office.allocate_to(staff) with self.assertRaises(ValueError): Room.reallocate(staff, office)
def load_state(file_name="default"): file_name = str(file_name) path = "db/" file_ = file_name + ".db" try: if os.path.isfile(path + file_): Person.load_state(file_name) LivingSpace.load_state(file_name) Office.load_state(file_name) Allocation.load_state(file_name) print_pretty( "The current state of the application has successfully been loaded from the file: %s under the directory db." % file_) else: raise Exception( "The specified db file (%s) does not exist under the db directory." % file_) except exc.DBAPIError: print_pretty( str("There is currently a problem with the specified file please try a different one." )) except Exception as e: print_pretty(str(e))
def test_reallocate_staff_to_livingspace(self): office = Office('M777848') Office.add(office) staff = Staff("jjjkkdsj", "liidsls", "0799034987") office.allocate_to(staff) livingspace1 = LivingSpace('U988934') LivingSpace.add(livingspace1) with self.assertRaises(Exception): Room.reallocate(staff, livingspace1)
class TestOffice(unittest.TestCase): """Test cases for the room class""" # Instantiating objects for using in the tests def setUp(self): self.tom_fellow = Fellow('Tom', 'Soyer', True) self.joe_staff = Staff('Joe', 'Swanson') self.becky_staff = Staff('Rebecca', 'Storm', 'The Office') self.occupants = { self.tom_fellow.first_name + ' ' + self.tom_fellow.last_name: self.tom_fellow, self.joe_staff.first_name + ' ' + self.joe_staff.last_name: self.joe_staff, self.becky_staff.first_name + ' ' + self.becky_staff.last_name: self.becky_staff } self.blue_office = Office('Blue Office', self.occupants) # Test if the class creates an instance of itself def test_office_instance(self): self.assertIsInstance( self.blue_office, Office, msg='The object should be an instance of the `Office` class') # Test if the class creates an instance of itself def test_room_instance(self): self.assertIsInstance( self.blue_office, Room, msg='The object should be an instance of the `Room` class') # Test if the class creates a type of itself def test_type(self): self.assertTrue((type(self.blue_office) is Office), msg='The object should be of type `Office`') # Test if the class assigns the right attributes to office instances def test_object_attributes(self): self.assertEqual('Blue Office', self.blue_office.name, msg='The object name should be `Blue Office`') # Test if the get_occupants method returns the right value for the given inputs def test_get_occupants(self): self.assertEqual(3, len(self.blue_office.get_occupants()), msg='The class has to return 3 occupants')
def test_print_existing_unallocated_to_screen(self): self.clear_stores() office = Office("NDO4") Dojo.add_room(office) fellow = Fellow("Xone4", "Ndobile4", "0856443354", "y") fellow.register() office.allocate_to(fellow) fellow = Fellow("Xone5", "Ndobile6", "0856443009", "n") fellow.register() office.allocate_to(fellow) fellow = Fellow("Xone3", "Ndobile3", "0856443344", "y") fellow.register() output = Room.all_unallocated_persons() expected_output = "0856443344, NDOBILE3, XONE3, FELLOW\n" self.assertEqual(output, expected_output)
def create_room(self, name, room_type): if room_type.lower() not in ['office', 'livingspace']: return (error('Only offices and livingspaces allowed!')) if not isinstance(name, str): return (error('Room names can only be strings!')) if name in self.all_rooms: return (error('Room %s exists!' % (name))) else: if room_type.lower() == 'office'.lower(): # create an office self.room = Office(name) self.offices[name] = self.room.members self.all_rooms[name] = [room_type, self.room.members] return (success( 'An office called %s has been created successfully!' % (name))) elif room_type.lower() == 'livingspace'.lower(): # create a livingspace self.room = LivingSpace(name) self.livingspaces[name] = self.room.members self.all_rooms[name] = [room_type, self.room.members] return (success( 'A Livingspace called %s has been created successfully!' % (name)))
def test_creates_office_instance(self): self.office = Office('Spire') self.assertEqual(self.office.capacity, 6) self.assertEqual(self.office.members, [])
def test_has_room(self): room = Office("x") x = Dojo.has_room(room) Dojo.add_room(room) y = Dojo.has_room(room) self.assertEqual([x, y], [False, True])
def test_reallocate_to_room_at_capacity(self): office = Office('0884oo848') Office.add(office) fellowx = Fellow("UNjlksd", "Ilnjndis", "070000345537", "N") office.allocate_to(fellowx) office1 = Office('M94llj87') Office.add(office1) fellow = Fellow("Usdsd", "Isdsds", "070015837", "N") office1.allocate_to(fellow) fellow = Fellow("rereed", "Iererds", "234235837", "N") office1.allocate_to(fellow) fellow = Fellow("Usdsdfsd", "Iskops", "079879787", "N") office1.allocate_to(fellow) fellow = Fellow("Uhoidfd", "Ioijfdsoids", "089437237", "N") office1.allocate_to(fellow) with self.assertRaises(ValueError): Room.reallocate(fellowx, office1) self.assertEqual([office1.has_allocation(fellowx), office.has_allocation(fellowx)], [False, True])
def test_reallocate_fellow_to_office(self): office = Office('000848') Office.add(office) fellow = Fellow("UNlsldg", "Ilslnis", "070555597537", "N") office.allocate_to(fellow) office1 = Office('M9498987') Office.add(office1) Room.reallocate(fellow, office1) self.assertEqual([office1.has_allocation(fellow), office.has_allocation(fellow)], [True, False])
def test_reallocate_staff_to_office(self): office = Office('Myok848') Office.add(office) staff = Staff("UNidng", "Inignis", "07089797537") office.allocate_to(staff) office1 = Office('M949438') Office.add(office1) Room.reallocate(staff, office1) self.assertEqual([office1.has_allocation(staff), office.has_allocation(staff)], [True, False])