diff --git a/__pycache__/diffusion2d.cpython-313.pyc b/__pycache__/diffusion2d.cpython-313.pyc new file mode 100644 index 0000000..ff179c6 Binary files /dev/null and b/__pycache__/diffusion2d.cpython-313.pyc differ diff --git a/coverage-report.pdf b/coverage-report.pdf new file mode 100644 index 0000000..cec8fe2 Binary files /dev/null and b/coverage-report.pdf differ diff --git a/coverage.xml b/coverage.xml new file mode 100644 index 0000000..48c3f94 --- /dev/null +++ b/coverage.xml @@ -0,0 +1,179 @@ + + + + + + /home/silas/git/testing-python-exercise + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/diffusion2d.py b/diffusion2d.py index 51a07f2..6e01fb0 100644 --- a/diffusion2d.py +++ b/diffusion2d.py @@ -38,6 +38,12 @@ def __init__(self): self.dt = None def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): + # Assertion functions for type checking + assert isinstance(w, float), "w must be a float" + assert isinstance(h, float), "h must be a float" + assert isinstance(dx, float), "dx must be a float" + assert isinstance(dy, float), "dy must be a float" + self.w = w self.h = h self.dx = dx @@ -45,7 +51,12 @@ def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1): self.nx = int(w / dx) self.ny = int(h / dy) - def initialize_physical_parameters(self, d=4., T_cold=300, T_hot=700): + def initialize_physical_parameters(self, d=4., T_cold=300., T_hot=700.): + # Add assertions + assert isinstance(d, float), "d must be a float" + assert isinstance(T_cold, float), "T_cold must be a float" + assert isinstance(T_hot, float), "T_hot must be a float" + self.D = d self.T_cold = T_cold self.T_hot = T_hot diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..021aae7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +numpy +matplotlib +pytest +coverage diff --git a/tests/integration/__pycache__/test_diffusion2d.cpython-313-pytest-9.0.2.pyc b/tests/integration/__pycache__/test_diffusion2d.cpython-313-pytest-9.0.2.pyc new file mode 100644 index 0000000..ef902ef Binary files /dev/null and b/tests/integration/__pycache__/test_diffusion2d.cpython-313-pytest-9.0.2.pyc differ diff --git a/tests/integration/test_diffusion2d.py b/tests/integration/test_diffusion2d.py index fd026b4..fa80346 100644 --- a/tests/integration/test_diffusion2d.py +++ b/tests/integration/test_diffusion2d.py @@ -1,7 +1,8 @@ """ Tests for functionality checks in class SolveDiffusion2D """ - +import unittest +import numpy as np from diffusion2d import SolveDiffusion2D @@ -10,10 +11,34 @@ def test_initialize_physical_parameters(): Checks function SolveDiffusion2D.initialize_domain """ solver = SolveDiffusion2D() + w = 10. + h = 10. + dx = 1. + dy = 1. + d = 4. + t_cold = 300. + t_hot = 700. + solver.initialize_domain(w, h, dx,dy) + solver.initialize_physical_parameters(d, t_cold, t_hot) + assert solver.dt == 0.0625 def test_set_initial_condition(): """ Checks function SolveDiffusion2D.get_initial_function """ solver = SolveDiffusion2D() + w = 4. + h = 4. + dx = 1. + dy = 1. + d = 4. + t_cold = 300. + t_hot = 700. + solver.initialize_domain(w, h, dx, dy) + solver.initialize_physical_parameters(d, t_cold, t_hot) + + u = solver.set_initial_condition() + + assert u.shape == (4, 4) + assert u[0, 0] == 300. diff --git a/tests/unit/__pycache__/test_diffusion2d_functions.cpython-313-pytest-9.0.2.pyc b/tests/unit/__pycache__/test_diffusion2d_functions.cpython-313-pytest-9.0.2.pyc new file mode 100644 index 0000000..fd20730 Binary files /dev/null and b/tests/unit/__pycache__/test_diffusion2d_functions.cpython-313-pytest-9.0.2.pyc differ diff --git a/tests/unit/test_diffusion2d_functions.py b/tests/unit/test_diffusion2d_functions.py index c4277ff..7bf2c1f 100644 --- a/tests/unit/test_diffusion2d_functions.py +++ b/tests/unit/test_diffusion2d_functions.py @@ -2,6 +2,7 @@ Tests for functions in class SolveDiffusion2D """ +import unittest from diffusion2d import SolveDiffusion2D @@ -9,18 +10,49 @@ def test_initialize_domain(): """ Check function SolveDiffusion2D.initialize_domain """ + w = 20. + h = 10. + dx = 2. + dy = 2. + # init solver solver = SolveDiffusion2D() - + + # run function and assert + solver.initialize_domain(w, h, dx, dy) + assert solver.nx == 10 + assert solver.ny == 5 def test_initialize_physical_parameters(): """ Checks function SolveDiffusion2D.initialize_domain """ + # init solver solver = SolveDiffusion2D() + solver.dx = 2. + solver.dy = 2. + + d = 4. + t_cold = 300. + t_hot = 700. + + solver.initialize_physical_parameters(d, t_cold, t_hot) + assert solver.dt == 0.25 def test_set_initial_condition(): """ Checks function SolveDiffusion2D.get_initial_function """ + # init solver solver = SolveDiffusion2D() + solver.nx = 3 + solver.ny = 3 + solver.dx = 1. + solver.dy = 1. + solver.T_cold = 200. + solver.T_hot = 500. + + # call function and assert + u = solver.set_initial_condition() + assert u.shape == (3, 3) + assert u[0, 0] == 200. diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..c7b541f --- /dev/null +++ b/tox.ini @@ -0,0 +1,10 @@ +[tox] +envlist = py313 +minversion = 4.0 +skipsdist = True + +[testenv] +deps = -rrequirements.txt +commands = + coverage run -m pytest + coverage xml