コード例 #1
0
ファイル: test_dojo.py プロジェクト: geeeh/boot-camp
class TestRoom(TestCase):
    """class for testing dojo functions"""
    def setUp(self):
        """setup defaults"""
        self.held, sys.stdout = sys.stdout, StringIO()
        self.my_instance = Dojo()

    def test_create_office(self):
        """test for successful office creation"""
        initial_room_count = len(self.my_instance.rooms)
        self.my_instance.create_room("office", "blue")
        new_room_count = len(self.my_instance.rooms)
        self.assertEqual(new_room_count - initial_room_count, 1)
        self.assertIn("blue", self.my_instance.office_allocations.keys())

    def test_create_living_space(self):
        """test for successful living space creation"""
        initial_room_count = len(self.my_instance.rooms)
        self.my_instance.create_room("living_space", "white")
        new_room_count = len(self.my_instance.rooms)
        self.assertEqual(new_room_count - initial_room_count, 1)
        self.assertIn("white",
                      self.my_instance.living_space_allocations.keys())

    def test_create_room_with_similar_names(self):
        """test whether someone can create rooms will similar names.
        This is not allowed"""
        self.my_instance.create_room("office", "blue")
        output = self.my_instance.create_room("office", "blue")
        self.assertIn("room already exists", output)

    def test_add_staff(self):
        """"test for successful staff addition to the dojo"""
        initial_person_count = len(self.my_instance.people)
        self.my_instance.add_person("Godwin", "Gitonga", "staff")
        new_person_count = len(self.my_instance.people)
        self.assertEqual(new_person_count > initial_person_count, True)

    def test_add_staff_requesting_accommodation(self):
        """"test for staff addition to the dojo with request for accommodation. It """
        initial_person_count = len(self.my_instance.people)
        self.my_instance.add_person("Godwin", "Gitonga", "staff", "Y")
        output = sys.stdout.getvalue()
        self.assertIn("Staff cannot be allocated living spaces", output)
        new_person_count = len(self.my_instance.people)
        self.assertEqual(new_person_count > initial_person_count, True)

    def test_add_fellow(self):
        """"test for successful fellow addition to the dojo"""
        initial_person_count = len(self.my_instance.people)
        self.my_instance.add_person("Godwin", "Gitonga", "fellow", "Y")
        new_person_count = len(self.my_instance.people)
        self.assertEqual(new_person_count > initial_person_count, True)

    def test_add_fellow_with_similar_names(self):
        """"test for successful fellow addition to the dojo"""
        initial_person_count = len(self.my_instance.people)
        self.my_instance.add_person("Godwin", "Gitonga", "fellow", "Y")
        self.my_instance.add_person("Godwin", "Gitonga", "fellow", "Y")
        output = sys.stdout.getvalue()
        self.assertIn("1", output)
        self.assertIn("2", output)
        new_person_count = len(self.my_instance.people)
        self.assertEqual(new_person_count - initial_person_count, 2)

    def test_add_person_on_full_office(self):
        """test for addition of people when an office is full"""
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        output = sys.stdout.getvalue()
        self.assertIn("unallocated office", output)

    def test_add_person_on_full_lspace(self):
        """test for addition of people when an living space is full"""
        self.my_instance.create_room("living_space", "pink")
        self.my_instance.add_person("godwin", "gitonga", "fellow", "Y")
        self.my_instance.add_person("godwin", "gitonga", "fellow", "Y")
        self.my_instance.add_person("godwin", "gitonga", "fellow", "Y")
        self.my_instance.add_person("godwin", "gitonga", "fellow", "Y")
        self.my_instance.add_person("godwin", "gitonga", "fellow", "Y")
        output = sys.stdout.getvalue()
        self.assertIn("unallocated living space", output)

    def test_auto_allocate_office(self):
        self.my_instance.add_person("godwin", "gitonga", "staff")
        self.assertIn("godwin gitonga",
                      self.my_instance.office_allocations["unallocated"])
        self.my_instance.create_room("office", "blue")
        self.assertIn("godwin gitonga",
                      self.my_instance.office_allocations["blue"])
        self.assertNotIn("godwin gitonga",
                         self.my_instance.office_allocations["unallocated"])

    def test_auto_allocate_lspace(self):
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("godwin", "gitonga", "fellow", "Y")
        self.assertIn("godwin gitonga",
                      self.my_instance.living_space_allocations["unallocated"])
        self.my_instance.create_room("living_space", "white")
        self.assertIn("godwin gitonga",
                      self.my_instance.living_space_allocations["white"])
        self.assertNotIn(
            "godwin gitonga",
            self.my_instance.living_space_allocations["unallocated"])

    def test_print_room(self):
        """test for the printing a rooms occupants"""
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("godwin", "gitonga", "staff")
        self.my_instance.print_room("blue")
        my_output = sys.stdout
        output = my_output.getvalue()
        self.assertIn("godwin gitonga", output)

    def test_print_non_existing_room(self):
        """test for the printing a non existing room"""
        self.my_instance.print_room("blue")
        my_output = sys.stdout
        output = my_output.getvalue()
        self.assertIn("room blue does not exist", output)

    def test_print_empty_room(self):
        """test for the printing a non existing room"""
        self.my_instance.create_room("office", "blue")
        output = self.my_instance.print_room("blue")
        self.assertIn("room blue is empty", output)

    def test_load_people(self):
        """test for loading people from a file"""
        initial_people_count = len(self.my_instance.people)
        output = self.my_instance.load_people("people.txt")
        self.assertIn("data loaded successfully", output)
        current_people_count = len(self.my_instance.people)
        self.assertEqual(current_people_count > initial_people_count, True)

    def test_load_people_from_unavailable_file(self):
        """test for loading people from a file that is not available"""
        initial_people_count = len(self.my_instance.people)
        output = self.my_instance.load_people("dummy.txt")
        self.assertIn("file not found", output)
        current_people_count = len(self.my_instance.people)
        self.assertEqual(current_people_count, initial_people_count)

    def test_print_with_no_allocations(self):
        """test for print allocations without people allocated"""
        my_output = self.my_instance.print_allocations()
        self.assertIn("no allocations to print", my_output)

    def test_print_allocations_on_file(self):
        """test for printing allocation on a file"""
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "fellow", "y")
        self.my_instance.print_allocations("allocations.txt")
        self.assertTrue(os.path.isfile("allocations.txt"))
        os.remove("allocations.txt")

    def test_print_allocations_on_screen(self):
        """test for displaying allocations"""
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "fellow", "y")
        self.my_instance.print_allocations()
        my_output = sys.stdout.getvalue()
        self.assertIn("martin white", my_output)

    def test_print_unallocated(self):
        """test for displaying unallocated people"""
        self.my_instance.create_room("office", "grey")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "fellow", "y")
        self.my_instance.print_unallocated()
        my_output = sys.stdout.getvalue()
        self.assertIn("martin", my_output)

    def test_print_unallocated_from_empty_list(self):
        """test for displaying unallocated people"""
        self.my_instance.create_room("office", "white")
        my_output = self.my_instance.print_unallocated()
        self.assertIn("no list of unallocated people", my_output)

    def test_print_unallocated_on_file(self):
        """test for printing unallocated people on a file"""
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "staff")
        self.my_instance.add_person("martin", "white", "fellow", "y")
        self.my_instance.add_person("martin", "white", "fellow", "y")
        self.my_instance.print_unallocated("unallocations.txt")
        self.assertTrue(os.path.isfile("unallocations.txt"))
        os.remove("unallocations.txt")

    def test_reallocate_office(self):
        """test for reallocation function"""
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("Godwin", "Gitonga", "staff")
        self.assertEqual(len(self.my_instance.office_allocations["blue"]), 1)
        self.my_instance.create_room("office", "white")
        self.my_instance.reallocate_person(1, "white")
        self.assertEqual(len(self.my_instance.office_allocations["blue"]), 0)
        self.assertEqual(len(self.my_instance.office_allocations["white"]), 1)

    def test_reallocate_living_space(self):
        """test for reallocation function"""
        self.my_instance.create_room("living_space", "blue")
        self.my_instance.add_person("Godwin", "Gitonga", "fellow", "Y")
        self.assertEqual(
            len(self.my_instance.living_space_allocations["blue"]), 1)
        self.my_instance.create_room("living_space", "white")
        self.my_instance.reallocate_person(1, "white")
        self.assertEqual(
            len(self.my_instance.living_space_allocations["blue"]), 0)
        self.assertEqual(
            len(self.my_instance.living_space_allocations["white"]), 1)

    def test_reallocate_non_existing_person(self):
        self.my_instance.create_room("office", "blue")
        self.assertEqual(len(self.my_instance.office_allocations["blue"]), 0)
        self.my_instance.create_room("office", "white")
        output = self.my_instance.reallocate_person(1, "white")
        self.assertIn("person to reallocate doesn't exist", output)

    def test_reallocate_non_existing_room(self):
        self.my_instance.create_room("office", "blue")
        self.my_instance.add_person("Godwin", "Gitonga", "staff")
        self.assertEqual(len(self.my_instance.office_allocations["blue"]), 1)
        output = self.my_instance.reallocate_person(1, "white")
        self.assertIn("room not available for reallocation", output)

    def test_default_save_state(self):
        """test for storing the current state in the database"""
        self.my_instance.save_state()
        self.assertTrue(os.path.isfile('my_default_db.sqlite'))
        os.remove('my_default_db.sqlite')

    def test_save_state_with_filename(self):
        """test for storing the current state in the database"""
        self.my_instance.save_state("my_db")
        self.assertTrue(os.path.isfile('my_db.sqlite'))
        os.remove('my_db.sqlite')

    def test_content_in_db(self):
        db_name = "my_db"
        self.my_instance.create_room("office", "blue")
        self.my_instance.save_state(db_name)
        conn = sqlite3.connect(db_name + ".sqlite")
        c = conn.cursor()
        query = "SELECT room_name FROM room"
        cursor = c.execute(query)
        last_id = cursor.fetchone()[0]
        self.assertIn(last_id, "blue")

    def test_load_state(self):
        """test for retrieving the current state from the database"""
        my_instance = Dojo()
        people_initial_list = len(my_instance.people)
        my_instance.load_state("alloc")
        people_new_list = len(my_instance.people)
        self.assertTrue(people_new_list > people_initial_list)
コード例 #2
0
ファイル: test_dojo.py プロジェクト: akash-011/andela_Project
class TestDojo(unittest.TestCase):
    def setUp(self):
        self.new = Dojo()

    def test_check_one_office_Added(self):
        self.new.create_room("office", ["black"])
        self.assertEqual(len(self.new.offices), 1)

    def test_multiple_offices_added(self):
        self.new.create_room("office", ["black", "blue", "green"])
        self.assertEqual(len(self.new.offices), 3)

    def test_livingspace_added(self):
        self.new.create_room('living space', ['amsterdam'])
        self.assertEqual(len(self.new.living_spaces), 1)

    def test_multiple_living_spaces(self):
        self.new.create_room('living space', ['amsterdam', 'london'])
        self.assertEqual(len(self.new.living_spaces), 2)

    def test_cant_create_same_office_twice(self):
        self.new.create_room('office', ['amsterdam'])
        self.new.create_room('office', ['amsterdam'])
        self.assertEqual(len(self.new.offices), 1)

    def test_cant_create_same_living_twice(self):
        self.new.create_room('living space', ['amsterdam'])
        self.new.create_room('living space', ['amsterdam'])
        self.assertEqual(len(self.new.living_spaces), 1)

    def test_cant_create_room_with_same_names(self):
        self.new.create_room('living space', ['amsterdam'])
        self.new.create_room('office', ['amsterdam'])
        self.assertEqual(len(self.new.living_spaces), 1)

    def test_add_fellow(self):
        self.new.add_person('Akash Baga', 'fellow', 'Y')
        self.assertEqual(len(self.new.all_people), 1)

    def test_add_staff(self):
        self.new.add_person('Akash Baga', 'staff', 'N')
        self.assertEqual(len(self.new.all_people), 1)

    def test_cant_add_same_person_again(self):
        self.new.add_person('Akash Baga', 'staff', 'N')
        self.new.add_person('Akash Baga', 'staff', 'N')
        self.assertEqual(len(self.new.all_people), 1)

    def test_cant_add_same_staff_again(self):
        self.new.add_person('Akash Baga', 'staff', 'N')
        self.new.add_person('Akash Baga', 'staff', 'N')
        self.assertEqual(len(self.new.all_people), 1)

    def test_allocated_office(self):
        self.new.create_room('office', ['jbo'])
        self.new.add_person('Joe', 'staff', 'N')
        members = self.new.offices[0].occupants
        self.assertEqual(len(members), 1)

    def test_allocated_living(self):
        self.new.create_room('living', ['jbo'])
        self.new.add_person('Joe', 'fellow', 'Y')
        members = self.new.living_spaces[0].occupants
        self.assertEqual(len(members), 1)

    def test_print_allocations_to_file(self):
        self.new.create_room('living', ['jbo'])
        self.new.add_person('Joe', 'fellow', 'Y')
        self.new.print_allocations_to_file('allocations.txt')
        self.assertTrue(os.path.exists("allocations.txt"))
        os.remove('allocations.txt')

    def test_print_unllocations_to_file(self):
        self.new.create_room('living', ['jbo'])
        self.new.add_person('Joe', 'fellow', 'Y')
        self.new.print_unallocations_to_file('unallocations.txt')
        self.assertTrue(os.path.exists("unallocations.txt"))
        os.remove('unallocations.txt')

    def test_unallocated_person(self):
        self.new.add_person('Joe', 'staff', 'N')
        self.assertEqual(len(self.new.unallocated_office), 1)

    def test_unallocated_no_unallocated_person(self):
        self.new.create_room('office', ['jbo'])
        self.new.add_person('Joe', 'staff', 'N')
        self.assertEqual(len(self.new.unallocated_office), 0)

    def test_reallocate_person_person_added(self):
        self.new.create_room('office', ['jbo'])
        self.new.add_person('Joe', 'staff', 'N')
        old_office = self.new.offices[0]
        self.new.create_room('office', ['ams'])
        new_office = self.new.offices[1]
        self.new.reallocate_person('Joe', 'ams')
        self.assertEqual(len(new_office.occupants), 1)

    def test_reallocate_person_person_removed(self):
        self.new.create_room('office', ['jbo'])
        self.new.add_person('Joe', 'staff', 'N')
        old_office = self.new.offices[0]
        self.new.create_room('office', ['ams'])
        new_office = self.new.offices[1]
        self.new.reallocate_person('Joe', 'ams')
        self.assertEqual(len(old_office.occupants), 0)

    def test_save_state(self):
        self.new.create_room('office', ['jbo'])
        self.new.add_person('Joe', 'staff', 'N')
        self.new.save_state('dojo_db')
        self.assertTrue(os.path.exists("dojo_db"))
        os.remove('dojo_db')

    def test_save_state_data(self):
        self.new.create_room('office', ['jbo'])
        self.new.add_person('Joe', 'staff', 'N')
        self.new.save_state('dojo_db')
        engine = create_engine('sqlite:///dojo')
        Session = sessionmaker()
        Session.configure(bind=engine)
        session = Session()
        everyone = session.query(People).all()
        all_rooms = session.query(Rooms).all()
        self.assertEqual(everyone[0].name, 'Joe')

    def test_load_state_data(self):
        self.new.load_state('dojo_db')
        self.assertEqual(len(self.new.all_people), 1)
        os.remove('dojo_db')

    def test_load_people(self):
        self.new.create_room('office', ['jbo'])
        f = open("test.txt", "a")
        f.write("Paul Pogba FELLOW Y" + "\n")
        f.close()
        self.new.load_people('test.txt')
        self.assertEqual(len(self.new.all_people), 1)
        os.remove('test.txt')