Skip to content

Commit 0ea9986

Browse files
Match exercise on mock testing to current state of analyse_data function
1 parent 83c4e4e commit 0ea9986

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

episodes/33-code-decoupling-abstractions.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,45 +478,53 @@ Now whenever you call `mock_version.method_to_mock()` the return value will be `
478478
::::::::::::::::::::::::::::::::::::::: challenge
479479

480480
## Exercise: Test Using a Mock Implementation
481+
Add a new test file called `test_compute_data.py` in the tests folder and add a test to verify
482+
whether we can successfully run `analyse_data()` when passing it a data source.
481483

482-
Complete this test for `analyse_data()`, using a mock object in place of the
483-
`data_source`:
484+
Complete this test for `analyse_data()`, using a mock object in place of the `data_source`:
484485

485486
```python
486487
from unittest.mock import Mock
487488

488-
def test_compute_data_mock_source():
489+
def test_analyse_data_mock_source():
489490
from inflammation.compute_data import analyse_data
490491
data_source = Mock()
491492

492493
# TODO: configure data_source mock
493494

494-
result = analyse_data(data_source)
495+
analyse_data(data_source)
495496

496-
# TODO: add assert on the contents of result
497497
```
498498

499499
Create a mock that returns some fixed data and to use as the `data_source` in order to test
500500
the `analyse_data` method.
501-
Use this mock in a test.
501+
502+
Use this mock in the test.
502503

503504
Do not forget to import `Mock` from the `unittest.mock` package.
504505

506+
Note that the `analyse_data()` function visualizes the data with `views.visualize(graph_data)`.
507+
You do not have to assert that this is done correctly. For now, it is fine to just check that
508+
the call to `analyse_data()` can proceed successfully.
509+
510+
In the next episode we will adapt the `analyse_data()` function
511+
so that we can write a test that asserts whether standard deviation calculations are correct.
512+
505513
::::::::::::::: solution
506514

507515
## Solution
508516

509517
```python
510518
from unittest.mock import Mock
511519

512-
def test_compute_data_mock_source():
520+
def test_analyse_data_mock_source():
513521
from inflammation.compute_data import analyse_data
514522
data_source = Mock()
515-
data_source.load_inflammation_data.return_value = [[[0, 2, 0]],
516-
[[0, 1, 0]]]
523+
mock_data = [[[0, 2, 0]],
524+
[[0, 1, 0]]]
525+
data_source.load_inflammation_data.return_value = mock_data
517526

518-
result = analyse_data(data_source)
519-
npt.assert_array_almost_equal(result, [0, math.sqrt(0.25) ,0])
527+
analyse_data(data_source)
520528
```
521529

522530
:::::::::::::::::::::::::
@@ -526,8 +534,8 @@ def test_compute_data_mock_source():
526534
## Safe Code Structure Changes
527535

528536
With the changes to the code structure we have done using code decoupling and abstractions we have
529-
already refactored our code to a certain extent but we have not tested that the changes work as
530-
intended.
537+
already refactored our code to a certain extent,
538+
but we have not fully tested that the changes work as intended.
531539
We will now look into how to properly refactor code to guarantee that the code still works
532540
as before any modifications.
533541

0 commit comments

Comments
 (0)