def test005_when_missing_csv_is_opened_error_is_raised(self):
        method = 'test20_when_missing_csv_is_opened_error_is_raised'
        csv_file = CSV_MISSING
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Started ------------------"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Expecting FileNotFoundException"))

        with self.assertRaises(
                FileNotFoundError,
                msg=f"FileNotFoundError should have been triggered"):
            _ = WordSearchRepository(csv_file=csv_file)

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Completed ----------------"))
        return
    def test037_when_grid_is_not_square_error_is_raised(self):
        method = 'test037_when_grid_is_not_square_error_is_raised'
        csv_file = CSV_ABC_BAD_GRID_GOOD_WORDS
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Started ------------------"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="GridDimensionException Expected"))

        with self.assertRaises(
                GridDimensionException,
                msg=f"GridDimensionException should have been triggered"):
            _ = WordSearchRepository(csv_file=csv_file)

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Completed ----------------"))
        return
    def test010_when_empty_csv_is_read_error_is_raised(self):
        method = 'test10_when_empty_csv_is_read_error_is_raised'
        csv_file = CSV_EMPTY

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Started ------------------"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Expecting FileFormatException"))

        with self.assertRaises(
                FileFormatException,
                msg=f"FileFormatException should have been triggered"):
            _ = WordSearchRepository(csv_file=csv_file)

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Completed ----------------"))
        return
    def test030_when_good_csv_is_read_a_grid_is_found(self):
        method = 'test030_when_good_csv_is_read_a_grid_is_found'
        csv_file = CSV_ABC_GOOD_GRID_GOOD_WORDS
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Started ------------------"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        repo = WordSearchRepository(csv_file=csv_file)

        for r in repo.get_word_search().get_grid():
            self.log.info(
                logger_helper.format_log(classname=self.classname,
                                         method=method,
                                         msg=f"Row {r}"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Completed ----------------"))
        return
Exemplo n.º 5
0
    def __search_diagonal_descending_reverse(self, word_as_chars=None):
        method = "__search_diagonal_descending_reverse"
        word_as_chars_reverse = list()
        word_as_chars_reverse.extend(word_as_chars)
        word_as_chars_reverse.reverse()

        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Original VERT Search {str(word_as_chars)}")
        )
        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Starting REVERSE DIAGONAL ASCENDING Search {str(word_as_chars_reverse)}")
        )
        tmp_coord_list = self.__search_diagonal_descending(word_as_chars=word_as_chars_reverse)
        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"RETURNING {len(tmp_coord_list)} matching characters")
        )

        coord_list = list()
        coord_list.extend(tmp_coord_list)
        coord_list.reverse()

        return coord_list
    def test020_when_good_csv_is_read_a_word_list_is_found(self):
        method = 'test020_when_good_csv_is_read_a_word_list_is_found'
        csv_file = CSV_ABC_GOOD_GRID_GOOD_WORDS
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Started ------------------"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        repo = WordSearchRepository(csv_file=csv_file)
        self.assertGreater(len(repo.get_word_search().get_word_list()),
                           0,
                           msg=f"Could not find words in {csv_file}")

        for w in repo.get_word_search().get_word_list():
            self.log.info(
                logger_helper.format_log(classname=self.classname,
                                         method=method,
                                         msg=f"Word {w}"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Completed ----------------"))
        return
    def test025_when_word_list_is_short_error_is_raised(self):
        method = 'test025_when_word_list_is_short_error_is_raised'
        csv_file = CSV_ABC_GOOD_GRID_BAD_WORDS
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Started ------------------"))

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Expecting WordLengthException"))

        with self.assertRaises(
                WordLengthException,
                msg=f"WordLengthException should have been triggered"):
            _ = WordSearchRepository(csv_file=csv_file)

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg="Completed ----------------"))
        return
 def __check_grid_size(self):
     method = "__check_grid_size"
     if len(self.get_word_search().get_grid()) > 0:
         dimension_dict = self.get_word_search().get_grid_dimension()
         row = dimension_dict[WordSearchModel.CONST_ROW]
         col = dimension_dict[WordSearchModel.CONST_COL]
         if row != col:
             msg = f"raising GridDimensionException since GRID is not square. It is {row}x{col}"
             self.log.error(
                 logger_helper.format_log(classname=self.classname,
                                          method=method,
                                          msg=msg))
             raise GridDimensionException(msg)
         else:
             msg = f"GRID is square at {row}x{col}"
             self.log.info(
                 logger_helper.format_log(classname=self.classname,
                                          method=method,
                                          msg=msg))
     else:
         msg = f"raising GridDimensionException since grid is missing"
         self.log.error(
             logger_helper.format_log(classname=self.classname,
                                      method=method,
                                      msg=msg))
         raise GridDimensionException(msg)
     return
Exemplo n.º 9
0
    def find_word(self, word_as_chars=None):
        method = "find_word"
        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Searching for {str(word_as_chars)}")
        )

        coord_list = self.__search_horizontal(word_as_chars=word_as_chars)
        if len(coord_list) < 1:
            coord_list = self.__search_vertical(word_as_chars=word_as_chars)
            if len(coord_list) < 1:
                coord_list = self.__search_horizontal_reverse(word_as_chars=word_as_chars)
                if len(coord_list) < 1:
                    coord_list = self.__search_vertical_reverse(word_as_chars=word_as_chars)
                    if len(coord_list) < 1:
                        coord_list = self.__search_diagonal_ascending(word_as_chars=word_as_chars)
                        if len(coord_list) < 1:
                            coord_list = self.__search_diagonal_ascending_reverse(word_as_chars=word_as_chars)
                            if len(coord_list) < 1:
                                coord_list = self.__search_diagonal_descending(word_as_chars=word_as_chars)
                                if len(coord_list) < 1:
                                    coord_list = self.__search_diagonal_descending_reverse(word_as_chars=word_as_chars)

        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Found {len(coord_list)} chars")
        )
        return coord_list
Exemplo n.º 10
0
    def test300_when_log_directory_is_missing_log_directory_is_created(self):
        method = "test300_when_log_directory_is_missing_log_directory_is_created"
        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )

        guid_sub_directory = str(uuid.uuid4())
        directory = os.path.join(config.LOG_DIR, guid_sub_directory)
        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg=f"Creating {directory}")
        )
        logger_helper.create_logger_directory(directory=directory)
        self.assertTrue(os.path.exists(directory), msg=f"{directory} was not created or has a problem")
        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg=f"Created {directory}")
        )
        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg=f"Removing {directory}")
        )
        os.rmdir(directory)
        self.assertFalse(os.path.exists(directory), msg=f"{directory} could not be removed")

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
Exemplo n.º 11
0
    def __search_horizontal_reverse(self, word_as_chars=None):
        method = "__search_horizontal_reverse"
        word_as_chars_reverse = list()
        word_as_chars_reverse.extend(word_as_chars)
        word_as_chars_reverse.reverse()

        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Original HORIZ Search {str(word_as_chars)}")
        )
        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Starting REVERSE HORIZ Search {str(word_as_chars_reverse)}")
        )
        tmp_coord_list = self.__search_horizontal(word_as_chars=word_as_chars_reverse)
        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"RETURNING {len(tmp_coord_list)} matching characters")
        )

        coord_list = list()
        coord_list.extend(tmp_coord_list)
        coord_list.reverse()

        return coord_list
Exemplo n.º 12
0
    def __search_diagonal_descending(self, word_as_chars=None):
        method = "__search_diagonal_descending"
        match_char_list = None
        char_list = list()
        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"Starting DIAG DESC Search {str(word_as_chars)}")
        )
        row_len = len(self.__grid)
        col_len = len(self.__grid[0])

        self.log.debug(
            logger_helper.format_log(classname=self.classname, method=method,
                                     msg=f"row_len={row_len} by col_len={col_len}")
        )
        for col_pos in range(0, col_len):
            char_list.clear()

            for row_pos in range(0, (row_len - col_pos)):
                r = (row_len - 1) - row_pos
                c = col_pos + row_pos

                coordinate = CharCoordinateModel(c=self.__grid[r][c], row=r, col=c)
                char_list.append(coordinate)

            match_char_list = self.__find_in_char_list(word_as_chars=word_as_chars, char_list_from_grid=char_list)
            if len(match_char_list) > 0:
                break

        if len(match_char_list) < 1:
            for row_pos in range(1, row_len):
                char_list.clear()

                for col_pos in range(0, (col_len - row_pos)):
                    c = col_pos
                    r = (row_len - 1) - col_pos - row_pos
                    coordinate = CharCoordinateModel(c=self.__grid[r][c], row=r, col=c)
                    char_list.append(coordinate)

                match_char_list = self.__find_in_char_list(word_as_chars=word_as_chars, char_list_from_grid=char_list)
                if len(match_char_list) > 0:
                    break

        if match_char_list:
            self.log.debug(
                logger_helper.format_log(classname=self.classname, method=method,
                                         msg=f"RETURNING {len(match_char_list)} matching characters")
            )
            return match_char_list
        else:
            self.log.debug(
                logger_helper.format_log(classname=self.classname, method=method,
                                         msg=f"RETURNING 0 matching characters")
            )
            return list()
Exemplo n.º 13
0
 def test150_when_words_are_notfound_error_is_not_raised(self):
     method = 'test150_when_words_are_notfound_error_is_not_raised'
     csv_file = CSV_ABC_GRID_NOTFOUND_WORDS
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Started ------------------"))
     self.__main_test_executor(csv_file=csv_file, not_found_ok=True)
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Completed ----------------"))
     return
Exemplo n.º 14
0
 def test105_when_horiz_word_is_in_grid_char_coordinates_are_returned(self):
     method = 'test105_when_horiz_word_is_in_grid_char_coordinates_are_returned'
     csv_file = CSV_ABC_GRID_HORIZ_WORDS
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Started ------------------"))
     self.__main_test_executor(csv_file=csv_file)
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Completed ----------------"))
     return
Exemplo n.º 15
0
 def test145_when_reverse_diag_desc_word_is_in_grid_char_coordinates_are_returned(
         self):
     method = 'test145_when_reverse_diag_desc_word_is_in_grid_char_coordinates_are_returned'
     csv_file = CSV_ABC_GRID_DIAG_DESC_REVERSE_WORDS
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Started ------------------"))
     self.__main_test_executor(csv_file=csv_file)
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Completed ----------------"))
     return
 def test200_when_raw_word_search_rows_is_none_lists_are_empty(self):
     method = "test200_when_raw_word_search_rows_is_none_lists_are_empty"
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Started ------------------"))
     m = WordSearchModel(raw_word_search_rows=None)
     self.assertEqual(len(m.get_word_list()), 0,
                      "get_word_list should be a length of zero")
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Completed ----------------"))
     return
Exemplo n.º 17
0
    def __main_test_executor(self, csv_file=None, not_found_ok=False):
        method = '__main_test_executor'

        self.log.info(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Using {csv_file}"))
        repo = WordSearchRepository(csv_file=csv_file)
        controller = WordSearchController(
            grid=repo.get_word_search().get_grid())

        not_found_count = len(repo.get_word_search().get_word_list())
        for w in repo.get_word_search().get_word_list():
            self.log.info(
                logger_helper.format_log(classname=self.classname,
                                         method=method,
                                         msg=f"Searching for {w}"))
            coord_list = controller.find_word(word_as_chars=w)
            if len(coord_list) > 0:
                not_found_count -= 1
                self.log.info(
                    logger_helper.format_log(classname=self.classname,
                                             method=method,
                                             msg=f"{w} FOUND"))
                c_string = ""
                for c in coord_list:
                    c_string += f"{str(c)} "
                self.log.info(
                    logger_helper.format_log(classname=self.classname,
                                             method=method,
                                             msg=f"{c_string} FOUND"))
            else:
                self.log.warning(
                    logger_helper.format_log(classname=self.classname,
                                             method=method,
                                             msg=f"{w} NOT FOUND"))

        self.assertFalse(
            not_found_count > 0 and not_found_ok is False,
            msg=
            f"{not_found_count} of {len(repo.get_word_search().get_word_list())} words notfound in {csv_file}"
        )
        self.assertFalse(
            not_found_count < 1 and not_found_ok is True,
            msg=
            f"{not_found_count} of {len(repo.get_word_search().get_word_list())} words notfound when some were expected in {csv_file}"
        )

        return
    def __read_csv_file(self):
        method = "__read_csv_file"
        self.log.debug(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Reading {self.__csv_file}"))

        with open(self.__csv_file) as fh:
            data = fh.readlines()

        self.log.debug(
            logger_helper.format_log(classname=self.classname,
                                     method=method,
                                     msg=f"Read {len(data)}"))
        return data
 def test255_when_c_is_returned_value_matches_what_is_passed(self):
     method = "test255_when_c_is_returned_value_matches_what_is_passed"
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Started ------------------"))
     c = CharCoordinateModel(c="X", row="5", col="6")
     self.assertEqual(c.get_c(),
                      "X",
                      msg=f"c={c.get_c()} does not equal 'X'")
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Completed ----------------"))
     return
Exemplo n.º 20
0
 def setUp(self):
     method = "setUp"
     msg = f"SetUp for {config.CONST_APP_NAME} {config.CONST_APP_VERSION}"
     self.log.info(
         logger_helper.format_log(classname=self.classname, method=method, msg=msg)
     )
     return
    def test450_when_main_is_called_with_no_file_zero_is_returned(self):
        method = "test450_when_main_is_called_with_no_file_zero_is_returned"

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )

        arg_list = list()
        arg_list.append(os.path.join(os.curdir, "dummy.py"))

        self.assertEqual(0, word_search.main(argv=arg_list), "Expected zero since no file was provided")

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
Exemplo n.º 22
0
    def test310_when_log_to_console_is_False_propagate_is_False(self):
        method = "test310_when_log_to_console_is_False_propagate_is_False"
        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )
        old_propagate = self.log.propagate
        logger_helper.set_log_propagation(log=self.log, log_to_console=False)
        self.assertFalse(self.log.propagate, msg=f"self.log.propagate expected to be False preventing console logging")

        # Setting propagate back to original value
        logger_helper.set_log_propagation(log=self.log, log_to_console=old_propagate)

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
 def __check_csv_length(self):
     method = "__check_csv_length"
     if len(self.__csv_data) == 0:
         msg = f"raising FileFormatException {self.__csv_file} is zero length"
         self.log.error(
             logger_helper.format_log(classname=self.classname,
                                      method=method,
                                      msg=msg))
         raise FileFormatException(msg)
     else:
         msg = f"{self.__csv_file} has a length of {len(self.__csv_data)}"
         self.log.debug(
             logger_helper.format_log(classname=self.classname,
                                      method=method,
                                      msg=msg))
     return
Exemplo n.º 24
0
 def tearDown(self):
     method = "tearDown"
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="TearDown Completed"))
     return
    def test460_when_main_is_called_with_one_file_one_is_returned(self):
        method = "test460_when_main_is_called_with_one_file_one_is_returned"

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )

        arg_list = list()
        arg_list.append(os.path.join(os.curdir, "dummy.py"))
        arg_list.append(CSV_STAR_TREK)

        self.assertEqual(1, word_search.main(argv=arg_list), "Expected one since a good file was provided")

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
Exemplo n.º 26
0
    def test320_when_log_to_console_is_True_propagate_is_True(self):
        method = "test320_when_log_to_console_is_True_propagate_is_True"
        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )

        old_propagate = self.log.propagate
        logger_helper.set_log_propagation(log=self.log, log_to_console=True)
        self.assertTrue(self.log.propagate, msg=f"self.log.propagate expected to be True which allows console logging")

        # Setting propagate back to original value
        logger_helper.set_log_propagation(log=self.log, log_to_console=old_propagate)

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
 def __check_word_length(self):
     method = "__check_word_length"
     for w in self.get_word_search().get_word_list():
         if len(w) < config.MIN_WORD_LENGTH:
             msg = f"raising WordLengthException - {w} is {len(w)} which FAILS minimum of {config.MIN_WORD_LENGTH}"
             self.log.error(
                 logger_helper.format_log(classname=self.classname,
                                          method=method,
                                          msg=msg))
             raise WordLengthException(msg)
         else:
             msg = f"{w} is {len(w)} which passes minimum of {config.MIN_WORD_LENGTH}"
             self.log.debug(
                 logger_helper.format_log(classname=self.classname,
                                          method=method,
                                          msg=msg))
     return
    def test410_when_word_search_is_created_with_empty_file_error_is_raised(self):
        method = "test410_when_word_search_is_created_with_empty_file_error_is_raised"
        csv_file = CSV_EMPTY

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Expecting FileFormatException")
        )
        with self.assertRaises(FileFormatException, msg=f"FileFormatException should have been triggered"):
            _ = WordSearch(word_search_csv=csv_file)

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
    def test420_when_word_search_is_created_with_bad_grid_error_is_raised(self):
        method = "test420_when_word_search_is_created_with_bad_grid_error_is_raised"
        csv_file = CSV_BAD_GRID

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Started ------------------")
        )

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Expecting GridDimensionException")
        )
        with self.assertRaises(GridDimensionException, msg=f"GridDimensionException should have been triggered"):
            _ = WordSearch(word_search_csv=csv_file)

        self.log.info(
            logger_helper.format_log(classname=self.classname, method=method, msg="Completed ----------------")
        )
        return
 def test250_when_x_and_y_are_returned_x_equals_col_and_y_equals_row(self):
     method = "test250_when_x_and_y_are_returned_x_equals_col_and_y_equals_row"
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Started ------------------"))
     c = CharCoordinateModel(c="X", row="5", col="6")
     self.assertEqual(c.get_col(),
                      c.get_x(),
                      msg=f"col={c.get_col()} does not equal x={c.get_x()}")
     self.assertEqual(c.get_row(),
                      c.get_y(),
                      msg=f"row={c.get_row()} does not equal y={c.get_y()}")
     self.log.info(
         logger_helper.format_log(classname=self.classname,
                                  method=method,
                                  msg="Completed ----------------"))
     return