Adding unit & mock test examples, new tests directory and a snapshot

This commit is contained in:
Donald Calloway 2025-10-04 11:17:09 -07:00
parent 03f1049786
commit 5be329909a
9 changed files with 320 additions and 0 deletions

View File

@ -0,0 +1,212 @@
pytest
# Project Structure
├─ 📁 code_challenges
└─ CodeChallenge_16.py
└─ CodeChallenge_17.py
└─ CodeChallenge_15.py
└─ CodeChallenge_14.py
└─ CodeChallenge_18.py
└─ CodeChallenge_13.py
└─ CodeChallenge_12.py
└─ CodeChallenge_11.py
└─ CodeChallenge_19.py
└─ CodeChallenge_2.py
└─ CodeChallenge_10.py
└─ CodeChallenge_1.py
└─ CodeChallenge_02(Medium).py
└─ CodeChallenge_20.py
└─ CodeChallenge_4.py
└─ CodeChallenge_39.py
└─ CodeChallenge_38.py
└─ CodeChallenge_37.py
└─ CodeChallenge_36.py
└─ CodeChallenge_35.py
└─ CodeChallenge_34.py
└─ CodeChallenge_33.py
└─ CodeChallenge_32.py
└─ CodeChallenge_31.py
└─ CodeChallenge_30.py
└─ CodeChallenge_3.py
└─ CodeChallenge_29.py
└─ CodeChallenge_28.py
└─ CodeChallenge_27.py
└─ CodeChallenge_26.py
└─ CodeChallenge_25.py
└─ CodeChallenge_24.py
└─ CodeChallenge_23.py
└─ CodeChallenge_22.py
└─ CodeChallenge_21.py
└─ using_zip.py
└─ URLSuffixHash.py
└─ practice_1.py
└─ myCode_2.py
└─ myCode_1.py
└─ cylinder_max.py
└─ Code_Challenge_medium_1.py
└─ CodeChallenge_9.py
└─ CodeChallenge_8.py
└─ CodeChallenge_7.py
└─ CodeChallenge_6.py
└─ CodeChallenge_5.py
└─ class_Pet.py
└─ decorator_chaining.py
└─ decorator_add.py
└─ Context_Managers.py
└─ console_print_with_function.py
└─ concatenating_arrays.py
└─ complex_class.py
└─ flattening_arrays.py
└─ hand_utils.py
└─ Graph_Database_Simulation.py
└─ global_variables.py
└─ Gin_Rummy_Card_Game.py
└─ generators.py
└─ func_calls_func.py
└─ flexible_decorators.py
└─ ExpensiveQuerySimulation.py
└─ enhanced_decorators.py
└─ dynamic_polymorphism.py
└─ high-order_functions.py
└─ Dog.py
└─ discounted_list_prices.py
└─ digital_to_hexidecimal.py
└─ digital_to_binary.py
└─ dict_merge.py
└─ determinate_test.py
└─ determinate3x3.py
└─ determinate2x2.py
└─ items.csv
└─ default_func_parameters.py
└─ decorator_validation.py
└─ decorator_exercise.py
└─ class_user_def_context_mgr.py
└─ class_inheritance.py
└─ classes_fundamentals.py
└─ item_class.py
└─ broadcasting_arrays.py
└─ assigning_values_to_indexed_elements.py
└─ array_statistical_operations.py
└─ array_scalar_operations.py
└─ array_manipulation.py
└─ array_indexing.py
└─ array_creation_functions.py
└─ array_copying.py
└─ array_comparisons.py
└─ array_boolean_indexing.py
└─ args_and_optionals..py
└─ args_and_kargs_in_func.py
└─ animal_talk.py
└─ activations.py
└─ 5-Card_Stud_Poker_Game (Final).py
└─ kargs_in_dynamic_funcs.py
└─ 3D_array_indexing.py
└─ 2D_int_array_indexing.py
└─ 2D_array_slicing.py
└─ 2D_array_indexing.py
└─ 2D_array_boolean_indexing.py
└─ numpy_2D_matrices.py
└─ orbital.py
└─ operator_overloading.py
└─ numpy_linspace_function.py
└─ numpy_array.py
└─ non_local_variables.py
└─ packing-unpacking.txt.py
└─ neural_network_example.py
└─ nested_functions.py
└─ Neo4j_graph_database_simulation.py
└─ my_module.py
└─ MyProductClass.py
└─ MyProductClass Enhanced.py
└─ mult_with_queue.py
└─ mult_with_pool.py
└─ mult_inheritance.py
└─ packing.py
└─ mult_array.py
└─ multi_layer_network.py
└─ multi-layer-perceptron.py
└─ mult.py
└─ mro_resolution_example.py
└─ modify_global_var_in_func.py
└─ modify_data_structure.py
└─ merge_arrays.py
└─ mat_trans.py
└─ mat_flat.py
└─ matrix_transpose.py
└─ matrix_mult.py
└─ matrix_class.py
└─ playing_cards.py
└─ LRUCache.py
└─ Lists_In_Functions.py
└─ linked_list.py
└─ linear_algebra.py
└─ layers.py
└─ lambda_with_lists.py
└─ product_revenues_exercise.py
└─ python_closures.py
└─ product_class.py
└─ pop_list.py
└─ poker_class_version.py
└─ poker.py
└─ lambda_funcs_with_built-ins.py
└─ Lambda_funcs.py
└─ keyword_arguments_in_func_solution.py
└─ keyword_arguments_in_func_exercise.py
└─ keyword_arguments_in_funcs.py
└─ timing_decorator.py
└─ threshold_checker.py
└─ test_digital_to_hexidecimal.py
└─ test_.py
└─ two_numbers_sum_to_9.py
└─ test2.py
└─ test.py
└─ square_matrix_transpose.py
└─ Unit_Test.py
└─ sorting_2d_arrays.py
└─ sorted_arrays.py
└─ slicing_1D_arrays.py
└─ using_eval_function.py
└─ unpacking.py
└─ sleeping_decorator.py
└─ single_node_neural_network.py
└─ runtime_polymorphism.py
└─ RMD_Calculator.py
└─ using_kwargs.py
└─ reshape_array.py
└─ random_arrays_2.py
└─ random_arrays.py
└─ zipping_lists.py
└─ python_decorators.py
# Project Files
- d:\Python_Code\test_.py
- d:\Python_Code\my_module.py
## d:\Python_Code\test_.py
```
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
```
## d:\Python_Code\my_module.py
```
# my_module.py
def greet(name):
return f"Hello, {name}!"
# test_my_module.py
def test_greet(mocker):
mock_greet = mocker.patch('my_module.greet', return_value="Hi!")
assert mock_greet("Alice") == "Hi!"
mock_greet.assert_called_once_with("Alice")
```

11
Unit_Test.py Normal file
View File

@ -0,0 +1,11 @@
import unittest
def add(a, b):
return a + b
class TestMath(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 4), 6)
if __name__ == '__main__':
unittest.main()

9
determinate_test.py Normal file
View File

@ -0,0 +1,9 @@
import determinate3x3
Matrix = [
[4, 3, 6],
[2, 9, 11],
[1, 4, 8]
]
print(determinate3x3.det3x3(Matrix))

9
my_module.py Normal file
View File

@ -0,0 +1,9 @@
# my_module.py
def greet(name):
return f"Hello, {name}!"
# test_my_module.py
def test_greet(mocker):
mock_greet = mocker.patch('my_module.greet', return_value="Hi!")
assert mock_greet("Alice") == "Hi!"
mock_greet.assert_called_once_with("Alice")

7
test_.py Normal file
View File

@ -0,0 +1,7 @@
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5

10
tests/area.py Normal file
View File

@ -0,0 +1,10 @@
PI = 3.14159
def area_of_circle(radius: float) -> float:
"""
Function to calculate area of a circle
:param radius: Radius of the circle
:return: Area of the circle
"""
return PI * radius * radius

18
tests/file_handler.py Normal file
View File

@ -0,0 +1,18 @@
import os
def create_file(filename: str) -> None:
"""
Function to create a file
:param filename: Name of the file to create
:return: None
"""
with open(f"{filename}", "w") as f:
f.write("hello")
def remove_file(filename: str) -> None:
"""
Function to remove a file
:param filename: Name of the file to remove
:return: None
"""
os.remove(filename)

8
tests/test_area.py Normal file
View File

@ -0,0 +1,8 @@
from tests.area import area_of_circle
def test_area_of_circle():
"""
Function to test area of circle
"""
assert area_of_circle(5) == 78.53975

View File

@ -0,0 +1,36 @@
import os
from tests.file_handler import create_file, remove_file
def test_create_file():
"""
Function to test make file
"""
create_file(filename="delete_me.txt")
assert os.path.isfile("delete_me.txt")
def test_remove_file_with_mock(mocker):
"""
Test the removal of a file using mocking to avoid actual file system operations.
"""
filename = "delete_me.txt"
# Mock os.remove to test file deletion without deleting anything
mock_remove = mocker.patch("os.remove")
# Mock os.path.isfile to control its return value
mocker.patch("os.path.isfile", return_value=False)
# Mock open for the create_file function
mocker.patch("builtins.open", mocker.mock_open())
# Simulate file creation and removal
create_file(filename)
remove_file(filename)
# Assert that os.remove was called correctly
mock_remove.assert_called_once_with(filename)
# Assert that os.path.isfile returns False, simulating that the file does not exist
assert not os.path.isfile(filename)