def do_reallocate_person(self, args): """ Reallocates a person to a new room using their unique identifier When loaded into the system, each person has a unique staff_id number This is used to reallocate them to specific rooms Usage: reallocate_person <person_identifier> <new_room_name> """ person = args["<person_identifier>"] room = args["<new_room_name>"] if person in Amity.fellows.keys(): perp = Amity.fellows[person] elif person in Amity.staff.keys(): perp = Amity.staff[person] if room in Amity.offices.keys(): print(Amity.reallocate(perp, room, "office")) elif room in Amity.livingspaces.keys(): room_type = "livingspace" if person in Amity.staff.keys(): print("Staff don't have livingspaces") else: print(Amity.reallocate(perp, room, "livingspace")) else: print("Room does not exist")
def do_save_state(self, args): """ Persists all data in the system's working set to a specified database If no database is specified, a default amity_db is used Usage: save_state [--db=sqlite_database] """ Amity.save_state(args["--db"])
def test_adds_person(self, input): #test that new people are actually assigned rooms with mock.patch("classes.amity.Staff") as patched_staff: self.assertEqual(len(Amity.staff), 0) Amity.add_person("Samuel", "Gaamuwa", "STAFF") self.assertEqual(len(Amity.staff), 1) with mock.patch("classes.amity.Fellow") as patched_fellow: self.assertEqual(len(Amity.fellows), 0) Amity.add_person("Samuel", "Gaamuwa", "FELLOW") self.assertEqual(len(Amity.fellows), 1)
def test_create_room(self): #test that create room creates both offices and livingspaces #query the dictionaries to see the rooms are created with mock.patch("classes.amity.Office") as patched_office: Amity.create_room("Oculus", "office") self.assertIn("Oculus", Amity.offices.keys()) #do the same for livingspace with mock.patch("classes.amity.LivingSpace") as patched_lspace: Amity.create_room("Python", "livingspace") self.assertIn("Python", Amity.livingspaces.keys())
def do_print_unallocated(self, args): """ Prints out all the unallocated people in the System Also writes out the unallocated people to a text file if specified Usage: print_unallocated [--o=filename] """ if args["--o"] is None: print(Amity.print_unallocated()) else: print(Amity.print_unallocated(args["--o"]))
def do_print_allocations(self, args): """ Prints all the rooms in the system and the people allocated to them Also writes out the allocations to a text file if specified Usage: print_allocations [--o=filename] """ if args["--o"] is None: print(Amity.print_allocations()) else: Amity.print_allocations(args["--o"])
def test_prints_allocations(self): #test it prints rooms and those allocated to them mock_office = mock.Mock() mock_office.name = "Narnia" mock_office.current_occupants = ["Samuel Gaamuwa"] Amity.offices["Narnia"] = mock_office Amity.print_allocations("test_out.txt") self.assertTrue(path.isfile("./datafiles/test_out.txt")) with open("./datafiles/test_out.txt") as data: input = data.readlines() self.assertIn("Narnia(office)\n", input) self.assertIn("Samuel Gaamuwa, \n", input)
def test_saves_state(self, mock_db): #test that it saves state to a specified database mock_db.database_insert_livingspace.return_value = None mock_db.database_insert_office.return_value = None mock_db.database_insert_fellow.return_value = None mock_db.database_insert_staff.return_value = None mock_db.database_return_fellow_ids.return_value = [] mock_db.database_return_staff_ids.return_value = [] mock_db.database_update_fellow.return_value = None mock_db.database_update_staff.return_value = None #test that information can be stored in a new specified database Amity.save_state("new_database") self.assertTrue(path.isfile("./new_database"))
def test_cant_assign_office_full(self, input): #test that new people cant be randomly assigned to full rooms #the save function in person automatically calls the assign room function with mock.patch("classes.room.Office") as patched_office: Amity.create_room("Oculus", "office") with mock.patch("classes.person.Staff") as patched_staff: Amity.offices["Oculus"].current_occupants = [ "Rehema Tadaa", "Ruth Tadaa", "Arnold Tadaa", "Whitney Tadaa", "Kimani Tadaa", "Migwi T" ] result = Amity.add_person("Samuel", "Gaamuwa", "STAFF") self.assertEqual("Samuel Gaamuwa added but not assigned room", result)
def test_assigns_office(self): #test that the room can assign a room #create mock object of the staff member mock_staff = mock.Mock() mock_staff.first_name = "Samuel" mock_staff.last_name = "Gaamuwa" mock_staff.staff_id = "ST-01" mock_staff.allocated_office = "" with mock.patch("classes.room.Office") as patched_office: Amity.create_room("Oculus", "office") self.assertEqual(mock_staff.allocated_office, "") Amity.assign_office(mock_staff) self.assertEqual(mock_staff.allocated_office, "Oculus")
def test_assigns_livingspace(self): #test assigns room if requested #create mock object of the fellow mock_fellow = mock.Mock() mock_fellow.first_name = "Samuel" mock_fellow.last_name = "Gaamuwa" mock_fellow.staff_id = "FL-01" mock_fellow.allocated_office = "Narnia" mock_fellow.allocated_livingspace = "" with mock.patch("classes.room.LivingSpace") as patched_livingspace: Amity.create_room("Python", "livingspace") self.assertEqual(mock_fellow.allocated_livingspace, "") Amity.assign_livingspace(mock_fellow) self.assertEqual(mock_fellow.allocated_livingspace, "Python")
def test_loads_people(self, mock_input): #test that it loads people from a specified file and allocates them rooms #mock the office object mock_office = mock.Mock() mock_office.name = "Narnia" mock_office.current_occupants = [] Amity.offices["Narnia"] = mock_office #assert that the office occupants are none self.assertEqual(len(Amity.offices["Narnia"].current_occupants), 0) #load people into the system Amity.load_people("new_people.txt") #assert that the loaded people are in the system self.assertIn("ST-01 Samuel Gaamuwa", Amity.offices["Narnia"].current_occupants) self.assertIn("ST-02 Isaac Dhibikirwa", Amity.offices["Narnia"].current_occupants)
def do_load_state(self, args): """ Loads data from a specified database into the system's working data set Usage: load_state <sqlite_database> """ print(Amity.load_state(args["<sqlite_database>"]))
def do_add_person(self, args): """ Adds a person to the System One specified person is added to the system using their details First name, last name, type of employee For Fellows, they have the option to request for accommodation Usage: add_person <first_name> <last_name> <title> [--wa=N] """ print( Amity.add_person(args["<first_name>"], args["<last_name>"], args["<title>"].upper(), args["--wa"]))
def do_create_room(self, args): """ Creates new rooms in Amity Mulitple rooms of the same kind can be created at once The rooms are added to the command the room type added at the end Usage: create_room <room_name>... """ kind = input("Enter the type of room: ") rooms = args["<room_name>"] for room in rooms: print(Amity.create_room(room, kind.lower()))
def test_reallocates_office(self, mock_fellow, mock_office): #tests that people are reallocated to requested rooms #mock the fellow object mock_fellow = mock.Mock() mock_fellow.first_name = "Samuel" mock_fellow.last_name = "Gaamuwa" mock_fellow.staff_id = "FL-01" mock_fellow.allocated_office = "Narnia" mock_fellow.allocated_livingspace = "Python" #mock the oculus office object and add to amity offices mock_office = mock.Mock() mock_office.name = "Narnia" mock_office.current_occupants = ["FL-01 Samuel Gaamuwa"] mock_office2 = mock.Mock() mock_office2.name = "Valhala" mock_office2.current_occupants = [] #add the mock object to the offices dictionary Amity.offices["Narnia"] = mock_office Amity.offices["Valhala"] = mock_office2 self.assertEqual(mock_fellow.allocated_office, "Narnia") Amity.reallocate(mock_fellow, "Valhala", "office") self.assertEqual(mock_fellow.allocated_office, "Valhala")
def do_load_people(self, args): """ Loads new people into the system from a specified text file in the format OLUWAFEMI SULE FELLOW Y DOMINIC WALTERS STAFF SIMON PATTERSON FELLOW Y MARI LAWRENCE FELLOW Y LEIGH RILEY STAFF TANA LOPEZ FELLOW Y KELLY McGUIRE STAFF The file is placed in the datafiles folder Usage: load_people <filename> """ print(Amity.load_people(args["<filename>"]))
def test_loads_state(self, mock_data): #test that it loads data from specified database mock_data.database_return_all_offices.return_value = [ "Hogwarts", "Narnia" ] mock_data.database_return_all_livingspaces.return_value = [ "Python", "Haskel" ] mock_data.database_return_all_fellows.return_value = [ ("sam", "g", "1", "Hogwarts", "haskel") ] mock_data.database_return_all_staff.return_value = [("rehema", "w", "2", "Hogwarts")] data = Amity.load_state("new_rooms_db") #assert confirmation of loading to system self.assertEqual( "\nDatabase new_rooms_db has been loaded in the working data set\n", data)
def test_prints_unallocated(self): #test that it prints out unallocated to a test file Amity.print_unallocated("unallocated.txt") self.assertTrue(path.isfile("./datafiles/unallocated.txt"))
def test_object_of(self): obj = Amity() self.assertTrue(type(obj) is Amity)