Skip to content

Commit b253431

Browse files
committed
✨ Refactor changelog management: update documentation, remove unused constants, and add comprehensive tests for route configuration
1 parent f63eb83 commit b253431

File tree

3 files changed

+33
-241
lines changed

3 files changed

+33
-241
lines changed

packages/common-library/src/common_library/changelog.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
1+
"""
2+
CHANGELOG formatted-messages for API routes
3+
4+
- Append at the bottom of the route's description
5+
- These are displayed in the swagger doc
6+
- These are displayed in client's doc as well (auto-generator)
7+
- Inspired on this idea https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#describing-changes-between-versions
8+
"""
9+
110
from abc import ABC, abstractmethod
211
from collections.abc import Sequence
312
from enum import Enum, auto
4-
from typing import Any, ClassVar, Final, cast
13+
from typing import Any, ClassVar, cast
514

615
from packaging.version import Version
716

8-
from ..._meta import API_VERSION
9-
10-
#
11-
# CHANGELOG formatted-messages for API routes
12-
#
13-
# - Append at the bottom of the route's description
14-
# - These are displayed in the swagger doc
15-
# - These are displayed in client's doc as well (auto-generator)
16-
# - Inspired on this idea https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#describing-changes-between-versions
17-
#
18-
19-
# newly created endpoint in given version
20-
FMSG_CHANGELOG_NEW_IN_VERSION: Final[str] = "New in *version {}*\n"
21-
22-
# changes in given version with message
23-
FMSG_CHANGELOG_CHANGED_IN_VERSION: Final[str] = "Changed in *version {}*: {}\n"
24-
25-
# marked as deprecated
26-
FMSG_CHANGELOG_DEPRECATED_IN_VERSION: Final[str] = (
27-
"🚨 **Deprecated**: This endpoint is deprecated and will be removed in a future release.\n"
28-
"Please use `{}` instead.\n\n"
29-
)
30-
31-
# marked as deprecated and will be removed in given version
32-
FMSG_CHANGELOG_REMOVED_IN_VERSION: Final[str] = "Removed in *version {}*: {}\n"
33-
34-
35-
DEFAULT_MAX_STRING_LENGTH: Final[int] = 500
36-
3717

3818
class ChangelogType(Enum):
3919
"""Types of changelog entries in their lifecycle order"""
@@ -201,6 +181,7 @@ def validate_changelog(changelog: Sequence[ChangelogEntry]) -> None:
201181
def create_route_config(
202182
base_description: str = "",
203183
*,
184+
current_version: str | Version,
204185
changelog: Sequence[ChangelogEntry] | None = None,
205186
) -> dict[str, Any]:
206187
"""
@@ -212,6 +193,7 @@ def create_route_config(
212193
213194
Args:
214195
base_description: Main route description
196+
current_version: Current version of the API
215197
changelog: List of changelog entries indicating version history
216198
217199
Returns:
@@ -222,10 +204,12 @@ def create_route_config(
222204

223205
validate_changelog(changelog_list)
224206

207+
if isinstance(current_version, str):
208+
current_version = Version(current_version)
209+
225210
# Determine endpoint state
226211
is_deprecated = False
227212
is_unreleased = False
228-
current_version = Version(API_VERSION)
229213

230214
# Get the first entry (NEW) to check if unreleased
231215
if changelog_list and changelog_list[0].entry_type == ChangelogType.NEW:

packages/common-library/tests/test_changelogs.py renamed to packages/common-library/tests/test_changelog.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
# pylint: disable=unused-variable
66

77
import pytest
8-
from packaging.version import Version
9-
from pytest_mock import MockerFixture
10-
from simcore_service_api_server.api.routes._constants import (
8+
from common_library.changelog import (
119
ChangedEndpoint,
1210
ChangelogType,
1311
DeprecatedEndpoint,
@@ -17,20 +15,14 @@
1715
create_route_description,
1816
validate_changelog,
1917
)
18+
from packaging.version import Version
19+
from pytest_mock import MockerFixture
2020

2121

2222
@pytest.fixture
2323
def mock_api_version(mocker: MockerFixture) -> str:
2424
"""Fixture to mock the API_VERSION for testing purposes"""
25-
import simcore_service_api_server.api.routes._constants
26-
27-
mock_version = "0.7.0"
28-
mocker.patch.object(
29-
simcore_service_api_server.api.routes._constants,
30-
"API_VERSION",
31-
mock_version,
32-
)
33-
return mock_version
25+
return "0.7.0"
3426

3527

3628
def test_changelog_entry_types():
@@ -122,7 +114,7 @@ def test_create_route_description():
122114
assert "Changed in *version 0.6.0*: Added authentication" in desc
123115

124116

125-
def test_create_route_config_for_deprecated_endpoints() -> None:
117+
def test_create_route_config_for_deprecated_endpoints(mock_api_version: str) -> None:
126118
"""Test route configuration for deprecated endpoints"""
127119
alternative_route = "/v1/new-endpoint"
128120
changelog = [
@@ -133,6 +125,7 @@ def test_create_route_config_for_deprecated_endpoints() -> None:
133125
config = create_route_config(
134126
base_description="This is a deprecated endpoint",
135127
changelog=changelog,
128+
current_version=Version(mock_api_version),
136129
)
137130

138131
expected_config = {
@@ -159,6 +152,7 @@ def test_create_route_config_for_to_be_released_endpoints(
159152
config = create_route_config(
160153
base_description=f"This is a feature coming in version {future_version}",
161154
changelog=changelog,
155+
current_version=Version(mock_api_version),
162156
)
163157

164158
expected_config = {
@@ -173,7 +167,7 @@ def test_create_route_config_for_to_be_released_endpoints(
173167
assert config == expected_config
174168

175169

176-
def test_create_route_config_with_removal_notice() -> None:
170+
def test_create_route_config_with_removal_notice(mock_api_version: str) -> None:
177171
"""Test route configuration with explicit removal notice in changelog"""
178172
removal_message = "Use the new endpoint instead"
179173
alternative_route = "/v1/better-endpoint"
@@ -187,6 +181,7 @@ def test_create_route_config_with_removal_notice() -> None:
187181
config = create_route_config(
188182
base_description="This endpoint will be removed in version 0.9.0",
189183
changelog=changelog,
184+
current_version=Version(mock_api_version),
190185
)
191186

192187
expected_config = {
@@ -201,13 +196,14 @@ def test_create_route_config_with_removal_notice() -> None:
201196
assert config == expected_config
202197

203198

204-
def test_create_route_config_with_regular_endpoint() -> None:
199+
def test_create_route_config_with_regular_endpoint(mock_api_version: str) -> None:
205200
"""Test route configuration for a standard endpoint (not deprecated, not upcoming)"""
206201
changelog = [NewEndpoint("0.5.0")]
207202

208203
config = create_route_config(
209204
base_description="This is a standard endpoint",
210205
changelog=changelog,
206+
current_version=mock_api_version,
211207
)
212208

213209
expected_config = {
@@ -222,19 +218,21 @@ def test_create_route_config_with_regular_endpoint() -> None:
222218
assert config == expected_config
223219

224220

225-
def test_create_route_config_with_mixed_changelog() -> None:
226-
"""Test route configuration with a complex changelog containing multiple entries"""
221+
def test_create_route_config_with_mixed_changelog(mock_api_version: str) -> None:
222+
227223
alternative_route = "/v1/better-endpoint"
228224
changelog = [
229225
NewEndpoint("0.5.0"),
230226
ChangedEndpoint("0.6.0", "Added authentication"),
227+
ChangedEndpoint("0.6.2", "Fixed a bug"),
231228
DeprecatedEndpoint(alternative_route),
232229
RemovedEndpoint("0.9.0", "Use the new endpoint instead"),
233230
]
234231

235232
config = create_route_config(
236233
base_description="This endpoint has a complex history",
237234
changelog=changelog,
235+
current_version=mock_api_version,
238236
)
239237

240238
expected_config = {
@@ -249,10 +247,11 @@ def test_create_route_config_with_mixed_changelog() -> None:
249247
assert config == expected_config
250248

251249

252-
def test_create_route_config_with_empty_changelog() -> None:
253-
"""Test route configuration with an empty changelog"""
250+
def test_create_route_config_with_empty_changelog(mock_api_version: str) -> None:
251+
254252
config = create_route_config(
255253
base_description="This endpoint has no changelog",
254+
current_version=mock_api_version,
256255
)
257256

258257
expected_config = {

0 commit comments

Comments
 (0)