This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
forked from normoes/python-monerorpc
-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.py
104 lines (89 loc) · 3.46 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
from decimal import Decimal
import pytest
import responses
from requests.exceptions import ConnectionError, Timeout, RequestException
from monerorpc.authproxy import (
AuthServiceProxy,
EncodeDecimal,
JSONRPCException,
)
class TestEncodeDecimal:
def test_encodes_ok(self):
assert json.dumps(Decimal(2), default=EncodeDecimal)
def test_encoding_fail(self):
with pytest.raises(TypeError):
json.dumps(self, default=EncodeDecimal)
class TestAuthServiceProxy:
dummy_url = "http://dummy-rpc:8000/json_rpc"
@responses.activate
def test_good_call(self):
responses.add(responses.POST, self.dummy_url, json={"result": "dummy"})
client = AuthServiceProxy(self.dummy_url)
resp = client.status()
assert resp == "dummy"
@responses.activate
@pytest.mark.parametrize("code", (500, 404))
def test_http_error_raises_error(self, code):
responses.add(responses.POST, self.dummy_url, status=code)
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(JSONRPCException):
client.dummy_method()
@responses.activate
def test_empty_response_raises_error(self):
responses.add(responses.POST, self.dummy_url, status=200, json={})
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(JSONRPCException):
client.dummy_method()
@responses.activate
def test_rpc_error_raises_error(self):
responses.add(
responses.POST,
self.dummy_url,
status=200,
json={"error": "dummy error"},
)
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(JSONRPCException):
client.dummy_method()
@responses.activate
def test_connection_error(self):
"""Mock no connection to server error."""
responses.add(responses.POST, self.dummy_url, body=ConnectionError(""))
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(JSONRPCException):
client.get_balance()
@responses.activate
def test_timeout_error(self):
"""Mock timeout connecting to server."""
responses.add(responses.POST, self.dummy_url, body=Timeout(""))
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(JSONRPCException):
client.get_balance()
@responses.activate
def test_jsondecode_request_error(self):
"""Mock JSONDecodeError when trying to get JSON form response."""
responses.add(responses.POST, self.dummy_url, body=RequestException(""))
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(JSONRPCException):
client.get_balance()
@responses.activate
def test_other_request_error(self):
"""Mock other errors connecting to server."""
responses.add(responses.POST, self.dummy_url, body="<html></html>")
client = AuthServiceProxy(self.dummy_url)
with pytest.raises(ValueError):
client.get_balance()
@responses.activate
def test_calls_batch(self):
for n in range(2):
responses.add(
responses.POST,
self.dummy_url,
status=200,
json={"result": "dummy - {}".format(n)},
)
client = AuthServiceProxy(self.dummy_url)
cases = [["dummy_method_1", {}], ["dummy_method_2", "dummy"]]
results = client.batch_(cases)
assert len(results) == len(cases)