Skip to content

[krampfkn] Adding tests #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
505 changes: 505 additions & 0 deletions README.md

Large diffs are not rendered by default.

Binary file added coverage-report.pdf
Binary file not shown.
10 changes: 9 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ def __init__(self):
self.dt = None

def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1):
# You could argue individual assertions are better, since it allows to understand which values have the wrong type.
# However, for this exercise I keep it like this for initialize_domain and add individual assertions in initialize_physical_parameters
# In some cases individual assertions might not even allow for better understanding, if the assertion is deep in a library and the user might
# not understand how the values end up there. It probably is a case-by-case decision.
assert all(map(lambda v: type(v) == float, [w, h, dx, dy])), "All inputs must be floats"
self.w = w
self.h = h
self.dx = dx
self.dy = dy
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.):
assert type(d) == float, "d must be a float"
assert type(T_cold) == float, "T_cold must be a float"
assert type(T_hot) == float, "T_cold must be a float"
self.D = d
self.T_cold = T_cold
self.T_hot = T_hot
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# I had to add this file so pytest would actually find diffusion2d.py
[ pyproject.toml]

[tool.pytest.ini_options]
pythonpath = "."
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy>=1.15.0
matplotlib>=3.0.0
27 changes: 27 additions & 0 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for functionality checks in class SolveDiffusion2D
"""
import pytest

from diffusion2d import SolveDiffusion2D

Expand All @@ -10,10 +11,36 @@ def test_initialize_physical_parameters():
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
solver.initialize_domain(10., 20., 0.2, 0.1)
solver.initialize_physical_parameters(2.4, 0., 1.)
# dx2 = 0.04
# dy2 = 0.01
# dt = 0.0004 / (2*2.4*0.05) = 1/600 = 0.0016666
assert solver.dt == pytest.approx(0.001666, abs=1e-6)


def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
solver.initialize_domain(10., 10., 1., 1.)
solver.initialize_physical_parameters(2.7, 0., 1.)
u = solver.set_initial_condition()

expected_array= [
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
]

for x in range(u.shape[0]):
for y in range(u.shape[1]):
assert expected_array[x][y] == u[x, y], f"Values at {x}, {y} do not match"
112 changes: 112 additions & 0 deletions tests/unit/pytest_test_diffusion2d_functions.py.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""
Tests for functions in class SolveDiffusion2D
"""
import numpy as np
import pytest

from diffusion2d import SolveDiffusion2D


def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Arrange test values
w = 20.
h = 10.
dx = 2.
dy = 0.2

# Act (= Perform test action)
solver.initialize_domain(w, h, dx, dy)

# Assert
# "cheap" assertions
assert solver.w == w
assert solver.h == h
assert solver.dx == dx
assert solver.dy == dy
# Calculated values
assert solver.nx == 10
assert solver.ny == 50


def test_initialize_domain_testcase_2():
"""
Second testcase for function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Arrange test values
# "unschöne" values
w = 15.2
h = 17.1
dx = 2.7
dy = 0.29

# Act (= Perform test action)
solver.initialize_domain(w, h, dx, dy)

# Assert
# "cheap" assertions
assert solver.w == w
assert solver.h == h
assert solver.dx == dx
assert solver.dy == dy
# Calculated values
assert solver.nx == 5
assert solver.ny == 58


def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Arrange
d = 3.5
T_cold = 342.4
T_hot = 723.15
solver.dx = 0.1
solver.dy = 0.2

# Act
solver.initialize_physical_parameters(d, T_cold, T_hot)

# Assert
assert solver.D == d
assert solver.T_cold == T_cold
assert solver.T_hot == T_hot
assert solver.dt == pytest.approx(0.001142, abs=1e-6)


def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
# Arrange
solver.nx = 100
solver.ny = 50
solver.dx = 0.1
solver.dy = 0.2
solver.T_cold = 100.
solver.T_hot = 600.

# Act
u: np.ndarray = solver.set_initial_condition()

# Assert
# Domain has expected dimensions
assert u.shape[0] == solver.nx
assert u.shape[1] == solver.ny
# Border is cold, center is hot
assert u[0][0] == 100.
assert u[99][0] == 100.
assert u[0][49] == 100.
assert u[99][49] == 100.

assert u[49][24] == 600.
assert u[50][24] == 600.
assert u[49][25] == 600.
assert u[50][25] == 600.
120 changes: 105 additions & 15 deletions tests/unit/test_diffusion2d_functions.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,116 @@
"""
Tests for functions in class SolveDiffusion2D
"""
from unittest import TestCase

import numpy as np
import unittest

from diffusion2d import SolveDiffusion2D


def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
class TestDiffusion2D(unittest.TestCase):

def setUp(self):
self.solver = SolveDiffusion2D()

def test_initialize_domain(self):
"""
Check function SolveDiffusion2D.initialize_domain
"""
# Arrange test values
w = 20.
h = 10.
dx = 2.
dy = 0.2

# Act (= Perform test action)
self.solver.initialize_domain(w, h, dx, dy)

# Assert
# "cheap" assertions
self.assertEqual(self.solver.w, w)
self.assertEqual(self.solver.h, h)
self.assertEqual(self.solver.dx, dx)
self.assertEqual(self.solver.dy, dy)
# Calculated values
self.assertEqual(self.solver.nx, 10)
self.assertEqual(self.solver.ny, 50)

def test_initialize_domain_testcase_2(self):
"""
Second testcase for function SolveDiffusion2D.initialize_domain
"""
# Arrange test values
# "unschöne" values
w = 15.2
h = 17.1
dx = 2.7
dy = 0.29

# Act (= Perform test action)
self.solver.initialize_domain(w, h, dx, dy)

# Assert
# "cheap" assertions
self.assertEqual(self.solver.w, w)
self.assertEqual(self.solver.h, h)
self.assertEqual(self.solver.dx, dx)
self.assertEqual(self.solver.dy, dy)
# Calculated values
self.assertEqual(self.solver.nx, 5)
self.assertEqual(self.solver.ny, 58)

def test_initialize_physical_parameters(self):
"""
Checks function SolveDiffusion2D.initialize_domain
"""
# Arrange
d = 3.5
T_cold = 342.4
T_hot = 723.15
self.solver.dx = 0.1
self.solver.dy = 0.2

# Act
self.solver.initialize_physical_parameters(d, T_cold, T_hot)

# Assert
self.assertEqual(self.solver.D, d)
self.assertEqual(self.solver.T_cold, T_cold)
self.assertEqual(self.solver.T_hot, T_hot)
# unittest assumes rounding here, so the last decimal differs from pytest.
self.assertAlmostEqual(self.solver.dt, 0.001143, 6)

def test_set_initial_condition(self):
"""
Checks function SolveDiffusion2D.get_initial_function
"""
# Arrange
self.solver.nx = 100
self.solver.ny = 50
self.solver.dx = 0.1
self.solver.dy = 0.2
self.solver.T_cold = 100.
self.solver.T_hot = 600.

# Act
u: np.ndarray = self.solver.set_initial_condition()

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Assert
# Domain has expected dimensions
self.assertEqual(u.shape[0], self.solver.nx)
self.assertEqual(u.shape[1], self.solver.ny)
# Border is cold, center is hot
self.assertEqual(u[0][0], 100.)
self.assertEqual(u[99][0], 100.)
self.assertEqual(u[0][49], 100.)
self.assertEqual(u[99][49], 100.)

self.assertEqual(u[49][24], 600.)
self.assertEqual(u[50][24], 600.)
self.assertEqual(u[49][25], 600.)
self.assertEqual(u[50][25], 600.)

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
requires = ["tox>=4"]
env_list = ["pytest", "unittest"]

[env.pytest]
description = "Run pytest"
deps = ["pytest>=8", "-r requirements.txt"]
commands = [["pytest", "tests/integration/test_diffusion2d.py"]]

[env.unittest]
description = "Run unittest"
deps = ["-r requirements.txt"]
commands = [["python3", "-m", "unittest", "tests/unit/test_diffusion2d_functions.py"]]