From acf91e414f85eb7ee567bde81a33ce1407b5ff76 Mon Sep 17 00:00:00 2001 From: robmarkcole Date: Fri, 13 Oct 2017 09:16:00 +0100 Subject: [PATCH] Updates testing --- Testing and usage 13-10-2017.ipynb | 239 ++++++++++++++++++----------- 1 file changed, 149 insertions(+), 90 deletions(-) diff --git a/Testing and usage 13-10-2017.ipynb b/Testing and usage 13-10-2017.ipynb index cef9915..3b35acf 100644 --- a/Testing and usage 13-10-2017.ipynb +++ b/Testing and usage 13-10-2017.ipynb @@ -38,6 +38,7 @@ "import json\n", "import pprint\n", "import requests\n", + "import requests_mock\n", "from datetime import datetime, timedelta\n", "import hue_sensors as hs\n", "\n", @@ -87,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -183,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -202,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -220,7 +221,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -228,8 +229,32 @@ "output_type": "stream", "text": [ "Remote bedroom\n", + "Hall motion Sensor\n", + "Bedroom motion sensor\n", + "Robins iPhone\n", + "Living room motion sensor\n", + "Living room remote\n", + "Hall remote\n" + ] + } + ], + "source": [ + "for sensor in sensors:\n", + " print(sensor.name)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Living room remote\n", "1_click\n", - "{'last updated': ['2017-10-13', '05:57:49'], 'battery': 100}\n" + "{'last updated': ['2017-10-13', '05:34:33'], 'battery': 100}\n" ] } ], @@ -237,7 +262,8 @@ "for sensor in sensors:\n", " sensor.update()\n", "\n", - "s = sensors[0]\n", + "\n", + "s = sensors[5]\n", "print(s.name)\n", "print(s.state)\n", "print(s.device_state_attributes)" @@ -253,124 +279,157 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ - "response = requests.get(url)\n", - "data = hs.parse_hue_api_response(response.json())" + "# Load the saved raw API data\n", + "with open('tests/hue_sensors.json', 'r') as fp:\n", + " mock_data = json.load(fp)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Geofence': {'model': 'GEO', 'name': 'Robins iPhone', 'state': 'on'},\n", + " 'RWL_1e-02': {'battery': 100,\n", + " 'last_updated': ['2017-10-13', '05:34:33'],\n", + " 'model': 'RWL',\n", + " 'name': 'Living room remote',\n", + " 'state': '1_click'},\n", + " 'RWL_9c-02': {'battery': 100,\n", + " 'last_updated': ['2017-10-13', '05:36:46'],\n", + " 'model': 'RWL',\n", + " 'name': 'Hall remote',\n", + " 'state': '1_click'},\n", + " 'RWL_dc-02': {'battery': 100,\n", + " 'last_updated': ['2017-10-13', '06:01:04'],\n", + " 'model': 'RWL',\n", + " 'name': 'Remote bedroom',\n", + " 'state': '2_hold'},\n", + " 'SML_28-02': {'battery': 100,\n", + " 'dark': True,\n", + " 'daylight': False,\n", + " 'last_updated': ['2017-10-13', '06:46:19'],\n", + " 'light_level': 0,\n", + " 'lux': 1.0,\n", + " 'model': 'SML',\n", + " 'name': 'Hall motion Sensor',\n", + " 'state': 'off',\n", + " 'temperature': 19.42},\n", + " 'SML_9c-02': {'battery': 100,\n", + " 'dark': True,\n", + " 'daylight': False,\n", + " 'last_updated': ['2017-10-13', '07:04:16'],\n", + " 'light_level': 7325,\n", + " 'lux': 5.4,\n", + " 'model': 'SML',\n", + " 'name': 'Living room motion sensor',\n", + " 'state': 'off',\n", + " 'temperature': 23.08},\n", + " 'SML_ce-02': {'battery': 100,\n", + " 'dark': True,\n", + " 'daylight': False,\n", + " 'last_updated': ['2017-10-13', '06:04:20'],\n", + " 'light_level': 2464,\n", + " 'lux': 1.76,\n", + " 'model': 'SML',\n", + " 'name': 'Bedroom motion sensor',\n", + " 'state': 'off',\n", + " 'temperature': 20.17}}\n" + ] + } + ], + "source": [ + "print_json(hs.parse_hue_api_response(mock_data))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Mock object to hold mock data" + ] + }, + { + "cell_type": "code", + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ - "# Don't delete, used to capture data \n", - "#with open('hue_sensors.json', 'w') as fp:\n", - "# json.dump(data, fp)" + "class MockHueSensorData():\n", + " \"\"\"Class to hold mock_data.\"\"\"\n", + "\n", + " def __init__(self):\n", + " \"\"\"Initialize the mock object.\"\"\"" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "# Load the saved data\n", - "with open('tests/hue_sensors.json', 'r') as fp:\n", - " mock_data = json.load(fp)" + "mock_data_object = MockHueSensorData()" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "mock_data_object.data = hs.parse_hue_api_response(mock_data)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "{'Geofence': {'model': 'GEO', 'name': 'Robins iPhone', 'state': 'on'},\n", - " 'RWL_1e-02': {'battery': 100,\n", - " 'last_updated': ['2017-10-13', '05:34:33'],\n", - " 'model': 'RWL',\n", - " 'name': 'Living room remote',\n", - " 'state': '1_click'},\n", - " 'RWL_9c-02': {'battery': 100,\n", - " 'last_updated': ['2017-10-13', '05:36:46'],\n", - " 'model': 'RWL',\n", - " 'name': 'Hall remote',\n", - " 'state': '1_click'},\n", - " 'RWL_dc-02': {'battery': 100,\n", - " 'last_updated': ['2017-10-13', '06:01:04'],\n", - " 'model': 'RWL',\n", - " 'name': 'Remote bedroom',\n", - " 'state': '2_hold'},\n", - " 'SML_28-02': {'battery': 100,\n", - " 'dark': True,\n", - " 'daylight': False,\n", - " 'last_updated': ['2017-10-13', '05:57:40'],\n", - " 'light_level': 0,\n", - " 'lux': 1,\n", - " 'model': 'SML',\n", - " 'name': 'Hall motion Sensor',\n", - " 'state': 'off',\n", - " 'temperature': 19.42},\n", - " 'SML_9c-02': {'battery': 100,\n", - " 'dark': True,\n", - " 'daylight': False,\n", - " 'last_updated': ['2017-10-13', '06:01:07'],\n", - " 'light_level': 0,\n", - " 'lux': 1,\n", - " 'model': 'SML',\n", - " 'name': 'Living room motion sensor',\n", - " 'state': 'off',\n", - " 'temperature': 22.8},\n", - " 'SML_ce-02': {'battery': 100,\n", - " 'dark': True,\n", - " 'daylight': False,\n", - " 'last_updated': ['2017-10-13', '06:00:53'],\n", - " 'light_level': 0,\n", - " 'lux': 1,\n", - " 'model': 'SML',\n", - " 'name': 'Bedroom motion sensor',\n", - " 'state': 'on',\n", - " 'temperature': 20.17}}" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "RWL_dc-02\n", + "SML_28-02\n", + "SML_ce-02\n", + "Geofence\n", + "SML_9c-02\n", + "RWL_1e-02\n", + "RWL_9c-02\n" + ] } ], "source": [ - "mock_data" + "for key in mock_data_object.data.keys():\n", + " print(key)" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "class TestLondonTubeSensor(unittest.TestCase):\n", + "class TestSensor(unittest.TestCase):\n", " \n", " def test_setup_platform(self):\n", " \"\"\"Set up the Hue sensors.\"\"\"\n", - " import hue_sensors as hs \n", - " #url = hass.data[DOMAIN] + '/sensors'\n", - " hs.parse_hue_api_response = MagicMock(return_value=mock_data) # Mock the return\n", - " data = HueSensorData(url, hs.parse_hue_api_response)\n", - " data.update()\n", + " \n", " sensors = []\n", - " for key in data.data.keys():\n", - " sensors.append(HueSensor(key, data))\n", - " for sensor in sensors:\n", - " sensor.update()\n", + " for key in mock_data_object.data.keys():\n", + " sensors.append(HueSensor(key, mock_data_object))\n", "\n", - " hall_motion_sensor = sensors[0]\n", - " assert hall_motion_sensor.name == 'Remote bedroom'\n", + " bedroom_remote = sensors[0]\n", + " assert bedroom_remote.name == 'Remote bedroom'\n", + " assert bedroom_remote.state == '2_hold'\n", " \n", " hall_motion_sensor = sensors[1]\n", " assert hall_motion_sensor.name == 'Hall motion Sensor'" @@ -378,18 +437,18 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "test_setup_platform (__main__.TestLondonTubeSensor)\n", + "test_setup_platform (__main__.TestSensor)\n", "Set up the Hue sensors. ... ok\n", "\n", "----------------------------------------------------------------------\n", - "Ran 1 test in 0.317s\n", + "Ran 1 test in 0.001s\n", "\n", "OK\n" ] @@ -397,10 +456,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 15, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" }