Skip to content

Commit 61df99b

Browse files
committed
test
1 parent 0539f81 commit 61df99b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

kenallclient/client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from .model import KenAllResult
21
import urllib.request
32
import urllib.parse
43
import json
54
from typing import Dict
65

6+
from kenallclient.model import KenAllResult
7+
78

89
class KenAllClient:
910
def __init__(self, api_key: str) -> None:
@@ -19,10 +20,13 @@ def create_request(self, postal_code) -> urllib.request.Request:
1920
req = urllib.request.Request(url, headers=self.authorization)
2021
return req
2122

22-
def get(self, postal_code) -> KenAllResult:
23-
req = self.create_request(postal_code)
23+
def fetch(self, req: urllib.request.Request) -> KenAllResult:
2424
with urllib.request.urlopen(req) as res:
2525
if not res.headers["Content-Type"].startswith("application/json"):
2626
ValueError("not json response", res.read())
2727
d = json.load(res)
2828
return KenAllResult.fromdict(d)
29+
30+
def get(self, postal_code) -> KenAllResult:
31+
req = self.create_request(postal_code)
32+
return self.fetch(req)

tests/test_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def test_it():
2+
pass
3+
4+
5+
def test_create_request():
6+
from kenallclient.client import KenAllClient
7+
8+
target = KenAllClient("testing-api-key")
9+
result = target.create_request("9999999")
10+
assert result.full_url == "https://api.kenall.jp/v1/postalcode/9999999"
11+
assert result.headers == {"Authorization": "Token testing-api-key"}
12+
13+
14+
def test_fetch(mocker, dummy_json):
15+
import json
16+
import io
17+
from kenallclient.client import KenAllClient
18+
19+
dummy_response = io.StringIO(json.dumps(dummy_json))
20+
dummy_response.headers = {"Content-Type": "application/json"}
21+
mock_urlopen = mocker.patch("kenallclient.client.urllib.request.urlopen")
22+
mock_urlopen.return_value = dummy_response
23+
24+
request = object()
25+
target = KenAllClient("testing-api-key")
26+
result = target.fetch(request)
27+
mock_urlopen.assert_called_with(request)
28+
assert result

0 commit comments

Comments
 (0)