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_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])
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_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 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 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_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_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 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_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_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_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)
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 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_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_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_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_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 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 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_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 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 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 test_available_office(self): result = Office.available() office = Office('MyO55e89') Office.add(office) staff = Staff("staff" + "Njsiritus", "staff" + "Otsdeno", "0700000537") office.allocate_to(staff) staff = Staff("staff" + "Njsiritus", "staff" + "Otsdeno", "0700001537") office.allocate_to(staff) result_2 = Office.available() staff = Staff("staff" + "Njsiritus", "staff" + "Otsdeno", "0700002537") office.allocate_to(staff) staff = Staff("staff" + "Njsiritus", "staff" + "Otsdeno", "0700003537") office.allocate_to(staff) result_3 = Office.available() self.assertTrue([result, result_3, type(result_2)], [False, False, "set"])
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_notinstance_of_LivingSpace(self): office = Office("staff" + 'Mkuru') self.assertNotIsInstance(office, LivingSpace)
def test_allocate_to_existing_staff_space(self): office = Office("staff" + "Focuspo") staff = Staff("staff" + "Nerits", "staff" + "Oteno", "0784334222", "N") office.allocate_to(staff) with self.assertRaises(ValueError): office.allocate_to(staff)
def test_creates_office_instance(self): self.office = Office('Spire') self.assertEqual(self.office.capacity, 6) self.assertEqual(self.office.members, [])
def test_isinstance_of_room(self): office = Office("staff" + 'Focuspoint') self.assertIsInstance(office, Room)
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])