-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make testing style more consistent (#168)
Several overall style changes: 1. Avoid plain `Mock()` and `Mock(spec=thing)`., prefer `mock.create_autospec()`. 1. Don't `mock.patch` without `autospec`. 1. Don't give mock instances special names. Prefer `thing` over `thing_mock` and `mock_thing`. 1. When using `mock.patch`, use the same name as the item being patched to refer to the mock. ```python with mock.patch('module.thing') as thing: ... ``` and ``` @mock.patch('module.thing') def test(thing): ... ``` 1. Test helper factories should follow the naming convention `make_thing()`. 1. Use `ThingStub` when creating semi-functioning subclasses for testing purposes.
- Loading branch information
Jon Wayne Parrott
committed
Jun 30, 2017
1 parent
cf93481
commit 78fec2c
Showing
14 changed files
with
377 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
|
||
from google.auth import _helpers | ||
from google.auth import exceptions | ||
from google.auth import transport | ||
from google.auth.compute_engine import credentials | ||
|
||
|
||
|
@@ -41,9 +42,9 @@ def test_default_state(self): | |
@mock.patch( | ||
'google.auth._helpers.utcnow', | ||
return_value=datetime.datetime.min + _helpers.CLOCK_SKEW) | ||
@mock.patch('google.auth.compute_engine._metadata.get') | ||
def test_refresh_success(self, get_mock, now_mock): | ||
get_mock.side_effect = [{ | ||
@mock.patch('google.auth.compute_engine._metadata.get', autospec=True) | ||
def test_refresh_success(self, get, utcnow): | ||
get.side_effect = [{ | ||
# First request is for sevice account info. | ||
'email': '[email protected]', | ||
'scopes': ['one', 'two'] | ||
|
@@ -59,7 +60,7 @@ def test_refresh_success(self, get_mock, now_mock): | |
# Check that the credentials have the token and proper expiration | ||
assert self.credentials.token == 'token' | ||
assert self.credentials.expiry == ( | ||
now_mock() + datetime.timedelta(seconds=500)) | ||
utcnow() + datetime.timedelta(seconds=500)) | ||
|
||
# Check the credential info | ||
assert (self.credentials.service_account_email == | ||
|
@@ -71,17 +72,17 @@ def test_refresh_success(self, get_mock, now_mock): | |
assert self.credentials.valid | ||
|
||
@mock.patch('google.auth.compute_engine._metadata.get', autospec=True) | ||
def test_refresh_error(self, get_mock): | ||
get_mock.side_effect = exceptions.TransportError('http error') | ||
def test_refresh_error(self, get): | ||
get.side_effect = exceptions.TransportError('http error') | ||
|
||
with pytest.raises(exceptions.RefreshError) as excinfo: | ||
self.credentials.refresh(None) | ||
|
||
assert excinfo.match(r'http error') | ||
|
||
@mock.patch('google.auth.compute_engine._metadata.get', autospec=True) | ||
def test_before_request_refreshes(self, get_mock): | ||
get_mock.side_effect = [{ | ||
def test_before_request_refreshes(self, get): | ||
get.side_effect = [{ | ||
# First request is for sevice account info. | ||
'email': '[email protected]', | ||
'scopes': 'one two' | ||
|
@@ -95,11 +96,12 @@ def test_before_request_refreshes(self, get_mock): | |
assert not self.credentials.valid | ||
|
||
# before_request should cause a refresh | ||
request = mock.create_autospec(transport.Request, instance=True) | ||
self.credentials.before_request( | ||
mock.Mock(), 'GET', 'http:https://example.com?a=1#3', {}) | ||
request, 'GET', 'http:https://example.com?a=1#3', {}) | ||
|
||
# The refresh endpoint should've been called. | ||
assert get_mock.called | ||
assert get.called | ||
|
||
# Credentials should now be valid. | ||
assert self.credentials.valid | ||
|
Oops, something went wrong.