Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
src/adafruit_circuitplayground
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamah committed Feb 24, 2020
1 parent e951947 commit 470282d
Showing 1 changed file with 124 additions and 9 deletions.
133 changes: 124 additions & 9 deletions src/common/test/test_debugger_communication_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import json # Remove
from unittest import mock
import socketio
import threading

from adafruit_circuitplayground import express
from .. import debugger_communication_client
from common import debugger_communication_client
from common import constants as CONSTANTS
from adafruit_circuitplayground.constants import CPX


class TestDebuggerCommunicationClient(object):
Expand All @@ -21,43 +24,155 @@ def test_init_connection1(self):
socketio.Client.connect.assert_called_once()

def test_update_state(self):
threading.Event.clear = mock.Mock()
threading.Event.wait = mock.Mock()
socketio.Client.emit = mock.Mock()
socketio.Client.emit.return_value = None
debugger_communication_client.update_state({})
debugger_communication_client.update_state(
{CONSTANTS.ACTIVE_DEVICE_FIELD: CPX, CONSTANTS.STATE_FIELD: {}}
)
socketio.Client.emit.assert_called_once()

@mock.patch.dict(
express.cpx._Express__state,
{"button_a": False, "button_b": False, "switch": True},
{
"brightness": 1.0,
"button_a": False,
"button_b": False,
"pixels": [
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
],
"red_led": False,
"switch": False,
"temperature": 0,
"light": 0,
"motion_x": 0,
"motion_y": 0,
"motion_z": 0,
"touch": [False] * 7,
"shake": False,
},
clear=True,
)
def test_button_press(self):
data = {"button_a": True, "button_b": True, "switch": True}
data = {
CONSTANTS.ACTIVE_DEVICE_FIELD: CPX,
CONSTANTS.STATE_FIELD: {"button_a": True, "button_b": True, "switch": True},
}
expected_data = {
"brightness": 1.0,
"button_a": True,
"button_b": True,
"pixels": [
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
],
"red_led": False,
"switch": True,
"temperature": 0,
"light": 0,
"motion_x": 0,
"motion_y": 0,
"motion_z": 0,
"touch": [False] * 7,
"shake": False,
}
serialized_data = json.dumps(data)
debugger_communication_client.input_changed(serialized_data)
assert data == express.cpx._Express__state
assert expected_data == express.cpx._Express__state

@mock.patch.dict(
express.cpx._Express__state,
{"temperature": 0, "light": 0, "motion_x": 0, "motion_y": 0, "motion_z": 0},
{
"brightness": 1.0,
"button_a": False,
"button_b": False,
"pixels": [
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
],
"red_led": False,
"switch": False,
"temperature": 0,
"light": 0,
"motion_x": 0,
"motion_y": 0,
"motion_z": 0,
"touch": [False] * 7,
"shake": False,
},
clear=True,
)
def test_sensor_changed(self):
def test_input_changed(self):
data = {
CONSTANTS.ACTIVE_DEVICE_FIELD: CPX,
CONSTANTS.STATE_FIELD: {
"temperature": 1,
"light": 2,
"motion_x": 3,
"motion_y": 4,
"motion_z": 5,
},
}
expected_data = {
"brightness": 1.0,
"button_a": False,
"button_b": False,
"pixels": [
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
(0, 0, 0),
],
"red_led": False,
"switch": False,
"temperature": 1,
"light": 2,
"motion_x": 3,
"motion_y": 4,
"motion_z": 5,
"touch": [False] * 7,
"shake": False,
}
serialized_data = json.dumps(data)
debugger_communication_client.input_changed(serialized_data)
assert data == express.cpx._Express__state
assert expected_data == express.cpx._Express__state

@mock.patch("builtins.print")
@mock.patch.dict(express.cpx._Express__state, {}, clear=True)
def test_update_api_state_fail(self, mocked_print):
data = []
debugger_communication_client.sensor_changed(data)
debugger_communication_client.input_changed(data)
# Exception is caught and a print is stated to stderr
mocked_print.assert_called_once()

0 comments on commit 470282d

Please sign in to comment.