This repository was archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Added unit tests and removed some unused test code #174
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5259563
Initial tests still a lot to do
fda4edc
Updated package.json script for python tests
c652f08
Purposely made error to test pipeline
d2b16ff
Corrected spelling error
ab18b47
commented out broken test
bca1b23
updated tests
5b9db94
updated tests
ffce53e
Updated imports
8c6e95d
updated imports
fc237c8
Merge branch 'dev' into users/t-vali/python-tests
vandyliu c452cb2
Removed comments and other accidental changes
97badd4
Merge branch 'users/t-vali/python-tests' of https://github.com/micros…
aad0cc3
removed unused import
bd87e99
removed unused import
vandyliu db53ed8
Updated tests from comments
f803372
added docs for testing
42b3d24
Updated broken test
fb178f5
uncommented some necessary code
060eb78
Merge branch 'dev' into users/t-vali/python-tests
xnkevinnguyen f9d2cef
fixed merge conflicts
08f3cf6
merge conflicts resolved
2872310
Updated with black formatting
4bc6f7f
added test
912d475
Merge branch 'dev' into users/t-vali/python-tests
xnkevinnguyen 328b613
Merge branch 'dev' into users/t-vali/python-tests
3b4035d
Uncommented necessary code
44f202a
Merge branch 'dev' into users/t-vali/python-tests
vandyliu 12e3eed
Added debugger tests
vandyliu f1b1157
Merge branch 'dev' into users/t-vali/python-tests
xnkevinnguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
63 changes: 63 additions & 0 deletions
63
src/adafruit_circuitplayground/test/test_debugger_communication_client.py
This file contains hidden or 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,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() |
This file contains hidden or 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
This file contains hidden or 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,120 @@ | ||
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): | ||
vandyliu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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_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 | ||
|
||
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))], | ||
) | ||
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 == pytest.approx(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 self.pixel._Pixel__valid_brightness(brightness) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.