From f7ec885d068e4d4f8acf1b410008ee143e3cd4c7 Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Sun, 15 Sep 2024 17:49:17 -0700 Subject: [PATCH] agriya comments and coverage --- conftest.py | 5 +++++ noxfile.py | 1 - src/pybamm/config.py | 31 ++++++++++++++----------------- src/pybamm/telemetry.py | 2 +- tests/unit/test_config.py | 30 ++++++++++++++++++++++++++++++ tests/unit/test_telemetry.py | 30 ++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 tests/unit/test_config.py create mode 100644 tests/unit/test_telemetry.py diff --git a/conftest.py b/conftest.py index 7ac6cf3c74..77513d56db 100644 --- a/conftest.py +++ b/conftest.py @@ -51,3 +51,8 @@ def set_random_seed(): @pytest.fixture(autouse=True) def set_debug_value(): pybamm.settings.debug_mode = True + + +@pytest.fixture(autouse=True) +def disable_telemetry(): + pybamm.telemetry.disable() diff --git a/noxfile.py b/noxfile.py index c71b906cb4..6567ed167c 100644 --- a/noxfile.py +++ b/noxfile.py @@ -63,7 +63,6 @@ def set_iree_state(): "IREE_INDEX_URL": os.getenv( "IREE_INDEX_URL", "https://iree.dev/pip-release-links.html" ), - "PYBAMM_OPTOUT_TELEMETRY": "true", # don't capture telemetry in tests } VENV_DIR = Path("./venv").resolve() diff --git a/src/pybamm/config.py b/src/pybamm/config.py index 3696c34cf2..be976cd2b7 100644 --- a/src/pybamm/config.py +++ b/src/pybamm/config.py @@ -1,19 +1,10 @@ import uuid -import pathlib import os +import platformdirs +from pathlib import Path -def _get_config_file(): - # Get the home directory - home_dir = pathlib.Path.home() - - # Create the file path for pybamm config - config_file = home_dir / ".config" / "pybamm" / "config.yml" - - return config_file - - -def is_running_tests(): +def is_running_tests(): # pragma: no cover """ Detect if the code is being run as part of a test suite. @@ -42,7 +33,7 @@ def is_running_tests(): return any(runner in sys.argv[0].lower() for runner in test_runners) -def generate(): +def generate(): # pragma: no cover if is_running_tests(): return @@ -50,8 +41,16 @@ def generate(): if read() is not None: return - config_file = _get_config_file() + config_file = Path(platformdirs.user_config_dir("pybamm")) / "config.yml" + write_uuid_to_file(config_file) + +def read(): # pragma: no cover + config_file = Path(platformdirs.user_config_dir("pybamm")) / "config.yml" + return read_uuid_from_file(config_file) + + +def write_uuid_to_file(config_file): # Create the directory if it doesn't exist config_file.parent.mkdir(parents=True, exist_ok=True) @@ -64,9 +63,7 @@ def generate(): f.write(f" uuid: {unique_id}\n") -def read(): - config_file = _get_config_file() - +def read_uuid_from_file(config_file): # Check if the config file exists if not config_file.exists(): return None diff --git a/src/pybamm/telemetry.py b/src/pybamm/telemetry.py index 7e8f588c8c..69152f1a07 100644 --- a/src/pybamm/telemetry.py +++ b/src/pybamm/telemetry.py @@ -19,7 +19,7 @@ def disable(): disable() -def capture(event): +def capture(event): # pragma: no cover # don't capture events in automated testing if pybamm.config.is_running_tests(): return diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py new file mode 100644 index 0000000000..45c1260c09 --- /dev/null +++ b/tests/unit/test_config.py @@ -0,0 +1,30 @@ +import pytest + +import pybamm +import uuid + + +class TestConfig: + def test_write_read_uuid(self, tmp_path): + # Create a temporary file path + config_file = tmp_path / "config.yml" + + # Call the function to write UUID to file + pybamm.config.write_uuid_to_file(config_file) + + # Check that the file was created + assert config_file.exists() + + # Read the UUID using the read_uuid_from_file function + uuid_dict = pybamm.config.read_uuid_from_file(config_file) + + # Check that the UUID was read successfully + assert uuid_dict is not None + assert "uuid" in uuid_dict + + # Verify that the UUID is valid + + try: + uuid.UUID(uuid_dict["uuid"]) + except ValueError: + pytest.fail("Invalid UUID format") diff --git a/tests/unit/test_telemetry.py b/tests/unit/test_telemetry.py new file mode 100644 index 0000000000..45c1260c09 --- /dev/null +++ b/tests/unit/test_telemetry.py @@ -0,0 +1,30 @@ +import pytest + +import pybamm +import uuid + + +class TestConfig: + def test_write_read_uuid(self, tmp_path): + # Create a temporary file path + config_file = tmp_path / "config.yml" + + # Call the function to write UUID to file + pybamm.config.write_uuid_to_file(config_file) + + # Check that the file was created + assert config_file.exists() + + # Read the UUID using the read_uuid_from_file function + uuid_dict = pybamm.config.read_uuid_from_file(config_file) + + # Check that the UUID was read successfully + assert uuid_dict is not None + assert "uuid" in uuid_dict + + # Verify that the UUID is valid + + try: + uuid.UUID(uuid_dict["uuid"]) + except ValueError: + pytest.fail("Invalid UUID format")