def test_sort_firmware_files__returns_sorted_list_of_all_firmware_files( mocker): expected_file_names = [ "test_1_0_5.bit", "test_1_3_4.bit", "test_1_4_0.bit", "test_2_1_5.bit", "test_2_3_4.bit", ] mocked_path_str = os.path.join("tests", "test_firmware") mocked_path = mocker.patch.object(firmware_manager, "resource_path", return_value=mocked_path_str) actual = sort_firmware_files() expected_base_path = os.path.normcase( os.path.join( os.path.dirname(get_current_file_abs_directory()), "src", "mantarray_desktop_app", os.pardir, os.pardir, )) expected_relative_path = os.path.join("src", "firmware") os.path.join(expected_base_path, expected_relative_path) expected_file_list = expected_file_names mocked_path.assert_called_once_with(expected_relative_path, base_path=expected_base_path) assert actual == expected_file_list
def test_MantarrayMcSimulator__get_interpolated_data_returns_correct_value( mantarray_mc_simulator, ): simulator = mantarray_mc_simulator["simulator"] relative_path = os.path.join("src", "simulated_data", "simulated_twitch.csv") absolute_path = os.path.normcase( os.path.join(get_current_file_abs_directory(), os.pardir, os.pardir)) file_path = resource_path(relative_path, base_path=absolute_path) with open(file_path, newline="") as csvfile: simulated_data_timepoints = next(csv.reader(csvfile, delimiter=",")) simulated_data_values = next(csv.reader(csvfile, delimiter=",")) test_sampling_period = 1000 interpolator = interpolate.interp1d( np.array(simulated_data_timepoints, dtype=np.uint64), simulated_data_values, ) expected_data = interpolator( np.arange(0, MICRO_TO_BASE_CONVERSION, test_sampling_period, dtype=np.uint64)).astype(np.uint16) actual_data = simulator.get_interpolated_data(test_sampling_period) assert actual_data.dtype == np.uint16 np.testing.assert_array_equal(actual_data, expected_data)
def parse_scripting_log(script_type: str) -> Dict[str, Any]: """Parse a log to run to create a sequence of XEM commands.""" file_name = f"xem_{script_type}.txt" relative_path = os.path.join("src", "xem_scripts", file_name) absolute_path = os.path.normcase( os.path.join(get_current_file_abs_directory(), os.pardir, os.pardir)) file_path = resource_path(relative_path, base_path=absolute_path) command_list: List[Dict[str, Any]] = list() script_dict = {"script_type": script_type, "command_list": command_list} is_parsing = False with open(file_path, "r") as log_file: is_script_done = False for line in log_file: if is_parsing: if "end_hardware_script" in line: is_script_done = True break if "mGET" in line: command_list.append(parse_scripting_log_line(line)) elif "begin_hardware_script" in line: script_details = parse_scripting_log_line(line) if script_details["script_type"] != script_type: log_script_type = script_details["script_type"] raise MismatchedScriptTypeError( f"Script type in log: '{log_script_type}' does not match file name: '{script_type}'" ) script_dict["version"] = script_details["version"] is_parsing = True if not is_script_done: raise ScriptDoesNotContainEndCommandError() return script_dict
def test_parse_scripting_log__calls_resource_path_correctly(mocker): test_script = "test_script" mocked_path_str = os.path.join("tests", "test_xem_scripts", f"xem_{test_script}.txt") def side_effect(*args, **kwargs): # Tanner (4/14/20): we want to truly call resource_path (not mocked), but need to patch it to get the return value needed for testing resource_path(*args, **kwargs) return mocked_path_str mocked_path = mocker.patch.object(ok_comm, "resource_path", autospec=True, side_effect=side_effect) parse_scripting_log(test_script) expected_base_path = os.path.normcase( os.path.join( os.path.dirname(os.path.dirname(get_current_file_abs_directory())), "src", "mantarray_desktop_app", os.pardir, os.pardir, )) expected_relative_path = os.path.join("src", "xem_scripts", f"xem_{test_script}.txt") mocked_path.assert_called_once_with(expected_relative_path, base_path=expected_base_path)
def test_get_current_file_abs_directory(): expected_to_contain = os.path.join("stdlib-utils", "tests", "extra_file_path_tests") actual = get_current_file_abs_directory() assert actual.endswith(expected_to_contain) # the actual beginning of the absolute path could vary system to system...so just make sure there is something in front of the known portion of the path minimum_length = len(expected_to_contain) + 1 assert len(actual) > minimum_length
def test_get_current_software_version__Given_code_is_not_bundled__When_the_function_is_called__Then_it_returns_version_from_package_json( ): path_to_package_json = os.path.join(get_current_file_abs_directory(), os.pardir, "package.json") with open(path_to_package_json) as in_file: parsed_json = json.load(in_file) expected = parsed_json["version"] actual = get_current_software_version() assert actual == expected
def test_correct_driver_exe_is_downloaded__and_put_in_correct_folder(): path_to_drivers_folder = os.path.normcase( os.path.join( os.path.dirname(get_current_file_abs_directory()), "src", "drivers", )) assert "FrontPanelUSB-DriverOnly-5.2.2.exe" in os.listdir( path_to_drivers_folder)
def _create_process_manager( shared_values_dict: Dict[str, Any]) -> MantarrayProcessesManager: base_path = os.path.join(get_current_file_abs_directory(), os.pardir, os.pardir) relative_path = "recordings" try: file_dir = shared_values_dict["config_settings"]["recording_directory"] except KeyError: file_dir = resource_path(relative_path, base_path=base_path) return MantarrayProcessesManager( file_directory=file_dir, values_to_share_to_server=shared_values_dict)
def test_all_xem_scripts_are_present_and_parse_without_error(): script_dir = os.path.join(get_current_file_abs_directory(), os.pardir, os.pardir, "src", "xem_scripts") script_list = list() for file in os.listdir(script_dir): if file.endswith(".txt"): script_list.append(file) script_types = frozenset(script_list) # Tanner (4/14/20): this loop is only to test that each script parses without error for script_file in script_list: script_type = script_file.strip("xem_").strip(".txt") parse_scripting_log(script_type) assert script_types == frozenset( ["xem_start_up.txt", "xem_start_calibration.txt"])
def get_current_software_version() -> str: """Return the current software version. Returns the constant if running in a bundle. Otherwise reads it from package.json """ if is_frozen_as_exe(): return CURRENT_SOFTWARE_VERSION path_to_package_json = os.path.join( get_current_file_abs_directory(), os.pardir, os.pardir, "package.json" ) with open(path_to_package_json) as in_file: parsed_json = json.load(in_file) version = parsed_json["version"] if not isinstance(version, str): raise NotImplementedError( f"The version in package.json should always be a string. It was: {version}" ) return version
def test_get_latest_firmware__returns_correct_path_to_latest_firmware_file( mocker): expected_base_path = os.path.normcase( os.path.join( os.path.dirname(get_current_file_abs_directory()), "src", "mantarray_desktop_app", os.pardir, os.pardir, )) mocked_path_str = os.path.join("tests", "test_firmware") mocked_path = mocker.patch.object(firmware_manager, "resource_path", return_value=mocked_path_str) expected_latest_file_path = os.path.join(mocked_path_str, "test_2_3_4.bit") actual = get_latest_firmware() assert actual == expected_latest_file_path expected_relative_path = os.path.join("src", "firmware") mocked_path.assert_called_with(expected_relative_path, base_path=expected_base_path)
import subprocess import time from mantarray_desktop_app import CALIBRATED_STATE from mantarray_desktop_app import CALIBRATION_NEEDED_STATE from mantarray_desktop_app import get_api_endpoint from mantarray_desktop_app import get_server_port_number from mantarray_desktop_app import system_state_eventually_equals from mantarray_desktop_app import wait_for_subprocesses_to_start import pytest import requests from stdlib_utils import confirm_port_in_use from stdlib_utils import get_current_file_abs_directory from stdlib_utils import is_system_windows PATH_OF_CURRENT_FILE = get_current_file_abs_directory() @pytest.mark.slow @pytest.mark.only_exe def test_exe_can_access_xem_script_and_firmware_folders(): exe_file_name = "mantarray-flask.exe" if is_system_windows( ) else "mantarray-flask" subprocess_args = [ os.path.join("dist-python", "mantarray-flask", exe_file_name) ] with subprocess.Popen(subprocess_args) as sub_process: port = get_server_port_number() confirm_port_in_use(port, timeout=10) wait_for_subprocesses_to_start()
import os import shutil import struct import tempfile import pytest from stdlib_utils import checksum from stdlib_utils import compute_crc32_and_write_to_file_head from stdlib_utils import compute_crc32_bytes_of_large_file from stdlib_utils import compute_crc32_hex_of_large_file from stdlib_utils import Crc32ChecksumValidationFailureError from stdlib_utils import Crc32InFileHeadDoesNotMatchExpectedValueError from stdlib_utils import get_current_file_abs_directory from stdlib_utils import validate_file_head_crc32 FILE_FOR_HASHING = os.path.join(get_current_file_abs_directory(), "checksum_files", "file_for_crc32_hashing.txt") FILE_WITH_CHECKSUM_AT_HEAD = os.path.join( get_current_file_abs_directory(), "checksum_files", "file_with_crc32_checksum_at_head.txt", ) FILE_WITH_INCORRECT_CHECKSUM_AT_HEAD = os.path.join( get_current_file_abs_directory(), "checksum_files", "file_with_incorrect_crc32_checksum_at_head.txt", ) def test_compute_crc32_bytes_of_large_file__returns_correct_hash():
def _get_firmware_dir() -> str: absolute_path = os.path.normcase( os.path.join(get_current_file_abs_directory(), os.pardir, os.pardir)) relative_path = os.path.join("src", "firmware") firmware_path: str = resource_path(relative_path, base_path=absolute_path) return firmware_path