Skip to content

Commit

Permalink
Addons: return the .first() object (#10868)
Browse files Browse the repository at this point in the history
* Addons: return the `.first()` object

Builds are sorted by `-date` by default, so we need the `.first()` instead of
the `.last()`.

* Test case for `builds.current` being the latest successful one
  • Loading branch information
humitos authored Oct 25, 2023
1 parent 277a5a4 commit dc2ec00
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions readthedocs/proxito/tests/test_hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone

from readthedocs.builds.constants import LATEST
from readthedocs.builds.models import Build, Version
Expand Down Expand Up @@ -403,6 +404,50 @@ def test_flyout_single_version_project(self):
]
assert r.json()["addons"]["flyout"]["downloads"] == expected

def test_builds_current_is_latest_one(self):
# Create 10 successful build objects
# The latest one (ordered by date) will be ``a1b2c3-9``
for i in range(10):
fixture.get(
Build,
date=timezone.now(),
project=self.project,
version=self.version,
commit=f"a1b2c3-{i}",
length=60,
state="finished",
success=True,
)

# Latest failed build
fixture.get(
Build,
date=timezone.now(),
project=self.project,
version=self.version,
commit=f"a1b2c3-failed",
length=60,
state="finished",
success=False,
)

r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
"url": "https://project.dev.readthedocs.io/en/latest/",
"client-version": "0.6.0",
"api-version": "0.1.0",
},
secure=True,
headers={
"host": "project.dev.readthedocs.io",
},
)
assert r.status_code == 200

# ``a1b2c3-9``is the latest successful build object created
assert r.json()["builds"]["current"]["commit"] == "a1b2c3-9"

def test_project_subproject(self):
subproject = fixture.get(
Project,
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _resolve_resources(self):
build = version.builds.filter(
success=True,
state=BUILD_STATE_FINISHED,
).last()
).first()

except UnresolverError as exc:
# If an exception is raised and there is a ``project`` in the
Expand All @@ -113,7 +113,7 @@ def _resolve_resources(self):
project = Project.objects.filter(slug=project_slug).first()
version = Version.objects.filter(slug=version_slug, project=project).first()
if version:
build = version.builds.last()
build = version.builds.first()

return project, version, build, filename

Expand Down

0 comments on commit dc2ec00

Please sign in to comment.