From 5259563e84389609c630861dd2195f81550efb16 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Tue, 21 Jan 2020 11:30:50 -0800 Subject: [PATCH 01/21] Initial tests still a lot to do --- .gitignore | 1 + src/test_code/code.py | 19 ----- src/test_code/control.py | 38 ---------- .../adafruit_circuitplayground}/__init__.py | 0 .../adafruit_circuitplayground}/sample.mp4 | Bin .../adafruit_circuitplayground}/sample.wav | Bin .../test_express.py | 68 +++++++++++++----- test/adafruit_circuitplayground/test_pixel.py | 8 +++ test/adafruit_circuitplayground/test_utils.py | 24 +++++++ 9 files changed, 84 insertions(+), 74 deletions(-) delete mode 100644 src/test_code/code.py delete mode 100644 src/test_code/control.py rename {src/adafruit_circuitplayground/test => test/adafruit_circuitplayground}/__init__.py (100%) rename {src/adafruit_circuitplayground/test => test/adafruit_circuitplayground}/sample.mp4 (100%) rename {src/adafruit_circuitplayground/test => test/adafruit_circuitplayground}/sample.wav (100%) rename {src/adafruit_circuitplayground/test => test/adafruit_circuitplayground}/test_express.py (74%) create mode 100644 test/adafruit_circuitplayground/test_pixel.py create mode 100644 test/adafruit_circuitplayground/test_utils.py diff --git a/.gitignore b/.gitignore index 51a3df207..4b32bc3a0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ package.nls.*.json # testing .vscode-test +htmlcov # User-specific files *.suo diff --git a/src/test_code/code.py b/src/test_code/code.py deleted file mode 100644 index 369561cc5..000000000 --- a/src/test_code/code.py +++ /dev/null @@ -1,19 +0,0 @@ -from adafruit_circuitplayground.express import cpx -import time - -cpx.pixels.brightness = 0.3 -cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on! -cpx.pixels.show() - -while True: - if cpx.button_a: - cpx.pixels[2] = (0, 255, 0) - else: - cpx.pixels[2] = (0, 0, 0) - - if cpx.button_b: - cpx.pixels[7] = (0, 0, 255) - else: - cpx.pixels[7] = (0, 0, 0) - cpx.pixels.show() - diff --git a/src/test_code/control.py b/src/test_code/control.py deleted file mode 100644 index 1aceb880b..000000000 --- a/src/test_code/control.py +++ /dev/null @@ -1,38 +0,0 @@ -import sys -import json - -# Read data from stdin - - -def read_in(): - lines = sys.stdin.readlines() - # Since our input would only be having one line, parse our JSON data from that - return json.loads(lines[0]) - - -def main(): - # get our data as an array from read_in() - # lines = read_in() - - openCmd = { - 'pixels': [ - (0, 0, 255), - (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), - ], - 'button_a': False, - 'button_b': False, - } - print(json.dumps(openCmd)) - - -# start process -if __name__ == '__main__': - main() diff --git a/src/adafruit_circuitplayground/test/__init__.py b/test/adafruit_circuitplayground/__init__.py similarity index 100% rename from src/adafruit_circuitplayground/test/__init__.py rename to test/adafruit_circuitplayground/__init__.py diff --git a/src/adafruit_circuitplayground/test/sample.mp4 b/test/adafruit_circuitplayground/sample.mp4 similarity index 100% rename from src/adafruit_circuitplayground/test/sample.mp4 rename to test/adafruit_circuitplayground/sample.mp4 diff --git a/src/adafruit_circuitplayground/test/sample.wav b/test/adafruit_circuitplayground/sample.wav similarity index 100% rename from src/adafruit_circuitplayground/test/sample.wav rename to test/adafruit_circuitplayground/sample.wav diff --git a/src/adafruit_circuitplayground/test/test_express.py b/test/adafruit_circuitplayground/test_express.py similarity index 74% rename from src/adafruit_circuitplayground/test/test_express.py rename to test/adafruit_circuitplayground/test_express.py index a360aa4fb..f2f3672c8 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/test/adafruit_circuitplayground/test_express.py @@ -1,7 +1,11 @@ import pytest +from unittest import mock +import sys -from ..express import Express -from ..pixel import Pixel +import playsound + +from src.adafruit_circuitplayground.express import Express +from src.adafruit_circuitplayground.pixel import Pixel class TestExpress(object): @@ -19,6 +23,15 @@ def setup_method(self): self.pixels = Pixel(self.__state) self.__speaker_enabled = False + def test_acceleration(self): + self.cpx._Express__state["motion_x"] = 10 + self.cpx._Express__state["motion_y"] = -10 + self.cpx._Express__state["motion_z"] = -20 + accel = self.cpx.acceleration + assert accel[0] == 10 + assert accel[1] == -10 + assert accel[2] == -20 + def test_button_a(self): self.cpx._Express__state['button_a'] = True assert True == self.cpx.button_a @@ -43,21 +56,12 @@ def test_switch(self): self.cpx._Express__state['switch'] = True assert True == self.cpx.switch - def test_set_item_tuple(self): - self.cpx.pixels[0] = (255, 255, 255) - assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] + def test_temperature(self): + self.cpx._Express__state['temperature'] = 31 + assert 31 == self.cpx.temperature - def test_set_item_list(self): - self.cpx.pixels[0] = [255, 255, 255] - assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] - - def test_set_item_hex(self): - self.cpx.pixels[0] = 0xFFFFFF - assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] - - def test_set_item_invalid(self): - with pytest.raises(ValueError): - self.cpx.pixels[0] = "hello" + def test_light(self): + self.cpx._Express__state['light'] = 255 def test_shake(self): self.cpx._Express__state['shake'] = True @@ -91,10 +95,24 @@ def test_touch_A7(self): self.cpx._Express__state['touch'][6] = True assert True == self.cpx.touch_A7 - def test_play_file_mp4(self): + def test_play_file_mp_wrong_type(self): with pytest.raises(TypeError): self.cpx.play_file('sample.mp4') + # Mock playsound.playsound and check that it is called #TODO + @mock.patch('playsound.playsound') + def test_play_file_mp(self, mock_playsound): + if sys.platform == "win32": + mock_playsound.return_value = None + print(mock_playsound) + self.cpx.play_file("sample.wav") + assert True == playsound.playsound.called() + + # with mock.patch('playsound.playsound') as mock_playsound: + # self.cpx.play_file("sample.wav") + # mock_playsound.assert_called_with("sample.wav") + + # Pixels tests def test_fill(self): self.cpx.pixels.fill((0, 255, 0)) expected_pixels = [(0, 255, 0)] * 10 @@ -121,3 +139,19 @@ def test_extract_pixel_value_invalid_length(self): def test_extract_pixel_value_invalid_tuple_value(self): with pytest.raises(ValueError): self.cpx.pixels._Pixel__extract_pixel_value((0, 222, "hello")) + + def test_set_item_tuple(self): + self.cpx.pixels[0] = (255, 255, 255) + assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] + + def test_set_item_list(self): + self.cpx.pixels[0] = [255, 255, 255] + assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] + + def test_set_item_hex(self): + self.cpx.pixels[0] = 0xFFFFFF + assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] + + def test_set_item_invalid(self): + with pytest.raises(ValueError): + self.cpx.pixels[0] = "hello" diff --git a/test/adafruit_circuitplayground/test_pixel.py b/test/adafruit_circuitplayground/test_pixel.py new file mode 100644 index 000000000..cb090774f --- /dev/null +++ b/test/adafruit_circuitplayground/test_pixel.py @@ -0,0 +1,8 @@ +import pytest + +from src.adafruit_circuitplayground.pixel import Pixel + +class TestPixel(object): + + def setup_method(self): + self.pixel = Pixel() \ No newline at end of file diff --git a/test/adafruit_circuitplayground/test_utils.py b/test/adafruit_circuitplayground/test_utils.py new file mode 100644 index 000000000..2eb6d9418 --- /dev/null +++ b/test/adafruit_circuitplayground/test_utils.py @@ -0,0 +1,24 @@ +import sys + +import pytest +from unittest import mock + +from src.adafruit_circuitplayground import utils + +class TestUtils(object): + + def test_remove_leading_slashes(self): + original = "///a//b/" + expected = "a//b/" + assert expected == utils.remove_leading_slashes(original) + + def test_escape_if_OSX_notOSX(self): + original = "a b" + assert original == utils.escape_if_OSX(original) + + def test_escape_if_OSX_isOSX(self): + utils.sys = mock.MagicMock() + utils.sys.configure_mock(platform='darwin') + original = "a b" + expected = "a%20b" + assert expected == utils.escape_if_OSX(original) \ No newline at end of file From fda4edc41b1437efc73726bbb9335c5fe9e0cc78 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Tue, 21 Jan 2020 11:36:21 -0800 Subject: [PATCH 02/21] Updated package.json script for python tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f0d1b4744..0a6a938d2 100644 --- a/package.json +++ b/package.json @@ -279,7 +279,7 @@ "pretest": "npm run compile", "test": "npm-run-all test:*", "test:extension-tests": "node ./out/test/runTest.js", - "test:api-tests": "pytest src", + "test:api-tests": "pytest test", "lint": "tslint -c tslint.json src/**/*.{ts,tsx}", "format": "prettier --write src/**/*.{ts,tsx}", "tslint-check": "tslint-config-prettier-check ./tslint.json", From c652f0891ab8a5c542c1c845341731261474869e Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Tue, 21 Jan 2020 13:58:10 -0800 Subject: [PATCH 03/21] Purposely made error to test pipeline --- src/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 0a523c3f8..85de1fa0f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -68,7 +68,7 @@ export async function activate(context: vscode.ExtensionContext) { // Add our library path to settings.json for autocomplete functionality updatePythonExtraPaths(); - pythonExecutableName = await utils.setPythonExectuableName(); + pythonExecutdableName = await utils.setPythonExectuableName(); await utils.checkPythonDependencies(context, pythonExecutableName) From d2b16ff6cd6efb4a4b8fff90002b84e365694a14 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Tue, 21 Jan 2020 14:08:53 -0800 Subject: [PATCH 04/21] Corrected spelling error --- src/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 85de1fa0f..0a523c3f8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -68,7 +68,7 @@ export async function activate(context: vscode.ExtensionContext) { // Add our library path to settings.json for autocomplete functionality updatePythonExtraPaths(); - pythonExecutdableName = await utils.setPythonExectuableName(); + pythonExecutableName = await utils.setPythonExectuableName(); await utils.checkPythonDependencies(context, pythonExecutableName) From ab18b47ddf01405d4508ba536986ec098602732e Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 10:18:51 -0800 Subject: [PATCH 05/21] commented out broken test --- test/adafruit_circuitplayground/test_express.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/adafruit_circuitplayground/test_express.py b/test/adafruit_circuitplayground/test_express.py index f2f3672c8..fa9cfa7b8 100644 --- a/test/adafruit_circuitplayground/test_express.py +++ b/test/adafruit_circuitplayground/test_express.py @@ -100,13 +100,13 @@ def test_play_file_mp_wrong_type(self): self.cpx.play_file('sample.mp4') # Mock playsound.playsound and check that it is called #TODO - @mock.patch('playsound.playsound') - def test_play_file_mp(self, mock_playsound): - if sys.platform == "win32": - mock_playsound.return_value = None - print(mock_playsound) - self.cpx.play_file("sample.wav") - assert True == playsound.playsound.called() + # @mock.patch('playsound.playsound') + # def test_play_file_mp(self, mock_playsound): + # if sys.platform == "win32": + # mock_playsound.return_value = None + # print(mock_playsound) + # self.cpx.play_file("sample.wav") + # assert True == playsound.playsound.called() # with mock.patch('playsound.playsound') as mock_playsound: # self.cpx.play_file("sample.wav") From bca1b23e51164cef198f0416ff2108904949f23c Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 17:31:20 -0800 Subject: [PATCH 06/21] updated tests --- package.json | 2 +- .../test}/__init__.py | 0 .../test}/sample.mp4 | Bin .../test}/sample.wav | Bin .../test_debugger_communication_client.py | 15 +++ .../test}/test_express.py | 29 +++-- .../test/test_pixel.py | 110 ++++++++++++++++++ .../test}/test_utils.py | 2 +- src/adafruit_circuitplayground/utils.py | 4 +- src/test/runTest.ts | 2 +- test/adafruit_circuitplayground/test_pixel.py | 8 -- 11 files changed, 149 insertions(+), 23 deletions(-) rename {test/adafruit_circuitplayground => src/adafruit_circuitplayground/test}/__init__.py (100%) rename {test/adafruit_circuitplayground => src/adafruit_circuitplayground/test}/sample.mp4 (100%) rename {test/adafruit_circuitplayground => src/adafruit_circuitplayground/test}/sample.wav (100%) create mode 100644 src/adafruit_circuitplayground/test/test_debugger_communication_client.py rename {test/adafruit_circuitplayground => src/adafruit_circuitplayground/test}/test_express.py (86%) create mode 100644 src/adafruit_circuitplayground/test/test_pixel.py rename {test/adafruit_circuitplayground => src/adafruit_circuitplayground/test}/test_utils.py (92%) delete mode 100644 test/adafruit_circuitplayground/test_pixel.py diff --git a/package.json b/package.json index 0a6a938d2..f0d1b4744 100644 --- a/package.json +++ b/package.json @@ -279,7 +279,7 @@ "pretest": "npm run compile", "test": "npm-run-all test:*", "test:extension-tests": "node ./out/test/runTest.js", - "test:api-tests": "pytest test", + "test:api-tests": "pytest src", "lint": "tslint -c tslint.json src/**/*.{ts,tsx}", "format": "prettier --write src/**/*.{ts,tsx}", "tslint-check": "tslint-config-prettier-check ./tslint.json", diff --git a/test/adafruit_circuitplayground/__init__.py b/src/adafruit_circuitplayground/test/__init__.py similarity index 100% rename from test/adafruit_circuitplayground/__init__.py rename to src/adafruit_circuitplayground/test/__init__.py diff --git a/test/adafruit_circuitplayground/sample.mp4 b/src/adafruit_circuitplayground/test/sample.mp4 similarity index 100% rename from test/adafruit_circuitplayground/sample.mp4 rename to src/adafruit_circuitplayground/test/sample.mp4 diff --git a/test/adafruit_circuitplayground/sample.wav b/src/adafruit_circuitplayground/test/sample.wav similarity index 100% rename from test/adafruit_circuitplayground/sample.wav rename to src/adafruit_circuitplayground/test/sample.wav diff --git a/src/adafruit_circuitplayground/test/test_debugger_communication_client.py b/src/adafruit_circuitplayground/test/test_debugger_communication_client.py new file mode 100644 index 000000000..b8773825b --- /dev/null +++ b/src/adafruit_circuitplayground/test/test_debugger_communication_client.py @@ -0,0 +1,15 @@ +import pytest +import debugger_communication_client + +@pytest.mark.parametrize("data, expected_events, updated_expected_events", [("js", [], [])]) +def test_update_api_state_fail(data, expected_events, updated_expected_events): + debugger_communication_client.__update_api_state(data, expected_events) + assert expected_events == updated_expected_events + +# @pytest.mark.parametrize("data, expected_events, updated_expected_events", +# [(" ", [], []), +# ]) +def test_update_api_state(data, expected_events, updated_expected_events): + express = mock.MagicMock() + debugger_communication_client.__update_api_state(data, expected_events) + assert expected_events == updated_expected_events diff --git a/test/adafruit_circuitplayground/test_express.py b/src/adafruit_circuitplayground/test/test_express.py similarity index 86% rename from test/adafruit_circuitplayground/test_express.py rename to src/adafruit_circuitplayground/test/test_express.py index fa9cfa7b8..6e2d4a9c4 100644 --- a/test/adafruit_circuitplayground/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -2,10 +2,10 @@ from unittest import mock import sys -import playsound +from playsound import playsound -from src.adafruit_circuitplayground.express import Express -from src.adafruit_circuitplayground.pixel import Pixel +from express import Express +from pixel import Pixel class TestExpress(object): @@ -40,6 +40,15 @@ def test_button_b(self): self.cpx._Express__state['button_b'] = True assert True == self.cpx.button_b + def test_taps(self): + self.cpx._Express__state["detect_taps"] = 2 + assert 2 == self.cpx.detect_taps + + @pytest.mark.parametrize("taps, expected", [(1, 1), (2, 2), (3, 1)]) + def test_taps_setter(self, taps, expected): + self.cpx.detect_taps = taps + assert expected == self.cpx.detect_taps + def test_red_led(self): self.cpx._Express__state['red_led'] = True assert True == self.cpx.red_led @@ -62,6 +71,7 @@ def test_temperature(self): def test_light(self): self.cpx._Express__state['light'] = 255 + assert 255 == self.cpx.light def test_shake(self): self.cpx._Express__state['shake'] = True @@ -99,14 +109,13 @@ def test_play_file_mp_wrong_type(self): with pytest.raises(TypeError): self.cpx.play_file('sample.mp4') - # Mock playsound.playsound and check that it is called #TODO + # TODO Mock playsound.playsound and check that it is called # @mock.patch('playsound.playsound') - # def test_play_file_mp(self, mock_playsound): - # if sys.platform == "win32": - # mock_playsound.return_value = None - # print(mock_playsound) - # self.cpx.play_file("sample.wav") - # assert True == playsound.playsound.called() + def test_play_file_mp(self):#, mock_playsound): + playsound = mock.Mock(return_value="mocked stuff") + if sys.platform == "win32": + self.cpx.play_file("sample.wav") + assert True == playsound.called() # with mock.patch('playsound.playsound') as mock_playsound: # self.cpx.play_file("sample.wav") diff --git a/src/adafruit_circuitplayground/test/test_pixel.py b/src/adafruit_circuitplayground/test/test_pixel.py new file mode 100644 index 000000000..6bf8cd5d0 --- /dev/null +++ b/src/adafruit_circuitplayground/test/test_pixel.py @@ -0,0 +1,110 @@ +import pytest + +from pixel import Pixel + +class TestPixel(object): + + def setup_method(self): + self.pixel = Pixel( + { + 'brightness': 1.0, + 'button_a': False, + 'button_b': False, + 'pixels': [(255, 0, 0), (0, 255, 0), (0, 0, 255)], + 'red_led': False, + 'switch': False + }) + + @pytest.mark.parametrize("debug_mode", [True, False]) + def test_set_debug_mode(self, debug_mode): + self.pixel._Pixel__set_debug_mode(debug_mode) + assert debug_mode == self.pixel._Pixel__debug_mode + + def test_get_item_out_of_bounds(self): + with pytest.raises(IndexError): + p = self.pixel[3] + + def test_get_item(self): + assert (0, 0, 255) == self.pixel[2] + + def test_get_item_splice(self): + assert [(255, 0, 0), (0, 255, 0)] == self.pixel[0:2] + + def test_set_item(self): + self.pixel[1] = (50, 50, 50) + assert (50, 50, 50) == self.pixel[1] + + def test_set_item_splice(self): + self.pixel[0:1] = [(100, 100, 100), (0, 0, 100)] + assert (100, 100, 100) == self.pixel[0] + assert (0, 0, 100) == self.pixel[1] + + def test_set_item_out_of_bounds(self): + with pytest.raises(IndexError): + self.pixel[3] = (0, 0, 0) + + def test_len(self): + assert 3 == len(self.pixel) + + @pytest.mark.parametrize("index, expected", [(0, True), (3, False)]) + def test_valid_index(self, index, expected): + assert self.pixel._Pixel__valid_index(index) == expected + + def test_fill(self): + self.pixel.fill((123, 123, 123)) + assert True == all(p == (123, 123, 123) for p in self.pixel._Pixel__state["pixels"]) + + @pytest.mark.parametrize("val, expected", + [([3, 4, 5], (3, 4, 5)), + (432, (0, 1, 176)), + ((1, 2, 3), (1, 2, 3))]) + def test_extract_pixel_values_not_slice(self, val, expected): + assert expected == self.pixel._Pixel__extract_pixel_value(val) + + @pytest.mark.parametrize("val, expected", + [([[3, 4, 5], [6, 7, 8]], [(3, 4, 5), (6, 7, 8)]), + ([444555, 666777], [(6, 200, 139), (10, 44, 153)]), + ([(10, 10, 10), (20, 20, 20)], [(10, 10, 10), (20, 20, 20)])]) + def test_extract_pixel_values_slice(self, val, expected): + assert expected == self.pixel._Pixel__extract_pixel_value(val, is_slice=True) + + @pytest.mark.parametrize("val", [[1, 2, 3, 4], [1, 2], 0.3]) + def test_extract_pixel_values_fail(self, val): + with pytest.raises(ValueError): + self.pixel._Pixel__extract_pixel_value(val) + + def test_hex_to_rgb_fail(self): + with pytest.raises(ValueError): + self.pixel._Pixel__hex_to_rgb("x") + + @pytest.mark.parametrize("hex, expected", + [("0xffffff", (255, 255, 255)), + ("0x0", (0, 0, 0)), + ("0xff0000", (255, 0, 0)), + ("0xabcdef", (171, 205, 239))]) + def test_hex_to_rgb(self, hex, expected): + assert expected == self.pixel._Pixel__hex_to_rgb(hex) + + @pytest.mark.parametrize("pixValue, expected", + [(0, True), + (200, True), + (255, True), + (-1, False), + (256, False), + ("1", False)]) + def test_valid_rgb_value(self, pixValue, expected): + assert expected == self.pixel._Pixel__valid_rgb_value(pixValue) + + def test_get_brightness(self): + self.pixel._Pixel__state['brightness'] = 0.4 + assert 0.4 == self.pixel.brightness + + @pytest.mark.parametrize("brightness", [-0.1, 1.1]) + def test_set_brightness_fail(self, brightness): + with pytest.raises(ValueError): + self.pixel.brightness = brightness + + @pytest.mark.parametrize("brightness", [0, 1, 0.4, 0.333]) + def test_set_brightness(self, brightness): + self.pixel.brightness = brightness + assert True == self.pixel._Pixel__valid_brightness(brightness) \ No newline at end of file diff --git a/test/adafruit_circuitplayground/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py similarity index 92% rename from test/adafruit_circuitplayground/test_utils.py rename to src/adafruit_circuitplayground/test/test_utils.py index 2eb6d9418..5f1a68924 100644 --- a/test/adafruit_circuitplayground/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -3,7 +3,7 @@ import pytest from unittest import mock -from src.adafruit_circuitplayground import utils +import utils class TestUtils(object): diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index 83a595d3b..21d6f1d1a 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -20,8 +20,8 @@ def show(state, debug_mode=False): if debug_mode: debugger_communication_client.update_state(json.dumps(message)) else: - print(json.dumps(message) + '\0', end='', - file=sys.__stdout__, flush=True) + # print(json.dumps(message) + '\0', end='', + # file=sys.__stdout__, flush=True) time.sleep(CONSTANTS.TIME_DELAY) diff --git a/src/test/runTest.ts b/src/test/runTest.ts index a3e10548d..093527df7 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -6,7 +6,7 @@ async function main() { try { // The folder containing the Extension Manifest package.json // Passed to `--extensionDevelopmentPath` - const extensionDevelopmentPath = path.resolve(__dirname, '../../'); + const extensionDevelopmentPath = path.resolve(__dirname, '../src'); // The path to the extension test script // Passed to --extensionTestsPath diff --git a/test/adafruit_circuitplayground/test_pixel.py b/test/adafruit_circuitplayground/test_pixel.py deleted file mode 100644 index cb090774f..000000000 --- a/test/adafruit_circuitplayground/test_pixel.py +++ /dev/null @@ -1,8 +0,0 @@ -import pytest - -from src.adafruit_circuitplayground.pixel import Pixel - -class TestPixel(object): - - def setup_method(self): - self.pixel = Pixel() \ No newline at end of file From 5b9db94d34e8fce55cbe913210271ce20e5e12f8 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 17:35:33 -0800 Subject: [PATCH 07/21] updated tests --- .../test/test_debugger_communication_client.py | 15 --------------- .../test/test_express.py | 12 ------------ 2 files changed, 27 deletions(-) delete mode 100644 src/adafruit_circuitplayground/test/test_debugger_communication_client.py diff --git a/src/adafruit_circuitplayground/test/test_debugger_communication_client.py b/src/adafruit_circuitplayground/test/test_debugger_communication_client.py deleted file mode 100644 index b8773825b..000000000 --- a/src/adafruit_circuitplayground/test/test_debugger_communication_client.py +++ /dev/null @@ -1,15 +0,0 @@ -import pytest -import debugger_communication_client - -@pytest.mark.parametrize("data, expected_events, updated_expected_events", [("js", [], [])]) -def test_update_api_state_fail(data, expected_events, updated_expected_events): - debugger_communication_client.__update_api_state(data, expected_events) - assert expected_events == updated_expected_events - -# @pytest.mark.parametrize("data, expected_events, updated_expected_events", -# [(" ", [], []), -# ]) -def test_update_api_state(data, expected_events, updated_expected_events): - express = mock.MagicMock() - debugger_communication_client.__update_api_state(data, expected_events) - assert expected_events == updated_expected_events diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index 6e2d4a9c4..1621e1a06 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -109,18 +109,6 @@ def test_play_file_mp_wrong_type(self): with pytest.raises(TypeError): self.cpx.play_file('sample.mp4') - # TODO Mock playsound.playsound and check that it is called - # @mock.patch('playsound.playsound') - def test_play_file_mp(self):#, mock_playsound): - playsound = mock.Mock(return_value="mocked stuff") - if sys.platform == "win32": - self.cpx.play_file("sample.wav") - assert True == playsound.called() - - # with mock.patch('playsound.playsound') as mock_playsound: - # self.cpx.play_file("sample.wav") - # mock_playsound.assert_called_with("sample.wav") - # Pixels tests def test_fill(self): self.cpx.pixels.fill((0, 255, 0)) From ffce53e935f7b282ee0e279dc3820412b36ebf61 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 19:40:24 -0800 Subject: [PATCH 08/21] Updated imports --- src/adafruit_circuitplayground/test/test_express.py | 4 ++-- src/adafruit_circuitplayground/test/test_pixel.py | 3 ++- src/adafruit_circuitplayground/test/test_utils.py | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index 1621e1a06..8e3d9e378 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -4,8 +4,8 @@ from playsound import playsound -from express import Express -from pixel import Pixel +from ..express import Express +from ..pixel import Pixel class TestExpress(object): diff --git a/src/adafruit_circuitplayground/test/test_pixel.py b/src/adafruit_circuitplayground/test/test_pixel.py index 6bf8cd5d0..7bc4a1290 100644 --- a/src/adafruit_circuitplayground/test/test_pixel.py +++ b/src/adafruit_circuitplayground/test/test_pixel.py @@ -1,6 +1,7 @@ import pytest -from pixel import Pixel +from ..pixel import Pixel + class TestPixel(object): diff --git a/src/adafruit_circuitplayground/test/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py index 5f1a68924..475138b8d 100644 --- a/src/adafruit_circuitplayground/test/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -3,7 +3,8 @@ import pytest from unittest import mock -import utils +import ..utils + class TestUtils(object): From 8c6e95df5f3e9547d5d5a34dfe0bd8a32ff9948c Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 19:52:48 -0800 Subject: [PATCH 09/21] updated imports --- src/adafruit_circuitplayground/test/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adafruit_circuitplayground/test/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py index 475138b8d..c021e4856 100644 --- a/src/adafruit_circuitplayground/test/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -3,7 +3,7 @@ import pytest from unittest import mock -import ..utils +from .. import utils class TestUtils(object): From c452cb21d30dff71b3a9815faa0080ff6169389b Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 20:10:34 -0800 Subject: [PATCH 10/21] Removed comments and other accidental changes --- .gitignore | 1 - src/adafruit_circuitplayground/test/test_express.py | 3 --- src/adafruit_circuitplayground/test/test_utils.py | 2 +- src/adafruit_circuitplayground/utils.py | 4 ++-- src/test/runTest.ts | 2 +- 5 files changed, 4 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 4b32bc3a0..51a3df207 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ package.nls.*.json # testing .vscode-test -htmlcov # User-specific files *.suo diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index 8e3d9e378..dbca3cc20 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -1,8 +1,5 @@ import pytest from unittest import mock -import sys - -from playsound import playsound from ..express import Express from ..pixel import Pixel diff --git a/src/adafruit_circuitplayground/test/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py index c021e4856..2df4a0467 100644 --- a/src/adafruit_circuitplayground/test/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -22,4 +22,4 @@ def test_escape_if_OSX_isOSX(self): utils.sys.configure_mock(platform='darwin') original = "a b" expected = "a%20b" - assert expected == utils.escape_if_OSX(original) \ No newline at end of file + assert expected == utils.escape_if_OSX(original) diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index 21d6f1d1a..83a595d3b 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -20,8 +20,8 @@ def show(state, debug_mode=False): if debug_mode: debugger_communication_client.update_state(json.dumps(message)) else: - # print(json.dumps(message) + '\0', end='', - # file=sys.__stdout__, flush=True) + print(json.dumps(message) + '\0', end='', + file=sys.__stdout__, flush=True) time.sleep(CONSTANTS.TIME_DELAY) diff --git a/src/test/runTest.ts b/src/test/runTest.ts index 093527df7..a3e10548d 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -6,7 +6,7 @@ async function main() { try { // The folder containing the Extension Manifest package.json // Passed to `--extensionDevelopmentPath` - const extensionDevelopmentPath = path.resolve(__dirname, '../src'); + const extensionDevelopmentPath = path.resolve(__dirname, '../../'); // The path to the extension test script // Passed to --extensionTestsPath From aad0cc36a760e3b8289ba121448b3b9244431bed Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Wed, 22 Jan 2020 20:19:56 -0800 Subject: [PATCH 11/21] removed unused import --- src/adafruit_circuitplayground/test/test_express.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index dbca3cc20..3ad69ace3 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -1,5 +1,4 @@ import pytest -from unittest import mock from ..express import Express from ..pixel import Pixel From bd87e99eca5a7a4e1ffd48828010b5e622e9607e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 22 Jan 2020 23:07:06 -0800 Subject: [PATCH 12/21] removed unused import --- src/adafruit_circuitplayground/test/test_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/adafruit_circuitplayground/test/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py index 2df4a0467..2e598a67d 100644 --- a/src/adafruit_circuitplayground/test/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -1,6 +1,5 @@ import sys -import pytest from unittest import mock from .. import utils From db53ed8a01ccb71c4a9f12ddb293b352784272d8 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Thu, 23 Jan 2020 11:27:14 -0800 Subject: [PATCH 13/21] Updated tests from comments --- .../test/test_express.py | 81 +++++-------------- .../test/test_pixel.py | 10 ++- .../test/test_utils.py | 13 ++- src/adafruit_circuitplayground/utils.py | 4 +- 4 files changed, 38 insertions(+), 70 deletions(-) diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index 3ad69ace3..a40d34d51 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -20,9 +20,12 @@ def setup_method(self): self.__speaker_enabled = False def test_acceleration(self): - self.cpx._Express__state["motion_x"] = 10 - self.cpx._Express__state["motion_y"] = -10 - self.cpx._Express__state["motion_z"] = -20 + mock_motion_x = 10 + mock_motion_y = -10 + mock_motion_z = -20 + self.cpx._Express__state["motion_x"] = mock_motion_x + self.cpx._Express__state["motion_y"] = mock_motion_y + self.cpx._Express__state["motion_z"] = mock_motion_z accel = self.cpx.acceleration assert accel[0] == 10 assert accel[1] == -10 @@ -30,11 +33,11 @@ def test_acceleration(self): def test_button_a(self): self.cpx._Express__state['button_a'] = True - assert True == self.cpx.button_a + assert self.cpx.button_a def test_button_b(self): self.cpx._Express__state['button_b'] = True - assert True == self.cpx.button_b + assert self.cpx.button_b def test_taps(self): self.cpx._Express__state["detect_taps"] = 2 @@ -47,19 +50,19 @@ def test_taps_setter(self, taps, expected): def test_red_led(self): self.cpx._Express__state['red_led'] = True - assert True == self.cpx.red_led + assert self.cpx.red_led def test_red_led_int(self): self.cpx.red_led = 3 - assert True == self.cpx.red_led + assert self.cpx.red_led def test_red_led_string(self): self.cpx.red_led = 'foo' - assert True == self.cpx.red_led + assert self.cpx.red_led def test_switch(self): self.cpx._Express__state['switch'] = True - assert True == self.cpx.switch + assert self.cpx.switch def test_temperature(self): self.cpx._Express__state['temperature'] = 31 @@ -71,80 +74,36 @@ def test_light(self): def test_shake(self): self.cpx._Express__state['shake'] = True - assert True == self.cpx.shake() + assert self.cpx.shake() def test_touch_A1(self): self.cpx._Express__state['touch'][0] = True - assert True == self.cpx.touch_A1 + assert self.cpx.touch_A1 def test_touch_A2(self): self.cpx._Express__state['touch'][1] = True - assert True == self.cpx.touch_A2 + assert self.cpx.touch_A2 def test_touch_A3(self): self.cpx._Express__state['touch'][2] = True - assert True == self.cpx.touch_A3 + assert self.cpx.touch_A3 def test_touch_A4(self): self.cpx._Express__state['touch'][3] = True - assert True == self.cpx.touch_A4 + assert self.cpx.touch_A4 def test_touch_A5(self): self.cpx._Express__state['touch'][4] = True - assert True == self.cpx.touch_A5 + assert self.cpx.touch_A5 def test_touch_A6(self): self.cpx._Express__state['touch'][5] = True - assert True == self.cpx.touch_A6 + assert self.cpx.touch_A6 def test_touch_A7(self): self.cpx._Express__state['touch'][6] = True - assert True == self.cpx.touch_A7 + assert self.cpx.touch_A7 def test_play_file_mp_wrong_type(self): with pytest.raises(TypeError): self.cpx.play_file('sample.mp4') - - # Pixels tests - def test_fill(self): - self.cpx.pixels.fill((0, 255, 0)) - expected_pixels = [(0, 255, 0)] * 10 - assert expected_pixels == self.cpx._Express__state['pixels'] - - def test_extract_pixel_value_list(self): - assert (0, 255, 0) == self.cpx.pixels._Pixel__extract_pixel_value((0, 255, 0)) - - def test_extract_pixel_value_list1(self): - assert (123, 123, 123) == self.cpx.pixels._Pixel__extract_pixel_value( - [123, 123, 123]) - - def test_extract_pixel_value_int(self): - assert (0, 0, 255) == self.cpx.pixels._Pixel__extract_pixel_value(255) - - def test_extract_pixel_value_tuple(self): - assert (0, 255, 0) == self.cpx.pixels._Pixel__extract_pixel_value( - (0, 255, 0)) - - def test_extract_pixel_value_invalid_length(self): - with pytest.raises(ValueError): - self.cpx.pixels._Pixel__extract_pixel_value((1, 2, 3, 4)) - - def test_extract_pixel_value_invalid_tuple_value(self): - with pytest.raises(ValueError): - self.cpx.pixels._Pixel__extract_pixel_value((0, 222, "hello")) - - def test_set_item_tuple(self): - self.cpx.pixels[0] = (255, 255, 255) - assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] - - def test_set_item_list(self): - self.cpx.pixels[0] = [255, 255, 255] - assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] - - def test_set_item_hex(self): - self.cpx.pixels[0] = 0xFFFFFF - assert (255, 255, 255) == self.cpx._Express__state['pixels'][0] - - def test_set_item_invalid(self): - with pytest.raises(ValueError): - self.cpx.pixels[0] = "hello" diff --git a/src/adafruit_circuitplayground/test/test_pixel.py b/src/adafruit_circuitplayground/test/test_pixel.py index 7bc4a1290..aa919872a 100644 --- a/src/adafruit_circuitplayground/test/test_pixel.py +++ b/src/adafruit_circuitplayground/test/test_pixel.py @@ -43,6 +43,10 @@ def test_set_item_splice(self): def test_set_item_out_of_bounds(self): with pytest.raises(IndexError): self.pixel[3] = (0, 0, 0) + + def test_set_item_invalid(self): + with pytest.raises(ValueError): + self.cpx.pixels[0] = "hello" def test_len(self): assert 3 == len(self.pixel) @@ -53,7 +57,7 @@ def test_valid_index(self, index, expected): def test_fill(self): self.pixel.fill((123, 123, 123)) - assert True == all(p == (123, 123, 123) for p in self.pixel._Pixel__state["pixels"]) + assert all(p == (123, 123, 123) for p in self.pixel._Pixel__state["pixels"]) @pytest.mark.parametrize("val, expected", [([3, 4, 5], (3, 4, 5)), @@ -98,7 +102,7 @@ def test_valid_rgb_value(self, pixValue, expected): def test_get_brightness(self): self.pixel._Pixel__state['brightness'] = 0.4 - assert 0.4 == self.pixel.brightness + assert 0.4 == pytest.approx(self.pixel.brightness) @pytest.mark.parametrize("brightness", [-0.1, 1.1]) def test_set_brightness_fail(self, brightness): @@ -108,4 +112,4 @@ def test_set_brightness_fail(self, brightness): @pytest.mark.parametrize("brightness", [0, 1, 0.4, 0.333]) def test_set_brightness(self, brightness): self.pixel.brightness = brightness - assert True == self.pixel._Pixel__valid_brightness(brightness) \ No newline at end of file + assert self.pixel._Pixel__valid_brightness(brightness) \ No newline at end of file diff --git a/src/adafruit_circuitplayground/test/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py index 2e598a67d..a9331ce2c 100644 --- a/src/adafruit_circuitplayground/test/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -2,6 +2,7 @@ from unittest import mock +from .. import constants as CONSTANTS from .. import utils @@ -12,13 +13,17 @@ def test_remove_leading_slashes(self): expected = "a//b/" assert expected == utils.remove_leading_slashes(original) - def test_escape_if_OSX_notOSX(self): + def test_escape_notOSX(self): + if sys.platform.startswith(CONSTANTS.MAC_OS): + utils.sys = mock.MagicMock() + utils.sys.configure_mock(platform='win32') original = "a b" assert original == utils.escape_if_OSX(original) - def test_escape_if_OSX_isOSX(self): - utils.sys = mock.MagicMock() - utils.sys.configure_mock(platform='darwin') + def test_escape_isOSX(self): + if not sys.platform.startswith(CONSTANTS.MAC_OS): + utils.sys = mock.MagicMock() + utils.sys.configure_mock(platform='darwin') original = "a b" expected = "a%20b" assert expected == utils.escape_if_OSX(original) diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index 83a595d3b..21d6f1d1a 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -20,8 +20,8 @@ def show(state, debug_mode=False): if debug_mode: debugger_communication_client.update_state(json.dumps(message)) else: - print(json.dumps(message) + '\0', end='', - file=sys.__stdout__, flush=True) + # print(json.dumps(message) + '\0', end='', + # file=sys.__stdout__, flush=True) time.sleep(CONSTANTS.TIME_DELAY) From f8033729b9a8d411e2f2d4235b2c3314a40c335d Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Thu, 23 Jan 2020 11:36:32 -0800 Subject: [PATCH 14/21] added docs for testing --- docs/developers-setup.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/developers-setup.md b/docs/developers-setup.md index 448cdc9ee..42234bbc3 100644 --- a/docs/developers-setup.md +++ b/docs/developers-setup.md @@ -43,6 +43,12 @@ 6. Start running the extension locally by pressing F5 or going to VS Code Debug menu and select 'Start debugging' +## Testing + +- To run unit tests, run the command: `npm run test` in the root level directory. + - You will need Pytest installed for the Python tests to run correctly +- To run just the python tests, run the command: `pytest src` or `python -m pytest src` in the root level directory. + ## Notes on how to use it - [Documentation to use the Extension](/docs/how-to-use.md) From 42b3d24b8c576c06ab03064095d8283ac6dee00d Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Thu, 23 Jan 2020 13:35:29 -0800 Subject: [PATCH 15/21] Updated broken test --- src/adafruit_circuitplayground/test/test_pixel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adafruit_circuitplayground/test/test_pixel.py b/src/adafruit_circuitplayground/test/test_pixel.py index aa919872a..100f96e3c 100644 --- a/src/adafruit_circuitplayground/test/test_pixel.py +++ b/src/adafruit_circuitplayground/test/test_pixel.py @@ -46,7 +46,7 @@ def test_set_item_out_of_bounds(self): def test_set_item_invalid(self): with pytest.raises(ValueError): - self.cpx.pixels[0] = "hello" + self.pixel[0] = "hello" def test_len(self): assert 3 == len(self.pixel) From fb178f5abdcf36a55304d5bc282c72c909dff3ec Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Thu, 23 Jan 2020 13:38:01 -0800 Subject: [PATCH 16/21] uncommented some necessary code --- src/adafruit_circuitplayground/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index 21d6f1d1a..83a595d3b 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -20,8 +20,8 @@ def show(state, debug_mode=False): if debug_mode: debugger_communication_client.update_state(json.dumps(message)) else: - # print(json.dumps(message) + '\0', end='', - # file=sys.__stdout__, flush=True) + print(json.dumps(message) + '\0', end='', + file=sys.__stdout__, flush=True) time.sleep(CONSTANTS.TIME_DELAY) From 08f3cf67e9f8b40fa1424687dd1d4c9364fb2164 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Fri, 24 Jan 2020 16:01:32 -0800 Subject: [PATCH 17/21] merge conflicts resolved --- .../test/test_express.py | 101 ------------------ 1 file changed, 101 deletions(-) diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index c5e97827c..3f9c7a611 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -31,7 +31,6 @@ def test_acceleration(self): assert accel[2] == -20 def test_button_a(self): -<<<<<<< HEAD self.cpx._Express__state['button_a'] = True assert self.cpx.button_a @@ -51,25 +50,12 @@ def test_taps_setter(self, taps, expected): def test_red_led(self): self.cpx._Express__state['red_led'] = True assert self.cpx.red_led -======= - self.cpx._Express__state["button_a"] = True - assert True == self.cpx.button_a - - def test_button_b(self): - self.cpx._Express__state["button_b"] = True - assert True == self.cpx.button_b - - def test_red_led(self): - self.cpx._Express__state["red_led"] = True - assert True == self.cpx.red_led ->>>>>>> dev def test_red_led_int(self): self.cpx.red_led = 3 assert self.cpx.red_led def test_red_led_string(self): -<<<<<<< HEAD self.cpx.red_led = 'foo' assert self.cpx.red_led @@ -116,94 +102,7 @@ def test_touch_A6(self): def test_touch_A7(self): self.cpx._Express__state['touch'][6] = True assert self.cpx.touch_A7 -======= - self.cpx.red_led = "foo" - assert True == self.cpx.red_led - - def test_switch(self): - self.cpx._Express__state["switch"] = True - assert True == self.cpx.switch - - def test_set_item_tuple(self): - self.cpx.pixels[0] = (255, 255, 255) - assert (255, 255, 255) == self.cpx._Express__state["pixels"][0] - - def test_set_item_list(self): - self.cpx.pixels[0] = [255, 255, 255] - assert (255, 255, 255) == self.cpx._Express__state["pixels"][0] - - def test_set_item_hex(self): - self.cpx.pixels[0] = 0xFFFFFF - assert (255, 255, 255) == self.cpx._Express__state["pixels"][0] - - def test_set_item_invalid(self): - with pytest.raises(ValueError): - self.cpx.pixels[0] = "hello" - - def test_shake(self): - self.cpx._Express__state["shake"] = True - assert True == self.cpx.shake() - - def test_touch_A1(self): - self.cpx._Express__state["touch"][0] = True - assert True == self.cpx.touch_A1 - - def test_touch_A2(self): - self.cpx._Express__state["touch"][1] = True - assert True == self.cpx.touch_A2 - - def test_touch_A3(self): - self.cpx._Express__state["touch"][2] = True - assert True == self.cpx.touch_A3 - - def test_touch_A4(self): - self.cpx._Express__state["touch"][3] = True - assert True == self.cpx.touch_A4 - - def test_touch_A5(self): - self.cpx._Express__state["touch"][4] = True - assert True == self.cpx.touch_A5 - - def test_touch_A6(self): - self.cpx._Express__state["touch"][5] = True - assert True == self.cpx.touch_A6 - - def test_touch_A7(self): - self.cpx._Express__state["touch"][6] = True - assert True == self.cpx.touch_A7 ->>>>>>> dev def test_play_file_mp_wrong_type(self): with pytest.raises(TypeError): -<<<<<<< HEAD self.cpx.play_file('sample.mp4') -======= - self.cpx.play_file("sample.mp4") - - def test_fill(self): - self.cpx.pixels.fill((0, 255, 0)) - expected_pixels = [(0, 255, 0)] * 10 - assert expected_pixels == self.cpx._Express__state["pixels"] - - def test_extract_pixel_value_list(self): - assert (0, 255, 0) == self.cpx.pixels._Pixel__extract_pixel_value((0, 255, 0)) - - def test_extract_pixel_value_list1(self): - assert (123, 123, 123) == self.cpx.pixels._Pixel__extract_pixel_value( - [123, 123, 123] - ) - - def test_extract_pixel_value_int(self): - assert (0, 0, 255) == self.cpx.pixels._Pixel__extract_pixel_value(255) - - def test_extract_pixel_value_tuple(self): - assert (0, 255, 0) == self.cpx.pixels._Pixel__extract_pixel_value((0, 255, 0)) - - def test_extract_pixel_value_invalid_length(self): - with pytest.raises(ValueError): - self.cpx.pixels._Pixel__extract_pixel_value((1, 2, 3, 4)) - - def test_extract_pixel_value_invalid_tuple_value(self): - with pytest.raises(ValueError): - self.cpx.pixels._Pixel__extract_pixel_value((0, 222, "hello")) ->>>>>>> dev From 2872310a8206e175e066e6fd7f2b1e3484974024 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Fri, 24 Jan 2020 16:02:39 -0800 Subject: [PATCH 18/21] Updated with black formatting --- .../test/test_express.py | 36 ++++----- .../test/test_pixel.py | 77 ++++++++++--------- .../test/test_utils.py | 7 +- 3 files changed, 62 insertions(+), 58 deletions(-) diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index 3f9c7a611..4fc7f3f2a 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -31,11 +31,11 @@ def test_acceleration(self): assert accel[2] == -20 def test_button_a(self): - self.cpx._Express__state['button_a'] = True + self.cpx._Express__state["button_a"] = True assert self.cpx.button_a def test_button_b(self): - self.cpx._Express__state['button_b'] = True + self.cpx._Express__state["button_b"] = True assert self.cpx.button_b def test_taps(self): @@ -48,7 +48,7 @@ def test_taps_setter(self, taps, expected): assert expected == self.cpx.detect_taps def test_red_led(self): - self.cpx._Express__state['red_led'] = True + self.cpx._Express__state["red_led"] = True assert self.cpx.red_led def test_red_led_int(self): @@ -56,53 +56,53 @@ def test_red_led_int(self): assert self.cpx.red_led def test_red_led_string(self): - self.cpx.red_led = 'foo' + self.cpx.red_led = "foo" assert self.cpx.red_led def test_switch(self): - self.cpx._Express__state['switch'] = True + self.cpx._Express__state["switch"] = True assert self.cpx.switch def test_temperature(self): - self.cpx._Express__state['temperature'] = 31 + self.cpx._Express__state["temperature"] = 31 assert 31 == self.cpx.temperature def test_light(self): - self.cpx._Express__state['light'] = 255 + self.cpx._Express__state["light"] = 255 assert 255 == self.cpx.light def test_shake(self): - self.cpx._Express__state['shake'] = True + self.cpx._Express__state["shake"] = True assert self.cpx.shake() def test_touch_A1(self): - self.cpx._Express__state['touch'][0] = True + self.cpx._Express__state["touch"][0] = True assert self.cpx.touch_A1 def test_touch_A2(self): - self.cpx._Express__state['touch'][1] = True + self.cpx._Express__state["touch"][1] = True assert self.cpx.touch_A2 def test_touch_A3(self): - self.cpx._Express__state['touch'][2] = True + self.cpx._Express__state["touch"][2] = True assert self.cpx.touch_A3 - + def test_touch_A4(self): - self.cpx._Express__state['touch'][3] = True + self.cpx._Express__state["touch"][3] = True assert self.cpx.touch_A4 def test_touch_A5(self): - self.cpx._Express__state['touch'][4] = True + self.cpx._Express__state["touch"][4] = True assert self.cpx.touch_A5 - + def test_touch_A6(self): - self.cpx._Express__state['touch'][5] = True + self.cpx._Express__state["touch"][5] = True assert self.cpx.touch_A6 def test_touch_A7(self): - self.cpx._Express__state['touch'][6] = True + self.cpx._Express__state["touch"][6] = True assert self.cpx.touch_A7 def test_play_file_mp_wrong_type(self): with pytest.raises(TypeError): - self.cpx.play_file('sample.mp4') + self.cpx.play_file("sample.mp4") diff --git a/src/adafruit_circuitplayground/test/test_pixel.py b/src/adafruit_circuitplayground/test/test_pixel.py index 100f96e3c..2532611db 100644 --- a/src/adafruit_circuitplayground/test/test_pixel.py +++ b/src/adafruit_circuitplayground/test/test_pixel.py @@ -4,23 +4,23 @@ class TestPixel(object): - def setup_method(self): self.pixel = Pixel( { - 'brightness': 1.0, - 'button_a': False, - 'button_b': False, - 'pixels': [(255, 0, 0), (0, 255, 0), (0, 0, 255)], - 'red_led': False, - 'switch': False - }) + "brightness": 1.0, + "button_a": False, + "button_b": False, + "pixels": [(255, 0, 0), (0, 255, 0), (0, 0, 255)], + "red_led": False, + "switch": False, + } + ) @pytest.mark.parametrize("debug_mode", [True, False]) def test_set_debug_mode(self, debug_mode): self.pixel._Pixel__set_debug_mode(debug_mode) assert debug_mode == self.pixel._Pixel__debug_mode - + def test_get_item_out_of_bounds(self): with pytest.raises(IndexError): p = self.pixel[3] @@ -39,7 +39,7 @@ def test_set_item_splice(self): self.pixel[0:1] = [(100, 100, 100), (0, 0, 100)] assert (100, 100, 100) == self.pixel[0] assert (0, 0, 100) == self.pixel[1] - + def test_set_item_out_of_bounds(self): with pytest.raises(IndexError): self.pixel[3] = (0, 0, 0) @@ -47,10 +47,10 @@ def test_set_item_out_of_bounds(self): def test_set_item_invalid(self): with pytest.raises(ValueError): self.pixel[0] = "hello" - + def test_len(self): assert 3 == len(self.pixel) - + @pytest.mark.parametrize("index, expected", [(0, True), (3, False)]) def test_valid_index(self, index, expected): assert self.pixel._Pixel__valid_index(index) == expected @@ -59,17 +59,21 @@ def test_fill(self): self.pixel.fill((123, 123, 123)) assert all(p == (123, 123, 123) for p in self.pixel._Pixel__state["pixels"]) - @pytest.mark.parametrize("val, expected", - [([3, 4, 5], (3, 4, 5)), - (432, (0, 1, 176)), - ((1, 2, 3), (1, 2, 3))]) + @pytest.mark.parametrize( + "val, expected", + [([3, 4, 5], (3, 4, 5)), (432, (0, 1, 176)), ((1, 2, 3), (1, 2, 3))], + ) def test_extract_pixel_values_not_slice(self, val, expected): assert expected == self.pixel._Pixel__extract_pixel_value(val) - @pytest.mark.parametrize("val, expected", - [([[3, 4, 5], [6, 7, 8]], [(3, 4, 5), (6, 7, 8)]), - ([444555, 666777], [(6, 200, 139), (10, 44, 153)]), - ([(10, 10, 10), (20, 20, 20)], [(10, 10, 10), (20, 20, 20)])]) + @pytest.mark.parametrize( + "val, expected", + [ + ([[3, 4, 5], [6, 7, 8]], [(3, 4, 5), (6, 7, 8)]), + ([444555, 666777], [(6, 200, 139), (10, 44, 153)]), + ([(10, 10, 10), (20, 20, 20)], [(10, 10, 10), (20, 20, 20)]), + ], + ) def test_extract_pixel_values_slice(self, val, expected): assert expected == self.pixel._Pixel__extract_pixel_value(val, is_slice=True) @@ -81,29 +85,30 @@ def test_extract_pixel_values_fail(self, val): def test_hex_to_rgb_fail(self): with pytest.raises(ValueError): self.pixel._Pixel__hex_to_rgb("x") - - @pytest.mark.parametrize("hex, expected", - [("0xffffff", (255, 255, 255)), - ("0x0", (0, 0, 0)), - ("0xff0000", (255, 0, 0)), - ("0xabcdef", (171, 205, 239))]) + + @pytest.mark.parametrize( + "hex, expected", + [ + ("0xffffff", (255, 255, 255)), + ("0x0", (0, 0, 0)), + ("0xff0000", (255, 0, 0)), + ("0xabcdef", (171, 205, 239)), + ], + ) def test_hex_to_rgb(self, hex, expected): assert expected == self.pixel._Pixel__hex_to_rgb(hex) - @pytest.mark.parametrize("pixValue, expected", - [(0, True), - (200, True), - (255, True), - (-1, False), - (256, False), - ("1", False)]) + @pytest.mark.parametrize( + "pixValue, expected", + [(0, True), (200, True), (255, True), (-1, False), (256, False), ("1", False)], + ) def test_valid_rgb_value(self, pixValue, expected): assert expected == self.pixel._Pixel__valid_rgb_value(pixValue) def test_get_brightness(self): - self.pixel._Pixel__state['brightness'] = 0.4 + self.pixel._Pixel__state["brightness"] = 0.4 assert 0.4 == pytest.approx(self.pixel.brightness) - + @pytest.mark.parametrize("brightness", [-0.1, 1.1]) def test_set_brightness_fail(self, brightness): with pytest.raises(ValueError): @@ -112,4 +117,4 @@ def test_set_brightness_fail(self, brightness): @pytest.mark.parametrize("brightness", [0, 1, 0.4, 0.333]) def test_set_brightness(self, brightness): self.pixel.brightness = brightness - assert self.pixel._Pixel__valid_brightness(brightness) \ No newline at end of file + assert self.pixel._Pixel__valid_brightness(brightness) diff --git a/src/adafruit_circuitplayground/test/test_utils.py b/src/adafruit_circuitplayground/test/test_utils.py index a9331ce2c..3c25128e6 100644 --- a/src/adafruit_circuitplayground/test/test_utils.py +++ b/src/adafruit_circuitplayground/test/test_utils.py @@ -7,23 +7,22 @@ class TestUtils(object): - def test_remove_leading_slashes(self): original = "///a//b/" expected = "a//b/" assert expected == utils.remove_leading_slashes(original) - + def test_escape_notOSX(self): if sys.platform.startswith(CONSTANTS.MAC_OS): utils.sys = mock.MagicMock() - utils.sys.configure_mock(platform='win32') + utils.sys.configure_mock(platform="win32") original = "a b" assert original == utils.escape_if_OSX(original) def test_escape_isOSX(self): if not sys.platform.startswith(CONSTANTS.MAC_OS): utils.sys = mock.MagicMock() - utils.sys.configure_mock(platform='darwin') + utils.sys.configure_mock(platform="darwin") original = "a b" expected = "a%20b" assert expected == utils.escape_if_OSX(original) From 4bc6f7fd9f3c83709c41db4e7ce94977aaca7670 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Fri, 24 Jan 2020 16:56:00 -0800 Subject: [PATCH 19/21] added test --- src/adafruit_circuitplayground/express.py | 4 ++-- src/adafruit_circuitplayground/test/test_express.py | 9 ++++++++- src/adafruit_circuitplayground/utils.py | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/adafruit_circuitplayground/express.py b/src/adafruit_circuitplayground/express.py index 99507a691..f08e1b65c 100644 --- a/src/adafruit_circuitplayground/express.py +++ b/src/adafruit_circuitplayground/express.py @@ -4,7 +4,7 @@ import json import sys import os -from playsound import playsound +import playsound from .pixel import Pixel from . import utils from . import constants as CONSTANTS @@ -162,7 +162,7 @@ def play_file(self, file_name): if sys.implementation.version[0] >= 3: if file_name.endswith(".wav"): try: - playsound(abs_path_wav_file) + playsound.playsound(abs_path_wav_file) except: # TODO TASK: 29054 Verfication of a "valid" .wav file raise EnvironmentError(CONSTANTS.NOT_SUITABLE_FILE_ERROR) diff --git a/src/adafruit_circuitplayground/test/test_express.py b/src/adafruit_circuitplayground/test/test_express.py index 4fc7f3f2a..b83ba3a23 100644 --- a/src/adafruit_circuitplayground/test/test_express.py +++ b/src/adafruit_circuitplayground/test/test_express.py @@ -1,5 +1,7 @@ import pytest +from unittest import mock +import playsound from ..express import Express from ..pixel import Pixel @@ -103,6 +105,11 @@ def test_touch_A7(self): self.cpx._Express__state["touch"][6] = True assert self.cpx.touch_A7 - def test_play_file_mp_wrong_type(self): + def test_play_file_mp4_wrong_type(self): with pytest.raises(TypeError): self.cpx.play_file("sample.mp4") + + def test_play_file_mp4(self): + playsound.playsound = mock.Mock() + self.cpx.play_file("sample.wav") + playsound.playsound.assert_called_once() diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index 14fddf227..a5ffbf3ea 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -20,7 +20,7 @@ def show(state, debug_mode=False): if debug_mode: debugger_communication_client.update_state(json.dumps(message)) else: - print(json.dumps(message) + "\0", end="", file=sys.__stdout__, flush=True) + # print(json.dumps(message) + "\0", end="", file=sys.__stdout__, flush=True) time.sleep(CONSTANTS.TIME_DELAY) From 3b4035d165acea987a8ff8b5e1084b045414ca37 Mon Sep 17 00:00:00 2001 From: Vandy Liu Date: Mon, 27 Jan 2020 16:56:08 -0800 Subject: [PATCH 20/21] Uncommented necessary code --- src/adafruit_circuitplayground/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adafruit_circuitplayground/utils.py b/src/adafruit_circuitplayground/utils.py index a5ffbf3ea..14fddf227 100644 --- a/src/adafruit_circuitplayground/utils.py +++ b/src/adafruit_circuitplayground/utils.py @@ -20,7 +20,7 @@ def show(state, debug_mode=False): if debug_mode: debugger_communication_client.update_state(json.dumps(message)) else: - # print(json.dumps(message) + "\0", end="", file=sys.__stdout__, flush=True) + print(json.dumps(message) + "\0", end="", file=sys.__stdout__, flush=True) time.sleep(CONSTANTS.TIME_DELAY) From 12e3eed5bf8e7962cd512db822ea9596ee1f60d8 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 28 Jan 2020 00:12:17 -0800 Subject: [PATCH 21/21] Added debugger tests --- .../debugger_communication_client.py | 2 +- .../test_debugger_communication_client.py | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/adafruit_circuitplayground/test/test_debugger_communication_client.py diff --git a/src/adafruit_circuitplayground/debugger_communication_client.py b/src/adafruit_circuitplayground/debugger_communication_client.py index 32f02d273..0536550ea 100644 --- a/src/adafruit_circuitplayground/debugger_communication_client.py +++ b/src/adafruit_circuitplayground/debugger_communication_client.py @@ -47,5 +47,5 @@ def button_press(data): # Event : Sensor changed (Temperature, light, Motion) @sio.on("sensor_changed") -def button_press(data): +def sensor_changed(data): __update_api_state(data, CONSTANTS.EVENTS_SENSOR_CHANGED) diff --git a/src/adafruit_circuitplayground/test/test_debugger_communication_client.py b/src/adafruit_circuitplayground/test/test_debugger_communication_client.py new file mode 100644 index 000000000..b25e27eb8 --- /dev/null +++ b/src/adafruit_circuitplayground/test/test_debugger_communication_client.py @@ -0,0 +1,63 @@ +import pytest +import json # Remove +from unittest import mock +import socketio + +from .. import express +from .. import debugger_communication_client + + +class TestDebuggerCommunicationClient(object): + @mock.patch("socketio.Client.connect") + def test_init_connection(self, mock_connect): + mock_connect.return_value = None + debugger_communication_client.init_connection() + mock_connect.assert_called_once() + + def test_init_connection1(self): + socketio.Client.connect = mock.Mock() + socketio.Client.connect.return_value = None + debugger_communication_client.init_connection() + socketio.Client.connect.assert_called_once() + + def test_update_state(self): + socketio.Client.emit = mock.Mock() + socketio.Client.emit.return_value = None + debugger_communication_client.update_state({}) + socketio.Client.emit.assert_called_once() + + @mock.patch.dict( + express.cpx._Express__state, + {"button_a": False, "button_b": False, "switch": True}, + clear=True, + ) + def test_button_press(self): + data = {"button_a": True, "button_b": True, "switch": True} + serialized_data = json.dumps(data) + debugger_communication_client.button_press(serialized_data) + assert 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}, + clear=True, + ) + def test_sensor_changed(self): + data = { + "temperature": 1, + "light": 2, + "motion_x": 3, + "motion_y": 4, + "motion_z": 5, + } + serialized_data = json.dumps(data) + debugger_communication_client.sensor_changed(serialized_data) + assert 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) + # Exception is caught and a print is stated to stderr + mocked_print.assert_called_once()