From edfa6b18a1caf419e207bdce49d64b004ce68715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Thu, 9 Nov 2023 13:45:07 -0300 Subject: [PATCH] Rename utils.sealed_date module (and tests) to utils.partial_date --- bookwyrm/models/fields.py | 2 +- bookwyrm/templatetags/date_ext.py | 2 +- ...st_sealed_date.py => test_partial_date.py} | 58 ++++++++++--------- .../utils/{sealed_date.py => partial_date.py} | 0 4 files changed, 33 insertions(+), 29 deletions(-) rename bookwyrm/tests/{test_sealed_date.py => test_partial_date.py} (67%) rename bookwyrm/utils/{sealed_date.py => partial_date.py} (100%) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 493a8e5c2..4bd580705 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -20,7 +20,7 @@ from markdown import markdown from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.utils.sanitizer import clean -from bookwyrm.utils.sealed_date import ( +from bookwyrm.utils.partial_date import ( PartialDate, PartialDateModel, from_partial_isoformat, diff --git a/bookwyrm/templatetags/date_ext.py b/bookwyrm/templatetags/date_ext.py index d4ca988de..d2490c13f 100644 --- a/bookwyrm/templatetags/date_ext.py +++ b/bookwyrm/templatetags/date_ext.py @@ -3,7 +3,7 @@ from django import template from django.template import defaultfilters from django.contrib.humanize.templatetags.humanize import naturalday -from bookwyrm.utils.sealed_date import PartialDate +from bookwyrm.utils.partial_date import PartialDate register = template.Library() diff --git a/bookwyrm/tests/test_sealed_date.py b/bookwyrm/tests/test_partial_date.py similarity index 67% rename from bookwyrm/tests/test_sealed_date.py rename to bookwyrm/tests/test_partial_date.py index 47532f40c..364d00933 100644 --- a/bookwyrm/tests/test_sealed_date.py +++ b/bookwyrm/tests/test_partial_date.py @@ -1,4 +1,4 @@ -""" test sealed_date module """ +""" test partial_date module """ import datetime import unittest @@ -7,7 +7,7 @@ from django.core.exceptions import ValidationError from django.utils import timezone from django.utils import translation -from bookwyrm.utils import sealed_date +from bookwyrm.utils import partial_date class PartialDateTest(unittest.TestCase): @@ -19,21 +19,21 @@ class PartialDateTest(unittest.TestCase): self._dt = datetime.datetime(2023, 10, 20, 17, 33, 10, tzinfo=timezone.utc) def test_day_seal(self): - sealed = sealed_date.PartialDate.from_datetime(self._dt) + sealed = partial_date.PartialDate.from_datetime(self._dt) self.assertEqual(self._dt, sealed) self.assertEqual("2023-10-20", sealed.partial_isoformat()) self.assertTrue(sealed.has_day) self.assertTrue(sealed.has_month) def test_month_seal(self): - sealed = sealed_date.MonthParts.from_datetime(self._dt) + sealed = partial_date.MonthParts.from_datetime(self._dt) self.assertEqual(self._dt, sealed) self.assertEqual("2023-10", sealed.partial_isoformat()) self.assertFalse(sealed.has_day) self.assertTrue(sealed.has_month) def test_year_seal(self): - sealed = sealed_date.YearParts.from_datetime(self._dt) + sealed = partial_date.YearParts.from_datetime(self._dt) self.assertEqual(self._dt, sealed) self.assertEqual("2023", sealed.partial_isoformat()) self.assertFalse(sealed.has_day) @@ -41,19 +41,19 @@ class PartialDateTest(unittest.TestCase): def test_no_naive_datetime(self): with self.assertRaises(ValueError): - sealed_date.PartialDate.from_datetime(datetime.datetime(2000, 1, 1)) + partial_date.PartialDate.from_datetime(datetime.datetime(2000, 1, 1)) def test_parse_year_seal(self): - parsed = sealed_date.from_partial_isoformat("1995") + parsed = partial_date.from_partial_isoformat("1995") expected = datetime.date(1995, 1, 1) self.assertEqual(expected, parsed.date()) self.assertFalse(parsed.has_day) self.assertFalse(parsed.has_month) def test_parse_year_errors(self): - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "995") - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "1995x") - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "1995-") + self.assertRaises(ValueError, partial_date.from_partial_isoformat, "995") + self.assertRaises(ValueError, partial_date.from_partial_isoformat, "1995x") + self.assertRaises(ValueError, partial_date.from_partial_isoformat, "1995-") def test_parse_month_seal(self): expected = datetime.date(1995, 5, 1) @@ -63,15 +63,15 @@ class PartialDateTest(unittest.TestCase): ] for desc, value in test_cases: with self.subTest(desc): - parsed = sealed_date.from_partial_isoformat(value) + parsed = partial_date.from_partial_isoformat(value) self.assertEqual(expected, parsed.date()) self.assertFalse(parsed.has_day) self.assertTrue(parsed.has_month) def test_parse_month_dash_required(self): - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "20056") - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "200506") - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "1995-7-") + self.assertRaises(ValueError, partial_date.from_partial_isoformat, "20056") + self.assertRaises(ValueError, partial_date.from_partial_isoformat, "200506") + self.assertRaises(ValueError, partial_date.from_partial_isoformat, "1995-7-") def test_parse_day_seal(self): expected = datetime.date(1995, 5, 6) @@ -82,19 +82,23 @@ class PartialDateTest(unittest.TestCase): ] for desc, value in test_cases: with self.subTest(desc): - parsed = sealed_date.from_partial_isoformat(value) + parsed = partial_date.from_partial_isoformat(value) self.assertEqual(expected, parsed.date()) self.assertTrue(parsed.has_day) self.assertTrue(parsed.has_month) def test_partial_isoformat_no_time_allowed(self): - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "2005-06-07 ") - self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "2005-06-07T") self.assertRaises( - ValueError, sealed_date.from_partial_isoformat, "2005-06-07T00:00:00" + ValueError, partial_date.from_partial_isoformat, "2005-06-07 " ) self.assertRaises( - ValueError, sealed_date.from_partial_isoformat, "2005-06-07T00:00:00-03" + ValueError, partial_date.from_partial_isoformat, "2005-06-07T" + ) + self.assertRaises( + ValueError, partial_date.from_partial_isoformat, "2005-06-07T00:00:00" + ) + self.assertRaises( + ValueError, partial_date.from_partial_isoformat, "2005-06-07T00:00:00-03" ) @@ -105,35 +109,35 @@ class PartialDateFormFieldTest(unittest.TestCase): def setUp(self): self._dt = datetime.datetime(2022, 11, 21, 17, 1, 0, tzinfo=timezone.utc) - self.field = sealed_date.PartialDateFormField() + self.field = partial_date.PartialDateFormField() def test_prepare_value(self): - sealed = sealed_date.PartialDate.from_datetime(self._dt) + sealed = partial_date.PartialDate.from_datetime(self._dt) self.assertEqual("2022-11-21", self.field.prepare_value(sealed)) def test_prepare_value_month(self): - sealed = sealed_date.MonthParts.from_datetime(self._dt) + sealed = partial_date.MonthParts.from_datetime(self._dt) self.assertEqual("2022-11-0", self.field.prepare_value(sealed)) def test_prepare_value_year(self): - sealed = sealed_date.YearParts.from_datetime(self._dt) + sealed = partial_date.YearParts.from_datetime(self._dt) self.assertEqual("2022-0-0", self.field.prepare_value(sealed)) def test_to_python(self): date = self.field.to_python("2022-11-21") - self.assertIsInstance(date, sealed_date.PartialDate) + self.assertIsInstance(date, partial_date.PartialDate) self.assertEqual("2022-11-21", date.partial_isoformat()) def test_to_python_month(self): date = self.field.to_python("2022-11-0") - self.assertIsInstance(date, sealed_date.PartialDate) + self.assertIsInstance(date, partial_date.PartialDate) self.assertEqual("2022-11", date.partial_isoformat()) with self.assertRaises(ValidationError): self.field.to_python("2022-0-25") def test_to_python_year(self): date = self.field.to_python("2022-0-0") - self.assertIsInstance(date, sealed_date.PartialDate) + self.assertIsInstance(date, partial_date.PartialDate) self.assertEqual("2022", date.partial_isoformat()) with self.assertRaises(ValidationError): self.field.to_python("0-05-25") @@ -142,5 +146,5 @@ class PartialDateFormFieldTest(unittest.TestCase): with translation.override("es"): # check super() is called date = self.field.to_python("5/6/97") - self.assertIsInstance(date, sealed_date.PartialDate) + self.assertIsInstance(date, partial_date.PartialDate) self.assertEqual("1997-06-05", date.partial_isoformat()) diff --git a/bookwyrm/utils/sealed_date.py b/bookwyrm/utils/partial_date.py similarity index 100% rename from bookwyrm/utils/sealed_date.py rename to bookwyrm/utils/partial_date.py