tests: add most-visited/recent and edge-case tests for special galleries
This commit is contained in:
@@ -90,3 +90,49 @@ class SpecialGalleriesTests(TestCase):
|
||||
self.assertIsNone(ctx.get("prev"))
|
||||
# Next should be c.jpg (since favorites are a.jpg and c.jpg sorted)
|
||||
self.assertIn("c.jpg", ctx.get("next"))
|
||||
|
||||
def test_most_visited_and_recent_directory_views(self):
|
||||
# most-visited should list images ordered by visits desc
|
||||
resp = self.client.get("/gallery/most-visited/")
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
names = [img["name"] for img in resp.context.get("images")]
|
||||
# b.jpg had visits=10, should be first
|
||||
self.assertEqual(names[0], "b.jpg")
|
||||
|
||||
# recent: modify last_visited so that c is newest
|
||||
from viewer.models import Image as ImModel
|
||||
|
||||
now = timezone.now()
|
||||
# update c to be most recent
|
||||
ImModel.objects.filter(path=str(self.gallery_root / "c.jpg")).update(
|
||||
last_visited=now
|
||||
)
|
||||
resp2 = self.client.get("/gallery/recent/")
|
||||
self.assertEqual(resp2.status_code, 200)
|
||||
recent_names = [img["name"] for img in resp2.context.get("images")]
|
||||
# c.jpg should be first in recent
|
||||
self.assertEqual(recent_names[0], "c.jpg")
|
||||
|
||||
def test_missing_and_outside_paths_are_skipped(self):
|
||||
# Create an Image row pointing to a missing file inside gallery
|
||||
from viewer.models import Image as ImModel
|
||||
|
||||
missing_path = self.gallery_root / "missing.jpg"
|
||||
ImModel.objects.create(
|
||||
user=self.user, path=str(missing_path), favorite=True, visits=1
|
||||
)
|
||||
|
||||
# Create a file outside gallery root and an Image row pointing to it
|
||||
outside_tmp = tempfile.NamedTemporaryFile(delete=False)
|
||||
outside_tmp.write(b"x")
|
||||
outside_tmp.flush()
|
||||
outside_tmp.close()
|
||||
ImModel.objects.create(
|
||||
user=self.user, path=str(Path(outside_tmp.name)), favorite=True, visits=1
|
||||
)
|
||||
|
||||
# favorites listing should still only include existing gallery files (a.jpg and c.jpg)
|
||||
resp = self.client.get("/gallery/favorites/")
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
names = sorted([img["name"] for img in resp.context.get("images")])
|
||||
self.assertEqual(names, ["a.jpg", "c.jpg"])
|
||||
|
||||
Reference in New Issue
Block a user