pytestのmockerについて

pythonでmockerを使用しています。
以下の例では、SolrAccessクラスのexecuteメソッドに対して、
mockerを使用し、rtn_executeを返却する事ができました。

from janus.solrAccess.solraccess import SolrAccess class TestReverseGeoSearchService: def test_No_001_01_and_001(self, mocker): rtn_execute = { "responseHeader": { "status": 0, "QTime": 0, "params": { } }, "response": { "numFound": 1, "start": 0, "maxScore": 1.0, "numFoundExact": True, "docs": [{ "name_full": "東京都文京区小石川5-5-5", "yomi_full": "とうきょうとぶんきょくこいしかわ5-5-5", "shape_x_wgs": 139.74800272437312, "shape_y_wgs": 35.73016903373604, "distance": 1.532441973041701, "score": 1.0, "route_point_func_wgs": '[{"kind": "1501","lon": 139.74800272437310,"lat": 35.73016903373600}]' }] } } mocker.patch.object(SolrAccess, 'execute', return_value=rtn_execute)

以下の要領で、parkingfullemptyに、外部APIのurlを渡します。

yobimoto = 'https://test.core.its-mo.com/zmaps/api/apicore/core/v1_0/poi/multi?' accessinfo = 'if_clientid=JSZ067b14f8df4f|xvSPL&if_auth_type=ip' contents = '&content=PPK_AKP,PPK_PARK,PPK_REPARK,PPK_NKSK,PPK_MKS' parameter = '&&latlon=' + q_latlon + '&radius=500' url = yobimoto + accessinfo + contents + parameter response = self.parkingfullempty(url)

pytestから以下のresに外部APIの情報を、mockerで、
渡す方法は分かりますか。
上記は、SolrAccessのexecuteメソッドになり、自分が作成したものです。
[with request.urlopen(get_req) as res]は、ライブラリですが、
mockerからres等に、返却値を渡す事はできますか。
with request.urlopen(get_req) as res

import copy import unicodedata from janus.azazel.azazel import Azazel from urllib import request import io,sys import math import json import janus.service.searchservicedef as SD def parkingfullempty(self, url): self._logger.writeLogDebug("ParkingAccess:parkingfullempty start") ## GET self._logger.writeLogDebug('url: {}'.format(url)) get_req = request.Request(url) with request.urlopen(get_req) as res: body = json.loads(res.read()) # レスポンスボディ headers = res.getheaders() # ヘッダー(dict) status = res.getcode() # ステータスコード self._logger.writeLogDebug('headers: {}'.format(headers)) self._logger.writeLogDebug('status: {}'.format(status)) self._logger.writeLogDebug('body: {}'.format(body))

コメントを投稿

0 コメント