Beispiel #1
0
class TestAmity(unittest.TestCase):
    def setUp(self):
        self.test_amity = Amity()
        """Create living space"""
        self.test_amity.create_room({
            "<room_name>": ["LivingA"],
            "Living": True,
            "Office": False
        })
        """Create office"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeA"],
            "Living": False,
            "Office": True
        })

        # Assign rooms to variables
        self.livinga = self.test_amity.livingspaces[0]
        self.officea = self.test_amity.offices[0]
        """Add fellow that wants space"""
        self.test_amity.add_person({
            "<first_name>": "Test",
            "<last_name>": "Fellow",
            "<wants_space>": "Y",
            "Fellow": True,
            "Staff": False
        })
        """Add staff member that wants space"""
        self.test_amity.add_person({
            "<first_name>": "Test",
            "<last_name>": "Staff",
            "<wants_space>": "Y",
            "Fellow": False,
            "Staff": True
        })
        # Assign people to variables
        self.testfellow = self.test_amity.people[0]
        self.teststaff = self.test_amity.people[1]

    def test_create_room(self):
        """Test creation of rooms"""
        # LivingA and OfficeA from setup() added to relevant lists
        self.assertEqual(2, len(self.test_amity.rooms))
        self.assertEqual(1, len(self.test_amity.livingspaces))
        self.assertEqual(1, len(self.test_amity.offices))
        """Test creation of multiple living spaces"""
        self.test_amity.create_room({
            "<room_name>": ["LivingB", "LivingC"],
            "Living": True,
            "Office": False
        })
        # Two living spaces added to list of rooms and list of living spaces
        self.assertEqual(4, len(self.test_amity.rooms))
        self.assertEqual(3, len(self.test_amity.livingspaces))
        """Test creation of multiple offices"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeB", "OfficeC"],
            "Living": False,
            "Office": True
        })
        # Two offices added to list of rooms and list of offices
        self.assertEqual(6, len(self.test_amity.rooms))
        self.assertEqual(3, len(self.test_amity.offices))
        """Test that duplicate rooms are not added"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeA", "OfficeB"],
            "Living": False,
            "Office": True
        })
        # Duplicate rooms not added to any list
        self.assertEqual(6, len(self.test_amity.rooms))
        self.assertEqual(3, len(self.test_amity.offices))

    def test_add_person(self):
        """Test addition of people"""
        #  Test Fellow and Test Staff from setup() added to relevant lists
        self.assertEqual(2, len(self.test_amity.people))
        self.assertEqual(1, len(self.test_amity.fellows))
        self.assertEqual(1, len(self.test_amity.staff))
        """Test allocation of those who want space"""
        # Check that people have been appended to rooms' lists of occupants
        self.assertEqual(2, len(self.officea.occupants))

    def test_vacant_offices(self):
        """Test that vacant rooms are added to relevant list"""
        # Add office
        self.test_amity.create_room({
            "<room_name>": ["OfficeB"],
            "Living": False,
            "Office": True
        })

        self.test_amity.check_vacant_rooms()

        # Check if OfficeB has been appended to relevant lists
        self.assertEqual(2, len(self.test_amity.vacant_offices))
        self.assertEqual(3, len(self.test_amity.vacant_rooms))

    def test_reallocate_person(self):
        """Add another office"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeB"],
            "Living": False,
            "Office": True
        })
        # Assign OfficeB to variable
        officeb = self.test_amity.offices[1]
        """Test reallocation of Test Staff from OfficeA to OfficeB"""
        self.test_amity.reallocate_person({
            "<employee_id>":
            int(self.teststaff.emp_id),
            "<new_room_name>":
            "OfficeB"
        })
        # Staff member no longer in OfficeA's list of occupants
        self.assertEqual(1, len(self.officea.occupants))
        # Staff member now in OfficeB's list of occupants
        self.assertEqual(1, len(officeb.occupants))
        """Add another living space"""
        self.test_amity.create_room({
            "<room_name>": ["LivingB"],
            "Living": True,
            "Office": False
        })
        # Assign LivingB to variable
        livingb = self.test_amity.livingspaces[1]
        """Test allocation of Test Fellow to LivingB"""
        self.test_amity.reallocate_person({
            "<employee_id>":
            int(self.testfellow.emp_id),
            "<new_room_name>":
            "LivingB"
        })
        # Fellow now in LivingB's list of occupants
        self.assertEqual(1, len(livingb.occupants))
        """Test that staff cannot be allocated to living space"""
        self.test_amity.reallocate_person({
            "<employee_id>":
            int(self.teststaff.emp_id),
            "<new_room_name>":
            "LivingB"
        })
        # Staff not added to LivingB's list of occupants
        self.assertEqual(1, len(livingb.occupants))

    def test_load_people(self):
        """
        Test that people can be added to the app from a
        user-defined text file
        """
        """Add office to have more vacant offices for people from text file """
        self.test_amity.create_room({
            "<room_name>": ["OfficeC"],
            "Living": False,
            "Office": True
        })

        self.test_amity.load_people({"<filename>": "people.txt"})
        # People from file are added to application
        self.assertEqual(12, len(self.test_amity.people))
        self.assertEqual(7, len(self.test_amity.fellows))
        self.assertEqual(5, len(self.test_amity.staff))

    def test_print_allocations(self):
        """
        Test that allocations are displayed on screen
        and printed to a text file if specified
        """
        self.test_amity.print_allocations({"--o": "myfile.txt"})
        # File is created
        self.assertTrue(os.path.exists("myfile.txt"))
        # Data is entered
        with open("myfile.txt") as myfile:
            lines = myfile.readlines()
            self.assertTrue("LivingA\n" in lines)
            self.assertTrue("OfficeA\n" in lines)
            self.assertTrue("Test Fellow, Test Staff\n" in lines)
        os.remove("myfile.txt")

    def test_print_unallocated(self):
        """
        Test that unallocated people are displayed on screen
        and printed to a text file if specified
        """
        # Add fellow that does not want space
        self.test_amity.add_person({
            "<first_name>": "Test",
            "<last_name>": "Fellow2",
            "<wants_space>": "N",
            "Fellow": True,
            "Staff": False
        })
        self.test_amity.print_unallocated({"--o": "myfile2.txt"})
        # File is created
        self.assertTrue(os.path.exists("myfile2.txt"))
        # Data is entered
        with open("myfile2.txt") as myfile:
            lines = myfile.readlines()
            self.assertTrue("Unallocated People\n" in lines)
            self.assertTrue("Test Fellow2\n" in lines)
        os.remove("myfile2.txt")
Beispiel #2
0
class TestAmityville(unittest.TestCase):
    """
    Class test Amity
    """
    def setUp(self):
        self.amityville = Amity()

    def tearDown(self):
        """
        provide cleanup for all
        clear list data for each test.
        """

        # remove all entries in dict

        self.amityville.office_allocations.clear()
        self.amityville.livingspace_allocations.clear()
        self.amityville.room_data.clear()
        self.amityville.person_data.clear()

        # remove all entries in list

        del self.amityville.rooms[:]
        del self.amityville.offices[:]
        del self.amityville.livingspaces[:]
        del self.amityville.people[:]
        del self.amityville.staff[:]
        del self.amityville.fellows[:]
        del self.amityville.unallocated_livingspace[:]
        del self.amityville.unallocated_office[:]

    def test_create_room_already_exists(self):
        """
        test if a room being created already exists
        """

        self.amityville.create_room('SUN', 'OFFICE')
        result = self.amityville.create_room('SUN', 'OFFICE')
        self.assertEqual(result, 'SUN already exists.')

    def test_create_room_livingspace(self):
        """
        test if a livingspace is created
        """

        result = self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.assertEqual(result, 'MOON created.')

    def test_create_room_office(self):
        """
        test if an office is created
        """

        result = self.amityville.create_room('SUN', 'OFFICE')
        self.assertEqual(result, 'SUN created.')

    def test_create_room_invalid_room_type(self):
        """
        Test for an invalid room type
        """

        result = self.amityville.create_room('SUN', 'OFICE')
        self.assertEqual(result, 'Invalid room type.')

    def test_add_person_invalid_person_name(self):
        """
        test if person name is valid
        """

        result = self.amityville.add_person('asce1062', 'FELLOW', 'NO')
        self.assertEqual(result, 'Invalid person name.')

    def test_add_person_fellow(self):
        """
        test if a fellow is added
        """

        result = self.amityville.add_person('ALEX', 'FELLOW', 'NO')

        self.assertEqual(
            result,
            'ALEX successfully added! \nAllocated to:\nOffice: No vaccant office.'
        )

    def test_add_person_staff(self):
        """
        test if a staff member is added
        """
        result = self.amityville.add_person('ALEX', 'STAFF', 'NO')
        self.assertEqual(
            result,
            'ALEX successfully added! \nAllocated to:\nOffice: No vaccant office.'
        )

    def test_add_person_invalid_job_description(self):
        """
        test if job description is valid
        """

        result = self.amityville.add_person('ALEX', 'FELOW', 'NO')
        self.assertEqual(result, 'Invalid job description.')

    def test_add_person_invalid_accommodation_choice_fellow(self):
        """
        test if accommodation input is valid
        """

        result = self.amityville.add_person('ALEX', 'FELLOW', 'YEZ')
        self.assertEqual(result, 'Invalid accommodation input.')

    def test_add_person_invalid_accommodation_choice_staff(self):
        """
        test if accommodation input is valid
        """

        result = self.amityville.add_person('ALEX', 'STAFF', 'NEIN')
        self.assertEqual(result, 'Invalid accommodation input.')

    def test_add_person_staff_wants_accommodation(self):
        """
        test if a staff request for accommodation
        """

        result = self.amityville.add_person('ALEX', 'STAFF', 'YES')
        self.assertEqual(result, 'Staff cannot be allocated a livingspace.')

    def test_allocate_livingspace_to_nonexistent_fellow(self):
        """
        test allocating a livingspace to a non existant fellow
        """

        result = self.amityville.allocate_livingspace('ALEX')
        self.assertEqual(result, 'ALEX does not exist.')

    def test_allocate_livingspace_already_allocated_livingspace(self):
        """
        test if a fellow has already been allocated a livingspace
        """

        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        result = self.amityville.allocate_livingspace('ALEX')
        self.assertEqual(result, 'ALEX already allocated a livingspace.')

    def test_allocate_livingspace(self):
        """
        test if a fellow is allocated a living space
        """

        self.amityville.create_room('MOON', 'LIVINGSPACE')
        result = self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        self.assertEqual(
            result, 'ALEX successfully added! \nAllocated to:\nLivingSpace:'
            ' ALEX allocated to MOON.\nOffice: No vaccant office.')

    def test_allocate_livingspace_no_vaccant_livingspace(self):
        """
        test that there are no vaccant livingspaces
        """

        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        self.amityville.add_person('JOE', 'FELLOW', 'YES')
        self.amityville.add_person('TINA', 'FELLOW', 'YES')
        self.amityville.add_person('IBRA', 'FELLOW', 'YES')
        self.amityville.add_person('MILLY', 'FELLOW', 'YES')
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.allocate_livingspace('MILLY')
        self.amityville.allocate_livingspace('IBRA')
        self.amityville.allocate_livingspace('TINA')
        self.amityville.allocate_livingspace('JOE')
        result = self.amityville.allocate_livingspace('ALEX')
        self.assertEqual(result, 'No vaccant livingspace.')

    def test_allocate_office_to_nonexistent_staff(self):
        """
        test allocating a livingspace to a non existant fellow
        """

        result = self.amityville.allocate_office('ALEX')
        self.assertEqual(result, 'ALEX does not exist.')

    def test_allocate_office_already_allocated_office(self):
        """
        test if a staff has already been allocated an office
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.allocate_office('ALEX')
        self.assertEqual(result, 'ALEX already allocated to an office.')

    def test_allocate_office(self):
        """
        test if a staff is allocated an office
        """

        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        self.amityville.create_room('SUN', 'OFFICE')
        result = self.amityville.allocate_office('ALEX')
        self.assertEqual(result, 'ALEX allocated to SUN.')

    def test_allocate_office_no_vaccant_office(self):
        """
        test that there are no vaccant offices
        """

        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        self.amityville.add_person('JOE', 'STAFF', 'NO')
        self.amityville.add_person('TINA', 'STAFF', 'NO')
        self.amityville.add_person('IBRA', 'STAFF', 'NO')
        self.amityville.add_person('MILLY', 'STAFF', 'NO')
        self.amityville.add_person('PAU', 'STAFF', 'NO')
        self.amityville.add_person('JONA', 'STAFF', 'NO')
        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.allocate_office('JONA')
        self.amityville.allocate_office('PAU')
        self.amityville.allocate_office('MILLY')
        self.amityville.allocate_office('IBRA')
        self.amityville.allocate_office('TINA')
        self.amityville.allocate_office('JOE')
        result = self.amityville.allocate_office('ALEX')
        self.assertEqual(result, 'No vaccant office.')

    def test_reallocate_person_nonexistent_rooms(self):
        """
        test there are no rooms added in order to reallocate
        """
        #

        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.reallocate_person('S1', 'SUN')
        self.assertEqual(result, 'SUN does not exist.')

    def test_reallocate_person_nonexistent_people(self):
        """
        test there is no one to reallocate
        """

        self.amityville.create_room('SUN', 'OFFICE')
        result = self.amityville.reallocate_person('S1', 'SUN')
        self.assertEqual(result, 'S1 does not exist.')

    def test_reallocate_person_livingspace(self):
        """
        test if person is reallocated to a livingspace
        """

        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        self.amityville.create_room('LIGHT', 'LIVINGSPACE')
        result = self.amityville.reallocate_person('F1', 'LIGHT')
        self.assertEqual(result, 'ALEX has been reallocated to LIGHT.')

    def test_reallocate_person_office(self):
        """
        test if person is reallocated to an office.
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        self.amityville.create_room('SHINE', 'OFFICE')
        result = self.amityville.reallocate_person('S1', 'SHINE')
        self.assertEqual(result, 'ALEX has been reallocated to SHINE.')

    def test_reallocate_person_staff_to_livingspace(self):
        """
        test if a staff is reallocated to a living space
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.reallocate_person('S1', 'MOON')
        self.assertEqual(result,
                         'Cannot reallocate staff member to a livingspace.')

    def test_reallocate_person_to_full_livingspace(self):
        """
        test if room being reallocated to is already full
        """

        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        self.amityville.add_person('JOE', 'FELLOW', 'YES')
        self.amityville.add_person('TINA', 'FELLOW', 'YES')
        self.amityville.add_person('IBRA', 'FELLOW', 'YES')
        self.amityville.create_room('LIGHT', 'LIVINGSPACE')
        self.amityville.add_person('MILLY', 'FELLOW', 'YES')
        result = self.amityville.reallocate_person('F5', 'MOON')
        self.assertEqual(result, 'MOON is already full.')

    def test_reallocate_person_to_full_office(self):
        """
        test if room being reallocated to is already full
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        self.amityville.add_person('JOE', 'STAFF', 'NO')
        self.amityville.add_person('TINA', 'STAFF', 'NO')
        self.amityville.add_person('IBRA', 'STAFF', 'NO')
        self.amityville.add_person('MILLY', 'STAFF', 'NO')
        self.amityville.add_person('PAU', 'STAFF', 'NO')
        self.amityville.create_room('SHINE', 'OFFICE')
        self.amityville.add_person('JONA', 'STAFF', 'NO')
        result = self.amityville.reallocate_person('s7', 'SUN')
        self.assertEqual(result, 'SUN is already full.')

    def test_reallocate_person_to_currently_occupied_livingspace(self):
        """
        test if the room being reallocated to is the one they are currently residing in
        """

        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        result = self.amityville.reallocate_person('F1', 'MOON')
        self.assertEqual(result, 'ALEX already allocated to MOON.')

    def test_reallocate_person_to_currently_occupied_office(self):
        """
        test if the room being reallocated to is the one they are currently residing in
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.reallocate_person('S1', 'SUN')
        self.assertEqual(result, 'ALEX already allocated to SUN.')

    def test_reallocate_person_to_a_livingspace_and_not_yet_allocated_a_livingspace(
            self):
        """
        test reallocating a person who is not yet allocated.
        """

        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        result = self.amityville.reallocate_person('F1', 'MOON')
        self.assertEqual(result, 'ALEX not yet allocated to any livingspace.')

    def test_reallocate_person_to_an_office_and_not_yet_allocated_an_office(
            self):
        """
        test reallocating a person who is not yet allocated.
        """

        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        self.amityville.create_room('SHINE', 'OFFICE')
        result = self.amityville.reallocate_person('S1', 'SHINE')
        self.assertEqual(result, 'ALEX not yet allocated to any office.')

    def test_print_people_details_no_data_yet(self):
        """
        test print people details and no people added to the system yet.
        """

        result = self.amityville.print_people_details()
        self.assertEqual(result, 'No one exists in the system yet.')

    def test_load_people_wrong_file_name(self):
        """
        test if people are loaded from wrong file.
        """

        filename = 'peoplee'
        result = self.amityville.load_people(filename)
        self.assertEqual(result, 'File does not exist.')

    def test_load_people(self):
        """
        test if people are loaded from a file.
        """

        filename = 'people'
        result = self.amityville.load_people(filename)
        self.assertEqual(result, 'People successfully loaded.')

    def test_load_people_no_filename(self):
        """
        test if no file name is provided.
        """

        result = self.amityville.load_people('')
        self.assertEqual(result, 'Filename must be provided.')

    def test_load_people_invalid_accommodation(self):
        """
        Test if invalid accommodation input is handled.
        """

        filename = 'invalid_accommodation'
        result = self.amityville.load_people(filename)
        self.assertEqual(result, 'Invalid accommodation input.')

    def test_load_people_invalid_number_of_arguments_less(self):
        """
        test if arguments provided are less than or equal to 2.
        """

        filename = 'invalid_number_of_arguments_less'
        result = self.amityville.load_people(filename)
        self.assertEqual(result, 'Invalid number of arguments input.')

    def test_load_people_invalid_number_of_arguments_more(self):
        """
        test if arguments provided are more than 4.
        """

        filename = 'invalid_number_of_arguments_more'
        result = self.amityville.load_people(filename)
        self.assertEqual(result, 'Invalid number of arguments input.')

    def test_load_people_invalid_job_description(self):
        """
        test for invalid job description.
        """

        filename = 'invalid_job_description'
        result = self.amityville.load_people(filename)
        self.assertEqual(result, 'Invalid job description.')

    def test_load_rooms_from_file_wrong_file_name(self):
        """
        test if rooms are loaded from a wrong file.
        """

        filename = 'room'
        result = self.amityville.load_rooms(filename)
        self.assertEqual(result, 'File does not exist.')

    def test_load_rooms_from_file(self):
        """
        test if rooms are loaded from file.
        """

        filename = 'rooms'
        result = self.amityville.load_rooms(filename)
        self.assertEqual(result, 'Rooms successfully loaded from file.')

    def test_load_rooms_from_file_no_filename(self):
        """
        test if no file name is provided.
        """

        filename = ''
        result = self.amityville.load_rooms(filename)
        self.assertEqual(result, 'Filename must be provided.')

    def test_load_rooms_from_file_invalid_input(self):
        """
        test if there are more than 2 arguments.
        """

        filename = 'rooms_invalid_input'
        result = self.amityville.load_rooms(filename)
        self.assertEqual(result, 'Invalid number of arguments input.')

    def test_print_allocations_to_screen(self):
        """
        test if allocations are successfully printed to the screen.
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        filename = ''
        result = self.amityville.print_allocations(filename)
        self.assertEqual(result, 'Done.')

    def test_print_allocations_to_file(self):
        """
        test if allocations are successfully printed out to a file.
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        filename = 'allocations'
        result = self.amityville.print_allocations(filename)
        self.assertEqual(result, 'Done.')

    def test_print_specific_room_allocations(self):
        """
        test if specific room allocations are printed to the screen.
        """

        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.print_specific_room_allocations('SUN')
        self.assertEqual(result, 'Done.')

    def test_print_specific_room_allocations_invalid_input(self):
        """
        test if invalid input is handled.
        """

        result = \
            self.amityville.print_specific_room_allocations('SUN(1)')
        self.assertEqual(result, 'Invalid input.')

    def test_print_unallocated_to_screen(self):
        """
        test if un-allocations are printed to the screen.
        """

        filename = ''
        result = self.amityville.print_unallocated(filename)
        self.assertEqual(result, 'Done.')

    def test_print_unallocated_to_file(self):
        """
        test if un-allocations are printed into a file.
        """

        filename = 'unallocated'
        result = self.amityville.print_unallocated(filename)
        self.assertEqual(result, 'Done.')

    def test_print_rooms(self):
        """
        test if existing rooms are printed to the screen.
        """

        result = self.amityville.print_rooms()
        self.assertEqual(result, 'Done.')

    def test_print_fellows(self):
        """
        test if existing fellows are printed to the screen.
        """

        result = self.amityville.print_fellows()
        self.assertEqual(result, 'Done.')

    def test_print_staff(self):
        """
        test if existing staff are printed to the screen.
        """

        result = self.amityville.print_staff()
        self.assertEqual(result, 'Done.')

    def test_print_all_people(self):
        """
        test if all existing people are printed to the screen.
        """

        result = self.amityville.print_all_people()
        self.assertEqual(result, 'Done.')

    def test_print_people_details(self):
        """
        test print people details.
        """

        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.print_people_details()
        self.assertEqual(result, 'Done.')

    def test_save_state(self):
        """
        test if program state is saved to db
        """

        rooms = 'rooms'
        self.amityville.load_rooms(rooms)
        people = 'people'
        self.amityville.load_people(people)
        dbname = 'amity'
        self.amityville.clear_db(dbname)
        result = self.amityville.save_state(dbname)
        self.assertEqual(result, 'Data saved successfully.')

    def test_save_state_no_dbname_provided(self):
        """
        test if program state is saved to db
        """

        rooms = 'rooms'
        self.amityville.load_rooms(rooms)
        people = 'people'
        self.amityville.load_people(people)
        dbname = ''
        self.amityville.clear_db(dbname)
        result = self.amityville.save_state()
        self.assertEqual(result, 'Data saved successfully.')

    def test_save_state_no_rooms_exist(self):
        """
        test if program state is saved to db
        """

        people = 'people'
        self.amityville.load_people(people)
        dbname = 'amity'
        self.amityville.clear_db(dbname)
        result = self.amityville.save_state(dbname)
        self.assertEqual(result, 'Data saved successfully.')

    def test_load_state(self):
        """
        test if program state saved to db is loaded to program.
        """

        dbname = 'amity'
        people = 'people'
        rooms = 'rooms'
        self.amityville.clear_db(dbname)
        self.amityville.load_rooms(rooms)
        self.amityville.load_people(people)
        self.amityville.save_state(dbname)
        result = self.amityville.load_state(dbname)
        self.assertEqual(result,
                         'Data has been loaded into the system successfully.')

    def test_load_state_no_db_name_provided(self):
        """
        test if program state saved to db is loaded to program.
        """

        dbname = ''
        people = 'people'
        rooms = 'rooms'
        self.amityville.clear_db(dbname)
        self.amityville.load_rooms(rooms)
        self.amityville.load_people(people)
        self.amityville.save_state(dbname)
        result = self.amityville.load_state(dbname)
        self.assertEqual(result,
                         'Data has been loaded into the system successfully.')

    def test_load_state_no_rooms_exist(self):
        """
        test if program state saved to db is loaded to program.
        """

        dbname = 'amity'
        people = 'people'
        self.amityville.clear_db(dbname)
        self.amityville.load_people(people)
        self.amityville.save_state(dbname)
        result = self.amityville.load_state(dbname)
        self.assertEqual(result,
                         'Data has been loaded into the system successfully.')

    def test_clear_db(self):
        """
        test if db is cleared
        """

        dbname = 'amity'
        result = self.amityville.clear_db(dbname)
        self.assertEqual(result, 'Database cleared successfully.')

    def test_clear_db_no_name(self):
        """
        test if db is cleared
        """

        dbname = ''
        result = self.amityville.clear_db(dbname)
        self.assertEqual(result, 'Database cleared successfully.')

    def test_delete_person_person_id_does_not_exist(self):
        """
        test if removing a person who does not exist in the system.
        """
        result = self.amityville.delete_person('F1')
        self.assertEqual(result, 'F1 does not exist.')

    def test_delete_person_fellow(self):
        """
        test deleting a fellow from amity
        """
        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        result = self.amityville.delete_person('F1')
        self.assertEqual(
            result, 'F1: ALEX who is a FELLOW has been removed from amity.')

    def test_delete_person_fellow_not_yet_allocated(self):
        """
        test deleting a fellow from amity who has not yet been allocated
        """
        self.amityville.add_person('ALEX', 'FELLOW', 'YES')
        result = self.amityville.delete_person('F1')
        self.assertEqual(
            result, 'F1: ALEX who is a FELLOW has been removed from amity.')

    def test_delete_person_staff(self):
        """
        test deleting a staff from amity
        """
        self.amityville.create_room('SUN', 'OFFICE')
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.delete_person('S1')
        self.assertEqual(
            result, 'S1: ALEX who is a STAFF has been removed from amity.')

    def test_delete_person_staff_not_yet_allocated(self):
        """
        test deleting a staff from amity who has not yet been allocated
        """
        self.amityville.add_person('ALEX', 'STAFF', 'NO')
        result = self.amityville.delete_person('S1')
        self.assertEqual(
            result, 'S1: ALEX who is a STAFF has been removed from amity.')

    def test_delete_room_non_existant(self):
        """
        test deleting a room that is not yet created
        """
        result = self.amityville.delete_room('MOON')
        self.assertEqual(result, 'MOON does not exist.')

    def test_delete_room_livingspace(self):
        """
        test deleting a livingspace from amity
        """
        self.amityville.create_room('MOON', 'LIVINGSPACE')
        result = self.amityville.delete_room('MOON')
        self.assertEqual(
            result,
            'Room MOON which is a LIVINGSPACE has been removed from amity.')

    def test_delete_room_office(self):
        """
        test deleting an office from amity
        """
        self.amityville.create_room('SUN', 'OFFICE')
        result = self.amityville.delete_room('SUN')
        self.assertEqual(
            result, 'Room SUN which is an OFFICE has been removed from amity.')
class TestAmity(unittest.TestCase):

    def setUp(self):
        self.test_amity = Amity()

        """Create living space"""
        self.test_amity.create_room({
            "<room_name>": ["LivingA"],
            "Living": True,
            "Office": False
        })

        """Create office"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeA"],
            "Living": False,
            "Office": True
        })

        # Assign rooms to variables
        self.livinga = self.test_amity.livingspaces[0]
        self.officea = self.test_amity.offices[0]

        """Add fellow that wants space"""
        self.test_amity.add_person({
            "<first_name>": "Test",
            "<last_name>": "Fellow",
            "<wants_space>": "Y",
            "Fellow": True,
            "Staff": False
        })

        """Add staff member that wants space"""
        self.test_amity.add_person({
            "<first_name>": "Test",
            "<last_name>": "Staff",
            "<wants_space>": "Y",
            "Fellow": False,
            "Staff": True
        })
        # Assign people to variables
        self.testfellow = self.test_amity.people[0]
        self.teststaff = self.test_amity.people[1]

    def test_create_room(self):
        """Test creation of rooms"""
        # LivingA and OfficeA from setup() added to relevant lists
        self.assertEqual(2, len(self.test_amity.rooms))
        self.assertEqual(1, len(self.test_amity.livingspaces))
        self.assertEqual(1, len(self.test_amity.offices))

        """Test creation of multiple living spaces"""
        self.test_amity.create_room({
            "<room_name>": ["LivingB", "LivingC"],
            "Living": True,
            "Office": False
        })
        # Two living spaces added to list of rooms and list of living spaces
        self.assertEqual(4, len(self.test_amity.rooms))
        self.assertEqual(3, len(self.test_amity.livingspaces))

        """Test creation of multiple offices"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeB", "OfficeC"],
            "Living": False,
            "Office": True
        })
        # Two offices added to list of rooms and list of offices
        self.assertEqual(6, len(self.test_amity.rooms))
        self.assertEqual(3, len(self.test_amity.offices))

        """Test that duplicate rooms are not added"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeA", "OfficeB"],
            "Living": False,
            "Office": True
        })
        # Duplicate rooms not added to any list
        self.assertEqual(6, len(self.test_amity.rooms))
        self.assertEqual(3, len(self.test_amity.offices))

    def test_add_person(self):
        """Test addition of people"""
        #  Test Fellow and Test Staff from setup() added to relevant lists
        self.assertEqual(2, len(self.test_amity.people))
        self.assertEqual(1, len(self.test_amity.fellows))
        self.assertEqual(1, len(self.test_amity.staff))

        """Test allocation of those who want space"""
        # Check that people have been appended to rooms' lists of occupants
        self.assertEqual(2, len(self.officea.occupants))

    def test_vacant_offices(self):
        """Test that vacant rooms are added to relevant list"""
        # Add office
        self.test_amity.create_room({
            "<room_name>": ["OfficeB"],
            "Living": False,
            "Office": True
        })

        self.test_amity.check_vacant_rooms()

        # Check if OfficeB has been appended to relevant lists
        self.assertEqual(2, len(self.test_amity.vacant_offices))
        self.assertEqual(3, len(self.test_amity.vacant_rooms))

    def test_reallocate_person(self):
        """Add another office"""
        self.test_amity.create_room({
            "<room_name>": ["OfficeB"],
            "Living": False,
            "Office": True
        })
        # Assign OfficeB to variable
        officeb = self.test_amity.offices[1]

        """Test reallocation of Test Staff from OfficeA to OfficeB"""
        self.test_amity.reallocate_person({
            "<employee_id>": int(self.teststaff.emp_id),
            "<new_room_name>": "OfficeB"
        })
        # Staff member no longer in OfficeA's list of occupants
        self.assertEqual(1, len(self.officea.occupants))
        # Staff member now in OfficeB's list of occupants
        self.assertEqual(1, len(officeb.occupants))

        """Add another living space"""
        self.test_amity.create_room({
            "<room_name>": ["LivingB"],
            "Living": True,
            "Office": False
        })
        # Assign LivingB to variable
        livingb = self.test_amity.livingspaces[1]

        """Test allocation of Test Fellow to LivingB"""
        self.test_amity.reallocate_person({
            "<employee_id>": int(self.testfellow.emp_id),
            "<new_room_name>": "LivingB"
        })
        # Fellow now in LivingB's list of occupants
        self.assertEqual(1, len(livingb.occupants))

        """Test that staff cannot be allocated to living space"""
        self.test_amity.reallocate_person({
            "<employee_id>": int(self.teststaff.emp_id),
            "<new_room_name>": "LivingB"
        })
        # Staff not added to LivingB's list of occupants
        self.assertEqual(1, len(livingb.occupants))

    def test_load_people(self):
        """
        Test that people can be added to the app from a
        user-defined text file
        """

        """Add office to have more vacant offices for people from text file """
        self.test_amity.create_room({
            "<room_name>": ["OfficeC"],
            "Living": False,
            "Office": True
        })

        self.test_amity.load_people({"<filename>": "people.txt"})
        # People from file are added to application
        self.assertEqual(12, len(self.test_amity.people))
        self.assertEqual(7, len(self.test_amity.fellows))
        self.assertEqual(5, len(self.test_amity.staff))

    def test_print_allocations(self):
        """
        Test that allocations are displayed on screen
        and printed to a text file if specified
        """
        self.test_amity.print_allocations({
            "--o": "myfile.txt"
        })
        # File is created
        self.assertTrue(os.path.exists("myfile.txt"))
        # Data is entered
        with open("myfile.txt") as myfile:
            lines = myfile.readlines()
            self.assertTrue("LivingA\n" in lines)
            self.assertTrue("OfficeA\n" in lines)
            self.assertTrue("Test Fellow, Test Staff\n" in lines)
        os.remove("myfile.txt")

    def test_print_unallocated(self):
        """
        Test that unallocated people are displayed on screen
        and printed to a text file if specified
        """
        # Add fellow that does not want space
        self.test_amity.add_person({
            "<first_name>": "Test",
            "<last_name>": "Fellow2",
            "<wants_space>": "N",
            "Fellow": True,
            "Staff": False
        })
        self.test_amity.print_unallocated({
            "--o": "myfile2.txt"
        })
        # File is created
        self.assertTrue(os.path.exists("myfile2.txt"))
        # Data is entered
        with open("myfile2.txt") as myfile:
            lines = myfile.readlines()
            self.assertTrue("Unallocated People\n" in lines)
            self.assertTrue("Test Fellow2\n" in lines)
        os.remove("myfile2.txt")
Beispiel #4
0
class AmityTest(unittest.TestCase):
    def setUp(self):
        self.amity = Amity()
        self.amity.room_types = {
            "office": {Office('MOMBASA'): ['Lavender Ayodi'],
                       Office('HOGWARTS'): [],
                       Office('LAGOS'): []},
            "livingspace": {LivingSpace('KENYA'): [],
                            LivingSpace('PLATFORM'): [],
                            LivingSpace('VALHALLA'): []}
        }

        self.amity.persons = {
            'fellow': [Fellow('Lavender Ayodi')],
            'staff': [Staff('John Doe')]
        }

    def _create_rooms(self):
        room_types = {
            "office": {Office('MOMBASA'), Office('HOGWARTS'), Office('LAGOS')},
            "livingspace": {LivingSpace('KENYA'), LivingSpace('PLATFORM'),
                            LivingSpace('VALHALLA')}
        }
        keys = room_types.keys()
        for room in keys:
            for names in room_types.get(room):
                self.amity._assign_room_name(room, names)

    def test_create_room_without_name(self):
        room_created = self.amity.create_room("", 'livingspace')
        self.assertIn("Error", room_created)

    def test_create_room_without_type(self):
        room_created = self.amity.create_room("Platform", "")
        self.assertIn("Error", room_created)

    def test_room_created_exists(self):
        self.amity.create_room('Lagos', 'office')
        room_created = self.amity.create_room('Lagos', 'office')
        print (self.amity.rooms)
        self.assertIn("Error", room_created)

    def test_amity_room_object(self):
        rooms = self.amity.rooms.keys()
        self.assertEqual(len(rooms), 2)
        self.assertIn("office", rooms)
        self.assertIn("livingspace", rooms)

    def test_create_office(self):
        office = self.amity.create_room("Platform", "office")
        self.assertIn("Success", office)

    def test_create_livingspace(self):
        livingspace = self.amity.create_room("Rongai", "livingspace")
        print (livingspace)
        self.assertIn("Success", livingspace)

    def test_create_invalid(self):
        livingspace = self.amity.create_room("Rongai", "school")
        self.assertIn("Error", livingspace)

    def test_add_person_without_name(self):
        person_added = self.amity.add_person('', 'fellow', 'N')
        self.assertIn('Error', person_added)

    def test_add_person_exists(self):
        self._create_rooms()
        self.amity.add_person('LESLEY AYODI', 'staff', 'Y')
        person_added = self.amity.add_person('LESLEY AYODI', 'staff', 'Y')
        self.assertIn('Error', person_added)

    def test_add_person_to_room(self):
        self.amity.create_room('MOMBASA', 'office')
        room_allocated = self.amity.add_person('EMILY MBELENGA', 'fellow', 'N')
        self.assertIn("Done", room_allocated)

    def test_add_invalid_person_type(self):
        self.amity.create_room('MOMBASA', 'office')
        type_added = self.amity.add_person('LESLEY AYODI', 'cook', 'N')
        self.assertIn('Error', type_added)

    def test_invalid_room_type(self):
        self.amity.create_room('MOMBASA', 'office')
        self.amity.add_person('LESLEY AYODI', 'staff', 'N')
        room_occupants = self.amity._find_room_occupant('offic', 'MOMBASA')
        self.assertIn("Error", room_occupants)

    def test_invalid_room_name(self):
        self.amity.create_room('MOMBASA', 'office')
        self.amity.add_person('LESLEY AYODI', 'staff', 'N')
        room_occupants = self.amity._find_room_occupant('office', 'MOMBA')
        self.assertIn("Error", room_occupants)

    def test_reallocate_person(self):
        reallocate_person = self.amity.reallocate_person(
            "office", 'MOMBASA', 'HOGWARTS', 'Lavender Ayodi')
        self.assertIn("Success", reallocate_person)

    def test_reallocate_invalid_person(self):
        reallocate_person = self.amity.reallocate_person(
            "office", 'MOMBASA', 'LAGOS', 'ISABEL AYODI')
        self.assertIn('Error', reallocate_person)

    def test_reallocate_invalid_room(self):
        invalid_room = self.amity.reallocate_person(
            "office", 'MOMBASA', 'LAGOS', 'ISABEL AYODI')
        self.assertIn('Error', invalid_room)

    def test_full_room(self):
        self.amity.create_room('Lagos', 'office')
        self.amity.add_person('Lesley Ayodi', 'staff', 'N')
        self.amity.create_room('Mombasa', 'office')
        occupants = ['A', 'B', 'C', 'D', 'E', 'F']
        office = self.amity.rooms.get('office')
        office['Mombasa'] = occupants
        reallocate_person = self.amity.reallocate_person(
            "office", 'Lagos', 'Mombasa', 'Lesley Ayodi')
        self.assertEqual(False, reallocate_person)

    def test_print_allocations(self):
        self.amity.add_person('Lesley Ayodi', 'staff', 'N')
        allocated_room = self.amity.print_allocations('room.txt')
        self.assertEqual(True, allocated_room)

    def test_print_allocations_terminal(self):
        self.amity.add_person('Lesley Ayodi', 'staff', 'N')
        allocated_room = self.amity.print_allocations('room.txt')
        self.assertEqual(True, allocated_room)

    def test_print_unallocated_persons(self):
        self.amity.add_person('LESLEY AYODI', 'fellow', 'Y')
        self.amity.add_person('LAVENDER AYODI', 'fellow', 'Y')
        self.amity.add_person('BRIAN MUTHUI', 'fellow', 'Y')
        self.amity.add_person('DENNIS MWANGI', 'fellow', 'Y')
        self.amity.add_person('MBARAK MBIGO', 'fellow', 'Y')
        self.amity.add_person('DENNIS YESWA', 'fellow', 'Y')
        self.amity.add_person('CYNTHIA ABURA', 'fellow', 'Y')
        unallocated = self.amity.print_unallocated('unallocated.txt')
        self.assertEqual(len(unallocated), 1)

    def test_print_room_occupants(self):
        self.amity.create_room('MOMBASA', 'office')
        self.amity.add_person('LESLEY AYODI', 'staff', 'N')
        self.amity.add_person('LAVENDER AYODI', 'fellow', 'N')
        self.amity.add_person('PATIENCE AYODI', 'staff', 'N')
        available_occupants = self.amity.print_room('office', 'MOMBASA')
        self.assertIn('Success', available_occupants)

    def test_load_people_from_file(self):
        persons_list = self.amity.persons.get('fellow')
        self.assertEqual(len(persons_list), 1)
        self.amity.load_people("people.txt")
        self.assertEqual(len(persons_list), 5)

    def test_save_state_works(self):
        saved_state = self.amity.save_state('test_amity.db')
        self.assertEqual(True, saved_state)

    def test_load_state_works(self):
        loaded_state = self.amity.load_state('test_amity.db')
        self.assertEqual(True, loaded_state)