Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
aodag committed Jun 12, 2021
1 parent 0539f81 commit 61df99b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
10 changes: 7 additions & 3 deletions kenallclient/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .model import KenAllResult
import urllib.request
import urllib.parse
import json
from typing import Dict

from kenallclient.model import KenAllResult


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

def get(self, postal_code) -> KenAllResult:
req = self.create_request(postal_code)
def fetch(self, req: urllib.request.Request) -> KenAllResult:
with urllib.request.urlopen(req) as res:
if not res.headers["Content-Type"].startswith("application/json"):
ValueError("not json response", res.read())
d = json.load(res)
return KenAllResult.fromdict(d)

def get(self, postal_code) -> KenAllResult:
req = self.create_request(postal_code)
return self.fetch(req)
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def test_it():
pass


def test_create_request():
from kenallclient.client import KenAllClient

target = KenAllClient("testing-api-key")
result = target.create_request("9999999")
assert result.full_url == "https://api.kenall.jp/v1/postalcode/9999999"
assert result.headers == {"Authorization": "Token testing-api-key"}


def test_fetch(mocker, dummy_json):
import json
import io
from kenallclient.client import KenAllClient

dummy_response = io.StringIO(json.dumps(dummy_json))
dummy_response.headers = {"Content-Type": "application/json"}
mock_urlopen = mocker.patch("kenallclient.client.urllib.request.urlopen")
mock_urlopen.return_value = dummy_response

request = object()
target = KenAllClient("testing-api-key")
result = target.fetch(request)
mock_urlopen.assert_called_with(request)
assert result

0 comments on commit 61df99b

Please sign in to comment.