Skip to content

Commit

Permalink
test: increase code coverage for main script
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicconike committed Jun 30, 2024
1 parent 72c964f commit f0a7f52
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
playwright install --with-deps firefox
- name: Run tests with coverage
run: pytest --cov=api.main --cov=api.steam_stats --cov=api.steam_workshop --cov=api.card --cov-report=xml --cov-report=term
run: pytest --cov=api.main --cov=api.steam_stats --cov=api.steam_workshop --cov=api.card --cov-report=xml --cov-report=term-missing
env:
INPUT_STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}
INPUT_STEAM_ID: ${{ vars.STEAM_ID }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ And thank you for considering to contribute.
- [Progress Bar Design](https://github.com/Nicconike/Steam-Stats/blob/master/assets/style.css)[^2] - [Ana Tudor](https://codepen.io/thebabydino)

***
Created with Game Sense & ❤️ by [Nicco](https://github.com/Nicconike)
Created with 🐍 & ❤️ by [Nicco](https://github.com/Nicconike)

[^1]: Unfortunately, Steam Web API doesn't support Web Sockets so the profile status cannot be updated in real time as it gets updated in steam profile 🥲

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Steam Stats"
description: "Update your GitHub Profile with your latest Steam Stats"
description: "Showcase your Steam Gaming Stats in your GitHub Profile"
author: "Nicconike"
inputs:
GH_TOKEN:
Expand Down
104 changes: 95 additions & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Test Main Runner Script"""
# Disable pylint warnings for false positives
# pylint: disable=duplicate-code
# pylint: disable=duplicate-code, redefined-outer-name
from unittest import mock
from unittest.mock import patch, ANY
import pytest
from api import main
from api.main import (main, update_readme, get_player_summaries, get_recently_played_games,
fetch_workshop_item_links, generate_steam_stats, generate_workshop_stats)


@pytest.fixture(autouse=True)
Expand All @@ -13,6 +15,90 @@ def mock_env_vars(monkeypatch):
monkeypatch.setenv("INPUT_STEAM_CUSTOM_ID", "dummy_custom_id")


@pytest.fixture
def readme_content():
"""Fixture to provide initial README content."""
return "Some initial content\n<!-- START -->\nOld content\n<!-- END -->\nSome final content"


@pytest.fixture
def updated_readme_content():
"""Fixture to provide expected updated README content."""
return "Some initial content\n<!-- START -->\nNew content\n<!-- END -->\nSome final content"


def test_update_readme_success(readme_content, updated_readme_content, tmp_path):
"""Test successful update of README.md."""
readme_path = tmp_path / "README.md"
readme_path.write_text(readme_content, encoding="utf-8")

with mock.patch("api.main.logger") as mock_logger:
update_readme("New content", "<!-- START -->",
"<!-- END -->", readme_path=str(readme_path))

result = readme_path.read_text(encoding="utf-8")
if result != updated_readme_content:
raise AssertionError("Expected README content to be updated")

if mock_logger.error.called:
raise AssertionError("Expected no errors to be logged")


def test_update_readme_start_marker_not_found(readme_content, tmp_path):
"""Test when start marker is not found in README.md."""
readme_path = tmp_path / "README.md"
readme_path.write_text(readme_content, encoding="utf-8")

with mock.patch("api.main.logger") as mock_logger:
update_readme("New content", "<!-- NON_EXISTENT_START -->",
"<!-- END -->", readme_path=str(readme_path))

result = readme_path.read_text(encoding="utf-8")
if result != readme_content:
raise AssertionError("Expected README content to remain unchanged")

if not mock_logger.error.called:
raise AssertionError(
"Expected an error to be logged for missing start marker")


def test_update_readme_end_marker_not_found(readme_content, tmp_path):
"""Test when end marker is not found in README.md."""
readme_path = tmp_path / "README.md"
readme_path.write_text(readme_content, encoding="utf-8")

with mock.patch("api.main.logger") as mock_logger:
update_readme("New content", "<!-- START -->",
"<!-- NON_EXISTENT_END -->", readme_path=str(readme_path))

result = readme_path.read_text(encoding="utf-8")
if result != readme_content:
raise AssertionError("Expected README content to remain unchanged")

if not mock_logger.error.called:
raise AssertionError(
"Expected an error to be logged for missing end marker")


def test_update_readme_no_markers(tmp_path):
"""Test when neither start nor end markers are found in README.md."""
readme_content = "Some initial content\nSome final content"
readme_path = tmp_path / "README.md"
readme_path.write_text(readme_content, encoding="utf-8")

with mock.patch("api.main.logger") as mock_logger:
update_readme("New content", "<!-- START -->",
"<!-- END -->", readme_path=str(readme_path))

result = readme_path.read_text(encoding="utf-8")
if result != readme_content:
raise AssertionError("Expected README content to remain unchanged")

if not mock_logger.error.called:
raise AssertionError(
"Expected errors to be logged for missing markers")


def test_get_player_summaries_success(requests_mock):
"""Test successful retrieval of player summaries"""
requests_mock.get(
Expand All @@ -23,7 +109,7 @@ def test_get_player_summaries_success(requests_mock):
"lastlogoff": 1609459200, "timecreated": 1609459200
}]}}
)
result = main.get_player_summaries()
result = get_player_summaries()
if result is None:
raise AssertionError("Expected result to be not None")
if result is None or "response" not in result:
Expand All @@ -41,7 +127,7 @@ def test_get_recently_played_games_success(requests_mock):
"appid": 12345, "img_icon_url": "icon.jpg"}
]}}
)
result = main.get_recently_played_games()
result = get_recently_played_games()
if result is None:
raise AssertionError("Expected result to be not None")
if result is None or "response" not in result:
Expand All @@ -56,7 +142,7 @@ def test_get_recently_played_games_no_games(requests_mock):
'http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/',
json={"response": {"total_count": 0}}
)
result = main.get_recently_played_games()
result = get_recently_played_games()
if result is not None:
raise AssertionError("Expected result to be None")

Expand All @@ -72,7 +158,7 @@ def test_fetch_workshop_item_links_success(requests_mock):
'https://api.steampowered.com/ISteamWebAPIUtil/GetServerInfo/v1/',
json={"servertime": 1234567890}
)
result = main.fetch_workshop_item_links("dummy_custom_id", "dummy_api_key")
result = fetch_workshop_item_links("dummy_custom_id", "dummy_api_key")
expected_result = [
"https://steamcommunity.com/sharedfiles/filedetails/?id=2984474065"]
if result != expected_result:
Expand All @@ -96,7 +182,7 @@ def test_generate_steam_stats(requests_mock):
"appid": 12345, "img_icon_url": "icon.jpg"}
]}}
)
result = main.generate_steam_stats()
result = generate_steam_stats()
if "![Steam Summary]" not in result:
raise AssertionError("Expected '![Steam Summary]' to be in result")
if "![Recently Played Games]" not in result:
Expand All @@ -123,7 +209,7 @@ def test_generate_workshop_stats(requests_mock):
'https://steamcommunity.com/id/dummy_custom_id/myworkshopfiles/?p=2',
text=''
)
result = main.generate_workshop_stats()
result = generate_workshop_stats()
if "![Steam Workshop Stats]" not in result:
raise AssertionError(
"Expected '![Steam Workshop Stats]' to be in result")
Expand Down Expand Up @@ -160,7 +246,7 @@ def test_main(requests_mock):
text='<table class="stats_table"><tr><td>1,000</td><td>Unique Visitors</td></tr></table>'
)
with patch('api.main.update_readme') as mock_update_readme:
main.main()
main()
mock_update_readme.assert_any_call(
ANY, "<!-- Steam-Stats start -->", "<!-- Steam-Stats end -->")
mock_update_readme.assert_any_call(
Expand Down

0 comments on commit f0a7f52

Please sign in to comment.