Skip to content

Commit

Permalink
agriya comments and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Sep 16, 2024
1 parent 4bf6d80 commit f7ec885
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
5 changes: 5 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 0 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
31 changes: 14 additions & 17 deletions src/pybamm/config.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -42,16 +33,24 @@ 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

# Check if the config file already exists
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)

Expand All @@ -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

Check warning on line 69 in src/pybamm/config.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/config.py#L69

Added line #L69 was not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion src/pybamm/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def disable():
disable()

Check warning on line 19 in src/pybamm/telemetry.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/telemetry.py#L19

Added line #L19 was not covered by tests


def capture(event):
def capture(event): # pragma: no cover
# don't capture events in automated testing
if pybamm.config.is_running_tests():
return
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -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")
30 changes: 30 additions & 0 deletions tests/unit/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit f7ec885

Please sign in to comment.