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

fixes #3206 - Switch from Nosetests to pytest #3209

Merged
merged 11 commits into from Feb 19, 2020
Merged

Conversation

@karlcow
Copy link
Contributor

karlcow commented Feb 13, 2020

This PR fixes issue #3206

Proposed PR background

For now a draft pull request, before figuring out all the bolts and screws.

@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 13, 2020

the tests pass locally but not on circleci.
needs to investigate.

Installing collected packages: WEBCOMPAT
  Running setup.py develop for WEBCOMPAT
Successfully installed WEBCOMPAT
You are using pip version 19.0.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /home/circleci/repo
collected 154 items                                                            

tests/unit/test_api_urls.py .....F.....                                  [  7%]
tests/unit/test_config.py ..                                             [  8%]
tests/unit/test_console_logs.py .....                                    [ 11%]
tests/unit/test_form.py .................                                [ 22%]
tests/unit/test_helpers.py ............................                  [ 40%]
tests/unit/test_http_caching.py ...                                      [ 42%]
tests/unit/test_issues.py ......                                         [ 46%]
tests/unit/test_rendering.py ......                                      [ 50%]
tests/unit/test_tools_changelog.py ..                                    [ 51%]
tests/unit/test_topsites.py ...                                          [ 53%]

Exited with code exit status 1
tests/unit/test_uploads.py ...                                           [ 55%]
tests/unit/test_urls.py .................................                [ 77%]
tests/unit/test_webhook.py ...................................           [100%]

=================================== FAILURES ===================================
________________ TestAPIURLs.test_api_patch_issue_valid_request ________________

self = <unit.test_api_urls.TestAPIURLs testMethod=test_api_patch_issue_valid_request>
github_data = <MagicMock name='proxy_request' id='139856178361400'>

    @patch('webcompat.helpers.proxy_request')
    def test_api_patch_issue_valid_request(self, github_data):
        """Patching the issue - Valid request."""
        with webcompat.app.app_context():
            github_data.return_value = mock_api_response(
                {'status_code': 200, 'content': '[]'})
            data = {'state': 'open', 'milestone': 2}
            patch_data = json.dumps(data)
            rv = self.app.patch('/api/issues/1/edit', data=patch_data,
                                environ_base=headers)
>           self.assertEqual(rv.status_code, 200)
E           AssertionError: 401 != 200

tests/unit/test_api_urls.py:187: AssertionError
=============================== warnings summary ===============================
tests/unit/test_uploads.py:45
  /home/circleci/repo/tests/unit/test_uploads.py:45: PytestCollectionWarning: cannot collect test class 'TestingFileStorage' because it has a __init__ constructor (from: tests/unit/test_uploads.py)
    class TestingFileStorage(FileStorage):

tests/unit/test_rendering.py::TestURIContent::test_form_rendering
  /home/circleci/repo/env/lib/python3.7/site-packages/jinja2/lexer.py:648: DeprecationWarning: invalid escape sequence '\d'
    .decode("unicode-escape")

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================== 1 failed, 153 passed, 2 warnings in 1.74s ===================
@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 13, 2020

ah the mocking is not happening! :)
Capture d’écran 2020-02-13 à 14 06 44
webcompat/webcompat-tests#1

@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 13, 2020

so this didn't solve it.
c241f4a

@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 13, 2020

So i remember having difficulties with our code in the past and blueprints. There is something specific on how we organize our code which makes it difficult to both test and mock the code.

pytest -v -s tests/unit/test_api_urls.py 
================================================================= test session starts =================================================================
platform darwin -- Python 3.7.4, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /Users/karl/code/webcompat.com/env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/karl/code/webcompat.com
collecting ... Writing logs to: /Users/karl/code/webcompat.com/tmp
Statuses Initialization…
Milestones in memory
collected 11 items                                                                                                                                    

tests/unit/test_api_urls.py::TestAPIURLs::test_api_comments_link_header_auth PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_issues_out_of_range PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_labels_without_auth PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_patch_issue_state_status PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_patch_issue_too_many_json_elements PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_patch_issue_valid_request FAILED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_search_wrong_parameter PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_set_labels_without_auth PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_user_activity_without_auth PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_wrong_category PASSED
tests/unit/test_api_urls.py::TestAPIURLs::test_api_wrong_route PASSED

====================================================================== FAILURES =======================================================================
___________________________________________________ TestAPIURLs.test_api_patch_issue_valid_request ____________________________________________________

thing = <flask.blueprints.Blueprint object at 0x103490d90>, comp = 'endpoints', import_path = 'webcompat.api.endpoints'

    def _dot_lookup(thing, comp, import_path):
        try:
>           return getattr(thing, comp)
E           AttributeError: 'Blueprint' object has no attribute 'endpoints'

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1101: AttributeError

During handling of the above exception, another exception occurred:

self = <unit.test_api_urls.TestAPIURLs testMethod=test_api_patch_issue_valid_request>

    def test_api_patch_issue_valid_request(self):
        """Patching the issue - Valid request."""
>       with patch('webcompat.api.endpoints.proxy_request') as github_data:

tests/unit/test_api_urls.py:179: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1257: in __enter__
    self.target = self.getter()
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1427: in <lambda>
    getter = lambda: _importer(target)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1114: in _importer
    thing = _dot_lookup(thing, comp, import_path)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

thing = <flask.blueprints.Blueprint object at 0x103490d90>, comp = 'endpoints', import_path = 'webcompat.api.endpoints'

    def _dot_lookup(thing, comp, import_path):
        try:
            return getattr(thing, comp)
        except AttributeError:
            __import__(import_path)
>           return getattr(thing, comp)
E           AttributeError: 'Blueprint' object has no attribute 'endpoints'

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1104: AttributeError
============================================================ 1 failed, 10 passed in 1.42s =============================================================

@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 13, 2020

So I think I know the source of the misery that i already experienced a couple of years ago.

The module, the directory and the blueprint names are the same: api. It's then super confusing for the application when it's time to hack strings into paths.

The best solutions would be to rename the blueprint name from api to api_bp probably.

@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 19, 2020

=================================== FAILURES ===================================
________________ TestAPIURLs.test_api_patch_issue_valid_request ________________

self = <unit.test_api_urls.TestAPIURLs testMethod=test_api_patch_issue_valid_request>
github_data = <MagicMock name='proxy_request' id='139856178361400'>

    @patch('webcompat.helpers.proxy_request')
    def test_api_patch_issue_valid_request(self, github_data):
        """Patching the issue - Valid request."""
        with webcompat.app.app_context():
            github_data.return_value = mock_api_response(
                {'status_code': 200, 'content': '[]'})
            data = {'state': 'open', 'milestone': 2}
            patch_data = json.dumps(data)
            rv = self.app.patch('/api/issues/1/edit', data=patch_data,
                                environ_base=headers)
>           self.assertEqual(rv.status_code, 200)
E           AssertionError: 401 != 200

tests/unit/test_api_urls.py:187: AssertionError
=============================== warnings summary ===============================
tests/unit/test_uploads.py:45
  /home/circleci/repo/tests/unit/test_uploads.py:45: PytestCollectionWarning: cannot collect test class 'TestingFileStorage' because it has a __init__ constructor (from: tests/unit/test_uploads.py)
    class TestingFileStorage(FileStorage):

tests/unit/test_rendering.py::TestURIContent::test_form_rendering
  /home/circleci/repo/env/lib/python3.7/site-packages/jinja2/lexer.py:648: DeprecationWarning: invalid escape sequence '\d'
    .decode("unicode-escape")

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================== 1 failed, 153 passed, 2 warnings in 1.74s ===================

Exited with code exit status 1

It means that we are not patching the right method. And i will probably need to go into blueprint renaming for the sake of checking if it's the reason.

karlcow added 4 commits Feb 19, 2020
The reason is that the TestingFileStorage class had the string 'test'
which makes it discoverable as a test by pytest.
renaming it solves the warning.
When trying to mock the api blueprint, we enter into a name conflict
this first change is here to prepare for the necessary changes
to test that we need to do.
Now that we change the name of the blueprint api, we do not have
a conflict name anymore.
@karlcow karlcow marked this pull request as ready for review Feb 19, 2020
@karlcow karlcow requested review from miketaylr and ksy36 Feb 19, 2020
@karlcow

This comment has been minimized.

Copy link
Contributor Author

karlcow commented Feb 19, 2020

We can switch to… 🥁 pytest

webcompat/templates/form.html Show resolved Hide resolved
@miketaylr

This comment has been minimized.

Copy link
Member

miketaylr commented Feb 19, 2020

The module, the directory and the blueprint names are the same: api. It's then super confusing for the application when it's time to hack strings into paths.

Sorry, this is my fault. :)

@miketaylr miketaylr merged commit a2f7696 into webcompat:master Feb 19, 2020
1 check passed
1 check passed
ci/circleci: build Your tests passed on CircleCI!
Details
@karlcow karlcow deleted the karlcow:3206/1 branch Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.