forked from studioimaginaire/phue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build framework for testing more complex API interactions
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
Showing
3 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters