class AmityModelTests(unittest.TestCase): def setUp(self): """Set up Amity ()""" self.amity = Amity() def tearDown(self): del self.amity def test_create_office(self): '''tests it creates office''' new_office = self.amity.create_room("office", "medical") self.assertEqual(new_office, "MEDICAL office created successfully") def test_create_living_space(self): '''tests it creates living space''' new_living_space = self.amity.create_room("Livingspace", "Mara") self.assertEqual(new_living_space, "MARA living space created successfully") def test_wrong_room_type(self): '''tests error message if wrong room type is put''' new_wrong_room_type = self.amity.create_room("Something Wrong", "Wrong Room") self.assertEqual(new_wrong_room_type, "invalid entry. Please enter office or living space") def test_staff_cannot_be_allocated_living_space(self): staff_to_living = self.amity.add_person("Kevin", "Kamau", "Staff", "Y") self.assertEqual(staff_to_living, "Staff cannot be allocated Living Space") def test_wrong_designation(self): person_wrong_des = self.amity.add_person("Evalyn", "Kyalo", "Watchman") self.assertEqual(person_wrong_des, "Incorrect entry. Please enter staff or fellow") def test_reallocate_person(self): self.amity.create_room("Office", "Jamuhuri") self.amity.add_person("James", "Kabue", "Staff") self.amity.create_room("Office", "Madaraka") test_reallocate = self.amity.reallocate_person("James", "Kabue", "Madaraka") self.assertEqual(test_reallocate, "JAMES KABUE reallocated to MADARAKA office") def test_reallocate_to_the_same_room(self): self.amity.create_room("Office", "Bagdad") self.amity.add_person("Alex", "Simanzi", "Staff") test_reallocate = self.amity.reallocate_person("Alex", "Simanzi", "Bagdad") self.assertEqual(test_reallocate, "Cannot reallocate to the same room") def test_reallocate_to_the_non_existent_room(self): self.amity.create_room("Office", "Bagdad") self.amity.add_person("Alex", "Simanzi", "Staff") test_reallocate = self.amity.reallocate_person("Alex", "Simanzi", "Israel") self.assertEqual(test_reallocate, "Room does not exist")
class ClassAmitySuccessTest(unittest.TestCase): def setUp(self): self.amity = Amity() self.amity.all_people = [] self.amity.all_rooms = [] self.amity.office_allocations = defaultdict(list) self.amity.lspace_allocations = defaultdict(list) self.amity.fellows_list = [] self.amity.staff_list = [] def test_add_person(self): self.amity.add_person("awesome", "fellow", "y") self.assertEqual(len(self.amity.all_people), 1) def test_add_person_failure(self): self.amity.add_person("angie", "staff") people_names = [people.person_name for people in self.amity.all_people] self.assertIn("angie", people_names) msg = self.amity.add_person("angie", "staff") self.assertEqual( msg, "sorry, this user already exists.please choose another name") def test_room_added_to_list(self): self.amity.create_room("purple", "office") self.assertEqual(len(self.amity.all_rooms), 1) def test_room_with_same_name_not_created(self): self.amity.create_room("purple", "office") r_names = [r.room_name for r in self.amity.all_rooms] self.assertIn("purple", r_names) msg = self.amity.create_room("purple", "office") self.assertEqual( msg, "sorry, room already exists!please choose another name") def test_office_allocation(self): self.amity.add_person("Angie", "Staff", "Y") for room, occupants in self.amity.office_allocations: self.assertIn("Angie", self.amity.office_allocations[occupants]) def test_lspace_allocation(self): self.amity.add_person("Angie", "fellow", "Y") for room, occupants in self.amity.lspace_allocations: self.assertIn("Angie", self.amity.lspace_allocations[occupants]) def test_reallocate_person(self): self.amity.create_room("blue", "office") self.amity.add_person("angie", "staff") print(self.amity.office_allocations) self.amity.reallocate_person("angie", "blue") self.assertIn("angie", self.amity.office_allocations["blue"]) def test_person_is_removed_from_old_room(self): self.amity.create_room("blue", "office") self.amity.add_person("angie", "staff") self.assertIn("angie", self.amity.office_allocations["blue"]) self.amity.create_room("yellow", "office") self.amity.reallocate_person("angie", "yellow") self.assertNotIn("angie", self.amity.office_allocations["blue"]) def test_load_from_file(self): dirname = os.path.dirname(os.path.realpath(__file__)) self.amity.load_people(os.path.join(dirname, "test.txt")) self.assertEqual(len(self.amity.fellows_list), 4) self.assertEqual(len(self.amity.staff_list), 3) def test_it_prints_unallocated(self): self.amity.print_unallocated('test_print') self.assertTrue(os.path.isfile('test_print.txt')) os.remove('test_print.txt') def test_it_saves_state(self): self.amity.save_state('test.db') self.assertTrue(os.path.isfile('test.db.db')) os.remove('test.db.db')
class TestAmity(unittest.TestCase): def setUp(self): self.amity = Amity() def test_create_room(self): '''test to confirm a room has been created''' rooms = self.amity.all_rooms # making sure the rooms are empty self.assertEqual(len(rooms), 0) self.amity.create_room('haskel', 'livingspace') # checking to see if rooms have been added self.assertEqual(len(rooms), 1) def test_office_is_created(self): '''test to confirm an office is created''' office = self.amity.office_rooms self.assertEqual(len(office), 0) self.amity.create_room('valhalla', 'office') self.assertEqual(len(office), 1) def test_living_space_is_created(self): '''test to confirm that living space is created''' livingspace = self.amity.livingspaces self.assertEqual(len(livingspace), 0) self.amity.create_room('php', 'livingspace') self.assertEqual(len(livingspace), 1) def test_fellow_is_created(self): '''tesst to confirm a fellow is created''' self.amity.create_room('valhalla', 'office') fellow = self.amity.fellows self.assertEqual(len(fellow), 0) self.amity.add_person('chironde', 'fellow') self.assertEqual(len(fellow), 1) def test_staff_is_created(self): '''test to confirm staff is created''' self.amity.create_room('hogwarts', 'office') staff = self.amity.staff self.assertEqual(len(staff), 0) self.amity.add_person('njira', 'staff') self.assertEqual(len(staff), 1) def test_people_are_added_to_all_peole_list(self): '''test if all people are added to the people list regardles wether they are fellows or staff ''' everyone = self.amity.all_people self.assertEqual(len(everyone), 0) self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('njira', 'staff') self.amity.add_person('joy', 'fellow', 'Y') self.assertEqual(len(everyone), 2) def test_person_is_allocated_a_room(self): '''test if a person has been reallocated to a specified room''' self.amity.create_room('occulus', 'office') self.amity.add_person('rehema', 'fellow') person = self.amity.fellows[0] roomname = person.office.name self.assertEqual(roomname, 'occulus') def test_a_room_has_a_person(self): '''test a that rooms take in people''' self.amity.create_room('cyan', 'office') self.amity.add_person('jackie', 'fellow') office = self.amity.office_rooms[0] person = office.members[0] person_name = person.name self.assertEqual(person_name, 'jackie') def test_a_person_has_been_reallocated(self): '''test that a person has been reallocated to a different room''' self.amity.create_room('mordor', 'office') self.amity.add_person('joshua', 'staff') staff = self.amity.staff[0] office_name = staff.office.name self.assertEqual(office_name, 'mordor') self.amity.create_room('winterfell', 'office') self.amity.reallocate_person('joshua', 'winterfell') new_office_name = staff.office.name self.assertEqual(new_office_name, 'winterfell') def test_a_person_has_been_removed_from_a_room_after_reallocation(self): '''test that a person has been removed from a room after reallocation''' self.amity.create_room('hogwarts', 'office') self.amity.add_person('rehema', 'fellow') office = self.amity.office_rooms[0] members = office.members self.assertEqual(len(members), 1) self.amity.create_room('Occulus', 'office') self.amity.reallocate_person('rehema', 'Occulus') office = self.amity.office_rooms[0] members = office.members self.assertEqual(len(members), 0) def test_print_allocations_to_terminal(self): '''test that allocated people are printed to the terminal''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.add_person('valt honks', 'fellow', 'Y') self.amity.print_allocations(None) def test_print_allocations_file(self): '''test that allocated people are printed to a file''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.add_person('valt honks', 'fellow', 'Y') self.amity.print_allocations('allocations.txt') def test_people_without_rooms_are_added_to_unallocated_list(self): '''test that people without rooms are added to unallocated list''' unallocated = self.amity.unallocated self.assertEqual(len(unallocated), 0) self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam gaamwa', 'fellow') self.amity.add_person('tom wilkins', 'fellow') self.amity.add_person('tom shitonde', 'fellow', 'Y') self.amity.print_un_allocated(None) unallocated = self.amity.unallocated self.assertEqual(len(unallocated), 2) def test_print_unallocated_people_to_terminal(self): '''test that unallocated people are printed to a the terminal''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.add_person('valt honks', 'fellow', 'Y') self.amity.print_un_allocated(None) def test_print_unallocated_people_to_a_file(self): '''test that unallocated people are printed to a file''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.add_person('valt honks', 'fellow', 'Y') self.amity.print_un_allocated('unallocated.txt') def test_members_of_a_room_are_printed(self): '''test that members of a room are printed''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam gaamwa', 'fellow') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.print_room('hogwarts') def test_people_are_added_to_allocated_(self): '''test that alloacted people are added to a list''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') allocated = self.amity.allocated self.assertEqual(len(allocated), 0) self.amity.add_person('sam gaamwa', 'fellow') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.print_allocations(None) self.assertEqual(len(allocated), 3) def test_fellow_are_allocated_to_allocated_list(self): '''test that an allocated staff is added to allocated list''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam maina', 'fellow') self.amity.print_allocations(None) allocated = self.amity.allocated fellow = allocated[0] self.assertIsInstance(fellow, Fellow) def test_fellow_are_allocated_to_unallocated_list(self): '''test that the person in an unallocated list is a fellow''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') self.amity.add_person('sam maina', 'fellow') self.amity.print_un_allocated(None) unallocated = self.amity.unallocated fellow = unallocated[0] self.assertIsInstance(fellow, Fellow) def test_unalloacted_people_are_added_to_unallocated_list(self): '''test that fellows without living space are added to a list''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') unallocated = self.amity.unallocated self.assertEqual(len(unallocated), 0) self.amity.add_person('sam gaamwa', 'fellow') self.amity.add_person('sam maina', 'staff') self.amity.add_person('tom wilkins', 'fellow') self.amity.print_un_allocated(None) self.assertEqual(len(unallocated), 2) def test_load_people_from_file(self): '''test that people are added from a list and given a room''' self.amity.create_room('hogwarts', 'office') self.amity.create_room('php', 'livingspace') staff_list = self.amity.staff fellows_list = self.amity.fellows self.assertEqual(len(staff_list), 0) self.assertEqual(len(fellows_list), 0) self.amity.load_people("test_people.txt") self.assertEqual(len(staff_list), 3) self.assertEqual(len(fellows_list), 4)
class AmityTestCase(unittest.TestCase): """ Tests for amity """ def setUp(self): self.amity = Amity() self.amity.rooms = [] self.amity.offices = [] self.amity.living_spaces = [] self.amity.people = [] self.amity.fellows = [] self.amity.staff = [] self.amity.waiting_list = [] self.amity.office_waiting_list = [] self.amity.living_space_waiting_list = [] # ... Tests for create_room ...# def test_create_room_add_room_successfully(self): """ Test that room was created successfully """ self.amity.create_room(["Hogwarts"], "office") self.assertEqual("Hogwarts", self.amity.rooms[0].room_name) def test_create_room_duplicates(self): """ Test that room can only be added once """ self.amity.create_room(["Hogwarts"], "office") self.assertIn("Hogwarts", [i.room_name for i in self.amity.rooms]) length_of_rooms = len(self.amity.rooms) self.amity.create_room(["Hogwarts"], "office") self.assertEqual(len(self.amity.rooms), length_of_rooms) # ... Tests for add_person ...# def test_add_person_add_fellow(self): """ Test that fellow was added successfully """ self.amity.create_room(["BlueRoom"], "Office") person = self.amity.add_person("Robley", "Gori", "fellow", "N") p_id = self.amity.people[0].person_id self.assertIn( "Robley Gori of id " + str(p_id) + " has been added to " "the system", person) def test_add_person_add_staff(self): """ Test that staff was added successfully """ self.amity.create_room(["Maathai"], "LivingSpace") person = self.amity.add_person("Robley", "Gori", "fellow", "Y") p_id = self.amity.people[0].person_id self.assertIn( "Robley Gori of id " + str(p_id) + " has been added to " "the system", person) def test_add_person_add_person_to_office(self): """ Test that person is successfully added to office """ self.amity.create_room(["red"], "Office") self.amity.add_person("Jack", "Line", "staff", "N") person = self.amity.add_person("Robley", "Gori", "fellow", "N") self.assertIn("", person) def test_add_person_add_fellow_to_living_space(self): """ Test that fellow is successfully added to living space """ self.amity.create_room(["blue"], "Office") self.amity.add_person("Jack", "Line", "staff", "N") person = self.amity.add_person("Robley", "Gori", "fellow", "N") self.assertIn("", person) def test_add_person_add_staff_to_living_space(self): """ Test that staff cannot be added to living space """ self.amity.create_room(["Maathai"], "LivingSpace") person = self.amity.add_person("Robley", "Gori", "staff", "Y") self.assertIn("Staff cannot be allocated a living space!", person) def test_add_person_add_person_full_office(self): """ Test that person is added to waiting list if offices are full """ self.amity.create_room(["PinkRoom"], "Office") self.amity.load_people("people.txt") person = self.amity.add_person("Jackline", "Maina", "Fellow", "Y") self.assertIn( "Jackline Maina has been added to the office waiting " "list", person) def test_add_person_add_fellow_full_living_space(self): """ Test that fellow is added to waiting list if living spaces are \ full """ self.amity.create_room(["Maathai"], "LivingSpace") self.amity.add_person("Flevian", "Kanaiza", "Fellow", "Y") self.amity.add_person("Robley", "Gori", "Fellow", "Y") self.amity.add_person("Jus", "Machungwa", "Fellow", "Y") self.amity.add_person("Angela", "Mutava", "Fellow", "Y") person = self.amity.add_person("Jackline", "Maina", "Fellow", "Y") self.assertIn( "Jackline Maina has been added to the living space " "waiting list", person) # ... Tests for reallocate person ...# def test_reallocate_person_reallocates_person(self): """ Tests that person is reallocated """ self.amity.create_room(["PinkRoom"], "Office") self.amity.add_person("Robley", "Gori", "fellow", "N") p_id = self.amity.people[0].person_id person = self.amity.reallocate_person(p_id, "ConferenceCentre") self.assertIn( "Sorry. Room ConferenceCentre does not exist in the " "system.", person) # ... Tests for load people ...# def test_load_people_loads_people_from_txt_file(self): """ Tests that people are successfully loaded from a txt file """ self.amity.load_people("people.txt") self.assertTrue("Data successfully loaded to amity!") # ... Tests for print allocations ...# def test_print_allocations_prints_allocations_to_screen(self): """To test if method prints allocations to screen.""" self.amity.create_room(["red"], "office") self.amity.load_people("people.txt") self.amity.print_allocations() self.assertTrue("These are the rooms and there allocations") def test_print_allocations_prints_allocations_to_txt_file(self): """To test if method prints allocations to txt file.""" self.amity.create_room(["red"], "office") self.amity.load_people("people.txt") self.amity.print_allocations("files/allocations.txt") self.assertTrue("Data has been dumped to file") # ... Tests for unallocated rooms ...# def test_print_unallocated_prints_unallocated_people_to_screen(self): """To test if method prints unallocated people to screen.""" self.amity.load_people("people.txt") self.amity.load_people("people.txt") self.amity.print_unallocated() self.assertTrue("These are the people in the waiting list") def test_print_unallocated_prints_unallocated_people_to_txt_file(self): """To test if method prints unallocated people to txt file.""" self.amity.load_people("people.txt") self.amity.print_unallocated("files/unallocated.txt") self.assertTrue("Data has been dumped to file") # ... Tests for print room ...# def test_print_room_prints_all_people_in_room_name_to_screen(self): """ It tests that all people in a room name are printed to screen """ self.amity.create_room(["red"], "office") self.amity.load_people("people.txt") self.amity.print_room("red") self.assertTrue("red") # ... Tests for save state ...# def test_save_state_adds_data_to_database(self): """ Test to affirm that data from the application is successfully \ added to the database """ self.amity.save_state() self.assertTrue("Data successfully saved to database!") # ... Tests for load state ...# def test_load_state_successfully_loads_data_from_database(self): """ Test that data is successfully loaded from database """ self.amity.save_state("amity.db") self.assertTrue("Data successfully loaded to amity!")
class AmityTestCase(unittest.TestCase): """Test cases for Amity class""" def setUp(self): self.living_space = LivingSpace("Swift") self.office = Office("Scala") self.fellow = Fellow("Mike Jon") self.fellow2 = Fellow("Lucas Chan", "Y") self.staff = Staff("Rick Man") self.amity = Amity() def test_rooms_are_added_successfully(self): """Tests offices of living spaces are created""" self.amity.create_room("Scala", "O") self.amity.create_room("Go", "L") self.assertEqual(len(self.amity.list_of_rooms), 2) def test_office_is_created_successfully(self): """Tests offices are created""" office = self.amity.create_room("Office", "O") self.assertIsInstance(office, Office) def test_living_space_is_created_successfully(self): """Tests living_spaces are created""" l_space = self.amity.create_room("Keja", "L") self.assertIsInstance(l_space, LivingSpace) def test_living_space_does_not_exceed_four_people(self): """Tests living space does not exceed capacity""" self.amity.list_of_rooms.append(self.living_space) self.living_space.occupants = ["Tom", "Dick", "Harry", "Johny"] self.amity.allocate_room(self.fellow, "Y") self.assertEqual(len(self.amity.unallocated_members), 1) self.assertEqual(len(self.living_space.occupants), 4) def test_office_does_not_exceed_six_people(self): """Tests office does not exceed capacity""" self.amity.list_of_rooms.append(self.office) self.office.occupants = [ "alpha", "beta", "charlie", "delta", "echo", "foxtrot" ] self.amity.allocate_room(self.staff, "N") self.assertEqual(len(self.amity.unallocated_members), 1) self.assertEqual(len(self.office.occupants), 6) def test_staff_is_not_allocated_living_space(self): """Tests staff members can only be in offices""" self.amity.list_of_rooms.append(self.living_space) self.amity.allocate_room(self.staff, "N") self.assertEqual(len(self.amity.list_of_rooms[0].occupants), 0) self.assertEqual(len(self.amity.unallocated_members), 1) def test_duplicate_rooms_are_not_added(self): """Tests rooms with same name are not allowed""" self.amity.list_of_rooms.append(self.living_space) self.assertEqual(self.amity.create_room("Swift", "L"), "Room already exists") def test_fellow_gets_office_by_default(self): """Tests fellow is created and allocated room""" self.amity.list_of_rooms.append(self.office) self.amity.add_person("Tom", "Riley", "Fellow") self.assertTrue(self.amity.list_of_rooms[0].occupants[0].person_name == "Tom Riley".upper()) def test_staff_member_is_added_successfully(self): """Tests staff member is created and allocated room""" self.amity.list_of_rooms.append(self.office) self.amity.add_person("Rick", "James", "Staff") self.assertEqual(len(self.amity.allocated_members), 1) def test_people_are_loaded_from_file_successfully(self): """Tests ii accepts data from file""" with patch("builtins.open", mock_open(read_data="sample text")) as m: self.amity.load_people("file") m.assert_called_with("file", 'r') def test_for_invalid_person_role(self): """Tests invalid person role is not allowed""" res = self.amity.add_person("Guy", "Ross", "Invalid") self.assertEqual(res, "Invalid is not a valid person role") def test_members_are_added_to_waiting_list_if_no_rooms(self): """Tests unallocated people are added to waiting list""" self.amity.add_person("Roomless", "Man", "Fellow", "Y") self.amity.add_person("Roomless", "Woman", "Staff") self.amity.add_person("Random", "Person", "Fellow") self.assertEqual(len(self.amity.unallocated_members), 3) def test_members_are_added_to_waiting_list_if_rooms_full(self): """Tests members who miss rooms are added to waiting list""" self.living_space.occupants += ["one", "two", "three", "four"] self.office.occupants += ["one", "two", "three", "four", "five", "six"] self.amity.add_person("Molly", "Sue", "Fellow", "Y") self.amity.add_person("Ledley", "Moore", "Staff") self.assertEqual(len(self.amity.unallocated_members), 2) def test_fellow_gets_office_and_living_space_if_wants_room(self): """Tests fellow who wants accomodation gets a living space""" self.amity.list_of_rooms.append(self.living_space) self.amity.list_of_rooms.append(self.office) self.amity.add_person("Martin", "Luther", "Fellow", "Y") self.assertTrue(self.amity.allocated_members[0].person_name == "Martin Luther".upper()) self.assertEqual(self.amity.list_of_rooms[0].occupants[0].person_name, "MARTIN LUTHER") self.assertEqual(self.amity.list_of_rooms[1].occupants[0].person_name, "MARTIN LUTHER") def test_cannot_reallocate_non_existent_person(self): """Tests members not allocated cannot be reallocated""" self.amity.list_of_rooms.append(self.living_space) res = self.amity.reallocate_person("No Name", "Swift") self.assertEqual(res, "Person not found") def test_cannot_reallocate_non_existent_room(self): """Tests reallocation only allows valid rooms""" self.amity.allocated_members.append(self.staff) res = self.amity.reallocate_person("Rick Man", "Inexistent") self.assertEqual(res, "Room not found") def test_cannot_reallocate_to_same_room(self): """ Tests person cannot be reallocated to the same room """ self.amity.list_of_rooms.append(self.office) self.amity.list_of_rooms[0].occupants.append(self.fellow) self.amity.allocated_members.append(self.fellow) res = self.amity.reallocate_person("Mike Jon", "Scala") self.assertEqual(res, "Person is already in room") def test_cannot_reallocate_staff_to_living_space(self): """Tests staff cannot be reallocated to livingspace""" self.amity.list_of_rooms += [self.office, self.living_space] self.amity.allocated_members.append(self.staff) self.amity.list_of_rooms[0].occupants.append(self.staff) res = self.amity.reallocate_person("Rick Man", "Swift") self.assertEqual(res, "Staff cannot be allocated living space") def test_cannot_reallocate_person_to_full_room(self): """Tests person cannot be reallocated a full room""" self.amity.list_of_rooms.append(self.living_space) self.amity.add_person("Fellow", "One", "Fellow", "Y") self.amity.add_person("Fellow", "Two", "Fellow", "Y") self.amity.add_person("Fellow", "Three", "Fellow", "Y") self.amity.add_person("Fellow", "Four", "Fellow", "Y") self.amity.list_of_rooms.append(self.office) self.amity.allocated_members.append(self.fellow) res = self.amity.reallocate_person("Mike Jon", "Swift") self.assertEqual(res, "Room is full") def test_person_is_reallocated_successfully(self): """Tests reallocate person works""" self.amity.list_of_rooms += [self.office, self.living_space] self.amity.allocated_members.append(self.fellow) self.amity.list_of_rooms[0].occupants.append(self.fellow) self.amity.reallocate_person("Mike Jon", "Swift") # Assert the person is transfered to new room self.assertEqual(self.amity.list_of_rooms[1].occupants[0].person_name, "Mike Jon") # Assert the person is removed from former room self.assertTrue(len(self.amity.list_of_rooms[0].occupants) == 0) def test_cannot_print_inexistent_room(self): """ Tests print_room raises alert if there are no occupants """ res = self.amity.print_room("None") self.assertEqual(res, "Room does not exist") def test_print_room_works(self): """ Tests print room returns the names of occupants """ self.amity.list_of_rooms.append(self.office) self.amity.list_of_rooms[0].occupants += \ [self.fellow, self.fellow2, self.staff] res = self.amity.print_room("Scala") self.assertEqual(res, "Print room successful") def test_print_allocations_raises_alert_if_no_rooms(self): """Tests alert is raised if no allocations available""" self.assertEqual(self.amity.print_allocations(None), "No rooms") def test_print_allocations_to_screen_works(self): """Tests allocations are printed to screen""" self.amity.list_of_rooms += [self.office, self.living_space] self.amity.add_person("Carla", "Bruni", "Staff") self.amity.add_person("Peter", "Pan", "Fellow") self.amity.add_person("Mike", "Ross", "Fellow", "Y") self.amity.add_person("Hype", "Mann", "Fellow") res = self.amity.print_allocations(None) self.assertEqual(res, "Print allocations successful") def test_allocations_are_written_to_file(self): """Tests if allocations are saved to file""" self.amity.list_of_rooms += [self.office, self.living_space] self.amity.allocated_members += [self.staff, self.fellow, self.fellow2] self.amity.list_of_rooms[0].occupants.append(self.staff) self.amity.list_of_rooms[0].occupants.append(self.fellow) self.amity.list_of_rooms[0].occupants.append(self.fellow2) self.amity.list_of_rooms[1].occupants.append(self.fellow2) m = mock_open() with patch('builtins.open', m): self.amity.print_allocations("file") m.assert_called_with("file", 'w') def test_print_unallocated_people_works(self): """Tests unallocated people are printed to screen""" self.amity.unallocated_members += [ self.fellow, self.fellow2, self.staff ] res = self.amity.print_unallocated(None) self.assertEqual(res, "Print unallocations successful") def test_it_prints_unallocated_people_to_file(self): """Tests if unallocated people are saved to file""" self.amity.unallocated_members += [ self.staff, self.fellow, self.fellow2 ] m = mock_open() with patch('builtins.open', m): self.amity.print_unallocated("file") m.assert_called_with("file", 'w') def test_load_state_from_invalid_path_raises_error(self): """Tests load state does not accept invalid path""" res = self.amity.load_people("invalid path") self.assertEqual(res, "Error. File not found") def test_cannot_save_blank_data_to_db(self): """Tests blank data is not saved to db""" res = self.amity.save_state(None) self.assertEqual(res, "No data") def tearDown(self): del self.living_space del self.office del self.fellow del self.fellow2 del self.staff del self.amity