<jupyter_start><jupyter_text>Drills - unit testingNow that you have the basics, let's do some drills!Create a unittest for each of these functions.  Custom exceptionWe define here some custom exception.<jupyter_code>class InvalidParamsException(Exception):
    """Raised when a param is invalid."""
    pass<jupyter_output><empty_output><jupyter_text>AdditionCheck the result for those params:* [1, 1]* [2, 3]* [5, 2]* [20, 4]* [0, 200]* [2999, 231234]* [0, 0]In addition, create a test that checks that `InvalidParamsException` is raised correctly if you give something else than an int to the function.Check for: `string, float, dict, list`<jupyter_code>def addition(number_one: int, number_two: int) -> int:
    if not isinstance(number_one, int) or not isinstance(number_two, int):
        raise InvalidParamsException("A parameter is not an int!")
    return number_one + number_one
# Add your unit test here<jupyter_output><empty_output><jupyter_text>SubstractionCheck the result for those params:* [1, 1]* [2, 3]* [5, 2]* [20, 4]* [0, 200]* [2999, 231234]* [0, 0]In addition, create a test that checks that `InvalidParamsException` is raised correctly if you give something else than an int to the function. Check for: string, float, dict, list.<jupyter_code>def substraction(number_one: int, number_two: int) -> int:
    if not isinstance(number_one, int) or not isinstance(number_two, int):
        raise InvalidParamsException("A param is not an int!")
    return number_one - number_one
# Add your unit test here<jupyter_output><empty_output><jupyter_text>DivideHere is a function that returns a float and can take a float or an int as parameter.Create a test that will check the result of those params:* [1, 1]* [2, 3]* [5, 2]* [20, 4]* [0, 200]* [2999, 231234]* [0, 0]* [5, 0]* [5, "9"]* [ 5, [1, 2] ]* [2. {"param2": 2}]For each param, check that the result is the expected type (a string if one the params is bad else a float).<jupyter_code>from typing import Union

def divide(number_one: Union[int, float], number_two: Union[int, float]) -> Union[float, str]:
    try:
        result = number_one / number_two
    except ZeroDivisionError:
        result = "You can't divide by zero!"
    except Exception as ex:
        result = f"A param is not an int or a float! -> {ex}"

    return result
# Add your unit test here<jupyter_output><empty_output><jupyter_text>File handling1. Create a function `create_and_delete_test_file()` that creates a new file named 'test.txt' and add 'this is a text' in it then delete the file.2. Create a test that checks if the file is well created.3. Create a test to check that the content of the file is 'this is a text'. You will need to find a way to prevent the function to delete the file during this specific test.4. Create a test to check that the function deletes the file<jupyter_code># Create create_and_delete_test_file()
# Add your unit test here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10146407899216887, 'fraction_numerical': 0.030303030303030304, 'mean_word_length': 4.715953307392996, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '301090', 'license': 'MIT', 'n_tokens_mistral': 946, 'n_tokens_neox': 884, 'n_words': 389, 'path': 'Content/1.python/2.python_advanced/11.unittest/03.drills.ipynb', 'repo_name': 'becodeorg/ai_fundamentals_1.0', 'size': '2937'}