Skip to content

Commit

Permalink
More thorough testing of remote state anomalies
Browse files Browse the repository at this point in the history
  • Loading branch information
jinnatar committed Apr 14, 2018
1 parent eb56073 commit 9727221
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
13 changes: 10 additions & 3 deletions cozify/test/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import os, pytest, tempfile, datetime
import hashlib, json

from absl import logging
from cozify import config, hub
Expand Down Expand Up @@ -78,7 +79,7 @@ def offline_device():
def online_device():
dev = None
store = None
devs = hub.devices(capabilities=hub.capability.COLOR_HS)
devs = hub.devices(capabilities=hub.capability.BRIGHTNESS)
for i, d in devs.items():
if d['state']['reachable'] and 'test' in d['name']:
dev = d
Expand All @@ -89,9 +90,9 @@ def online_device():
logging.error(
'Cannot run certain device tests, no COLOR_HS device online where name includes \'test\'.'
)
logging.info('Stored state before yield: {0}'.format(store))
logging.info('online_device state before use ({1}): {0}'.format(store, _h6_dict(store)))
yield dev
logging.info('Rolling back state of test device')
logging.info('online_device state after use, before rollback ({1}): {0}'.format(dev['state'], _h6_dict(dev['state'])))
hub.device_state_replace(dev['id'], store)


Expand Down Expand Up @@ -123,3 +124,9 @@ def devices(self):

def states(self):
return dev.states

def _h6_dict(d):
j = json.dumps(d)
w = j.encode('utf8')
h = hashlib.md5(w)
return h.hexdigest()[:6]
12 changes: 8 additions & 4 deletions cozify/test/test_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ def test_hub_in_range():
@pytest.mark.destructive
@pytest.mark.remote
def test_hub_dirty_remote(live_hub):
# test if we are remote to get meaningful results
live_hub.ping(live_hub.default())
if not live_hub.remote(live_hub.default()):
pytest.xfail("Not remote, cannot run this test")

live_hub.remote(live_hub.default(), True)
assert live_hub.ping()
assert live_hub.remote(live_hub.default())
else:
# fuck up the state on purpose to say we're not remote
assert not live_hub.remote(live_hub.default(), False)
# attempt to repair the state
assert live_hub.ping()
# verify we're now considered to be remote
assert live_hub.remote(live_hub.default())
13 changes: 9 additions & 4 deletions cozify/test/test_hub_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def test_hub_device_toggle(live_hub, online_device):
def test_hub_device_on_off(live_hub, online_device):
if online_device['state']['isOn']:
live_hub.device_off(online_device['id'])
time.sleep(delay)
live_hub.device_on(online_device['id'])
else:
live_hub.device_on(online_device['id'])
time.sleep(delay)
Expand All @@ -98,14 +100,17 @@ def test_hub_device_state_replace(live_hub, online_device):

old_brightness = online_device['state']['brightness']
if old_brightness > 0.1:
set_brightness = old_brightness / 2
set_brightness = old_brightness - 0.1
else:
set_brightness = 1
set_brightness = 0.5
online_device['state']['brightness'] = set_brightness
online_device['state']['isOn'] = True
live_hub.device_state_replace(online_device['id'], online_device['state'])
time.sleep(delay)
devs = live_hub.devices()
new_brightness = devs[online_device['id']]['state']['brightness']
new_isOn = devs[online_device['id']]['state']['isOn']

assert new_brightness != old_brightness, 'brightness did not change'
assert new_brightness == set_brightness, 'brightness changed unexpectedly'
assert new_brightness != old_brightness, 'brightness did not change, expected {0}'.format(new_brightness)
assert new_brightness == set_brightness, 'brightness changed unexpectedly, expected {0}'.format(set_brightness)
assert new_isOn == True

0 comments on commit 9727221

Please sign in to comment.