Skip to content

Readability changes to 05Mocks #221

Draft
mxochicale wants to merge 2 commits intomainfrom
220-improving-05Mocks
Draft

Readability changes to 05Mocks #221
mxochicale wants to merge 2 commits intomainfrom
220-improving-05Mocks

Conversation

@mxochicale
Copy link
Copy Markdown
Member

Addressing #220

@alessandrofelder
Copy link
Copy Markdown
Contributor

All looks great to me. I am unsure about the best way to merge .ipynb files as the diff contains metadata around the cell status (I used nbdime for the first time today, to look at the notebook diff). Any suggestions @dpshelio ?

Copy link
Copy Markdown
Contributor

@alessandrofelder alessandrofelder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@dpshelio
Copy link
Copy Markdown
Member

dpshelio commented Nov 4, 2022

Added nbreview to this repository to allow reviews of notebboks.

Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
Comment thread ch03tests/05Mocks.ipynb
@@ -4,54 +4,75 @@
"cell_type": "markdown",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason to make params an argument for the function? I see you've added an example with a different set of parameters below. But tin the example we are building a test and the parameters need to be the good ones, the ones we want to check the call is being done so when the function is called that way. In other words, the defaults to the map_at function. Showing two set of params in this example diverts the understanding of what's being tested.


Reply via ReviewNB

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dpshelio,
I think the example in the notebook is testing the use of the right parameters to do a right remote call: " Instead, we can use mocks to assert that our code does the right thing in terms of the messages it sends: the parameters of the function calls it makes to the remote resource". Just amended the cell to show this:

def test_build_default_params(_base, _params):
    with patch.object(requests,'get') as mock_get:
        default_map = map_at(51.0, 0.0)
        mock_get.assert_called_with(_base, params=_params)

base="https://static-maps.yandex.ru/1.x/?"
good_params={
    'z':12,
    'size':'400,400',
    'll':'0.0,51.0',
    'lang':'en_US',
    'l': 'map'
}        
test_build_default_params(base, good_params)

and

base="https://static-maps.yandex.ru/1.x/?"
bad_params={
    'z':15,
    'size':'400,400',    
    'll':'0.0,51.0',
    'lang':'en_US',
    'l': 'map'
}        
test_build_default_params(base, bad_params)

In any case, as I am new to this kind of testing. Examples require to be more intuitive and perhaps add more details for patching as there are few handy tests (e.g. https://kimsereylam.com/python/2021/03/19/how-to-use-patch-in-python-unittest.html). Happy to chat about this bits with @alessandrofelder and yourself next Tuesday :)

Thanks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a test called test_build_default_params should only test the default parameters. But I like the idea of showing the versatility of mocking. Could we have an additional bit of text after test_build_default_params that says something like "Mocking can also be used to test how we handle errors we receive from the remote resource" and then have a negative test that checks we handle a mocked dodgy internet connection as expected?

@mxochicale mxochicale marked this pull request as draft November 4, 2022 15:51
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mxochicale mxochicale changed the title [WIP] readability changes to 05Mocks Readability changes to 05Mocks Nov 4, 2022
Copy link
Copy Markdown
Contributor

@alessandrofelder alessandrofelder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tiny additional comments :)

Comment thread ch03tests/05Mocks.ipynb
"## 1. The Mock Object\n",
"**1.1 `unittest.mock` offers a base class for mocking objects called Mock.**"
"## The Mock Object\n",
"`unittest.mock` offers a base class for mocking objects called Mock. The following is an example how to define and object, call it and return id.\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"`unittest.mock` offers a base class for mocking objects called Mock. The following is an example how to define and object, call it and return id.\n"
"`unittest.mock` offers a base class for mocking objects called Mock. The following is an example how to define an object, call it and return it.\n"

small typos

Comment thread ch03tests/05Mocks.ipynb
"### 3.1 Using mocks to model test resources\n",
"### Using mocks to model test resources\n",
"Often we want to write tests for code which interacts with remote resources. (E.g. databases, the internet, or data files.)\n",
"We don't want to have our tests *actually* interact with the remote resource, as this would mean our tests failed\n",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"We don't want to have our tests *actually* interact with the remote resource, as this would mean our tests failed\n",
"We don't want to have our tests *actually* interact with the remote resource, as this would mean our tests could fail unpredictably from time to time, through no fault of our own, \n",

mini-improvement, while we're here :)

Comment thread ch03tests/05Mocks.ipynb
"source": [
"That was quiet, so it passed. \n",
"When writing tests, one usually modifies one of the expectations, to something 'wrong', just to check it's not\n",
"When writing tests, we usually modifies one of the expectations, to something 'wrong', just to check it's not\n",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"When writing tests, we usually modifies one of the expectations, to something 'wrong', just to check it's not\n",
"When writing tests, we usually modify one of the expectations, to something 'wrong', just to check it's not\n",

small typo

Comment thread ch03tests/05Mocks.ipynb
@@ -4,54 +4,75 @@
"cell_type": "markdown",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a test called test_build_default_params should only test the default parameters. But I like the idea of showing the versatility of mocking. Could we have an additional bit of text after test_build_default_params that says something like "Mocking can also be used to test how we handle errors we receive from the remote resource" and then have a negative test that checks we handle a mocked dodgy internet connection as expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants