Пример #1
0
class AmityDbTestCase(unittest.TestCase):
    """Test cases for Amity database"""
    def setUp(self):
        self.test_db = "temp"
        self.db = AmityDb(self.test_db)
        self.amity = Amity()

        # Create_rooms
        self.amity.create_room("Snow", "O")
        self.amity.create_room("Scala", "O")
        self.amity.create_room("Shire", "L")
        self.amity.create_room("Swift", "L")

        # Add_people to rooms
        self.amity.add_person("John", "Doe", "Fellow")
        self.amity.add_person("Tom", "Harry", "Fellow", "Y")
        self.amity.add_person("Jim", "Jones", "Staff")
        self.amity.add_person("Guy", "Fawkes", "Staff")

    def test_save_state_successful(self):
        """
        Tests that data is saved to database successfully
        """

        res = self.amity.save_state(self.test_db)
        self.assertEqual(res, "Save state successful")

    def test_load_state_works(self):
        """Tests data is loaded from database"""

        self.amity.save_state(self.test_db)
        res = self.amity.load_state(self.test_db)
        self.assertEqual(res, "Load state successful")

    def test_load_state_from_invalid_path_raises_error(self):
        """Tests load state does not allow invalid paths"""

        res = self.amity.load_state("invalid")
        self.assertEqual(res, "Error. Path not found")

    def tearDown(self):
        os.remove(self.test_db)
Пример #2
0
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")
Пример #3
0
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')
Пример #4
0
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!")
Пример #5
0
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)
Пример #6
0
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
Пример #7
0
class TestAmity(unittest.TestCase):
    '''
    This class tests all the funtionality of the amity famility allocation
    system. It has only focussed on the amity module since the class amity
    interacts with all the other modules and performs all the logical
    functioning of the application
    '''
    def setUp(self):
        self.amity = Amity()

    def test_returns_error_if_input_is_nonalphabetical(self):
        self.assertIn('123 is an invalid name type!',
                      self.amity.create_room(['123'], 'office'))

    def test_create_room(self):
        self.amity.create_room(["Pink"], "office")
        self.amity.create_room(['Blue'], 'livingspace')
        self.assertIn("Pink", Room.rooms['office'])
        self.assertIn('Blue', Room.rooms['livingspace'])

    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    def test_correct_message_room_creation(self):
        res = self.amity.create_room(["Pink"], "office")
        self.assertIn('There are 1 offices and 0 livingspaces in the system.',
                      res)
        self.assertIn('Enter a valid room type.(office|livingspace)',
                      self.amity.create_room(["Orange"], "ofice"))

    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    def test_create_room_increments_rooms_by_one(self):
        self.assertEqual(len(Room.rooms['office']), 0)
        self.amity.create_room(["Orange"], "office")
        self.assertEqual(len(Room.rooms['office']), 1)

    @patch.dict('app.room.Room.rooms', {
        "office": {
            'Yellow': []
        },
        "livingspace": {}
    })
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    def test_create_room_duplicate_rooms(self):
        self.assertIn('A room with that name exists. Select another name',
                      self.amity.create_room(['Yellow'], 'office'))

    ''' This section tests the functionality of adding a person to the
    system, allocation of rooms and reallocation of a person from one room
    to another'''

    def test_only_alphabetical_names_allowed(self):
        self.assertIn('Invalid name format. Alphabets only',
                      self.amity.add_person('1234', 'Menjo', 'fellow'))
        self.assertIn('Invalid name format. Alphabets only',
                      self.amity.add_person('Menjo', '1234', 'fellow'))

    def test_restriction_on_job_designation(self):
        self.assertIn('Enter a valid designation',
                      self.amity.add_person('Charlie', 'Kip', 'worker'))

    def test_restriction_on_residence(self):
        self.assertIn('Respond with Y or N for residence',
                      self.amity.add_person('Charlie', 'Kip', 'fellow', 'R'))

    def test_new_person_gets_ID(self):
        self.amity.add_person('Mary', 'Chepkoech', 'staff')
        self.assertIn('ST01', Person.people['staff'])
        self.amity.add_person('Kevin', 'Leo', 'fellow', 'Y')
        self.assertIn('FE01', Person.people['fellow'])

    @patch.dict(
        'app.room.Room.rooms', {
            "office": {
                'Yellow': {
                    "Room_name": 'Yellow',
                    "Max_occupants": 6,
                    "Total_occupants": 0,
                    "Occupants": []
                }
            },
            "livingspace": {}
        })
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 1)
    @patch.object(Amity, "livingspaces", 0)
    def test_add_person_adds_person_to_lists(self):
        self.amity.add_person('Mary', 'Chepkoech', 'staff')
        self.amity.add_person('Kevin', 'Leo', 'fellow', 'Y')
        self.assertIn('Mary Chepkoech', Person.people['staff']['ST01']['Name'])
        self.assertIn('Kevin Leo', Person.people['fellow']['FE01']['Name'])

    @patch.dict(
        'app.room.Room.rooms', {
            "office": {
                'Brown': {
                    "Room_name": 'Brown',
                    "Max_occupants": 6,
                    "Total_occupants": 0,
                    "Occupants": []
                }
            },
            "livingspace": {
                'Beige': {
                    "Room_name": 'Beige',
                    "Max_occupants": 4,
                    "Total_occupants": 0,
                    "Occupants": []
                }
            }
        })
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 1)
    @patch.object(Amity, "livingspaces", 1)
    def test_person_is_allocated_room_after_creation(self):
        self.amity.add_person('Sophie', 'Njeri', 'fellow', 'Y')
        self.assertIn('FE01', Room.rooms['office']['Brown']['Occupants'])
        self.assertIn('FE01', Room.rooms['livingspace']['Beige']['Occupants'])

    def test_reallocate_person(self):
        self.amity.create_room(['Brown'], 'office')
        self.amity.create_room(['Beige'], 'livingspace')
        self.amity.add_person('Sophie', 'Njeri', 'fellow', 'Y')
        self.amity.create_room(["Orange"], "office")
        self.amity.reallocate('FE01', 'Orange')
        self.assertIn('FE01', Room.rooms['office']['Orange']['Occupants'])

    @patch.dict(
        'app.room.Room.rooms', {
            "office": {
                'Brown': {
                    "Room_name": 'Brown',
                    "Max_occupants": 6,
                    "Total_occupants": 0,
                    "Occupants": []
                }
            },
            "livingspace": {
                'Beige': {
                    "Room_name": 'Beige',
                    "Max_occupants": 4,
                    "Total_occupants": 0,
                    "Occupants": []
                }
            }
        })
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 1)
    @patch.object(Amity, "livingspaces", 1)
    def test_reallocation_to_nonexistent_room(self):
        self.amity.add_person('Sophie', 'Njeri', 'fellow', 'Y')
        self.assertIn('No such room exists',
                      self.amity.reallocate('FE01', 'Orange'))

    # Tests the print_room functionality
    def test_print_room_nonexistent_room(self):
        self.assertIn('No such room exists', self.amity.print_room('Orange'))

    def test_print_room_with_no_occupants(self):
        self.amity.create_room(['Brown'], 'office')
        self.assertEqual(self.amity.print_room('Brown'), 'No occupants.')

    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    def test_print_room_with_occupants(self):
        self.amity.create_room(['Brown'], 'office')
        self.amity.add_person('Sophie', 'Njeri', 'fellow')
        self.assertIn('FE01 Sophie Njeri', self.amity.print_room('Brown'))

    # Tests the printing of unallocated persons
    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    @patch.object(Amity, "unallocated_people", [])
    def test_empty_unallocated(self):
        self.assertEqual(self.amity.print_unallocated(),
                         'There are no unallocated people')

    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    def test_unallocated_with_entries(self):
        self.amity.add_person('Sophie', 'Njeri', 'fellow')
        self.assertIn('Sophie Njeri', self.amity.print_unallocated())

    # Tests for the print allocations begin here
    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    def test_print_allocations_with_no_rooms(self):
        self.assertEqual(self.amity.print_allocations(),
                         'There are no rooms in the system at the moment.')

    @patch.dict('app.room.Room.rooms', {"office": {}, "livingspace": {}})
    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    @patch.dict(
        'app.room.Room.rooms', {
            "office": {
                'Brown': {
                    "Room_name": 'Brown',
                    "Max_occupants": 6,
                    "Total_occupants": 0,
                    "Occupants": ['ST01']
                }
            },
            "livingspace": {}
        })
    @patch.dict(
        'app.person.Person.people', {
            "staff": {
                'ST01': {
                    'Name': 'Mary Chepkoech',
                    'Resident': 'N'
                }
            },
            "fellow": {}
        })
    @patch.object(Amity, "offices", 1)
    @patch.object(Amity, "livingspaces", 0)
    def test_print_allocations_with_occupants_in_rooms(self):
        res = self.amity.print_allocations()
        self.assertIn('ST01 Mary Chepkoech', res['off_occupants'])

    # Tests for the load people method begins here

    @patch.dict('app.person.Person.people', {"staff": {}, "fellow": {}})
    def test_load_people(self):
        if os.path.exists('app'):
            os.chdir('app')
        self.amity.load_people('test.txt')
        assert (Person.people['staff'] > 1)

    # Tests for the database
    def test_save_state(self):
        if os.path.exists('app'):
            os.chdir('app')
        self.assertEqual(self.amity.save_state('savefile.db'),
                         'The current data has been saved to the database.')

    @patch.object(Room, "rooms", {"office": {}, "livingspace": {}})
    @patch.object(Person, "people", {"staff": {}, "fellow": {}})
    @patch.object(Amity, "offices", 0)
    @patch.object(Amity, "livingspaces", 0)
    @patch.object(Amity, "available_rooms", [])
    @patch.object(Amity, "unallocated_people", [])
    def test_load_state(self):
        if os.path.exists('app'):
            os.chdir('app')
        self.assertEqual(
            self.amity.load_state('new_infile.db'),
            "Data has been successfully loaded from the database.")
class OfficeSpaceAllocationTests(unittest.TestCase):
    def setUp(self):
        self.facility = Amity()
        self.room = Rooms()

    def test_create_single_office(self):
        '''Tests creating a single office room'''
        self.assertEqual(0, len(self.facility.rooms_list))
        self.facility.create_room({
            "room_type": "office",
            "room_name": "Krypton"
        })
        num = len(self.facility.rooms_list)
        self.assertEqual(1, num)

    def test_create_single_living_room(self):
        ''''Tests creating a single living space'''
        self.assertEqual(0, len(self.facility.rooms_list))
        self.facility.create_room({"room_type": "living", "room_name": "PHP"})
        self.assertEqual(1, len(self.facility.rooms_list))

    def test_create_multiple_offices(self):
        self.assertEqual(0, len(self.facility.rooms_list))
        self.facility.create_room({
            "room_type":
            "office",
            "room_name": ["Hogwarts", "Bumblebee", "Cyan"]
        })
        self.assertEqual(3, len(self.facility.rooms_list))

    def test_create_multiple_living_spaces(self):
        self.assertEqual(0, len(self.facility.rooms_list))
        self.facility.create_room({
            "room_type": "living",
            "room_name": ["Java", "Python", "Android"]
        })
        self.assertEqual(3, len(self.facility.rooms_list))

    def test_create_person(self):
        '''Asserts that person is added to a list and they are in the list'''

        s_fname = "Percila"
        f_fname = "Paul"
        self.assertEqual(0, len(self.facility.persons_list))

        self.facility.create_person(s_fname, "Njira", "Staff", "N")
        self.assertEqual(1, len(self.facility.persons_list))

    def test_person_values_added_are_of_correct_types(self):

        self.assertEqual(0, len(self.facility.persons_list))
        self.assertRaises(TypeError, self.facility.create_person, 456,
                          "Muthama", "Fellow", "Y")
        self.assertRaises(ValueError, self.facility.create_person, "Paul", 89,
                          "Fellow", "Y")
        self.assertRaises(TypeError, self.facility.create_person, "Percila",
                          "Njira", "Fellow", 0)

    def test_add_person_to_room(self):
        r_name = "PHP"
        f_fname = "Paul"
        self.assertEqual(0, len(self.facility.allocated_persons))
        self.facility.create_room({"room_type": "living", "room_name": r_name})
        self.facility.create_room({
            "room_type": "office",
            "room_name": 'krypton'
        })
        self.facility.create_person(f_fname, "Kahohi", "Fellow", "Y")

        self.assertEqual(1, len(self.facility.allocated_persons))

    def test_reallocate(self):
        r_name = "Go"
        r2_name = "Java"

        self.facility.create_room({"room_type": "living", "room_name": r_name})
        self.assertEqual(0,
                         len(self.facility.rooms_list[0]['Go']['occupants']))
        self.facility.create_person("Sophie", "Kahohi", "Fellow", "Y")
        self.assertEqual(1,
                         len(self.facility.rooms_list[0]['Go']['occupants']))

        self.facility.create_room({
            "room_type": "living",
            "room_name": r2_name
        })
        self.assertEqual(0, len(self.facility.rooms_list['Java']['occupants']))

        # Reallocate person
        self.reallocate(self.persons_list[0]['id'], 'Java')
        self.assertEqual(1, len(self.rooms_list['Java']['occupants']))
        self.assertEqual(0, len(self.rooms_list['Go']['occupants']))

    def test_maximum_capacities_of_room_and_office(self):
        living = LivingSpace("Shell")
        office = Office("Camelot")
        self.assertEqual(4, living.capacity)
        self.assertEqual(6, office.capacity)

    def test_room_availability(self):
        '''test that only unfilled rooms are available for allocation'''
        self.facility.create_room({"room_type": "living", "room_name": 'Go'})
        living = LivingSpace('Go')
        self.assertEqual(1, len(self.facility.unfilled_living))
        self.facility.create_person('paul', "Kahohi", "Fellow", "Y")
        self.facility.create_person('sophie', "Kibanani", "Fellow", "Y")
        self.facility.create_person('victor', "wabwoba", "Fellow", "Y")
        self.facility.create_person('Madeline', "Kwambok", "Fellow", "Y")
        self.assertEqual(4, len(self.facility.living_list[0]['occupants']))
        self.facility.room_availability()
        self.assertEqual(0, len(self.facility.unfilled_living))

    def test_return_office_name(self):
        self.facility.create_room({"room_type": "office", "room_name": 'Go'})
        self.facility.create_person('paul', "Kahohi", "Fellow", "N")
        fid = self.facility.persons_list[0]['id']
        self.assertEqual('go', self.facility.return_office_name(fid))

    def test_return_living_name(self):
        self.facility.create_room({"room_type": "living", "room_name": 'Go'})

        fid = self.facility.persons_list[0]['id']
        self.assertEqual('go', self.facility.return_living_name(fid))

    def test_save_state(self):
        self.facility.save_state('test')
        self.assertTrue(os.path.isfile('test'))

    def test_save_state_for_default(self):
        self.facility.save_state()
        self.assertTrue(os.path.isfile('amity'))

    def test_print_allocations(self):
        self.facility.print_allocations('test')
        self.assertTrue(os.path.isfile('test'))

    def test_create_db(self):
        create_db("awesome")
        self.assertTrue(os.path.isfile('awesome'))