-
Notifications
You must be signed in to change notification settings - Fork 2
Add Validators, Utils, and Unit Tests for Data Integrity #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
| def pretty_print_error(error: Exception) -> None: | ||
| """Pretty print errors using rich if available, else fallback to plain text""" | ||
| try: | ||
| from rich.console import Console | ||
|
|
||
| console = Console() | ||
| console.print(f"[bold red]Error:[/bold red] {error}") | ||
castroofelipee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except ImportError: | ||
| print(f"Error: {error}") | ||
This file contains hidden or 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,7 @@ | ||
| from src.venvalid.errors import EnvSafeError | ||
|
|
||
|
|
||
| def validate_env_var(name: str, value: str | None): | ||
castroofelipee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if not value: | ||
| raise EnvSafeError(f"Missing required environment variable: {name}") | ||
| return True | ||
This file contains hidden or 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,40 @@ | ||
| import sys | ||
| import types | ||
|
|
||
| import src.venvalid.utils_errors as utils_errors | ||
|
|
||
|
|
||
| class DummyConsole: | ||
| def __init__(self): | ||
| self.last_message = None | ||
|
|
||
| def print(self, msg): | ||
| self.last_message = msg | ||
|
|
||
|
|
||
| def test_pretty_print_error_with_rich(monkeypatch): | ||
| dummy_console = DummyConsole() | ||
|
|
||
| fake_rich_pkg = types.ModuleType("rich") | ||
| fake_rich_pkg.__path__ = [] # mark as package | ||
|
|
||
| fake_console_mod = types.ModuleType("rich.console") | ||
| setattr(fake_console_mod, "Console", lambda: dummy_console) | ||
castroofelipee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| monkeypatch.setitem(sys.modules, "rich", fake_rich_pkg) | ||
| monkeypatch.setitem(sys.modules, "rich.console", fake_console_mod) | ||
|
|
||
| utils_errors.pretty_print_error(ValueError("boom")) | ||
|
|
||
| assert dummy_console.last_message is not None | ||
| assert "boom" in dummy_console.last_message | ||
|
|
||
|
|
||
| def test_pretty_print_error_without_rich(monkeypatch, capsys): | ||
| monkeypatch.delitem(sys.modules, "rich.console", raising=False) | ||
| monkeypatch.delitem(sys.modules, "rich", raising=False) | ||
|
|
||
| utils_errors.pretty_print_error(ValueError("fail")) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert "Error: fail" in out | ||
This file contains hidden or 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,15 @@ | ||
| import pytest | ||
|
|
||
| from src.venvalid.errors import EnvSafeError | ||
| from src.venvalid.validator import validate_env_var | ||
|
|
||
|
|
||
| def test_validate_env_var_success(): | ||
| assert validate_env_var("API_KEY", "secret") is True | ||
|
|
||
|
|
||
| def test_validate_env_var_missing(): | ||
| with pytest.raises(EnvSafeError) as exc_info: | ||
| validate_env_var("API_KEY", None) | ||
|
|
||
| assert "Missing required environment variable: API_KEY" in str(exc_info.value) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.