Add tests for repo fetching, byte totals and chart output
This commit is contained in:
1 parent
d2b92fc5a5
commit
ab905577b8
1 file changed
+189
@@ -0,0 +1,189 @@
|
|||||||
|
"""Fetching repos, adding up language bytes, and drawing the chart."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
|
||||||
|
import language_stats
|
||||||
|
from language_stats import (
|
||||||
|
aggregate_language_bytes,
|
||||||
|
fetch_repos,
|
||||||
|
generate_markdown,
|
||||||
|
get_session,
|
||||||
|
subtract_excluded_files,
|
||||||
|
)
|
||||||
|
|
||||||
|
REPOS_PAGE = "https://gitea.test/api/v1/users/alice/repos?page={}&limit=50"
|
||||||
|
WEBSITE_LANGS = "https://gitea.test/api/v1/repos/alice/website/languages"
|
||||||
|
SCRIPTS_LANGS = "https://gitea.test/api/v1/repos/alice/scripts/languages"
|
||||||
|
AFRAME_FILE = "https://gitea.test/api/v1/repos/alice/game/contents/static/aframe.js"
|
||||||
|
VUE4_FILE = "https://gitea.test/api/v1/repos/alice/guide/contents/home/Vue3-v.4.html"
|
||||||
|
VUE5_FILE = "https://gitea.test/api/v1/repos/alice/guide/contents/home/Vue3-v.5.html"
|
||||||
|
|
||||||
|
|
||||||
|
# --- fetch_repos ------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_repos_follows_pages_until_an_empty_page(gitea):
|
||||||
|
gitea.add("GET", REPOS_PAGE.format(1), [{"name": "website"}, {"name": "scripts"}])
|
||||||
|
gitea.add("GET", REPOS_PAGE.format(2), [{"name": "notes"}])
|
||||||
|
gitea.add("GET", REPOS_PAGE.format(3), [])
|
||||||
|
assert fetch_repos(get_session()) == [
|
||||||
|
{"name": "website"},
|
||||||
|
{"name": "scripts"},
|
||||||
|
{"name": "notes"},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_repos_raises_when_the_server_errors(gitea):
|
||||||
|
gitea.add("GET", REPOS_PAGE.format(1), {"message": "internal error"}, status=500)
|
||||||
|
with pytest.raises(requests.HTTPError):
|
||||||
|
fetch_repos(get_session())
|
||||||
|
|
||||||
|
|
||||||
|
# --- aggregate_language_bytes ---------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_language_bytes_are_added_up_across_repos(gitea):
|
||||||
|
gitea.add("GET", WEBSITE_LANGS, {"HTML": 1000, "JavaScript": 500})
|
||||||
|
gitea.add("GET", SCRIPTS_LANGS, {"Python": 2000, "JavaScript": 250})
|
||||||
|
repos = [{"name": "website"}, {"name": "scripts"}]
|
||||||
|
assert aggregate_language_bytes(get_session(), repos) == {
|
||||||
|
"HTML": 1000,
|
||||||
|
"JavaScript": 750,
|
||||||
|
"Python": 2000,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_language_bytes_sent_as_strings_are_counted(gitea):
|
||||||
|
# Regression test for d8f72b7: the API sometimes returns "2000", not 2000.
|
||||||
|
gitea.add("GET", SCRIPTS_LANGS, {"Python": "2000", "Lua": 300})
|
||||||
|
assert aggregate_language_bytes(get_session(), [{"name": "scripts"}]) == {
|
||||||
|
"Python": 2000,
|
||||||
|
"Lua": 300,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_numeric_language_bytes_are_skipped(gitea):
|
||||||
|
gitea.add("GET", SCRIPTS_LANGS, {"Python": 2000, "Lua": "lots", "Bash": None})
|
||||||
|
assert aggregate_language_bytes(get_session(), [{"name": "scripts"}]) == {
|
||||||
|
"Python": 2000
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_repo_whose_languages_are_forbidden_is_skipped_with_a_warning(gitea, capsys):
|
||||||
|
# Regression test for 2b4ef71: a limited token gets a 403 for private repos.
|
||||||
|
gitea.add(
|
||||||
|
"GET",
|
||||||
|
WEBSITE_LANGS,
|
||||||
|
{"message": "token scope is limited to public repos"},
|
||||||
|
status=403,
|
||||||
|
)
|
||||||
|
gitea.add("GET", SCRIPTS_LANGS, {"Python": 2000})
|
||||||
|
repos = [{"name": "website"}, {"name": "scripts"}]
|
||||||
|
assert aggregate_language_bytes(get_session(), repos) == {"Python": 2000}
|
||||||
|
assert (
|
||||||
|
"Warning: Could not fetch languages for website: 403" in capsys.readouterr().err
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_message_inside_a_success_response_is_skipped_with_a_warning(
|
||||||
|
gitea, capsys
|
||||||
|
):
|
||||||
|
gitea.add("GET", WEBSITE_LANGS, {"message": "token scope is limited"})
|
||||||
|
gitea.add("GET", SCRIPTS_LANGS, {"Python": 2000})
|
||||||
|
repos = [{"name": "website"}, {"name": "scripts"}]
|
||||||
|
assert aggregate_language_bytes(get_session(), repos) == {"Python": 2000}
|
||||||
|
assert "Warning: Unexpected response for website" in capsys.readouterr().err
|
||||||
|
|
||||||
|
|
||||||
|
def test_response_that_is_not_a_dict_is_skipped(gitea):
|
||||||
|
gitea.add("GET", WEBSITE_LANGS, ["HTML", "JavaScript"])
|
||||||
|
gitea.add("GET", SCRIPTS_LANGS, {"Python": 2000})
|
||||||
|
repos = [{"name": "website"}, {"name": "scripts"}]
|
||||||
|
assert aggregate_language_bytes(get_session(), repos) == {"Python": 2000}
|
||||||
|
|
||||||
|
|
||||||
|
# --- subtract_excluded_files ------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_every_excluded_file_size_is_subtracted_from_its_language(gitea, monkeypatch):
|
||||||
|
monkeypatch.setattr(
|
||||||
|
language_stats,
|
||||||
|
"EXCLUDE_FILES",
|
||||||
|
[
|
||||||
|
("game", "static/aframe.js", "JavaScript"),
|
||||||
|
("guide", "home/Vue3-v.4.html", "HTML"),
|
||||||
|
("guide", "home/Vue3-v.5.html", "HTML"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
gitea.add("GET", AFRAME_FILE, {"size": 300})
|
||||||
|
gitea.add("GET", VUE4_FILE, {"size": 100})
|
||||||
|
gitea.add("GET", VUE5_FILE, {"size": 150})
|
||||||
|
totals = {"JavaScript": 1000, "HTML": 500, "Python": 400}
|
||||||
|
assert subtract_excluded_files(get_session(), totals) == {
|
||||||
|
"JavaScript": 700,
|
||||||
|
"HTML": 250,
|
||||||
|
"Python": 400,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_subtracting_an_excluded_file_never_goes_below_zero(gitea, monkeypatch):
|
||||||
|
monkeypatch.setattr(
|
||||||
|
language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")]
|
||||||
|
)
|
||||||
|
gitea.add("GET", AFRAME_FILE, {"size": 5000})
|
||||||
|
totals = {"JavaScript": 1000, "Python": 400}
|
||||||
|
assert subtract_excluded_files(get_session(), totals) == {
|
||||||
|
"JavaScript": 0,
|
||||||
|
"Python": 400,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_excluded_file_that_no_longer_exists_is_ignored(gitea, monkeypatch):
|
||||||
|
monkeypatch.setattr(
|
||||||
|
language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")]
|
||||||
|
)
|
||||||
|
gitea.add("GET", AFRAME_FILE, {"message": "not found"}, status=404)
|
||||||
|
totals = {"JavaScript": 1000}
|
||||||
|
assert subtract_excluded_files(get_session(), totals) == {"JavaScript": 1000}
|
||||||
|
|
||||||
|
|
||||||
|
def test_network_error_on_an_excluded_file_is_ignored(gitea, monkeypatch):
|
||||||
|
monkeypatch.setattr(
|
||||||
|
language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")]
|
||||||
|
)
|
||||||
|
gitea.add_error("GET", AFRAME_FILE, requests.ConnectionError("connection reset"))
|
||||||
|
totals = {"JavaScript": 1000}
|
||||||
|
assert subtract_excluded_files(get_session(), totals) == {"JavaScript": 1000}
|
||||||
|
|
||||||
|
|
||||||
|
# --- generate_markdown --------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_lists_languages_largest_first_with_bars_and_percentages():
|
||||||
|
# Python 75% -> 75/2 = 37 bar blocks; JavaScript 25% -> 25/2 = 12 blocks.
|
||||||
|
# Lines are joined with a backslash (a markdown line break); the heading is
|
||||||
|
# followed by a single newline and the last line has no trailing break
|
||||||
|
# (regression tests for 95aaf91 and 57a5188).
|
||||||
|
assert generate_markdown({"JavaScript": 250, "Python": 750}) == (
|
||||||
|
"## 📊 Languages\n"
|
||||||
|
+ "█" * 37
|
||||||
|
+ " Python 75.0%\\\n"
|
||||||
|
+ "█" * 12
|
||||||
|
+ " JavaScript 25.0%"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_percentages_are_rounded_to_one_decimal():
|
||||||
|
# Python 2/3 = 66.67% -> 66.7, 33 blocks; Lua 1/3 = 33.33% -> 33.3, 16 blocks.
|
||||||
|
assert generate_markdown({"Python": 2, "Lua": 1}) == (
|
||||||
|
"## 📊 Languages\n" + "█" * 33 + " Python 66.7%\\\n" + "█" * 16 + " Lua 33.3%"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_is_empty_when_there_are_no_languages():
|
||||||
|
assert generate_markdown({}) == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_is_empty_when_every_language_has_zero_bytes():
|
||||||
|
assert generate_markdown({"Python": 0, "Lua": 0}) == ""
|
||||||
Reference in new issue
Block a user