I'm having difficulty in writing test cases for the ZabbixAPILayer class mentioned below. I'm not sure how I can mock the 'zabbix_conn_obj' there. Any help will be appreciated. Thanks!
file: externalapi/apilayer.py
from zabbix.api import ZabbixAPI
import json
import time
class ZabbixAPILayer(object):
def uptime(self,arg,zabbix_conn_obj):
try:
getUpdateItem = zabbix_conn_obj.do_request("item.get", {"host":arg})
lastclock=getUpdateItem['result'][37].get('lastclock')
lastclock=int(lastclock)
curclock=int(time.time())
check_val=curclock-lastclock
limit=60*1000
if check_val<limit:
lastval=getUpdateItem['result'][37].get('lastvalue')
return time.strftime("%H:%M:%S", time.gmtime(float(getUpdateItem['result'][37].get('lastvalue'))))
else:
return "-"
except:
return "NOT AVAILABLE"
.....
class APILayer(ZabbixAPILayer):
def __init__(self):
self.zabbix_conn_obj=ZabbixAPI(url=settings.ZABBIX_URL, user=settings.ZABBIX_USER, password=settings.ZABBIX_PWD)
def uptime(self,arg):
return super(APILayer,self).uptime(arg,self.zabbix_conn_obj)
.....
file: base/admin.py
......
from ..externalapis.apilayer import APILayer
......
gen_obj= APILayer()
gen_obj.uptime()
......
Thanks for your comments. Got this working! Here the way i'm doing this
import mock
...
def test_uptime(self):
zabbix_conn_obj = mock.Mock()
json_blob = {} # This is the json blob i'm using as return value from do request
zabbix_conn_obj.do_request = mock.Mock(return_value=json_blob)
obj = ZabbixAPILayer()
value = obj.uptime("TestHOST",zabbix_conn_obj)
desired_result = '' #whatever my desired value is
self.assertEqual(value, desired_result)