Skip to content

Commit

Permalink
fix flaky test caused by missing ordering, add ordering tests for Alb…
Browse files Browse the repository at this point in the history
…um and BaseFile, add explicit order_by to BaseFile and Album managers
  • Loading branch information
tykling committed Dec 10, 2024
1 parent b1c3bab commit 9463101
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ convention = "google"
"D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d
"ANN201", # https://docs.astral.sh/ruff/rules/missing-return-type-undocumented-public-function/
"SLF001", # https://docs.astral.sh/ruff/rules/private-member-access/
"TRY003", # https://docs.astral.sh/ruff/rules/raise-vanilla-args/
]
"*/migrations/*" = [
"D" # https://docs.astral.sh/ruff/rules/#pydocstyle-d
Expand Down
3 changes: 2 additions & 1 deletion src/albums/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def get_queryset(self): # type: ignore[no-untyped-def] # noqa: ANN201
.prefetch_related("hits")
.annotate(hitcount=Count("hits", distinct=True))
.prefetch_active_files_list(recursive=True)
)
# ordering from Album META gets lost for some reason :(
).order_by("created_at")


class AlbumQuerySet(models.QuerySet): # type: ignore[type-arg]
Expand Down
9 changes: 9 additions & 0 deletions src/albums/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def test_album_list_api(self) -> None:
assert response.status_code == 200
assert len(response.json()["bma_response"]) == 10, "Did not get 10 albums"

# make sure albums are sorted as expected
latest = None
for album in response.json()["bma_response"]:
if not latest:
latest = album["created_at"]
if latest > album["created_at"]:
raise AssertionError(f"Albums are sorted wrong! {latest} > {album['created_at']}")
latest = album["created_at"]

# test the file filter with files in different albums
response = self.client.get(
reverse("api-v1-json:album_list"),
Expand Down
3 changes: 2 additions & 1 deletion src/files/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def get_queryset(self) -> models.QuerySet["BaseFile"]:
.prefetch_related("thumbnails")
.prefetch_related(models.Prefetch("thumbnails", to_attr="thumbnail_list"))
.prefetch_related(models.Prefetch("image_versions", to_attr="image_version_list"))
)
# ordering by BaseFile Meta gets lost :(
).order_by("created_at")


class BaseFileQuerySet(PolymorphicQuerySet):
Expand Down
13 changes: 13 additions & 0 deletions src/files/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,19 @@ def test_file_list_permissions(self) -> None:
assert response.status_code == 200
assert len(response.json()["bma_response"]) == 0

def test_file_list_ordering(self) -> None:
"""Make sure files are ordered by date with the oldest file first."""
# upload 15 files and get them all
[self.file_upload(title=f"title{i}") for i in range(15)]
response = self.client.get(reverse("api-v1-json:file_list"), headers={"authorization": self.superuser.auth})
latest = None
for f in response.json()["bma_response"]:
if not latest:
latest = f["created_at"]
if latest > f["created_at"]:
raise AssertionError(f"Files are sorted wrong! {latest} > {f['created_at']}")
latest = f["created_at"]

def test_metadata_get(self) -> None:
"""Get file metadata from the API."""
self.file_upload()
Expand Down

0 comments on commit 9463101

Please sign in to comment.