|
1 |
| -from unittest.mock import patch |
| 1 | +from unittest import mock |
2 | 2 | from pathlib import Path
|
3 | 3 |
|
4 | 4 | import pytest
|
|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TestGenerateProject:
|
10 |
| - """A class with common parameters, `param1` and `param2`.""" |
| 10 | + DEFAULT_OPTIONS = {"arduino_cli_cmd": "arduino-cli", "arduino_board": "nano33ble"} |
11 | 11 |
|
12 | 12 | def test_print_c_array(self):
|
13 | 13 | handler = microtvm_api_server.Handler()
|
14 | 14 | c_arr = handler._print_c_array([1, 32, 32, 3])
|
15 | 15 | assert c_arr == "{1, 32, 32, 3}"
|
16 | 16 |
|
17 | 17 | def _set_pathlib_path_exists(self, value):
|
18 |
| - with patch.object(Path, "exists") as mock_exists: |
| 18 | + with mock.patch.object(Path, "exists") as mock_exists: |
19 | 19 | mock_exists.return_value = value
|
20 | 20 |
|
21 |
| - @patch("pathlib.Path") |
22 |
| - def test_find_modified_include_path(self, MockPath): |
| 21 | + @mock.patch("pathlib.Path") |
| 22 | + def test_find_modified_include_path(self, mock_pathlib_path): |
23 | 23 | handler = microtvm_api_server.Handler()
|
24 | 24 |
|
25 |
| - project_dir = MockPath("/dummy/project") |
26 |
| - file_path = project_dir / "src/standalone_crt/src/runtime/crt/graph_executor/load_json.c" |
| 25 | + project_dir = mock_pathlib_path("/dummy/project") |
| 26 | + file_path = ( |
| 27 | + project_dir |
| 28 | + / "src" |
| 29 | + / "standalone_crt" |
| 30 | + / "src" |
| 31 | + / "runtime" |
| 32 | + / "crt" |
| 33 | + / "graph_executor" |
| 34 | + / "load_json.c" |
| 35 | + ) |
27 | 36 |
|
28 | 37 | # Should return C standard libs unmodified
|
29 | 38 | clib_output = handler._find_modified_include_path(project_dir, file_path, "math.h")
|
30 | 39 | assert clib_output == "math.h"
|
31 | 40 |
|
32 | 41 | # If import already works, should return unmodified
|
33 |
| - valid_ardino_import = "../../../../include/tvm/runtime/crt/platform.h" |
| 42 | + valid_arduino_import = "../../../../include/tvm/runtime/crt/platform.h" |
34 | 43 | self._set_pathlib_path_exists(True)
|
35 | 44 | valid_output = handler._find_modified_include_path(
|
36 |
| - project_dir, file_path, valid_ardino_import |
| 45 | + project_dir, file_path, valid_arduino_import |
37 | 46 | )
|
38 |
| - assert valid_output == valid_ardino_import |
| 47 | + assert valid_output == valid_arduino_import |
| 48 | + |
| 49 | + BOARD_CONNECTED_OUTPUT = bytes( |
| 50 | + "Port Type Board Name FQBN Core \n" |
| 51 | + "/dev/ttyACM0 Serial Port (USB) Arduino Nano 33 BLE arduino:mbed_nano:nano33ble arduino:mbed_nano\n" |
| 52 | + "/dev/ttyS4 Serial Port Unknown \n" |
| 53 | + "\n", |
| 54 | + "utf-8", |
| 55 | + ) |
| 56 | + BOARD_DISCONNECTED_OUTPUT = bytes( |
| 57 | + "Port Type Board Name FQBN Core\n" |
| 58 | + "/dev/ttyS4 Serial Port Unknown \n" |
| 59 | + "\n", |
| 60 | + "utf-8", |
| 61 | + ) |
| 62 | + |
| 63 | + @mock.patch("subprocess.check_output") |
| 64 | + def test_auto_detect_port(self, mock_subprocess_check_output): |
| 65 | + process_mock = mock.Mock() |
| 66 | + handler = microtvm_api_server.Handler() |
| 67 | + |
| 68 | + # Test it returns the correct port when a board is connected |
| 69 | + mock_subprocess_check_output.return_value = self.BOARD_CONNECTED_OUTPUT |
| 70 | + detected_port = handler._auto_detect_port(self.DEFAULT_OPTIONS) |
| 71 | + assert detected_port == "/dev/ttyACM0" |
| 72 | + |
| 73 | + # Test it raises an exception when no board is connected |
| 74 | + mock_subprocess_check_output.return_value = self.BOARD_DISCONNECTED_OUTPUT |
| 75 | + with pytest.raises(microtvm_api_server.BoardAutodetectFailed): |
| 76 | + handler._auto_detect_port(self.DEFAULT_OPTIONS) |
| 77 | + |
| 78 | + @mock.patch("subprocess.check_call") |
| 79 | + def test_flash(self, mock_subprocess_check_call): |
| 80 | + handler = microtvm_api_server.Handler() |
| 81 | + handler._port = "/dev/ttyACM0" |
| 82 | + |
| 83 | + # Test no exception thrown when code 0 returned |
| 84 | + mock_subprocess_check_call.return_value = 0 |
| 85 | + handler.flash(self.DEFAULT_OPTIONS) |
| 86 | + mock_subprocess_check_call.assert_called_once() |
| 87 | + |
| 88 | + # Test InvalidPortException raised when port incorrect |
| 89 | + mock_subprocess_check_call.return_value = 2 |
| 90 | + with pytest.raises(microtvm_api_server.InvalidPortException): |
| 91 | + handler.flash(self.DEFAULT_OPTIONS) |
| 92 | + |
| 93 | + # Test SketchUploadException raised for other issues |
| 94 | + mock_subprocess_check_call.return_value = 1 |
| 95 | + with pytest.raises(microtvm_api_server.SketchUploadException): |
| 96 | + handler.flash(self.DEFAULT_OPTIONS) |
0 commit comments