Skip to content

Commit c3b7d53

Browse files
committed
Add tests directory and add Python3 support to camtest & PEP8
1 parent bc89859 commit c3b7d53

3 files changed

Lines changed: 67 additions & 64 deletions

File tree

foscam/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from foscam import FoscamCamera
1+
from foscam.foscam import FoscamCamera
2+

tests/__init__.py

Whitespace-only changes.
Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
import unittest
2-
import os
31
import os.path
2+
import unittest
43
from time import sleep
5-
import ConfigParser
64

7-
from foscam import FoscamCamera, FOSCAM_SUCCESS
5+
try: # PY3
6+
from configparser import ConfigParser
7+
except ImportError:
8+
from ConfigParser import SafeConfigParser as ConfigParser
9+
10+
from foscam.foscam import FoscamCamera, FOSCAM_SUCCESS
811

9-
config = ConfigParser.SafeConfigParser()
12+
config = ConfigParser()
1013
config_filepath = os.path.join(os.path.dirname(__file__), 'camtest.cfg')
1114

1215
if os.path.exists(config_filepath):
1316
config.read([config_filepath])
1417

1518
config_defaults = config.defaults()
1619

17-
CAM_HOST = config_defaults.get('host') or ''
18-
CAM_PORT = config_defaults.get('port') or 88
19-
CAM_USER = config_defaults.get('user') or 'admin'
20-
CAM_PASS = config_defaults.get('pass') or 'foscam'
20+
CAM_HOST = config_defaults.get('host') or ''
21+
CAM_PORT = config_defaults.get('port') or 88
22+
CAM_USER = config_defaults.get('user') or 'admin'
23+
CAM_PASS = config_defaults.get('pass') or 'foscam'
2124
CAM_WIFI_SSID = config_defaults.get('wifi_ssid') or ''
2225
CAM_WIFI_PASS = config_defaults.get('wifi_pass') or ''
2326

27+
2428
class CallbackForTest(object):
2529
def __call__(self, *args, **kwargs):
2630
self.args = args
27-
self.kwargs = kwargs;
31+
self.kwargs = kwargs
2832

29-
class TestFoscam(unittest.TestCase):
3033

34+
class TestFoscam(unittest.TestCase):
3135
def setUp(self):
3236
self.foscam = FoscamCamera(CAM_HOST, CAM_PORT, CAM_USER, CAM_PASS)
3337

@@ -53,7 +57,7 @@ def test_sub_video_stream(self):
5357
def test_video_stream_param(self):
5458
self.foscam.get_main_video_stream_type()
5559
self.foscam.get_video_stream_param()
56-
self.foscam.set_video_stream_param(0, 0, 2*1024*1024, 30, 30, 1)
60+
self.foscam.set_video_stream_param(0, 0, 2 * 1024 * 1024, 30, 30, 1)
5761
self.foscam.get_video_stream_param()
5862

5963
def test_mirror_video(self):
@@ -76,27 +80,25 @@ def test_get_mirror_and_flip_setting(self):
7680
rc, args = self.foscam.get_mirror_and_flip_setting()
7781
self.assertEqual(rc, 0)
7882

79-
8083
# ***************** Test Network Functions *********************
8184
def test_get_ip_info(self):
8285
rc, info = self.foscam.get_ip_info()
8386
self.assertTrue(rc == 0)
8487

85-
8688
# ***************** Test Network Functions *********************
8789
def test_set_ip_info(self):
8890
old = self.foscam.get_ip_info()
89-
self.foscam.set_ip_info(is_dhcp=0, ip='192.168.0.110', \
90-
gate='192.168.0.1', mask='255.255.255.0', \
91-
dns1='192.168.0.1', dns2='8.8.8.8')
91+
self.foscam.set_ip_info(is_dhcp=0, ip='192.168.0.110',
92+
gate='192.168.0.1', mask='255.255.255.0',
93+
dns1='192.168.0.1', dns2='8.8.8.8')
9294
# Wait to reboot.
9395
sleep(30)
9496
self.foscam.get_ip_info()
9597

9698
def test_set_port(self):
9799
rc, args = self.foscam.get_port_info()
98-
rc, args = self.foscam.set_port_info(webport=88, mediaport=88, \
99-
httpsport=443, onvifport=888)
100+
rc, args = self.foscam.set_port_info(webport=88, mediaport=88,
101+
httpsport=443, onvifport=888)
100102
self.assertEqual(rc, 0)
101103
rc, args = self.foscam.get_port_info()
102104

@@ -111,15 +113,15 @@ def test_wifi(self):
111113
self.foscam.get_wifi_config()
112114

113115
self.foscam.set_wifi_setting(
114-
ssid=CAM_WIFI_SSID,
115-
psk=CAM_WIFI_PASS,
116-
isenable=0,
117-
isusewifi=0,
118-
nettype=0,
119-
encryptype=4,
120-
authmode=1,
121-
keyformat=0,
122-
defaultkey=1)
116+
ssid=CAM_WIFI_SSID,
117+
psk=CAM_WIFI_PASS,
118+
isenable=0,
119+
isusewifi=0,
120+
nettype=0,
121+
encryptype=4,
122+
authmode=1,
123+
keyformat=0,
124+
defaultkey=1)
123125

124126
# *************** PTZ Move ********************************
125127

@@ -194,7 +196,7 @@ def test_get_set_system_time(self):
194196
hour=args['hour'],
195197
minute=args['minute'],
196198
sec=args['sec'],
197-
)
199+
)
198200

199201
def test_devname(self):
200202
args = self.foscam.get_dev_name()
@@ -234,43 +236,43 @@ def test_get_product_all_info(self):
234236
# ************ Test AV Function *************************
235237

236238
def test_get_alarm_record_config(self):
237-
print self.foscam.get_alarm_record_config()
239+
print(self.foscam.get_alarm_record_config())
238240

239241
def test_set_alarm_record_config(self):
240242
rc, old_args = self.foscam.get_alarm_record_config()
241243
new_args = {'isEnablePreRecord': 1,
242-
'alarmRecordSecs' : 240,
243-
'preRecordSecs' : 5
244-
}
244+
'alarmRecordSecs': 240,
245+
'preRecordSecs': 5
246+
}
245247
self.foscam.set_alarm_record_config(
246-
alarm_record_secs=new_args['alarmRecordSecs'],
247-
prerecord_secs=new_args['preRecordSecs'])
248+
alarm_record_secs=new_args['alarmRecordSecs'],
249+
prerecord_secs=new_args['preRecordSecs'])
248250

249251
rc, args = self.foscam.get_alarm_record_config()
250252
self.assertTrue(rc == 0)
251-
self.assertTrue(int(args['alarmRecordSecs']) == \
252-
new_args['alarmRecordSecs'])
253-
self.assertTrue(int(args['preRecordSecs']) == \
254-
new_args['preRecordSecs'])
253+
self.assertTrue(int(args['alarmRecordSecs']) ==
254+
new_args['alarmRecordSecs'])
255+
self.assertTrue(int(args['preRecordSecs']) ==
256+
new_args['preRecordSecs'])
255257

256258
self.foscam.set_alarm_record_config(
257-
alarm_record_secs=old_args['alarmRecordSecs'],
258-
prerecord_secs=old_args['preRecordSecs'])
259+
alarm_record_secs=old_args['alarmRecordSecs'],
260+
prerecord_secs=old_args['preRecordSecs'])
259261

260262
def test_get_local_alarm_record_config(self):
261263
rc, args = self.foscam.get_local_alarm_record_config()
262-
self.assertTrue(set(args) == set(['isEnableLocalAlarmRecord',
263-
'localAlarmRecordSecs']))
264+
self.assertTrue(set(args) == {'isEnableLocalAlarmRecord',
265+
'localAlarmRecordSecs'})
264266

265267
def test_set_local_alarm_recor_config(self):
266268
rc, args = self.foscam.set_local_alarm_record_config(
267-
is_enable_local_alarm_record=1,
268-
local_alarm_record_secs=60)
269+
is_enable_local_alarm_record=1,
270+
local_alarm_record_secs=60)
269271
self.assertTrue(rc == 0)
270272
rc, args = self.foscam.get_local_alarm_record_config()
271-
self.assertTrue(rc == 0 \
272-
and args['isEnableLocalAlarmRecord'] == '1' \
273-
and args['localAlarmRecordSecs'] == '60')
273+
self.assertTrue(all([rc == 0,
274+
args['isEnableLocalAlarmRecord'] == '1',
275+
args['localAlarmRecordSecs'] == '60']))
274276

275277
def test_get_h264_frm_ref_mode(self):
276278
rc, args = self.foscam.get_h264_frm_ref_mode()
@@ -286,24 +288,24 @@ def test_set_h264_frm_ref_mode(self):
286288
self.assertEqual(args['mode'], str(mode))
287289

288290
def test_get_schedule_record_config(self):
289-
all_args = set(['isEnable', 'isEnableAudio', 'recordLevel',
290-
'schedule0', 'schedule1', 'schedule2',
291-
'schedule3', 'schedule4', 'schedule5',
292-
'schedule6', 'spaceFullMode'])
291+
all_args = {'isEnable', 'isEnableAudio', 'recordLevel',
292+
'schedule0', 'schedule1', 'schedule2',
293+
'schedule3', 'schedule4', 'schedule5',
294+
'schedule6', 'spaceFullMode'}
293295
rc, args = self.foscam.get_schedule_record_config()
294296
self.assertTrue(rc == 0)
295297
self.assertTrue(set(args) == all_args)
296298

297299
def test_set_schedule_record_config(self):
298-
rc, args = self.foscam.set_schedule_record_config(
299-
is_enable=1, record_level=4,
300-
space_full_mode=0, is_enable_audio=0)
300+
rc, args = self.foscam.set_schedule_record_config(
301+
is_enable=1, record_level=4,
302+
space_full_mode=0, is_enable_audio=0)
301303
self.assertTrue(rc == 0)
302304

303305
def test_get_record_path(self):
304306
rc, args = self.foscam.get_record_path()
305307
self.assertTrue(rc == 0)
306-
self.assertTrue(set(args) == set(['path', 'free', 'total']))
308+
self.assertTrue(set(args) == {'path', 'free', 'total'})
307309

308310
def test_set_record_path(self):
309311
rc, args = self.foscam.get_record_path()
@@ -317,7 +319,7 @@ def test_get_ptz_preset_point_list(self):
317319

318320
# ******************* Other *****************************
319321
def test_unblocked_execute(self):
320-
self.foscam.daemon=True
322+
self.foscam.daemon = True
321323
self.foscam.ptz_move_up()
322324
sleep(0.5)
323325
self.foscam.ptz_stop_run()
@@ -328,23 +330,23 @@ def print_res(*args, **kwargs):
328330
f.write(str(args))
329331
f.write(str(kwargs))
330332

331-
self.foscam.daemon=True
333+
self.foscam.daemon = True
332334
self.foscam.get_ip_info(print_res)
333335
timeout = 10
334336
flag = False
335337
while timeout >= 0:
336338
try:
337-
with open('temp.txt', 'r') as f:
338-
self.assertTrue(open('temp.txt', 'r').read() != '')
339+
with open('temp.txt', 'r') as new_f:
340+
self.assertTrue(new_f.read() != '')
339341
flag = True
340342
break
341-
except Exception:
343+
except Exception as e:
344+
print(e)
342345
pass
343346
sleep(0.5)
344347
timeout -= 0.5
345348
self.assertTrue(flag)
346349

347-
348350
# *************** SnapPicture Function *******************
349351

350352
def test_snap_picture_2(self):
@@ -356,7 +358,6 @@ def test_snap_picture_2(self):
356358
fp.write(data)
357359
self.assertSequenceEqual(callback.args, (rc, data))
358360

359-
360361
# ********************** Misc ****************************
361362

362363
def test_get_log(self):
@@ -367,5 +368,6 @@ def test_get_log(self):
367368
self.assertTrue('log0' in args)
368369
self.assertSequenceEqual(callback.args, (rc, args))
369370

371+
370372
if __name__ == '__main__':
371373
unittest.main()

0 commit comments

Comments
 (0)