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

Readability changes to 05Mocks #221

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

mxochicale
Copy link
Member

Addressing #220

@alessandrofelder
Copy link
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
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
Member

dpshelio commented Nov 4, 2022

Added nbreview to this repository to allow reviews of notebboks.

ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
ch03tests/05Mocks.ipynb Show resolved Hide resolved
@@ -4,54 +4,75 @@
"cell_type": "markdown",
Copy link
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
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
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

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
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 :)

"## 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
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

]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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
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 :)

]
},
{
"cell_type": "markdown",
"metadata": {},
"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
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

@@ -4,54 +4,75 @@
"cell_type": "markdown",
Copy link
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