Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kk7ds committed Nov 17, 2015
1 parent 033cb81 commit c5ffd20
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
packages=['uvcclient'],
scripts=['uvc'],
install_requires=[],
tests_require=['mock'],
)
35 changes: 35 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest

import mock

from uvcclient import uvcclient


class TestCliUtils(unittest.TestCase):
FAKE_ENV1 = {
'UVC': 'http://192.168.1.1:7080/?apiKey=foo',
}

FAKE_ENV2 = {
'UVC_HOST': '192.168.1.2',
'UVC_PORT': '7443',
'UVC_APIKEY': 'myKey',
}

@mock.patch('os.getenv')
def test_get_auth_combined(self, mock_getenv):
mock_getenv.side_effect = self.FAKE_ENV1.get
host, port, key, path = uvcclient.get_auth_from_env()
self.assertEqual('192.168.1.1', host)
self.assertEqual(7080, port)
self.assertEqual('foo', key)
self.assertEqual('/', path)

@mock.patch('os.getenv')
def test_get_separate(self, mock_getenv):
mock_getenv.side_effect = self.FAKE_ENV2.get
host, port, key, path = uvcclient.get_auth_from_env()
self.assertEqual('192.168.1.2', host)
self.assertEqual(7443, port)
self.assertEqual('myKey', key)
self.assertEqual('/', path)
121 changes: 121 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
try:
import httplib
except ImportError:
from http import client as httplib

import json
import unittest
import zlib

import mock

from uvcclient import uvcclient


class TestClientLowLevel(unittest.TestCase):
def setUp(self):
super(TestClientLowLevel, self).setUp()
self._patches = []
try:
import httplib
http_mock = mock.patch('httplib.HTTPConnection')
except ImportError:
http_mock = mock.patch('http.client.HTTPConnection')
http_mock.start()
self._patches.append(http_mock)

def cleanUp(self):
for i in self._patches:
i.stop()

def test_uvc_request_get(self):
client = uvcclient.UVCRemote('foo', 7080, 'key')
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 200
resp.read.return_value = json.dumps({}).encode()
client._uvc_request('/bar')
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, sdch',
}
conn.request.assert_called_once_with('GET', '/bar?apiKey=key',
None, headers)

def test_uvc_request_put(self):
client = uvcclient.UVCRemote('foo', 7080, 'key')
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 200
resp.read.return_value = json.dumps({}).encode()
result = client._uvc_request('/bar?foo=bar', method='PUT',
data='foobar')
self.assertEqual({}, result)
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, sdch',
}
conn.request.assert_called_once_with('PUT', '/bar?foo=bar&apiKey=key',
'foobar', headers)

def test_uvc_request_failed(self):
client = uvcclient.UVCRemote('foo', 7080, 'key')
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 404
result = client._uvc_request('/bar', method='PUT', data='foobar')
self.assertEqual(None, result)

def test_uvc_request_deflated(self):
client = uvcclient.UVCRemote('foo', 7080, 'key')
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 200
resp.read.return_value = zlib.compress(json.dumps({}).encode())
resp.getheaders.return_value = [('Content-Encoding', 'gzip')]
client._uvc_request('/bar')
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, sdch',
}
conn.request.assert_called_once_with('GET', '/bar?apiKey=key',
None, headers)


class TestClient(unittest.TestCase):
def test_set_recordmode(self):
fake_resp1 = {'data': [{'recordingSettings': {
'fullTimeRecordEnabled': False,
'motionRecordEnabled': False,
}}]}
fake_resp2 = {'data': [{'recordingSettings': {
'fullTimeRecordEnabled': True,
'motionRecordEnabled': False,
'channel': 1,
}}]}

def fake_req(path, method='GET', data=None):
if method == 'GET':
return fake_resp1
elif method == 'PUT':
self.assertEqual(json.dumps(fake_resp2['data'][0]), data)
return fake_resp2

client = uvcclient.UVCRemote('foo', 7080, 'key')
with mock.patch.object(client, '_uvc_request') as mock_r:
mock_r.side_effect = fake_req
client.set_recordmode('uuid', 'full', chan='medium')
self.assertTrue(mock_r.called)

fake_resp2['data'][0]['recordingSettings'] = {
'fullTimeRecordEnabled': False,
'motionRecordEnabled': True,
'channel': 0,
}
with mock.patch.object(client, '_uvc_request') as mock_r:
mock_r.side_effect = fake_req
client.set_recordmode('uuid', 'motion', chan='high')
self.assertTrue(mock_r.called)
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tox]
envlist = py27,py34
[testenv]
deps =
nose
mock
commands = nosetests
2 changes: 1 addition & 1 deletion uvcclient/uvcclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _uvc_request(self, path, method='GET', data=None,
data = resp.read()
if (headers.get('content-encoding') == 'gzip' or
headers.get('Content-Encoding') == 'gzip'):
data = zlib.decompress(data, 16 + zlib.MAX_WBITS)
data = zlib.decompress(data, 32 + zlib.MAX_WBITS)
return json.loads(data.decode())

def dump(self, uuid):
Expand Down

0 comments on commit c5ffd20

Please # to comment.