Skip to content
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

[sashanje] Adding tests #10

Open
wants to merge 1 commit 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
209 changes: 209 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,217 @@ Please follow the instructions in [python_testing_exercise.md](https://github.co

### pytest log

```
========================================================== test session starts ===========================================================
platform darwin -- Python 3.11.7, pytest-7.4.0, pluggy-1.0.0
rootdir: /Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425
plugins: anyio-4.2.0
collected 3 items

tests/unit/test_diffusion2d_functions.py FFF [100%]

================================================================ FAILURES ================================================================
_________________________________________________________ test_initialize_domain _________________________________________________________

def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()

w = 10.
h = 5.
dx = 0.1
dy = 0.2

expected_nx = 100
expected_ny = 25

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)

> assert expected_nx == solver.nx
E assert 100 == 50
E + where 50 = <diffusion2d.SolveDiffusion2D object at 0x11c0eb010>.nx

tests/unit/test_diffusion2d_functions.py:25: AssertionError
__________________________________________________ test_initialize_physical_parameters ___________________________________________________

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()

d = 5.
T_cold = 500.
T_hot = 1000.

solver.dx = 0.1
solver.dy = 0.2

expected_dt = 0.0008000000000000001

solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)
> assert expected_dt == solver.dt
E assert 0.0008000000000000001 == 0.0016000000000000003
E + where 0.0016000000000000003 = <diffusion2d.SolveDiffusion2D object at 0x11c1b2d10>.dt

tests/unit/test_diffusion2d_functions.py:44: AssertionError
---------------------------------------------------------- Captured stdout call ----------------------------------------------------------
dt = 0.0016000000000000003
_______________________________________________________ test_set_initial_condition _______________________________________________________

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()

solver.nx = 3
solver.ny = 5
solver.dx = 4.
solver.dy = 2.
solver.T_cold = 200.
solver.T_hot = 600.

expected_u = np.array([
[200., 200., 200., 200., 200.],
[200., 200., 600., 600., 200.],
[200., 200., 200., 200., 200.]])

actual_u = solver.set_initial_condition()

> assert((expected_u == actual_u).all())
E assert False
E + where False = <built-in method all of numpy.ndarray object at 0x11c18f990>()
E + where <built-in method all of numpy.ndarray object at 0x11c18f990> = array([[200.,... 200., 200.]]) == array([[200.,... 200., 200.]])
E Use -v to get more diff.all

tests/unit/test_diffusion2d_functions.py:67: AssertionError
======================================================== short test summary info =========================================================
FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_domain - assert 100 == 50
FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_physical_parameters - assert 0.0008000000000000001 == 0.0016000000000000003
FAILED tests/unit/test_diffusion2d_functions.py::test_set_initial_condition - assert False
=========================================================== 3 failed in 0.33s ============================================================
```

### unittest log

```
Fdt = 0.0016000000000000003
FF
======================================================================
FAIL: test_initialize_domain (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_initialize_domain)
Check function SolveDiffusion2D.initialize_domain
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425/tests/unit/test_diffusion2d_functions.py", line 83, in test_initialize_domain
self.assertEqual(expected_nx,self.solver.nx)
AssertionError: 100 != 50

======================================================================
FAIL: test_initialize_physical_parameters (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_initialize_physical_parameters)
Checks function SolveDiffusion2D.initialize_domain
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425/tests/unit/test_diffusion2d_functions.py", line 100, in test_initialize_physical_parameters
self.assertAlmostEqual(expected_dt,self.solver.dt)
AssertionError: 0.0008000000000000001 != 0.0016000000000000003 within 7 places (0.0008000000000000001 difference)

======================================================================
FAIL: test_set_initial_condition (tests.unit.test_diffusion2d_functions.TestDiffusion2D.test_set_initial_condition)
Checks function SolveDiffusion2D.get_initial_function
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425/tests/unit/test_diffusion2d_functions.py", line 120, in test_set_initial_condition
self.assertTrue(np.allclose(expected_u,actual_u))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 3 tests in 0.001s

FAILED (failures=3)
```

### integration test log

```
====================================================== test session starts =======================================================
platform darwin -- Python 3.11.7, pytest-7.4.0, pluggy-1.0.0
rootdir: /Users/sashan/Documents/MSc/Semester 3/Simulation Software Engineering/Exercises/python_testing/testing-python-exercise-wt2425
plugins: anyio-4.2.0
collected 2 items

tests/integration/test_diffusion2d.py FF [100%]

============================================================ FAILURES ============================================================
______________________________________________ test_initialize_physical_parameters _______________________________________________

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()

w = 10.
h = 5.
dx = 0.1
dy = 0.2
d = 5.
T_cold = 500.
T_hot = 1000.

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)

expected_dt = 0.0008000000000000001

> assert expected_dt == solver.dt
E assert 0.0008000000000000001 == 0.0016000000000000003
E + where 0.0016000000000000003 = <diffusion2d.SolveDiffusion2D object at 0x11d3ac690>.dt

tests/integration/test_diffusion2d.py:28: AssertionError
------------------------------------------------------ Captured stdout call ------------------------------------------------------
dt = 0.0016000000000000003
___________________________________________________ test_set_initial_condition ___________________________________________________

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()

w = 12.
h = 10.
dx = 4.
dy = 2.
d = 4.
T_cold = 200.
T_hot = 600.

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)
actual_u = solver.set_initial_condition()

expected_u = np.array([
[200., 200., 200., 200., 200.],
[200., 200., 600., 600., 200.],
[200., 200., 200., 200., 200.]])

> assert np.allclose(actual_u, expected_u)
E assert False
E + where False = <function allclose at 0x10273ed70>(array([[200., 200., 200., 200., 200.],\n [200., 200., 200., 200., 200.],\n [200., 200., 200., 200., 200.]]), array([[200., 200., 200., 200., 200.],\n [200., 200., 600., 600., 200.],\n [200., 200., 200., 200., 200.]]))
E + where <function allclose at 0x10273ed70> = np.allclose

tests/integration/test_diffusion2d.py:54: AssertionError
------------------------------------------------------ Captured stdout call ------------------------------------------------------
dt = 0.8
==================================================== short test summary info =====================================================
FAILED tests/integration/test_diffusion2d.py::test_initialize_physical_parameters - assert 0.0008000000000000001 == 0.0016000000000000003
FAILED tests/integration/test_diffusion2d.py::test_set_initial_condition - assert False
======================================================= 2 failed in 0.27s ========================================================
```

## Citing

The code used in this exercise is based on [Chapter 7 of the book "Learning Scientific Programming with Python"](https://scipython.com/book/chapter-7-matplotlib/examples/the-two-dimensional-diffusion-equation/).
Binary file added coverage-report.pdf
Binary file not shown.
11 changes: 10 additions & 1 deletion diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ def __init__(self):
self.dt = None

def initialize_domain(self, w=10., h=10., dx=0.1, dy=0.1):
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
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 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
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy
matplotlib
Empty file added tests/__init__.py
Empty file.
Empty file added tests/integration/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions tests/integration/test_diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for functionality checks in class SolveDiffusion2D
"""

import numpy as np
from diffusion2d import SolveDiffusion2D


Expand All @@ -10,10 +11,44 @@ def test_initialize_physical_parameters():
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()

w = 10.
h = 5.
dx = 0.1
dy = 0.2
d = 5.
T_cold = 500.
T_hot = 1000.

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)

expected_dt = 0.0008000000000000001

assert expected_dt == solver.dt


def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()

w = 12.
h = 10.
dx = 4.
dy = 2.
d = 4.
T_cold = 200.
T_hot = 600.

solver.initialize_domain(w=w, h=h, dx=dx, dy=dy)
solver.initialize_physical_parameters(d=d, T_cold=T_cold, T_hot=T_hot)
actual_u = solver.set_initial_condition()

expected_u = np.array([
[200., 200., 200., 200., 200.],
[200., 200., 600., 600., 200.],
[200., 200., 200., 200., 200.]])

assert np.allclose(actual_u, expected_u)
Empty file added tests/unit/__init__.py
Empty file.
Loading