Skip to content

Commit 486eeea

Browse files
committed
Add tests
1 parent 9f31273 commit 486eeea

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/test_config.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from pathlib import Path
2+
from unittest.mock import MagicMock, mock_open, patch
3+
4+
import pytest
5+
from piny import ValidationError
6+
7+
from gitea_github_sync.config import Config, config_file_location, load_config
8+
9+
VALID_CONFIG_FILE = """
10+
github_token: some-github-token
11+
gitea_token: some-gitea-token
12+
"""
13+
14+
VALID_CONFIG = Config(github_token="some-github-token", gitea_token="some-gitea-token")
15+
16+
DEFAULT_CONFIG_FILE_PATH = config_file_location()
17+
18+
19+
@patch("builtins.open", new_callable=mock_open, read_data=VALID_CONFIG_FILE)
20+
def test_load_config(mock_file_open: MagicMock) -> None:
21+
config = load_config()
22+
23+
assert config == VALID_CONFIG
24+
mock_file_open.assert_called_once_with(DEFAULT_CONFIG_FILE_PATH)
25+
26+
27+
@patch("builtins.open", new_callable=mock_open, read_data=VALID_CONFIG_FILE)
28+
def test_load_config_non_default_path(mock_file_open: MagicMock) -> None:
29+
other_path = Path("/config.yml")
30+
config = load_config(config_location=other_path)
31+
32+
assert config == VALID_CONFIG
33+
mock_file_open.assert_called_once_with(other_path)
34+
35+
36+
@patch("builtins.open", new_callable=mock_open, read_data="bad-file")
37+
def test_load_config_bad_file(mock_file_open: MagicMock) -> None:
38+
with pytest.raises(ValidationError):
39+
load_config()
40+
41+
mock_file_open.assert_called_once_with(DEFAULT_CONFIG_FILE_PATH)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ commands =
3131
isort -c .
3232
flake8 gitea_github_sync tests
3333
mypy -p gitea_github_sync --install-types --non-interactive
34+
mypy tests/ --install-types --non-interactive
3435
pytest --cov-report term-missing --cov-report=xml:./coverage.xml --cov=gitea_github_sync tests/ {posargs}

0 commit comments

Comments
 (0)