-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7042460
commit 0a21ce3
Showing
6 changed files
with
147 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/ragbits-core/tests/unit/utils/pyproject/test_find.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from ragbits.core.utils._pyproject import find_pyproject | ||
|
||
projects_dir = Path(__file__).parent / "testprojects" | ||
|
||
|
||
def test_find_in_current_dir(): | ||
"""Test finding a pyproject.toml file in the current directory.""" | ||
found = find_pyproject(projects_dir / "happy_project") | ||
assert found == projects_dir / "happy_project" / "pyproject.toml" | ||
|
||
|
||
def test_find_in_parent_dir(): | ||
"""Test finding a pyproject.toml file in a parent directory.""" | ||
found = find_pyproject(projects_dir / "happy_project" / "subdirectory") | ||
assert found == projects_dir / "happy_project" / "pyproject.toml" | ||
|
||
|
||
def test_find_not_found(): | ||
"""Test that it raises FileNotFoundError if the file is not found.""" | ||
with pytest.raises(FileNotFoundError): | ||
find_pyproject(Path("/")) |
26 changes: 26 additions & 0 deletions
26
packages/ragbits-core/tests/unit/utils/pyproject/test_get_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from pathlib import Path | ||
|
||
from ragbits.core.utils._pyproject import get_ragbits_config | ||
|
||
projects_dir = Path(__file__).parent / "testprojects" | ||
|
||
|
||
def test_get_config(): | ||
"""Test getting config from pyproject.toml file.""" | ||
config = get_ragbits_config(projects_dir / "happy_project") | ||
|
||
assert config == { | ||
"lorem": "ipsum", | ||
"happy-project": { | ||
"foo": "bar", | ||
"is_happy": True, | ||
"happiness_level": 100, | ||
}, | ||
} | ||
|
||
|
||
def test_get_config_no_file(): | ||
"""Test getting config when the pyproject.toml file is not found.""" | ||
config = get_ragbits_config(Path("/")) | ||
|
||
assert config == {} |
68 changes: 68 additions & 0 deletions
68
packages/ragbits-core/tests/unit/utils/pyproject/test_get_instace.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from pathlib import Path | ||
|
||
from pydantic import BaseModel | ||
|
||
from ragbits.core.utils._pyproject import get_config_instance | ||
|
||
projects_dir = Path(__file__).parent / "testprojects" | ||
|
||
|
||
class HappyProjectConfig(BaseModel): | ||
foo: str | ||
is_happy: bool | ||
happiness_level: int | ||
|
||
|
||
class PartialHappyProjectConfig(BaseModel): | ||
foo: str | ||
is_happy: bool | ||
|
||
|
||
class OptionalHappyProjectConfig(BaseModel): | ||
foo: str = "bar" | ||
is_happy: bool = True | ||
happiness_level: int = 100 | ||
|
||
|
||
def test_get_config_instance(): | ||
"""Test getting Pydantic model instance from pyproject.toml file.""" | ||
config = get_config_instance( | ||
HappyProjectConfig, | ||
subproject="happy-project", | ||
current_dir=projects_dir / "happy_project", | ||
) | ||
|
||
assert config == HappyProjectConfig(foo="bar", is_happy=True, happiness_level=100) | ||
|
||
|
||
def test_get_config_instance_additional_fields(): | ||
"""Test that unknown fields are ignored.""" | ||
config = get_config_instance( | ||
PartialHappyProjectConfig, | ||
subproject="happy-project", | ||
current_dir=projects_dir / "happy_project", | ||
) | ||
|
||
assert config == PartialHappyProjectConfig(foo="bar", is_happy=True) | ||
|
||
|
||
def test_get_config_instance_optional_fields(): | ||
"""Test that optional fields are filled with default values if not present in the file.""" | ||
config = get_config_instance( | ||
OptionalHappyProjectConfig, | ||
subproject="happy-project", | ||
current_dir=projects_dir / "happy_project", | ||
) | ||
|
||
assert config == OptionalHappyProjectConfig(foo="bar", is_happy=True, happiness_level=100) | ||
|
||
|
||
def test_get_config_instance_no_file(): | ||
"""Test getting config when the pyproject.toml file is not found (wich no required fields).""" | ||
config = get_config_instance( | ||
OptionalHappyProjectConfig, | ||
subproject="happy-project", | ||
current_dir=Path("/"), | ||
) | ||
|
||
assert config == OptionalHappyProjectConfig() |
10 changes: 10 additions & 0 deletions
10
packages/ragbits-core/tests/unit/utils/pyproject/testprojects/happy_project/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[project] | ||
name = "happy-project" | ||
|
||
[tool.ragbits] | ||
lorem = "ipsum" | ||
|
||
[tool.ragbits.happy-project] | ||
foo = "bar" | ||
is_happy = true | ||
happiness_level = 100 |