Skip to content

[zipfeljs] Adding tests #7

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 9 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
310 changes: 310 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,319 @@ Please follow the instructions in [python_testing_exercise.md](https://github.co

## Test logs (for submission)


### pytest log
#### test_initialize_domain
```powershell
==================================== test session starts =====================================
platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425
collected 5 items

tests\integration\test_diffusion2d.py .. [ 40%]
tests\unit\test_diffusion2d_functions.py F.. [100%]

========================================== FAILURES ==========================================
___________________________________ test_initialize_domain ___________________________________

def test_initialize_domain():
"""
Check function SolveDiffusion2D.initialize_domain
"""
# Fixture
w=60.
h=30.
dx=0.2
dy=0.15

# Expected result
nx= 300
ny= 200

# Actual result
solver = SolveDiffusion2D()
solver.initialize_domain(w, h, dx, dy)
actual_nx = solver.nx
actual_ny = solver.ny

# Test
> assert actual_nx == nx
E assert 150 == 300

tests\unit\test_diffusion2d_functions.py:30: AssertionError
================================== short test summary info ===================================
FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_domain - assert 150 == 300
================================ 1 failed, 4 passed in 0.54s =================================
```
#### test_initialize_physical_parameters
```powershell
=============================================== test session starts ================================================
platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425
collected 5 items

tests\integration\test_diffusion2d.py .. [ 40%]
tests\unit\test_diffusion2d_functions.py .F. [100%]

===================================================== FAILURES =====================================================
_______________________________________ test_initialize_physical_parameters ________________________________________

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Fixture
d=3.5
T_cold=200.
T_hot=600.

# Expected result
dt= 0.00113


# Actual result
solver = SolveDiffusion2D()
solver.dx = 0.1
solver.dy = 0.2
solver.initialize_physical_parameters(d, T_cold, T_hot)
actual_dt = pytest.approx(solver.dt, abs=0.0001)

# Test
> assert actual_dt == dt
E assert 0.004571428571428573 ± 1.0e-04 == 0.00113
E
E comparison failed
E Obtained: 0.00113
E Expected: 0.004571428571428573 ± 1.0e-04

tests\unit\test_diffusion2d_functions.py:55: AssertionError
----------------------------------------------- Captured stdout call -----------------------------------------------
dt = 0.004571428571428573
============================================= short test summary info ==============================================
FAILED tests/unit/test_diffusion2d_functions.py::test_initialize_physical_parameters - assert 0.004571428571428573 ± 1.0e-04 == 0.00113
=========================================== 1 failed, 4 passed in 0.50s ============================================
```
#### test_set_initial_condition
```powershell
==================================================================================================== test session starts ====================================================================================================
platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425
collected 5 items

tests\integration\test_diffusion2d.py .. [ 40%]
tests\unit\test_diffusion2d_functions.py ..F [100%]

========================================================================================================= FAILURES ==========================================================================================================
________________________________________________________________________________________________ test_set_initial_condition _________________________________________________________________________________________________

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
# Fixture

# Expected result
u= np.array([[300., 300., 300., 300.],
[300., 300., 300., 300.],
[300., 300., 300., 300.],
[300., 300., 300., 300.],
[300., 300., 300., 300.]])


# Actual result
solver = SolveDiffusion2D()
solver.nx=5
solver.ny=4
solver.dx=0.5
solver.dy=0.5
solver.T_cold=300.
solver.T_hot=900.
actual_u = solver.set_initial_condition()


# Test
assert actual_u.shape == u.shape
> assert np.allclose(actual_u, u, atol=50)
E assert False
E + where False = <function allclose at 0x00000254BF0838B0>(array([[900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.]]), array([[300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.]]), atol=50)
E + where <function allclose at 0x00000254BF0838B0> = np.allclose

tests\unit\test_diffusion2d_functions.py:87: AssertionError
================================================================================================== short test summary info ==================================================================================================
FAILED tests/unit/test_diffusion2d_functions.py::test_set_initial_condition - assert False
================================================================================================ 1 failed, 4 passed in 0.67s ================================================================================================
```

### unittest log
#### test_initialize_domain
```powershell
Fdt = 0.0011428571428571432
..
======================================================================
FAIL: test_initialize_domain (test_diffusion2d_functions.TestDiffusion2D.test_initialize_domain)
Check function SolveDiffusion2D.initialize_domain
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 43, in test_initialize_domain
self.assertEqual(actual_ny, ny)
AssertionError: 4 != 200

----------------------------------------------------------------------
Ran 3 tests in 0.002s

FAILED (failures=1)
```
#### test_initialize_physical_parameters
```powershell
.dt = 0.0019047619047619052
F.
======================================================================
FAIL: test_initialize_physical_parameters (test_diffusion2d_functions.TestDiffusion2D.test_initialize_physical_parameters)
Checks function SolveDiffusion2D.initialize_domain
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 67, in test_initialize_physical_parameters
self.assertAlmostEqual(actual_dt, dt , 4)
AssertionError: 0.0019047619047619052 != 0.00113 within 4 places (0.0007747619047619053 difference)

----------------------------------------------------------------------
Ran 3 tests in 0.002s

FAILED (failures=1)
```
#### test_set_initial_condition
```powershell
.dt = 0.0011428571428571432
.F
======================================================================
FAIL: test_set_initial_condition (test_diffusion2d_functions.TestDiffusion2D.test_set_initial_condition)
Checks function SolveDiffusion2D.get_initial_function
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\unit\test_diffusion2d_functions.py", line 96, in test_set_initial_condition
self.assertTrue(np.allclose(actual_u, u))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 3 tests in 0.002s

FAILED (failures=1)
```
### Integration test log
#### test_initialize_physical_parameters
```powershell
==================================================================================================== test session starts ====================================================================================================
platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\integration
collected 2 items

test_diffusion2d.py F. [100%]

========================================================================================================= FAILURES ==========================================================================================================
____________________________________________________________________________________________ test_initialize_physical_parameters ____________________________________________________________________________________________

def test_initialize_physical_parameters():
"""
Checks function SolveDiffusion2D.initialize_domain
"""
solver = SolveDiffusion2D()
# Fixture
w=30.
h=20.
dx=0.2
dy=0.5

d=3.5
T_cold=200.
T_hot=600.

# Expected result
dt= 0.0049

# Actual result
solver.initialize_domain(w, h, dx, dy)
solver.initialize_physical_parameters(d, T_cold, T_hot)
actual_dt = pytest.approx(solver.dt, 0.01)

# Test
> assert actual_dt == dt
E assert 0.010204081632653062 ± 1.0e-04 == 0.0049
E
E comparison failed
E Obtained: 0.0049
E Expected: 0.010204081632653062 ± 1.0e-04

test_diffusion2d.py:39: AssertionError
--------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------
dt = 0.010204081632653062
================================================================================================== short test summary info ==================================================================================================
FAILED test_diffusion2d.py::test_initialize_physical_parameters - assert 0.010204081632653062 ± 1.0e-04 == 0.0049
================================================================================================ 1 failed, 1 passed in 0.55s ================================================================================================
```
#### test_set_initial_condition
```powershell
==================================================================================================== test session starts ====================================================================================================
platform win32 -- Python 3.12.8, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\tests\integration
collected 2 items

test_diffusion2d.py .F [100%]

========================================================================================================= FAILURES ==========================================================================================================
________________________________________________________________________________________________ test_set_initial_condition _________________________________________________________________________________________________

def test_set_initial_condition():
"""
Checks function SolveDiffusion2D.get_initial_function
"""
solver = SolveDiffusion2D()
# Fixture
w=15.
h=12.
dx=3.
dy=3.

d=0.001
T_cold=300.
T_hot=900.

# Expected result
u= np.array([[300., 300., 300., 300.],
[300., 300., 300., 300.],
[300., 300., 900., 300.],
[300., 300., 300., 300.],
[300., 300., 300., 300.]])

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

# Test
assert actual_u.shape == u.shape

> assert np.allclose(actual_u, u, 0.0001)
E assert False
E + where False = <function allclose at 0x000001D08F189E70>(array([[900., 300., 300., 300.],\n [900., 900., 900., 900.],\n [900., 900., 900., 900.],\n [900., 300., 300., 900.],\n [300., 300., 300., 300.]]), array([[300., 300., 300., 300.],\n [300., 300., 300., 300.],\n [300., 300., 900., 300.],\n [300., 300., 300., 300.],\n [300., 300., 300., 300.]]), 0.0001)
E + where <function allclose at 0x000001D08F189E70> = np.allclose

test_diffusion2d.py:72: AssertionError
--------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------
dt = 2249.9999999999995
================================================================================================== short test summary info ==================================================================================================
FAILED test_diffusion2d.py::test_set_initial_condition - assert False
================================================================================================ 1 failed, 1 passed in 0.50s ================================================================================================
```

## tox
```cmd
python -m tox
testing: recreate env because requirements removed: pytest>=8
testing: remove tox env folder C:\Users\johzi\OneDrive\Desktop\Master\Simulation\Exercise\Ex6\testing-python-exercise-wt2425\.tox\testing
testing: OK (1.25 seconds)
congratulations :) (1.36 seconds)
```

## Citing

Expand Down
Binary file added coverage-report.pdf.pdf
Binary file not shown.
14 changes: 11 additions & 3 deletions diffusion2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +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)
assert isinstance(h, float)
assert isinstance(dx, float)
assert isinstance(dy, 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)
assert isinstance(T_cold, float) #This assertion failed (Step2)
assert isinstance(T_hot, float) #This assertion failed (Step2)
self.D = d
self.T_cold = T_cold
self.T_cold = T_cold
self.T_hot = T_hot

# Computing a stable time step
Expand Down Expand Up @@ -109,7 +116,8 @@ def main():
DiffusionSolver.initialize_physical_parameters()

u0 = DiffusionSolver.set_initial_condition()


print(u0)
# Number of timesteps
nsteps = 101

Expand Down
2 changes: 2 additions & 0 deletions requirement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy>=1.15.0
matplotlib>=3.0.0
Loading