Skip to content

Commit

Permalink
Add docstring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomass-dev committed Jan 29, 2025
1 parent 8204d30 commit a9af26a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
18 changes: 17 additions & 1 deletion skore/src/skore/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,28 @@ def __init__(
*,
exist_ok: bool = False,
):
"""
Initialize a Project.
Initialize a project, by creating a new project or by loading an existing one.
Parameters
----------
path : str or Path, optional
The path of the project to initialize, default "./project.skore".
exist_ok: bool, optional
Ignore if the project already exists or raise an error, default False.
Raises
------
FileExistsError
"""
self.path = Path(path)
self.path = self.path.with_suffix(".skore")
self.path = self.path.resolve()

if not exist_ok and self.path.exists():
raise FileExistsError
raise FileExistsError(f"Project '{str(path)}' already exists.")

self.__item_storage_dirpath = self.path / "items"
self.__view_storage_dirpath = self.path / "views"
Expand Down
19 changes: 19 additions & 0 deletions skore/tests/unit/project/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@
from matplotlib.testing.compare import compare_images
from PIL import Image
from sklearn.ensemble import RandomForestClassifier
from skore import Project


@pytest.fixture(autouse=True)
def monkeypatch_datetime(monkeypatch, MockDatetime):
monkeypatch.setattr("skore.persistence.item.item.datetime", MockDatetime)


def test_init(tmp_path):
dirpath = tmp_path / "my-project.skore"

# Ensure missing project can be created
Project(dirpath)

assert dirpath.exists()
assert (dirpath / "items").exists()
assert (dirpath / "views").exists()

# Ensure existing project raise an error with `exist_ok=False`
with pytest.raises(FileExistsError):
Project(dirpath, exist_ok=False)

# Ensure existing project can be loaded with `exist_ok=True`
Project(dirpath, exist_ok=True)


def test_put_string_item(in_memory_project):
in_memory_project.put("string_item", "Hello, World!")
assert in_memory_project.get("string_item") == "Hello, World!"
Expand Down

0 comments on commit a9af26a

Please sign in to comment.