From db6fd22b6b1fceb8f8e6a2b23c7cb615b55c2c5e Mon Sep 17 00:00:00 2001 From: Dukastlik <49510873+Dukastlik@users.noreply.github.com> Date: Wed, 13 Dec 2023 08:22:55 +0300 Subject: [PATCH] WEB: Better management of releases (#56207) --- .github/workflows/docbuild-and-upload.yml | 3 + web/pandas_web.py | 15 +++- web/tests/test_pandas_web.py | 88 +++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 web/tests/test_pandas_web.py diff --git a/.github/workflows/docbuild-and-upload.yml b/.github/workflows/docbuild-and-upload.yml index af452363666b5..4e88bfff69327 100644 --- a/.github/workflows/docbuild-and-upload.yml +++ b/.github/workflows/docbuild-and-upload.yml @@ -46,6 +46,9 @@ jobs: - name: Build Pandas uses: ./.github/actions/build_pandas + - name: Test website + run: python -m pytest web/ + - name: Build website run: python web/pandas_web.py web/pandas --target-path=web/build diff --git a/web/pandas_web.py b/web/pandas_web.py index 407debf5828be..ab2e72da850c4 100755 --- a/web/pandas_web.py +++ b/web/pandas_web.py @@ -27,6 +27,7 @@ import collections import datetime import importlib +import itertools import json import operator import os @@ -40,6 +41,7 @@ import feedparser import jinja2 import markdown +from packaging import version import requests import yaml @@ -245,6 +247,7 @@ def home_add_releases(context): context["releases"].append( { "name": release["tag_name"].lstrip("v"), + "parsed_version": version.parse(release["tag_name"].lstrip("v")), "tag": release["tag_name"], "published": published, "url": ( @@ -254,7 +257,17 @@ def home_add_releases(context): ), } ) - + # sorting out obsolete versions + grouped_releases = itertools.groupby( + context["releases"], + key=lambda r: (r["parsed_version"].major, r["parsed_version"].minor), + ) + context["releases"] = [ + max(release_group, key=lambda r: r["parsed_version"].minor) + for _, release_group in grouped_releases + ] + # sorting releases by version number + context["releases"].sort(key=lambda r: r["parsed_version"], reverse=True) return context @staticmethod diff --git a/web/tests/test_pandas_web.py b/web/tests/test_pandas_web.py new file mode 100644 index 0000000000000..827c1d4dbea40 --- /dev/null +++ b/web/tests/test_pandas_web.py @@ -0,0 +1,88 @@ +from unittest.mock import ( + mock_open, + patch, +) + +import pytest +import requests + +from web.pandas_web import Preprocessors + + +class MockResponse: + def __init__(self, status_code: int, response: dict): + self.status_code = status_code + self._resp = response + + def json(self): + return self._resp + + @staticmethod + def raise_for_status(): + return + + +@pytest.fixture +def context() -> dict: + return { + "main": {"github_repo_url": "pandas-dev/pandas"}, + "target_path": "test_target_path", + } + + +@pytest.fixture(scope="function") +def mock_response(monkeypatch, request): + def mocked_resp(*args, **kwargs): + status_code, response = request.param + return MockResponse(status_code, response) + + monkeypatch.setattr(requests, "get", mocked_resp) + + +_releases_list = [ + { + "prerelease": False, + "published_at": "2024-01-19T03:34:05Z", + "tag_name": "v1.5.6", + "assets": None, + }, + { + "prerelease": False, + "published_at": "2023-11-10T19:07:37Z", + "tag_name": "v2.1.3", + "assets": None, + }, + { + "prerelease": False, + "published_at": "2023-08-30T13:24:32Z", + "tag_name": "v2.1.0", + "assets": None, + }, + { + "prerelease": False, + "published_at": "2023-04-30T13:24:32Z", + "tag_name": "v2.0.0", + "assets": None, + }, + { + "prerelease": True, + "published_at": "2023-01-19T03:34:05Z", + "tag_name": "v1.5.3xd", + "assets": None, + }, + { + "prerelease": False, + "published_at": "2027-01-19T03:34:05Z", + "tag_name": "v10.0.1", + "assets": None, + }, +] + + +@pytest.mark.parametrize("mock_response", [(200, _releases_list)], indirect=True) +def test_web_preprocessor_creates_releases(mock_response, context): + m = mock_open() + with patch("builtins.open", m): + context = Preprocessors.home_add_releases(context) + release_versions = [release["name"] for release in context["releases"]] + assert release_versions == ["10.0.1", "2.1.3", "2.0.0", "1.5.6"]