-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend Consumer Methods to Reduce Boilerplate (#159)
* Allow extending consumer methods * Remove Python 3 function annotations * Document consumer method extension * Fix whitespace issues * Fix broken logic with @returns.*
- Loading branch information
Showing
5 changed files
with
311 additions
and
54 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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Third-party imports | ||
import pytest | ||
|
||
# Local imports | ||
import uplink | ||
|
||
# Constants | ||
BASE_URL = "https://api.github.com" | ||
|
||
|
||
class GitHubError(Exception): | ||
pass | ||
|
||
|
||
@uplink.response_handler | ||
def github_error(response): | ||
if "errors" in response.json(): | ||
raise GitHubError() | ||
return response | ||
|
||
|
||
@uplink.timeout(10) | ||
class GitHubService(uplink.Consumer): | ||
@github_error | ||
@uplink.json | ||
@uplink.post("graphql", args=(uplink.Body,)) | ||
def graphql(self, **body): | ||
pass | ||
|
||
@uplink.returns.json(member=("data", "repository")) | ||
@uplink.args(body=uplink.Body) | ||
@graphql | ||
def get_repository(self, **body): | ||
pass | ||
|
||
@uplink.returns.json(member=("data", "repository")) | ||
@graphql.extend("graphql2", args=(uplink.Body,)) | ||
def get_repository2(self, **body): | ||
pass | ||
|
||
|
||
def test_get_repository(mock_client, mock_response): | ||
data = { | ||
"query": """\ | ||
query { | ||
repository(owner: "prkumar", name: "uplink") { | ||
nameWithOwner | ||
} | ||
}""" | ||
} | ||
result = {"data": {"repository": {"nameWithOwner": "prkumar/uplink"}}} | ||
mock_response.with_json(result) | ||
mock_client.with_response(mock_response) | ||
github = GitHubService(base_url=BASE_URL, client=mock_client) | ||
response = github.get_repository(**data) | ||
request = mock_client.history[0] | ||
assert request.method == "POST" | ||
assert request.base_url == BASE_URL | ||
assert request.endpoint == "/graphql" | ||
assert request.timeout == 10 | ||
assert request.json == data | ||
assert response == result["data"]["repository"] | ||
|
||
|
||
def test_get_repository2_failure(mock_client, mock_response): | ||
data = { | ||
"query": """\ | ||
query { | ||
repository(owner: "prkumar", name: "uplink") { | ||
nameWithOwner | ||
} | ||
}""" | ||
} | ||
result = { | ||
"data": {"repository": None}, | ||
"errors": [ | ||
{ | ||
"type": "NOT_FOUND", | ||
"path": ["repository"], | ||
"locations": [{"line": 7, "column": 3}], | ||
"message": "Could not resolve to a User with the username 'prkussmar'.", | ||
} | ||
], | ||
} | ||
mock_response.with_json(result) | ||
mock_client.with_response(mock_response) | ||
github = GitHubService(base_url=BASE_URL, client=mock_client) | ||
with pytest.raises(GitHubError): | ||
github.get_repository2(**data) | ||
request = mock_client.history[0] | ||
assert request.method == "POST" | ||
assert request.base_url == BASE_URL | ||
assert request.endpoint == "/graphql2" | ||
assert request.timeout == 10 |
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
Oops, something went wrong.