def test_fellow_load_state(self): path = "db/" file_name = "mydb" file_ = file_name + ".db" #clean up to avoid conflict between tests os.remove(path + file_) #clear memory stores self.clear_stores() #memory fellow1 = Fellow("osdflkjsd", "eksdlkfjk", "774881228", "Y") fellow1.register() Person.save_state(file_name) #clear memory stores self.clear_stores() #memory Person.load_state(file_name) fellow = Fellow.from_phone("774881228") #db engine = create_engine("sqlite:///" + path + file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_fellow = session.query(Person).filter_by( phonenumber="774881228").first() session.close() #compare full_fellow = [ fellow.first_name, fellow.last_name, fellow.phone, fellow.type_, fellow.opt_in ] full_db_fellow = [ db_fellow.firstname, db_fellow.lastname, db_fellow.phonenumber, db_fellow.role, db_fellow.optin ] self.assertEqual(full_db_fellow, full_fellow)
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_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 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 setUp(self): self.tom_fellow = Fellow('Tom', 'Soyer', True) self.becky_fellow = Fellow('Rebecca', 'Storm', False, 'The Office') self.reagan_fellow = Fellow('Reagan', 'West', True, 'The Office', 'The Pad') self.fellows = { self.tom_fellow.first_name + ' ' + self.tom_fellow.last_name: self.tom_fellow, self.becky_fellow.first_name + ' ' + self.becky_fellow.last_name: self.becky_fellow, self.reagan_fellow.first_name + ' ' + self.reagan_fellow.last_name: self.reagan_fellow } self.pink_living_space = LivingSpace('Pink Living Space', self.fellows)
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_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 get_person(phone): try: return Fellow.from_phone(phone) except ValueError: pass try: return Staff.from_phone(phone) except ValueError: raise ValueError("specifed phone is unknown")
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_fellow_to_livingspace(self): livingspace = LivingSpace('My9990') LivingSpace.add(livingspace) fellow = Fellow("jjjsj", "lksls", "07009987", "Y") livingspace.allocate_to(fellow) livingspace1 = LivingSpace('My9991') LivingSpace.add(livingspace1) Room.reallocate(fellow, livingspace1) self.assertEqual([livingspace1.has_allocation(fellow), livingspace.has_allocation(fellow)], [True, False])
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)
class TestFellow(unittest.TestCase): """Test cases for the fellow class""" # Instantiating objects for using in the tests def setUp(self): self.tom_fellow = Fellow('Tom', 'Soyer', True) self.becky_fellow = Fellow('Rebecca', 'Storm', False, 'The Office') self.reagan_fellow = Fellow('Reagan', 'West', True, 'The Office', 'The Pad') # Test if the class creates an instance of itself def test_fellow_instance(self): self.assertIsInstance( self.tom_fellow, Fellow, msg='The object should be an instance of the `Fellow` class') # Test if the class creates an instance of Person def test_person_instance(self): self.assertIsInstance( self.reagan_fellow, Person, msg='The object should be an instance of the `Person` class') # Test if the class creates a type of itself def test_type(self): self.assertTrue((type(self.becky_fellow) is Fellow), msg='The object should be of type `Fellow`') # Test if reallocate_living_space method returns the right value for the given inputs def test_allocate_living_space(self): self.assertTrue( self.tom_fellow.reallocate_living_space('The Man Cave'), msg='Tom should get a new living space') # Test if reallocate_office method returns the right value for the given inputs def test_allocate_office(self): self.assertTrue(self.tom_fellow.reallocate_office('The New Office'), msg='Tom should get a new office')
def test_register_new_phone(self): fellow = Fellow("Standard", "SKIDH", "84900874855", "Y") initial_person_count = len(persons_detail) fellow.register() new_person_count = len(persons_detail) self.assertEqual(initial_person_count + 1, new_person_count)
def test_constructor_upper(self): fellow = Fellow("Lolz", "Skid", "8483774855", "Y") self.assertEqual([fellow.first_name, fellow.last_name, fellow.phone, fellow.opt_in], ["LOLZ", "SKID", "8483774855", "Y"])
def test_register_phone_exists_error(self): fellow = Fellow("LIOLZ", "SKIDH", "8483664855", "Y") fellow.register() fellow_1 = Staff("LIOLZ", "SKIDH", "8483664855", "Y") with self.assertRaises(ValueError): fellow_1.register()
def test_constructor_input_too_large(self): with self.assertRaises(ValueError): fellow = Fellow("Theraininspainstaysmainlyontheplainwashingawaythegrain", "Diredes", "0794038434", "Y")
def test_constructor_spaced_name(self): fellow = Fellow("ri ck", "Dir ede", "0794 838434", "Y") self.assertEqual([fellow.first_name, fellow.last_name, fellow.phone, fellow.opt_in], ["RICK", "DIREDE", "0794838434", "Y"])
def test_has_opt_in(self): fellow = Fellow("Randode", "Direde", "0794838434", "N") self.assertEqual(fellow.opt_in, "N")
def test_constructor_no_argument(self): with self.assertRaises(TypeError): fellow = Fellow()
def test_has_last_name(self): fellow = Fellow("Randod", "Dired", "0994838434", "Y") self.assertEqual(fellow.last_name, "DIRED")
def test_has_phone(self): fellow = Fellow("Randode", "Direde", "0794838434", "N") self.assertEqual(fellow.phone, "0794838434")
def test_has_first_name(self): fellow = Fellow("Rando", "Dire", "0754838434", "Y") self.assertEqual(fellow.first_name, "RANDO")
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 add_person(self, firstname, surname, person_type, wants_accomodation='N'): if person_type.lower() not in ['fellow', 'staff']: return (error('Only fellow and staff allowed!')) if not isinstance(firstname, str) or not isinstance(surname, str): return (error('People names can only be strings!')) if firstname + ' ' + surname in self.all_people: return (error('%s %s exists!' % (firstname, surname))) else: if represents_int(firstname) or represents_int(surname): return error('Names can not be or contain integers!') if person_type.lower() == 'fellow': # create a fellow firstname = firstname.capitalize() surname = surname.capitalize() fellow = Fellow(firstname, surname) self.fellows.append(firstname + ' ' + surname) self.all_people.append(firstname + ' ' + surname) if self.offices: all_offices = list(self.offices.keys()) checked_offices = [] while True: office = random.choice(list(self.offices)) if len(self.offices[office]) < 6: self.offices[office].append(firstname + ' ' + surname) print( success( 'Fellow %s %s has been assigned office %s!' % (firstname, surname, office))) break if office not in checked_offices: checked_offices.append(office) if checked_offices == all_offices: print(error('All offices are full at the moment!')) break else: print(error('No office to assign!')) if wants_accomodation == 'Y': self.wants_accomodation.append(firstname + ' ' + surname) if wants_accomodation == 'Y' and self.livingspaces: all_livingspaces = list(self.livingspaces.keys()) checked_livingspaces = [] while True: room = random.choice(list(self.livingspaces)) if len(self.livingspaces[room]) < 4: self.livingspaces[room].append(firstname + ' ' + surname) print( success( 'Fellow %s %s has been assigned livingspace %s!' % (firstname, surname, room))) break if room not in checked_livingspaces: checked_livingspaces.append(room) if checked_livingspaces == all_livingspaces: print( error( 'All livingspaces are full at the moment!') ) break return (success('Fellow %s %s has been added successfully!' % (firstname, surname))) elif person_type.lower() == 'staff': # create a staff member if wants_accomodation == 'Y': print(error('Staff can not be allocated livingspace!')) staff = Staff(firstname, surname) self.staff.append(firstname + ' ' + surname) self.all_people.append(firstname + ' ' + surname) if self.offices: office = random.choice(list(self.offices)) self.offices[office].append(firstname + ' ' + surname) print( success('Staff %s %s has been assigned office %s!' % (firstname, surname, office))) else: print(error('No office to assign!')) return (success('Staff %s %s has been added successfully!' % (firstname, surname)))
def test_creates_fellow_instance(self): self.fellow = Fellow('Joshua', 'Ondieki') self.assertTrue('Joshua' == self.fellow.firstname and 'Ondieki' == self.fellow.surname)
def add_person(first_name, last_name, person_type='', wants_accomodation=False): name = first_name + ' ' + last_name output = '' if name and person_type: if name and name not in get_all_office_occupants(): if not [ a_room for a_room in the_dojo.get_vacant_rooms().values() if isinstance(a_room, Office) ]: initial_room_count = len(the_dojo.rooms) final_room_count = 0 while initial_room_count >= final_room_count: appendix = random.choice( range(0, total_number_of_rooms + 1)) create_room('office', 'Office ' + str(appendix)) final_room_count = len(the_dojo.rooms) an_office = random.choice([ a_room for a_room in the_dojo.get_vacant_rooms().values() if isinstance(a_room, Office) ]) if person_type.lower() == 'fellow': if not wants_accomodation and name not in get_all_office_occupants( ): a_fellow = Fellow(first_name, last_name, wants_accomodation, an_office.name) an_office.occupants[name] = a_fellow if name in get_all_office_occupants(): output += 'Fellow ' + name + ' has been successfully added. \n' + \ first_name + ' has been allocated the office ' + an_office.name elif wants_accomodation and name not in get_all_livingspace_occupants( ) and name not in get_all_office_occupants(): if not [ a_room for a_room in the_dojo.get_vacant_rooms().values() if isinstance(a_room, LivingSpace) ]: initial_room_count = len(the_dojo.rooms) final_room_count = 0 while initial_room_count >= final_room_count: appendix = random.choice( range(0, total_number_of_rooms + 1)) create_room('livingspace', 'Livingspace ' + str(appendix)) final_room_count = len(the_dojo.rooms) a_livingspace = random.choice([ a_room for a_room in the_dojo.get_vacant_rooms().values() if isinstance(a_room, LivingSpace) ]) a_fellow = Fellow(first_name, last_name, wants_accomodation, an_office.name, a_livingspace.name) a_livingspace.occupants[name] = a_fellow an_office.occupants[name] = a_fellow if name in get_all_office_occupants( ) and name in get_all_livingspace_occupants(): output += 'Fellow ' + name + ' has been successfully added. \n' + \ first_name + ' has been allocated the office ' + an_office.name + '\n' + \ first_name + ' has been allocated the livingspace ' + a_livingspace.name elif person_type.lower() == 'staff': a_staff = Staff(first_name, last_name, an_office.name) an_office.occupants[name] = a_staff if name in get_all_office_occupants(): output += 'Staff ' + name + ' has been successfully added. \n' + \ first_name + ' has been allocated the office ' + an_office.name if name not in get_all_office_occupants(): if person_type.lower( ) == 'fellow' and name not in get_all_livingspace_occupants(): new_fellow = Fellow(first_name, last_name) unallocated_people[name] = new_fellow output = 'Fellow ' + name + ' is unallocated' elif person_type.lower() == 'staff': new_staff = Staff(first_name, last_name) unallocated_people[name] = new_staff output = 'Staff ' + name + ' is unallocated' else: new_person = Person(first_name, last_name) unallocated_people[name] = new_person output = 'Person ' + name + ' is unallocated' return output