Skip to content

Commit 96b34b4

Browse files
committed
Simplify is_snap_old to return boolean and update notification message
- Changed notification message to vague 'hasn't been updated in a while' - Simplified is_snap_old to return boolean instead of dictionary - Removed convert_date_month_year function (no longer needed) - Updated all tests to work with boolean return value - Fixed test_is_snap_old_exactly_two_years timestamp to match exactly 2 years
1 parent 94fd8d0 commit 96b34b4

3 files changed

Lines changed: 19 additions & 71 deletions

File tree

templates/store/snap-details/_details.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ <h3 class="p-muted-heading">Last updated</h3>
1616
<li>{{ updates[1]["released-at-display"] }} - <small>{{ updates[1]["track"] }}/{{ updates[1]["risk"] }}</small></li>
1717
{% endif %}
1818
</ul>
19-
{% if old_snap_info and old_snap_info.is_old %}
19+
{% if old_snap_info %}
2020
<div class="p-notification--caution is-borderless">
2121
<div class="p-notification__content">
2222
<p class="p-notification__message">
23-
This snap hasn't been updated since {{ old_snap_info.last_updated_formatted }}. It might be unmaintained and have stability or security issues.
23+
This snap hasn't been updated in a while. It might be unmaintained and have stability or security issues.
2424
</p>
2525
</div>
2626
</div>

tests/store/tests_public_logic.py

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -486,54 +486,43 @@ def test_get_last_updated_versions(self):
486486
def test_is_snap_old_empty_date(self):
487487
"""Test that empty or None date returns not old"""
488488
result = logic.is_snap_old(None)
489-
self.assertFalse(result["is_old"])
490-
self.assertEqual(result["years_since_update"], 0)
489+
self.assertFalse(result)
491490

492491
result = logic.is_snap_old("")
493-
self.assertFalse(result["is_old"])
494-
self.assertEqual(result["years_since_update"], 0)
492+
self.assertFalse(result)
495493

496494
def test_is_snap_old_invalid_date(self):
497495
"""Test that invalid date returns not old"""
498496
result = logic.is_snap_old("invalid-date")
499-
self.assertFalse(result["is_old"])
500-
self.assertEqual(result["years_since_update"], 0)
497+
self.assertFalse(result)
501498

502499
@freeze_time("2024-01-01")
503500
def test_is_snap_old_recent_snap(self):
504501
"""Test that a recently updated snap is not considered old"""
505502
recent_date = "2023-06-01T10:00:00Z"
506503
result = logic.is_snap_old(recent_date)
507-
self.assertFalse(result["is_old"])
508-
self.assertEqual(result["years_since_update"], 0)
509-
self.assertIn("last_updated_formatted", result)
504+
self.assertFalse(result)
510505

511-
@freeze_time("2024-01-02")
506+
@freeze_time("2024-01-02T10:00:00Z")
512507
def test_is_snap_old_exactly_two_years(self):
513-
"""Test that a snap updated over 2 years ago is considered old"""
514-
old_date = "2021-12-01T10:00:00Z" # Over 2 years ago
508+
"""Test that a snap updated exactly 2 years ago is considered old"""
509+
old_date = "2022-01-02T10:00:00Z" # Exactly 2 years ago
515510
result = logic.is_snap_old(old_date)
516-
self.assertTrue(result["is_old"])
517-
self.assertEqual(result["years_since_update"], 2)
518-
self.assertIn("last_updated_formatted", result)
511+
self.assertTrue(result)
519512

520513
@freeze_time("2024-01-02")
521514
def test_is_snap_old_very_old_snap(self):
522515
"""Test that a very old snap is considered old"""
523516
very_old_date = "2020-01-01T10:00:00Z"
524517
result = logic.is_snap_old(very_old_date)
525-
self.assertTrue(result["is_old"])
526-
self.assertEqual(result["years_since_update"], 4)
527-
self.assertIn("last_updated_formatted", result)
518+
self.assertTrue(result)
528519

529520
@freeze_time("2024-01-01")
530521
def test_is_snap_old_just_under_two_years(self):
531522
"""Test that a snap updated just under 2 years ago is not old"""
532523
almost_old_date = "2022-02-01T10:00:00Z"
533524
result = logic.is_snap_old(almost_old_date)
534-
self.assertFalse(result["is_old"])
535-
self.assertEqual(result["years_since_update"], 1)
536-
self.assertIn("last_updated_formatted", result)
525+
self.assertFalse(result)
537526

538527
@freeze_time("2024-01-02")
539528
def test_is_snap_old_custom_threshold(self):
@@ -542,12 +531,11 @@ def test_is_snap_old_custom_threshold(self):
542531

543532
# With default threshold (2 years), should not be old
544533
result = logic.is_snap_old(date_one_year_ago)
545-
self.assertFalse(result["is_old"])
534+
self.assertFalse(result)
546535

547536
# With custom threshold (1 year), should be old
548537
result = logic.is_snap_old(date_one_year_ago, old_threshold_years=1)
549-
self.assertTrue(result["is_old"])
550-
self.assertEqual(result["years_since_update"], 1)
538+
self.assertTrue(result)
551539

552540
def test_is_snap_old_different_date_formats(self):
553541
"""Test that different ISO date formats are handled correctly"""
@@ -562,19 +550,4 @@ def test_is_snap_old_different_date_formats(self):
562550
for date_str in dates_to_test:
563551
with freeze_time("2024-01-02"):
564552
result = logic.is_snap_old(date_str)
565-
self.assertTrue(
566-
result["is_old"], f"Failed for date format: {date_str}"
567-
)
568-
self.assertEqual(result["years_since_update"], 4)
569-
570-
def test_convert_date_month_year(self):
571-
"""Test the month-year date formatting function"""
572-
test_cases = [
573-
("2022-01-14T10:00:00Z", "January 2022"),
574-
("2020-12-25T15:30:00Z", "December 2020"),
575-
("2019-06-01T00:00:00Z", "June 2019"),
576-
]
577-
578-
for date_str, expected in test_cases:
579-
result = logic.convert_date_month_year(date_str)
580-
self.assertEqual(result, expected, f"Failed for date: {date_str}")
553+
self.assertTrue(result, f"Failed for date format: {date_str}")

webapp/store/logic.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,6 @@ def convert_date(date_to_convert):
204204
return date_parsed.strftime("%-d %B %Y")
205205

206206

207-
def convert_date_month_year(date_to_convert):
208-
"""Convert date to month and year format: Month Year
209-
210-
Format of date to convert: 2019-01-12T16:48:41.821037+00:00
211-
Output: January 2019
212-
213-
:param date_to_convert: Date to convert
214-
:returns: Month and year only
215-
"""
216-
date_parsed = parser.parse(date_to_convert)
217-
if date_parsed.tzinfo is None:
218-
date_parsed = date_parsed.replace(tzinfo=datetime.timezone.utc)
219-
220-
return date_parsed.strftime("%B %Y")
221-
222-
223207
def is_snap_old(last_updated_date, old_threshold_years=2.0):
224208
"""Check if a snap is considered 'old' based on its last update date
225209
@@ -229,11 +213,10 @@ def is_snap_old(last_updated_date, old_threshold_years=2.0):
229213
:param last_updated_date: The last updated date string in ISO format
230214
:param old_threshold_years: Number of years to consider a snap old
231215
(default: 2)
232-
:returns: Dictionary with 'is_old' boolean and 'years_since_update'
233-
integer
216+
:returns: True if snap is old, False otherwise
234217
"""
235218
if not last_updated_date:
236-
return {"is_old": False, "years_since_update": 0}
219+
return False
237220

238221
try:
239222
date_parsed = parser.parse(last_updated_date)
@@ -245,18 +228,10 @@ def is_snap_old(last_updated_date, old_threshold_years=2.0):
245228
delta = relativedelta(now, date_parsed)
246229
years_since_update = delta.years
247230

248-
is_old = years_since_update >= old_threshold_years
249-
250-
return {
251-
"is_old": is_old,
252-
"years_since_update": years_since_update,
253-
"last_updated_formatted": convert_date_month_year(
254-
last_updated_date
255-
),
256-
}
231+
return years_since_update >= old_threshold_years
257232
except (ValueError, TypeError):
258233
# If we can't parse the date, assume it's not old
259-
return {"is_old": False, "years_since_update": 0}
234+
return False
260235

261236

262237
categories_list = [

0 commit comments

Comments
 (0)