-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_mock.py
48 lines (38 loc) · 1.74 KB
/
test_mock.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
import requests_mock
from taobaopy.taobao import TaoBaoAPIClient, TaoBaoAPIError
from io import BytesIO
class NamedBytesIO(BytesIO):
def __init__(self, content, name):
self.name = name
self.length = len(content)
super(NamedBytesIO, self).__init__(content)
def test_no_json():
with requests_mock.mock() as m:
m.post("http://test.com/router/rest", text='not json')
c = TaoBaoAPIClient(app_key="key", app_secret="sec", domain="test.com", retry_count=1)
try:
c.time_get()
assert False
except TaoBaoAPIError as e:
if e.sub_code != "ism.json-decode-error":
assert False
except Exception:
assert False
def test_normal():
with requests_mock.mock() as m:
m.post("http://test.com/router/rest", text='{"time_get_response":{"time":"2000-01-01 00:00:00"}}')
c = TaoBaoAPIClient(app_key="key", app_secret="sec", domain="test.com", retry_count=1)
res = c.time_get()
assert res["time_get_response"]["time"] == '2000-01-01 00:00:00'
def test_json_fix():
with requests_mock.mock() as m:
m.post("http://test.com/router/rest", text='''{"time_get_response":{"time":"\n\r\t2000-01-01 00:00:00"}}''')
c = TaoBaoAPIClient(app_key="key", app_secret="sec", domain="test.com", retry_count=1)
res = c.time_get(a=123)
assert res["time_get_response"]["time"] == '\n\r\t2000-01-01 00:00:00'
def test_upload():
with requests_mock.Mocker() as m:
m.post("http://test.com/router/rest", text='{}')
c = TaoBaoAPIClient(app_key="key", app_secret="sec", domain="http://test.com", retry_count=1)
res = c.time_get(file1=NamedBytesIO(b"abc", "test.png"))
assert res == {}