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

Bug 1453482 - Use d-r-f's APIClient instead of WebTest's TestApp #3439

Merged
merged 1 commit into from Apr 12, 2018

Conversation

edmorley
Copy link
Contributor

@edmorley edmorley commented Apr 11, 2018

To avoid unnecessary dependencies, and use a more conventional django-rest-framework testing approach:
http://www.django-rest-framework.org/api-guide/testing/#apiclient

APIClient has a few API differences:

  • .json -> .json()
  • .status_int -> .status_code
  • Get parameters are passed as keyword argument data not params
  • The default hostname is http://testserver not http://localhost
  • Additional HTTP headers are passed directly as keyword arguments, rather than nested under a headers property.
  • It doesn't check the status code itself, so explicit checks are required, along with removing expect_errors.
  • The .post_json() and .put_json() methods don't exist.

See also the docs for the Django test client (which APIClient wraps):
https://docs.djangoproject.com/en/1.11/topics/testing/tools/#the-test-client

Whilst making these changes, I also cleaned up the session fetching in test_auth.py and test_backends.py, and added a status_code check in conftest.py's mock_post_json() - which makes the root cause of test failures clearer.

@edmorley edmorley self-assigned this Apr 11, 2018
@edmorley edmorley changed the title Bug 1453482 - Use django.test.client instead of WebTest's TestApp Bug 1453482 - Use d-r-f's APIClient instead of WebTest's TestApp Apr 11, 2018
To avoid unnecessary dependencies, and use a more conventional
django-rest-framework testing approach:
http://www.django-rest-framework.org/api-guide/testing/#apiclient

APIClient has a few API differences:
* `.json` -> `.json()`
* `.status_int` -> `.status_code`
* Get parameters are passed as keyword argument `data` not `params`
* The default hostname is `http://testserver` not `http://localhost`
* Additional HTTP headers are passed directly as keyword arguments,
  rather than nested under a `headers` property.
* It doesn't check the status code itself, so explicit checks are
  required, along with removing `expect_errors`.
* The `.post_json()` and `.put_json()` methods don't exist.

See also the docs for the Django test client (which APIClient wraps):
https://docs.djangoproject.com/en/1.11/topics/testing/tools/#the-test-client

Whilst making these changes, I also cleaned up the session fetching
in `test_auth.py` and `test_backends.py`, and added a `status_code`
check in `conftest.py`'s `mock_post_json()` - which makes the root
cause of test failures clearer.
Copy link
Contributor

@camd camd left a comment

Choose a reason for hiding this comment

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

Awesome cleanup!! :)

assert resp.json["id"] == test_job.id
assert not resp.json.get("taskcluster_metadata")
assert resp.status_code == 200
assert isinstance(resp.json(), dict)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know how expensive the call to resp.json() is, but I'd be tempted to set that to a temp var and then act on that. This is such a nit that it really doesn't matter much, just something that jumped out at me. :) Feel free to ignore, if you like. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know how expensive the call to resp.json() is

Ah same thing occurred to me - but I checked it and turns out it's memoized:
https://github.com/django/django/blob/1.11.12/django/test/client.py#L693-L701

@edmorley edmorley merged commit a502319 into master Apr 12, 2018
@edmorley edmorley deleted the rm-webtest branch April 12, 2018 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants