Skip to content

Commit

Permalink
Add support for PEP 700 (#12727)
Browse files Browse the repository at this point in the history
* Change API version to 1.1

* Fix flaky test

* Add versions field

* Add file size field

* Add file upload-time field

---------

Co-authored-by: Donald Stufft <[email protected]>
  • Loading branch information
di and dstufft authored Feb 25, 2023
1 parent f096db0 commit 80d136a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions tests/common/db/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Meta:
]
)
)
size = factory.Faker("pyint")


class RoleFactory(WarehouseFactory):
Expand Down
37 changes: 26 additions & 11 deletions tests/unit/api/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import pretend
import pytest

from packaging.version import parse
from pyramid.httpexceptions import HTTPMovedPermanently
from pyramid.testing import DummyRequest

from warehouse.api import simple
from warehouse.packaging.utils import API_VERSION

from ...common.db.accounts import UserFactory
from ...common.db.packaging import (
Expand Down Expand Up @@ -81,7 +83,7 @@ class TestSimpleIndex:
def test_no_results_no_serial(self, db_request, content_type, renderer_override):
db_request.accept = content_type
assert simple.simple_index(db_request) == {
"meta": {"_last-serial": 0, "api-version": "1.0"},
"meta": {"_last-serial": 0, "api-version": API_VERSION},
"projects": [],
}
assert db_request.response.headers["X-PyPI-Last-Serial"] == "0"
Expand All @@ -99,7 +101,7 @@ def test_no_results_with_serial(self, db_request, content_type, renderer_overrid
user = UserFactory.create()
je = JournalEntryFactory.create(submitted_by=user)
assert simple.simple_index(db_request) == {
"meta": {"_last-serial": je.id, "api-version": "1.0"},
"meta": {"_last-serial": je.id, "api-version": API_VERSION},
"projects": [],
}
assert db_request.response.headers["X-PyPI-Last-Serial"] == str(je.id)
Expand All @@ -119,7 +121,7 @@ def test_with_results_no_serial(self, db_request, content_type, renderer_overrid
for x in [ProjectFactory.create() for _ in range(3)]
]
assert simple.simple_index(db_request) == {
"meta": {"_last-serial": 0, "api-version": "1.0"},
"meta": {"_last-serial": 0, "api-version": API_VERSION},
"projects": [
{"name": x[0], "_last-serial": 0}
for x in sorted(projects, key=lambda x: x[1])
Expand Down Expand Up @@ -147,7 +149,7 @@ def test_with_results_with_serial(
je = JournalEntryFactory.create(submitted_by=user)

assert simple.simple_index(db_request) == {
"meta": {"_last-serial": je.id, "api-version": "1.0"},
"meta": {"_last-serial": je.id, "api-version": API_VERSION},
"projects": [
{"name": x[0], "_last-serial": 0}
for x in sorted(projects, key=lambda x: x[1])
Expand Down Expand Up @@ -187,9 +189,10 @@ def test_no_files_no_serial(self, db_request, content_type, renderer_override):
JournalEntryFactory.create(submitted_by=user)

assert simple.simple_detail(project, db_request) == {
"meta": {"_last-serial": 0, "api-version": "1.0"},
"meta": {"_last-serial": 0, "api-version": API_VERSION},
"name": project.normalized_name,
"files": [],
"versions": [],
}

assert db_request.response.headers["X-PyPI-Last-Serial"] == "0"
Expand All @@ -210,9 +213,10 @@ def test_no_files_with_serial(self, db_request, content_type, renderer_override)
je = JournalEntryFactory.create(name=project.name, submitted_by=user)

assert simple.simple_detail(project, db_request) == {
"meta": {"_last-serial": je.id, "api-version": "1.0"},
"meta": {"_last-serial": je.id, "api-version": API_VERSION},
"name": project.normalized_name,
"files": [],
"versions": [],
}

assert db_request.response.headers["X-PyPI-Last-Serial"] == str(je.id)
Expand All @@ -229,6 +233,7 @@ def test_with_files_no_serial(self, db_request, content_type, renderer_override)
db_request.accept = content_type
project = ProjectFactory.create()
releases = [ReleaseFactory.create(project=project) for _ in range(3)]
release_versions = sorted([r.version for r in releases], key=parse)
files = [
FileFactory.create(release=r, filename=f"{project.name}-{r.version}.tar.gz")
for r in releases
Expand All @@ -242,15 +247,18 @@ def test_with_files_no_serial(self, db_request, content_type, renderer_override)
JournalEntryFactory.create(submitted_by=user)

assert simple.simple_detail(project, db_request) == {
"meta": {"_last-serial": 0, "api-version": "1.0"},
"meta": {"_last-serial": 0, "api-version": API_VERSION},
"name": project.normalized_name,
"versions": release_versions,
"files": [
{
"filename": f.filename,
"url": f"/file/{f.filename}",
"hashes": {"sha256": f.sha256_digest},
"requires-python": f.requires_python,
"yanked": False,
"size": f.size,
"upload-time": f.upload_time.isoformat() + "Z",
}
for f in files
],
Expand All @@ -270,28 +278,32 @@ def test_with_files_with_serial(self, db_request, content_type, renderer_overrid
db_request.accept = content_type
project = ProjectFactory.create()
releases = [ReleaseFactory.create(project=project) for _ in range(3)]
release_versions = sorted([r.version for r in releases], key=parse)
files = [
FileFactory.create(release=r, filename=f"{project.name}-{r.version}.tar.gz")
for r in releases
]
# let's assert the result is ordered by string comparison of filename
files = sorted(files, key=lambda key: key.filename)
# let's assert the result is ordered by version and filename
files = sorted(files, key=lambda f: (parse(f.release.version), f.filename))
urls_iter = (f"/file/{f.filename}" for f in files)
db_request.matchdict["name"] = project.normalized_name
db_request.route_url = lambda *a, **kw: next(urls_iter)
user = UserFactory.create()
je = JournalEntryFactory.create(name=project.name, submitted_by=user)

assert simple.simple_detail(project, db_request) == {
"meta": {"_last-serial": je.id, "api-version": "1.0"},
"meta": {"_last-serial": je.id, "api-version": API_VERSION},
"name": project.normalized_name,
"versions": release_versions,
"files": [
{
"filename": f.filename,
"url": f"/file/{f.filename}",
"hashes": {"sha256": f.sha256_digest},
"requires-python": f.requires_python,
"yanked": False,
"size": f.size,
"upload-time": f.upload_time.isoformat() + "Z",
}
for f in files
],
Expand Down Expand Up @@ -361,15 +373,18 @@ def test_with_files_with_version_multi_digit(
je = JournalEntryFactory.create(name=project.name, submitted_by=user)

assert simple.simple_detail(project, db_request) == {
"meta": {"_last-serial": je.id, "api-version": "1.0"},
"meta": {"_last-serial": je.id, "api-version": API_VERSION},
"name": project.normalized_name,
"versions": release_versions,
"files": [
{
"filename": f.filename,
"url": f"/file/{f.filename}",
"hashes": {"sha256": f.sha256_digest},
"requires-python": f.requires_python,
"yanked": False,
"size": f.size,
"upload-time": f.upload_time.isoformat() + "Z",
}
for f in files
],
Expand Down
6 changes: 5 additions & 1 deletion warehouse/packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from warehouse.packaging.interfaces import ISimpleStorage
from warehouse.packaging.models import File, Project, Release

API_VERSION = "1.0"
API_VERSION = "1.1"


def _simple_index(request, serial):
Expand All @@ -48,10 +48,12 @@ def _simple_detail(project, request):
.all(),
key=lambda f: (parse(f.release.version), f.filename),
)
versions = sorted({f.release.version for f in files}, key=parse)

return {
"meta": {"api-version": API_VERSION, "_last-serial": project.last_serial},
"name": project.normalized_name,
"versions": versions,
"files": [
{
"filename": file.filename,
Expand All @@ -60,6 +62,8 @@ def _simple_detail(project, request):
"sha256": file.sha256_digest,
},
"requires-python": file.release.requires_python,
"size": file.size,
"upload-time": file.upload_time.isoformat() + "Z",
"yanked": file.release.yanked_reason
if file.release.yanked and file.release.yanked_reason
else file.release.yanked,
Expand Down

0 comments on commit 80d136a

Please sign in to comment.