コード例 #1
0
class TestsForTask3SaveState(unittest.TestCase):
    """
    Test cases for save_state functionality and method
    """

    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_if_filename_is_too_short(self):
        """Tests to see if the file name passed to be written is to short to be a valid txt file
        """
        file_name_entered = self.dojo.save_state("sql")
        self.assertEqual(file_name_entered, "short", msg="The file name entered is too short to be valid")

    def test_if_filename_does_not_end_with_sqlite(self):
        """
        Tests if a file name entered is not a valid text file since it doesn't end with .txt
        :return:
        """
        another_file_name = self.dojo.save_state("plain_text")
        self.assertEqual(another_file_name, "not sqlite file", msg="The file name entered doesn't end with .sqlite")

    def test_if_a_valid_file_name_is_used(self):
        """
        Tests to see if a valid text file name is recognized and written to
        :return:
        """
        valid_file = self.dojo.save_state("valid_db.sqlite")
        self.assertEqual(valid_file, "valid_db.sqlite")

    def test_if_no_file_name_runs_successfully(self):
        """
        Tests to see if when argument is provided, the function runs successfully but doesn't create a file
        :return:
        """
        no_db_name = self.dojo.save_state()
        self.assertEqual(no_db_name, None)

    def test_if_people_are_actually_saved_in_the_database(self):
        """
        Tests storing people in the database and seeing if they are retrieved and added back to the system
        :return:
        """
        self.dojo.load_people("test_load_people.txt")
        initial_people_count = len(self.dojo.all_people)  # shd return 7

        self.dojo.save_state("test_sqlite_db.sqlite")
        self.dojo.all_rooms.clear()
        self.dojo.all_people.clear()

        self.dojo.load_state("test_sqlite_db.sqlite")
        new_people_count = len(self.dojo.all_people)  # shd return same 7

        self.assertEqual(new_people_count, initial_people_count)
コード例 #2
0
ファイル: dojo.py プロジェクト: lotus321/optimal-trading-btc
def run():
    """
     Start blueprint
    """
    dojo = Dojo()
    start_time = time.time()
    args = arg_parser.parse_known_args()[0]
    dojo.train(blueprint=args.blueprint)
    end_time = time.time()
    time_delta = end_time - start_time
    print('Finished in ' + str(int(time_delta)) + ' sec.')
コード例 #3
0
class TestsForTask0CreateRoom(unittest.TestCase):
    """
    Test cases for create_room functionalities
    """
    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_create_room_successfully(self):
        """
        Test if a room is created successfully with the create_room method in Dojo
        """
        blue_office = self.dojo.create_room("office", "Blue")
        self.assertTrue(blue_office)

    def test_create_room_added_to_list(self):
        """Test if the total room count increases after creating a room
        """
        initial_room_count = len(self.dojo.all_rooms)
        self.dojo.create_room("office", "Blue")
        new_room_count = len(self.dojo.all_rooms)
        self.assertEqual(new_room_count - initial_room_count, 1)

    def test_bad_room_type_not_created(self):
        """Test that a room is not created when the room type is not office or livingspace
        """
        no_office_created = self.dojo.create_room("blue", "office")
        self.assertFalse(no_office_created)

    def test_duplicate_room_not_created(self):
        """Test that a room that has a name as an already created room is not created
        """
        self.dojo.create_room("livingspace", "Love")
        duplicate_room = self.dojo.create_room("livingspace", "Love")
        self.assertEqual(duplicate_room, False)
class TestsForTask2LoadPeople(unittest.TestCase):
    """
    Test cases for the load people functionality and method
    """

    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_if_filename_is_too_short(self):
        """Tests to see if the file name passed to be written is to short to be a valid txt file
        """
        file_name_entered = self.dojo.load_people("txt")
        self.assertEqual(file_name_entered, "short", msg="The file name entered is too short to be valid")

    def test_if_filename_does_not_end_with_txt(self):
        """
        Tests if a file name entered is not a valid text file since it doesn't end with .txt
        :return:
        """
        another_file_name = self.dojo.load_people("plain_text")
        self.assertEqual(another_file_name, "not txt file", msg="The file name entered doesn't end with .txt")

    def test_if_a_valid_file_name_is_used(self):
        """
        Tests to see if a valid text file name is recognized and read
        :return:
        """
        valid_file = self.dojo.load_people("valid_file.txt")
        self.assertEqual(valid_file, "valid_file.txt")

    def test_if_people_are_actually_loaded_successfully_to_system(self):
        """
        Tests if people loaded from a file are stored in the people list
        :return:
        """
        initial_people_count = len(self.dojo.all_people)
        self.dojo.load_people("test_load_people.txt")
        new_people_count = len(self.dojo.all_people)
        self.assertEqual(new_people_count - initial_people_count, 7)
コード例 #5
0
class TestsForTask1PrintRoom(unittest.TestCase):
    """Test cases for the print_room method and functionality
    """
    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_room_name_not_exists(self):
        """Tests if there is an already created room with the given name
        """
        self.dojo.create_room("office", "allan")
        exists = self.dojo.print_room("jumbo")
        self.assertListEqual(exists, [False],
                             msg="should return a list containing False")

    def test_get_list_of_names(self):
        """Tests to see if it prints a list of people's names that are in a room
        """
        self.dojo.create_room("livingspace", "test")

        allan = Person("allan", "great")
        john = Person("john", "doe")
        mark = Person("mark", "alvin")

        people = [allan, john, mark]

        self.dojo.all_rooms[
            0].occupants = people  # assigned the people to the room's occupants

        self.assertListEqual(people,
                             self.dojo.print_room("test"),
                             msg="list of names found and printed")
コード例 #6
0
 def setUp(self):
     """Set up the test case class"""
     self.dojo = Dojo()
コード例 #7
0
class TestsForTask2ReallocatePerson(unittest.TestCase):
    """
    Test cases for the reallocate_room method and functionality
    """
    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_reallocate_to_no_non_existent_room(self):
        """"
        Test if reallocating to a room that doesn't exist fails with a message
        """
        no_room = self.dojo.reallocate_person("one", "two", "non_existent")
        self.assertEqual(no_room, "no_room")

    def test_if_available_room_is_reallocated_to(self):
        """
        Test if an available room to be reallocated to is actually found
        :return:
        """
        self.dojo.create_room("livingspace", "my_room")
        self.dojo.reallocate_person("fName", "lName", "my_room")
        self.assertEqual(self.dojo.all_rooms[-1].room_name,
                         "my_room")  # the last entered room is found

    def test_reallocate_to_non_existent_person(self):
        """
        Test if reallocating to a person who is not in the system fails
        :return:
        """
        self.dojo.create_room("livingspace", "my_room")
        no_person = self.dojo.reallocate_person("romain", "guy", "my_room")
        self.assertEqual(no_person, "no_person", msg="Should return no_person")

    def test_reallocate_to_an_unallocated_person(self):
        """
        Test if reallocating a person who has not been fully allocated before
        :return:
        """
        self.dojo.create_room("livingspace", "my_space")
        self.dojo.add_person("welike", "elly", "fellow", "y")
        unallocated = self.dojo.reallocate_person("welike", "elly", "my_space")
        self.assertEqual(unallocated, "unallocated_person")

    def test_reallocate_an_allocated_person(self):
        """
        Test if a fully allocated person is found and the room entered is also found
        :return:
        """
        self.dojo.create_room("office", "my_office")
        self.dojo.add_person("john", "ike", "staff")
        self.dojo.reallocate_person("john", "ike", "my_office")
        self.assertEqual(self.dojo.all_people[-1].is_allocated, True)

    def test_cant_move_staff_to_living_space(self):
        """
        Test if Staff can't be reallocated to a living space room
        :return:
        """
        self.dojo.create_room("livingspace", "my_space")
        self.dojo.create_room("office", "my_office")
        self.dojo.add_person("john", "ike", "staff")
        no_living_space = self.dojo.reallocate_person("john", "ike",
                                                      "my_space")
        self.assertEqual(no_living_space, "staff_no_living_space")

    def test_reallocate_to_the_same_room(self):
        """
        Test if the room to reallocate to is the same as what the person is already assigned to
        :return:
        """
        self.dojo.create_room("livingspace", "my_space")
        self.dojo.create_room("office", "my_office")
        self.dojo.add_person("john", "ike", "staff")
        same_room = self.dojo.reallocate_person("john", "ike", "my_office")
        self.assertEqual(same_room, "same_room")

    def test_if_person_has_been_moved_from_the_former_room(self):
        """
        Test the number of occupants of the old room have been reduced by 1
        :return:
        """
        self.dojo.create_room("office", "my_first_office")
        self.dojo.add_person("john", "ike", "staff")
        first_office_occupants_number_before = len(
            self.dojo.all_rooms[0].occupants)
        self.dojo.create_room("office", "my_second_office")
        self.dojo.reallocate_person("john", "ike", "my_second_office")
        first_office_occupants_number_after = len(
            self.dojo.all_rooms[0].occupants)
        self.assertEqual(
            first_office_occupants_number_before -
            first_office_occupants_number_after, 1)

    def test_if_person_has_been_added_to_the_new_room(self):
        """
        Test if the number of occupants of the room moved to has increased by 1
        :return:
        """
        self.dojo.create_room("office", "my_first_office")
        self.dojo.add_person("john", "ike", "staff")
        self.dojo.create_room("office", "my_second_office")
        second_office_occupants_number_before = len(
            self.dojo.all_rooms[1].occupants)
        self.dojo.reallocate_person("john", "ike", "my_second_office")
        second_office_occupants_number_after = len(
            self.dojo.all_rooms[1].occupants)
        self.assertEqual(
            second_office_occupants_number_after -
            second_office_occupants_number_before, 1)

    def test_if_function_runs_successfully_as_expected(self):
        """
        Test if the expected strings are returned from each conditions
        :return:
        """
        self.dojo.create_room("office", "my_first_office")
        self.dojo.add_person("john", "ike", "staff")
        self.dojo.create_room("office", "my_second_office")
        string_expected = self.dojo.reallocate_person("john", "ike",
                                                      "my_second_office")
        self.assertEqual(
            string_expected,
            "my_second_office, john ike, staff, office, my_first_office")
class TestsForTask1PrintAllocations(unittest.TestCase):
    """Test cases for the print_allocations method and functionality
    """

    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_if_filename_is_too_short(self):
        """Tests to see if the file name passed to be written is to short to be a valid txt file
        """
        file_name_entered = self.dojo.print_allocations("txt")
        self.assertEqual(file_name_entered, "short", msg="The file name entered is too short to be valid")

    def test_if_filename_does_not_end_with_txt(self):
        """
        Tests if a file name entered is not a valid text file since it doesn't end with .txt
        :return:
        """
        another_file_name = self.dojo.print_allocations("plain_text")
        self.assertEqual(another_file_name, "not txt file", msg="The file name entered doesn't end with .txt")

    def test_if_a_valid_file_name_is_used(self):
        """
        Tests to see if a valid text file name is recognized and written to. check if the right assignments are returned
        :return:
        """
        self.dojo.create_room("office", "red")
        self.dojo.add_person("allan", "muhwezi", "fellow", "y")

        valid_file = self.dojo.print_allocations("valid_file.txt")
        expected_string = "valid_file.txt" + ":" + "RED" + "\n" + "----------------------------------------------\n" + "allan muhwezi" + "\n\n"
        self.assertMultiLineEqual(valid_file, expected_string)

    def test_if_no_file_name_runs_successfully(self):
        """
        Tests to see if when argument is provided, the function runs successfully but doesn't create a file. check to
        see if the correct assignments are retrieved
        :return:
        """
        self.dojo.create_room("office", "red")
        self.dojo.add_person("allan", "muhwezi", "fellow", "y")

        valid_file = self.dojo.print_allocations()
        expected_string = "none:" + "RED" + "\n" + "----------------------------------------------\n" + "allan muhwezi" + "\n\n"
        self.assertMultiLineEqual(valid_file, expected_string)

        """TODO test like in print_room - same as - unallocated - load_people - add people to the text file. save_state: save and retrieve happen well."""
        """Spread some functionality across the classes"""
class TestsForTask1PrintUnAllocated(unittest.TestCase):
    """Test cases for the print_allocations method and functionality
    """

    def setUp(self):
        """Set up the test case class"""
        self.dojo = Dojo()

    def test_if_filename_is_too_short(self):
        """Tests to see if the file name passed to be written is to short to be a valid txt file
        """
        file_name_entered = self.dojo.print_unallocated("txt")
        self.assertEqual(file_name_entered, "short", msg="The file name entered is too short to be valid")

    def test_if_filename_does_not_end_with_txt(self):
        """
        Tests if a file name entered is not a valid text file since it doesn't end with .txt
        :return:
        """
        another_file_name = self.dojo.print_unallocated("plain_text")
        self.assertEqual(another_file_name, "not txt file", msg="The file name entered doesn't end with .txt")

    def test_if_a_valid_file_name_is_used(self):
        """
        Tests to see if a valid text file name is recognized and written to. check to see if the unallocated people are
        successfully retrieved
        :return:
        """
        self.dojo.add_person("allan", "muhwezi", "fellow", "y")
        self.dojo.add_person("van", "bro", "fellow", "y")
        valid_file = self.dojo.print_unallocated("valid_file.txt")
        self.assertEqual(valid_file, "valid_file.txt" + ":" + "allan muhwezi" + "\n" + "van bro" + "\n")

    def test_if_no_file_name_runs_successfully(self):
        """
        Tests to see if when argument is provided, the function runs successfully but doesn't create a file
        :return:
        """
        self.dojo.add_person("allan", "muhwezi", "fellow", "y")
        self.dojo.add_person("van", "bro", "fellow", "y")
        no_file_name = self.dojo.print_unallocated()
        self.assertEqual(no_file_name, "none" + ":" + "allan muhwezi" + "\n" + "van bro" + "\n")
コード例 #10
0
        except SystemExit:
            # The SystemExit exception prints the usage for --help
            # We do not need to do the print here.

            return

        return func(self, opt)

    fn.__name__ = func.__name__
    fn.__doc__ = func.__doc__
    fn.__dict__.update(func.__dict__)
    return fn


my_dojo = Dojo()  # get the Dojo object to use to call its methods


class App(cmd.Cmd):
    """
    The App class ties everything together.
    It inherits from CMD and hence is used to get arguments and commands from the command line
    using docopt.
    The commands entered direct which methods to call from Dojo so as to carry out the tasks
    """

    intro = '\n\t\tWelcome to my Dojo Space Allocation program!' \
        + '\n\n\t\t\t----------------------\n' \
        + '\nCommands: \n\tcreate_room <room_type> <room_name>...' \
        + '\n\tadd_person <first_name> <last_name> <person_type> [<wants_accommodation>]' \
        + '\n\tprint_room <room_name>' \
コード例 #11
0
 def setUp(self):
     """Initialize the test case class"""
     self.dojo = Dojo()
コード例 #12
0
class TestsForTask0AddPerson(unittest.TestCase):
    """Test cases for add_person functionalities
    """
    def setUp(self):
        """Initialize the test case class"""
        self.dojo = Dojo()

    def test_add_person_successfully(self):
        """
        Test if a person is added successfully using the add_person method in Dojo
        """
        staff_one = self.dojo.add_person("Stanley", "Lee", "staff")
        self.assertTrue(staff_one)

    def test_new_person_added_to_list(self):
        """Test if the total people count increases after creating a person
        """
        initial_people_count = len(self.dojo.all_people)
        self.dojo.add_person("Stan", "Lee", "staff")
        new_people_count = len(self.dojo.all_people)
        self.assertEqual(new_people_count - initial_people_count, 1)

    def test_duplicate_person_not_created(self):
        """Test that a person that has a name as an already created person is not created
        """
        self.dojo.add_person("Stan", "Lee", "staff")
        staff_duplicate = self.dojo.add_person("Stan", "Lee", "staff")
        self.assertEqual(staff_duplicate, False)

    def test_if_person_is_assigned_an_office(self):
        """Test and see if a person is created when there is an available room, that person is assigned to the room
        """
        self.dojo.create_room("livingspace", "sample")
        self.dojo.add_person("Jumped", "Out", "fellow", "Y")
        self.assertTrue(self.dojo.all_people[0].is_allocated)

    def test_person_with_unknown_person_type_not_created(self):
        """Test if a person with person type not fellow or staff is not added
        """
        alien_person = self.dojo.add_person("Jim", "Kim", "star")
        self.assertFalse(alien_person)

    def test_staff_not_assigned_living_space(self):
        """Test if only a livingspace room is available and a staff is added, whether the staff will be assigned
         the livingspace room
        """
        self.dojo.create_room("livingspace", "sample")
        self.dojo.add_person("Mr", "John", "staff")
        self.assertFalse(self.dojo.all_people[0].is_allocated)