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_fellow_space(self): livingspace = LivingSpace('Focuspoin') fellow = Fellow("Neritus", "Otieno", "0784334220", "Y") result = len(allocations) livingspace.allocate_to(fellow) result_1 = len(allocations) self.assertEqual(result+1, result_1)
def test_add_living_space(self): livingspace = LivingSpace('MySpace78') initial_room_count = len(Dojo.rooms()) initial_livingspace_count = len(LivingSpace.rooms()) LivingSpace.add(livingspace) new_room_count = len(Dojo.rooms()) new_livingspace_count = len(LivingSpace.rooms()) self.assertEqual([initial_room_count+1, initial_livingspace_count+1], [new_room_count, new_livingspace_count])
def test_allocate_to_fellow_no_space(self): livingspace = LivingSpace('Focusp') with self.assertRaises(ValueError): x = 0 while (x <= 7): suffix = str(x) fellow = Fellow("Neris"+suffix, "Oten"+suffix, "078433448"+suffix,"N") livingspace.allocate_to(fellow) x += 1
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_livingspace_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 livingspace1 = LivingSpace('oju5nf89') LivingSpace.add(livingspace1) LivingSpace.save_state(file_name) #db engine = create_engine("sqlite:///"+path+file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_livingspace = session.query(Room).filter_by(roomname='oju5nf89'.upper()).first() #clear memory stores self.clear_stores() #memory LivingSpace.load_state(file_name) livingspace = LivingSpace.from_name('oju5nf89') #compare full_livingspace = [livingspace.name, livingspace.capacity, livingspace.type_] full_db_livingspace = [db_livingspace.roomname, db_livingspace.roomcapacity, db_livingspace.roomtype] session.close() self.assertEqual(full_db_livingspace, full_livingspace)
def test_arrogate_from_existing_fellow(self): livingspace = LivingSpace('Focs') fellow = Fellow("Erits", "Teno", "0785534224", "Y") livingspace.allocate_to(fellow) allocated_1 = livingspace.has_allocation(fellow) livingspace.arrogate_from(fellow) allocated_2 = livingspace.has_allocation(fellow) self.assertEqual([allocated_1, allocated_2], [True, False])
def test_add_new_room(self): room = LivingSpace("hl") initial_room_count = Dojo.room_count() initial_found_state = Dojo.has_room(room) 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 + 1, not initial_found_state])
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 test_remove_existing_room(self): room = LivingSpace("xd") Dojo.add_room(room) initial_room_count = Dojo.room_count() initial_found_state = Dojo.has_room(room) Dojo.remove_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 - 1, not initial_found_state])
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_remove_living_space(self): livingspace = LivingSpace('MySpace89') LivingSpace.add(livingspace) initial_room_count = len(Dojo.rooms()) initial_livingspace_count = len(LivingSpace.rooms()) LivingSpace.remove(livingspace) new_room_count = len(Dojo.rooms()) new_livingspace_count = len(LivingSpace.rooms()) self.assertEqual([initial_room_count-1, initial_livingspace_count-1], [new_room_count, new_livingspace_count])
def test_allocations_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 livingspace = LivingSpace('hwan') LivingSpace.add(livingspace) fellow = Fellow("onon", "ekek", "000009", "Y") fellow.register() livingspace.allocate_to(fellow) prev_allocations = Room.all_allocations().copy() Allocation.save_state(file_name) #clear memory stores self.clear_stores() empty_allocations = Room.all_allocations().copy() #db Allocation.load_state(file_name) loaded_allocations = Room.all_allocations().copy() #compare expected_output = ["HWAN", "LIVINGSPACE", "000009"] output = [prev_allocations[0], empty_allocations, loaded_allocations[0]] self.assertEqual(output, [expected_output, [], expected_output])
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_allocations_save_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 livingspace = LivingSpace('hwan') LivingSpace.add(livingspace) fellow = Fellow("onon", "ekek", "000009", "Y") fellow.register() livingspace.allocate_to(fellow) Allocation.save_state(file_name) #db engine = create_engine("sqlite:///"+path+file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_allocation = session.query(Allocation).first() #compare db_output = [db_allocation.roomname, db_allocation.roomtype, db_allocation.phonenumber] self.assertEqual(Room.all_allocations()[0], db_output) session.close()
def test_available_livingspace(self): result = LivingSpace.available() livingspace = LivingSpace('MyO55e80') LivingSpace.add(livingspace) fellow = Fellow("staff"+"Njsiritus", "staff"+"Otsdeno", "0700004537", "Y") livingspace.allocate_to(fellow) fellow = Fellow("staff"+"Njsiritus", "staff"+"Otsdeno", "0700005537", "Y") livingspace.allocate_to(fellow) fellow = Fellow("staff"+"Njsiritus", "staff"+"Otsdeno", "0700006537", "Y") livingspace.allocate_to(fellow) result_2 = LivingSpace.available() fellow = Fellow("staff"+"Njsiritus", "staff"+"Otsdeno", "0700007537", "Y") livingspace.allocate_to(fellow) fellow = Fellow("staff"+"Njsiritus", "staff"+"Otsdeno", "0700008537", "Y") livingspace.allocate_to(fellow) fellow = Fellow("staff"+"Njsiritus", "staff"+"Otsdeno", "0700009537", "Y") livingspace.allocate_to(fellow) result_3 = LivingSpace.available() self.assertTrue([result, result_3, type(result_2)], [False, False, "set"])
def test_has_name(self): livingspace = LivingSpace("Rando") self.assertEqual(livingspace.name, "RANDO")
def test_allocate_to_new_staff_space(self): livingspace = LivingSpace('Focus') staff = Staff("Neritus", "Otieno", "0784334123") with self.assertRaises(TypeError): result = livingspace.allocate_to(staff)
def reallocate(cls, person, room): #Check if room user is allocating to has the allocation or is lacking capacity if room.has_allocation(person): raise ValueError( "%s-%s is already in %s-%s" % (person.phone, person.last_name, room.name, room.type_)) if room.has_capacity() is False: raise ValueError("Sorry %s is at capacity" % room.name) #Get all current allocations to the given person current_allocations = [] allocated_phones = cls.all_allocated_phones() if person.phone in allocated_phones: for allocation in allocations: phone = allocation[2] if phone == person.phone: current_allocations.append(allocation) """Fellow or Staff allocated to Office or LivingSpace if person is Fellow and person has allocation to office alone: if destined room type is office: allow reallocation from office if destined room type is LivingSpace: allow allocation to LivingSpace if person is Fellow and person has allocation to livingspace alone: if destined room type is office: allow allocation to office if destined room type is LivingSpace: allow reallocation from LivingSpace if person is Staff and has allocation to office alone: allow reallocation from office """ """Fellow allocated to Office and LivingSpace if person is Fellow and person has allocation to both office and livingspace: if destined room type is office: allow reallocation from office if destined room type is LivingSpace: allow reallocation from LivingSpace """ if len(current_allocations) > 0: if len(current_allocations) is 1: allocation = current_allocations[0] allocated_type = allocation[1] allocated_name = allocation[0] if person.type_ is "FELLOW": if allocated_type == room.type_: if room.type_ == "LIVINGSPACE": #if person is Fellow and person has allocation to livingspace alone and destined room type is LivingSpace from app.models.livingspace import LivingSpace old = LivingSpace.from_name(allocated_name) old.arrogate_from(person) room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) elif room.type_ == "OFFICE": #if person is Fellow and person has allocation to office alone and destined room type is office from app.models.office import Office old = Office.from_name(allocated_name) old.arrogate_from(person) room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) else: raise ValueError( "Data in Invalid State. Room type other than LivingSpace and Office allocated." ) else: #if person is Fellow and person has allocation to either livingspace or office alone and destined room type is the opposite if person.type_ == "STAFF": raise ValueError( "Cannot reallocate staff to livingspace.") room.allocate_to(person) print("%s-%s reallocated to %s-%s" % (person.last_name, person.type_, room.name, room.type_)) elif person.type_ == "STAFF": if allocated_type == room.type_: if room.type_ == "OFFICE": #if person is Staff and has allocation to office alone from app.models.office import Office old = Office.from_name(allocated_name) old.arrogate_from(person) room.allocate_to(person) return "%s-%s reallocated to %s-%s" % ( person.last_name, person.type_, room.name, room.type_) else: raise ValueError( "Cannot reallocate staff to livingspace.") else: raise ValueError( "Staff may only be allocated to offices. Allocated room type and destined room type to dont match." ) elif len(current_allocations) is 2: allocation1 = current_allocations[0] allocated_type1 = allocation1[1] allocated_name1 = allocation1[0] allocation2 = current_allocations[1] allocated_type2 = allocation2[1] allocated_name2 = allocation2[0] if person.type_ == "FELLOW": if allocated_type1 == room.type_: if room.type_ == "LIVINGSPACE": #if person is Fellow and person has allocation to both office and livingspace and destined room type is LivingSpace from app.models.livingspace import LivingSpace old = LivingSpace.from_name(allocated_name1) old.arrogate_from(person) room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) elif room.type_ == "OFFICE": #if person is Fellow and person has allocation to both office and livingspace and destined room type is office from app.models.office import Office old = Office.from_name(allocated_name1) old.arrogate_from(person) room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) else: raise ValueError( "Data in Invalid State. Room type other than LivingSpace and Office allocated" ) elif allocated_type2 == room.type_: if room.type_ == "LIVINGSPACE": #if person is Fellow and person has allocation to both office and livingspace and destined room type is LivingSpace from app.models.livingspace import LivingSpace old = LivingSpace.from_name(allocated_name2) old.arrogate_from(person) room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) elif room.type_ == "OFFICE": #if person is Fellow and person has allocation to both office and livingspace and destined room type is office from app.models.office import Office old = Office.from_name(allocated_name2) old.arrogate_from(person) room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) else: raise ValueError( "Data in Invalid State. Room type other than LivingSpace and Office allocated" ) else: raise ValueError( "Data in Invalid State. Allocated room type and destined room type to dont match" ) else: raise ValueError( "Data in Invalid State. A non fellow cannot have more than one allocation" ) else: raise ValueError( "Data in Invalid State. Person can have no more than 2 allocations" ) else: if room.type_ == "LIVINGSPACE": if person.type_ == "STAFF": raise ValueError("Cannot reallocate staff to livingspace.") room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name) elif room.type_ == "OFFICE": room.allocate_to(person) return "The %s: %s %s has been successfully reallocated to the %s: %s" % ( person.type_, person.first_name, person.last_name, room.type_, room.name)
def test_isinstance_of_room(self): livingspace = LivingSpace('Focuspoint') self.assertIsInstance(livingspace, Room)
def test_add_an_existing_living_space(self): livingspace = LivingSpace('MySpace4545') LivingSpace.add(livingspace) with self.assertRaises(ValueError): LivingSpace.add(livingspace)
def test_constructor_negative_float(self): livingspace = LivingSpace(-2.3) self.assertEqual(livingspace.name, "2.3")
def test_constructor_capitalize(self): livingspace = LivingSpace("Lolz") self.assertEqual(livingspace.name, "LOLZ")
def test_constructor_negative_integer(self): livingspace = LivingSpace(-23) self.assertEqual(livingspace.name, "23")
def test_constructor_spaced_name(self): livingspace = LivingSpace("ri ck") self.assertEqual(livingspace.name, "RI_CK")
def test_constructor_name_too_large(self): with self.assertRaises(ValueError): livingspace = LivingSpace("Theraininspainstaysmainlyontheplainwashingawaythegrain")
def test_constructor_none(self): with self.assertRaises(ValueError): livingspace = LivingSpace(None)
def test_capacity_is_six(self): livingspace = LivingSpace("Rand") self.assertEqual(livingspace.capacity, 6)
def test_print_empty_room(self): self.clear_stores() livingspace = LivingSpace("NDO1") with self.assertRaises(Exception): output = livingspace.allocations()
def test_constructor_empty_string(self): with self.assertRaises(ValueError): livingspace = LivingSpace("")