Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Dębowski committed Apr 18, 2019
1 parent 57b4f76 commit 12ac6a8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
1 change: 0 additions & 1 deletion starlette_jsonrpc/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Dispatcher:

def __init__(self):
self.methods_map = {}

Expand Down
5 changes: 4 additions & 1 deletion starlette_jsonrpc/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ async def _get_response(self, request: Request) -> dict:

@staticmethod
def _is_notification(params: dict) -> bool:
if all(k in params for k in ("jsonrpc", "method", "params")) and not "id" in params:
if (
all(k in params for k in ("jsonrpc", "method", "params"))
and not "id" in params
):
return True
return False

Expand Down
92 changes: 64 additions & 28 deletions tests/test_validating.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ def test_payload_as_empty_dict():
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
"id": 'None',
"error": {
"code": -32602,
"message": "Invalid params.",
"data": {},
},
"id": "None",
"error": {"code": -32602, "message": "Invalid params.", "data": {}},
}


Expand All @@ -23,12 +19,8 @@ def test_payload_as_empty_list():
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
"id": 'None',
"error": {
"code": -32602,
"message": "Invalid params.",
"data": {},
},
"id": "None",
"error": {"code": -32602, "message": "Invalid params.", "data": {}},
}


Expand All @@ -37,12 +29,8 @@ def test_incorrect_payload():
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
"id": 'None',
"error": {
"code": -32602,
"message": "Invalid params.",
"data": {},
},
"id": "None",
"error": {"code": -32602, "message": "Invalid params.", "data": {}},
}


Expand Down Expand Up @@ -111,15 +99,22 @@ def test_params_as_invalid_object():


def test_params_as_invalid_list():
payload = {"jsonrpc": "2.0", "method": "subtract_positional", "params": [1, ], "id": "1"}
payload = {
"jsonrpc": "2.0",
"method": "subtract_positional",
"params": [1],
"id": "1",
}
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
"id": "1",
"error": {
"code": -32602,
"message": "Invalid params.",
"data": {"params": "subtract_positional() missing 1 required positional argument: 'y'"},
"data": {
"params": "subtract_positional() missing 1 required positional argument: 'y'"
},
},
}

Expand All @@ -132,26 +127,47 @@ def test_without_params():

# ID


def test_id_as_integer():
payload = {"jsonrpc": "2.0", "method": "subtract", "params": {"x": 42, "y": 23}, "id": 1}
payload = {
"jsonrpc": "2.0",
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": 1,
}
response = client.post("/api/", json=payload)
assert response.json() == {"jsonrpc": "2.0", "id": 1, "result": 19}


def test_id_as_string():
payload = {"jsonrpc": "2.0", "method": "subtract", "params": {"x": 42, "y": 23}, "id": "abc"}
payload = {
"jsonrpc": "2.0",
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": "abc",
}
response = client.post("/api/", json=payload)
assert response.json() == {"jsonrpc": "2.0", "id": "abc", "result": 19}


def test_id_as_null():
payload = {"jsonrpc": "2.0", "method": "subtract", "params": {"x": 42, "y": 23}, "id": None}
payload = {
"jsonrpc": "2.0",
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": None,
}
response = client.post("/api/", json=payload)
assert response.json() == {"jsonrpc": "2.0", "id": None, "result": 19}


def test_empty_id():
payload = {"jsonrpc": "2.0", "method": "subtract", "params": {"x": 42, "y": 23}, "id": ""}
payload = {
"jsonrpc": "2.0",
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": "",
}
response = client.post("/api/", json=payload)
assert response.json() == {"jsonrpc": "2.0", "id": None, "result": 19}

Expand All @@ -169,7 +185,12 @@ def test_without_id():


def test_jsonrpc_as_integer():
payload = {"jsonrpc": 2, "method": "subtract", "params": {"x": 42, "y": 23}, "id": "1"}
payload = {
"jsonrpc": 2,
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": "1",
}
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
Expand All @@ -183,7 +204,12 @@ def test_jsonrpc_as_integer():


def test_empty_jsonrpc():
payload = {"jsonrpc": "", "method": "subtract", "params": {"x": 42, "y": 23}, "id": "1"}
payload = {
"jsonrpc": "",
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": "1",
}
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
Expand All @@ -197,7 +223,12 @@ def test_empty_jsonrpc():


def test_jsonrpc_wrong_value():
payload = {"jsonrpc": "3.0", "method": "subtract", "params": {"x": 42, "y": 23}, "id": "1"}
payload = {
"jsonrpc": "3.0",
"method": "subtract",
"params": {"x": 42, "y": 23},
"id": "1",
}
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
Expand Down Expand Up @@ -228,7 +259,12 @@ def test_without_jsonrpc():


def test_not_registered_method():
payload = {"jsonrpc": "2.0", "method": "non_existing_method", "params": {"x": 42, "y": 23}, "id": "1"}
payload = {
"jsonrpc": "2.0",
"method": "non_existing_method",
"params": {"x": 42, "y": 23},
"id": "1",
}
response = client.post("/api/", json=payload)
assert response.json() == {
"jsonrpc": "2.0",
Expand Down

0 comments on commit 12ac6a8

Please sign in to comment.