Skip to content

Commit

Permalink
build framework for testing more complex API interactions
Browse files Browse the repository at this point in the history
This builds a framework for having a set of canned test responses
based on HTTP Requests. It does this by monkey patching out
HTTPConnection, and redirecting it into a static multi level
dictionary. This allows complicated flows to be tested against known
sample data, and ensure that we're actually testing the various calls
back to the server.

A simple use of this in getting the data for one of the lights is
added. This will be expanded from here once it's accepted as an
approach.
  • Loading branch information
sdague committed Oct 27, 2016
1 parent d8e81a6 commit 7026ad9
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/fakes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import sys
import samples

if sys.version_info[0] > 2:
from io import BytesIO as StringIO
def dump(data):
return json.dumps(data).encode('utf-8')
else:
from StringIO import StringIO
def dump(data):
return json.dumps(data)


class Request(object):
def __init__(self, mode, addr, data):
self.mode = mode
self.addr = addr
self.data = data


class FakeHTTP(object):

def __init__(self, *args, **kwargs):
super(FakeHTTP, self).__init__()
self.call = None

def request(self, mode, addr, data=None):
self.call = Request(mode, addr, data)

def getresponse(self):
data = samples.RESP[self.call.mode][self.call.addr]
return StringIO(dump(data))

def close(self):
pass
211 changes: 211 additions & 0 deletions tests/samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
LIGHTS1 = {
u'1': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Living Room Bulb',
u'state': {u'alert': u'none',
u'bri': 254,
u'colormode': u'xy',
u'ct': 382,
u'effect': u'none',
u'hue': 14665,
u'on': True,
u'reachable': True,
u'sat': 156,
u'xy': [0.4677, 0.4121]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:d1:fd:53-0b'},
u'10': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Porch 4',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 500,
u'effect': u'none',
u'hue': 11004,
u'on': False,
u'reachable': True,
u'sat': 252,
u'xy': [0.5593, 0.406]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:ec:74:ab-0b'},
u'11': {u'manufacturername': u'Philips',
u'modelid': u'LST002',
u'name': u'Living Room light strips',
u'state': {u'alert': u'none',
u'bri': 254,
u'colormode': u'xy',
u'ct': 389,
u'effect': u'none',
u'hue': 64967,
u'on': True,
u'reachable': True,
u'sat': 79,
u'xy': [0.472, 0.353]},
u'swversion': u'5.50.2.19072',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:01:1a:22:cd-0b'},
u'12': {u'manufacturername': u'Philips',
u'modelid': u'LCT007',
u'name': u'Arwen Dresser',
u'state': {u'alert': u'none',
u'bri': 251,
u'colormode': u'xy',
u'ct': 403,
u'effect': u'none',
u'hue': 14314,
u'on': False,
u'reachable': True,
u'sat': 172,
u'xy': [0.4791, 0.4139]},
u'swversion': u'5.50.1.19085',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:10:47:9a:fe-0b'},
u'13': {u'manufacturername': u'Philips',
u'modelid': u'LCT007',
u'name': u'Arwen Changing Table',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 488,
u'effect': u'none',
u'hue': 12713,
u'on': False,
u'reachable': True,
u'sat': 222,
u'xy': [0.5224, 0.414]},
u'swversion': u'5.50.1.19085',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:10:31:ee:f0-0b'},
u'2': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Bedroom Susan',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 153,
u'effect': u'none',
u'hue': 52067,
u'on': False,
u'reachable': True,
u'sat': 237,
u'xy': [0.3089, 0.1334]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:f9:02:ae-0b'},
u'3': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Bedroom Sean',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 153,
u'effect': u'none',
u'hue': 52067,
u'on': False,
u'reachable': True,
u'sat': 237,
u'xy': [0.3089, 0.1334]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:fe:00:e3-0b'},
u'4': {u'manufacturername': u'Philips',
u'modelid': u'LLC010',
u'name': u'Living Room Iris',
u'state': {u'alert': u'none',
u'bri': 254,
u'colormode': u'xy',
u'effect': u'none',
u'hue': 58619,
u'on': True,
u'reachable': True,
u'sat': 81,
u'xy': [0.4715, 0.3499]},
u'swversion': u'5.23.1.13452',
u'type': u'Color light',
u'uniqueid': u'00:17:88:01:00:12:01:83-0b'},
u'5': {u'manufacturername': u'Philips',
u'modelid': u'LST001',
u'name': u'Living Room Dim Strip',
u'state': {u'alert': u'none',
u'bri': 254,
u'colormode': u'xy',
u'effect': u'none',
u'hue': 58619,
u'on': True,
u'reachable': True,
u'sat': 81,
u'xy': [0.4715, 0.3499]},
u'swversion': u'5.23.1.13452',
u'type': u'Color light',
u'uniqueid': u'00:17:88:01:00:cd:b2:03-0b'},
u'6': {u'manufacturername': u'Philips',
u'modelid': u'LLC011',
u'name': u'Arwen Flower',
u'state': {u'alert': u'none',
u'bri': 3,
u'colormode': u'xy',
u'effect': u'none',
u'hue': 64654,
u'on': True,
u'reachable': True,
u'sat': 249,
u'xy': [0.6855, 0.2927]},
u'swversion': u'5.23.1.13452',
u'type': u'Color light',
u'uniqueid': u'00:17:88:01:00:c5:23:7e-0b'},
u'7': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Porch Fan 1',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 500,
u'effect': u'none',
u'hue': 11004,
u'on': False,
u'reachable': True,
u'sat': 252,
u'xy': [0.5593, 0.406]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:fe:8a:df-0b'},
u'8': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Porch Fan 2',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 500,
u'effect': u'none',
u'hue': 11004,
u'on': False,
u'reachable': True,
u'sat': 252,
u'xy': [0.5593, 0.406]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:f9:88:37-0b'},
u'9': {u'manufacturername': u'Philips',
u'modelid': u'LCT001',
u'name': u'Porch 3',
u'state': {u'alert': u'none',
u'bri': 117,
u'colormode': u'xy',
u'ct': 500,
u'effect': u'none',
u'hue': 11004,
u'on': False,
u'reachable': True,
u'sat': 252,
u'xy': [0.5593, 0.406]},
u'swversion': u'5.23.1.13452',
u'type': u'Extended color light',
u'uniqueid': u'00:17:88:01:00:fe:88:17-0b'}}

RESP = dict(GET=dict(), POST=dict(), PUT=dict(), DELETE=dict())
RESP['GET']['/api/username/lights/'] = LIGHTS1
for key, value in LIGHTS1.items():
RESP['GET']['/api/username/lights/%s' % key] = LIGHTS1[key]
19 changes: 19 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
import fixtures
import mock
import os
import sys
import testtools

import phue
import fakes

if sys.version_info[0] > 2:
httplib = 'http.client.HTTPConnection'
else:
httplib = 'httplib.HTTPConnection'


class TestRequest(testtools.TestCase):
Expand Down Expand Up @@ -53,3 +60,15 @@ def test_register_unknown_user(self):
req.return_value = [{'error': {'type': 7}}]
self.assertRaises(phue.PhueException,
phue.Bridge, ip="10.0.0.0")


class TestLights(testtools.TestCase):

def setUp(self):
super(TestLights, self).setUp()
self.useFixture(fixtures.MonkeyPatch(httplib, fakes.FakeHTTP))
self.bridge = phue.Bridge(ip="10.0.0.0", username="username")

def test_get_lights(self):
lights = self.bridge.get_light_objects('id')
self.assertEqual(lights[1].name, "Living Room Bulb")

0 comments on commit 7026ad9

Please sign in to comment.