Rename utils.sealed_date module (and tests) to utils.partial_date

This commit is contained in:
Adeodato Simó 2023-11-09 13:45:07 -03:00
parent fa80aa54a9
commit edfa6b18a1
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
4 changed files with 33 additions and 29 deletions

View file

@ -20,7 +20,7 @@ from markdown import markdown
from bookwyrm import activitypub from bookwyrm import activitypub
from bookwyrm.connectors import get_image from bookwyrm.connectors import get_image
from bookwyrm.utils.sanitizer import clean from bookwyrm.utils.sanitizer import clean
from bookwyrm.utils.sealed_date import ( from bookwyrm.utils.partial_date import (
PartialDate, PartialDate,
PartialDateModel, PartialDateModel,
from_partial_isoformat, from_partial_isoformat,

View file

@ -3,7 +3,7 @@ from django import template
from django.template import defaultfilters from django.template import defaultfilters
from django.contrib.humanize.templatetags.humanize import naturalday 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() register = template.Library()

View file

@ -1,4 +1,4 @@
""" test sealed_date module """ """ test partial_date module """
import datetime import datetime
import unittest import unittest
@ -7,7 +7,7 @@ from django.core.exceptions import ValidationError
from django.utils import timezone from django.utils import timezone
from django.utils import translation from django.utils import translation
from bookwyrm.utils import sealed_date from bookwyrm.utils import partial_date
class PartialDateTest(unittest.TestCase): 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) self._dt = datetime.datetime(2023, 10, 20, 17, 33, 10, tzinfo=timezone.utc)
def test_day_seal(self): 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(self._dt, sealed)
self.assertEqual("2023-10-20", sealed.partial_isoformat()) self.assertEqual("2023-10-20", sealed.partial_isoformat())
self.assertTrue(sealed.has_day) self.assertTrue(sealed.has_day)
self.assertTrue(sealed.has_month) self.assertTrue(sealed.has_month)
def test_month_seal(self): 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(self._dt, sealed)
self.assertEqual("2023-10", sealed.partial_isoformat()) self.assertEqual("2023-10", sealed.partial_isoformat())
self.assertFalse(sealed.has_day) self.assertFalse(sealed.has_day)
self.assertTrue(sealed.has_month) self.assertTrue(sealed.has_month)
def test_year_seal(self): 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(self._dt, sealed)
self.assertEqual("2023", sealed.partial_isoformat()) self.assertEqual("2023", sealed.partial_isoformat())
self.assertFalse(sealed.has_day) self.assertFalse(sealed.has_day)
@ -41,19 +41,19 @@ class PartialDateTest(unittest.TestCase):
def test_no_naive_datetime(self): def test_no_naive_datetime(self):
with self.assertRaises(ValueError): 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): 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) expected = datetime.date(1995, 1, 1)
self.assertEqual(expected, parsed.date()) self.assertEqual(expected, parsed.date())
self.assertFalse(parsed.has_day) self.assertFalse(parsed.has_day)
self.assertFalse(parsed.has_month) self.assertFalse(parsed.has_month)
def test_parse_year_errors(self): def test_parse_year_errors(self):
self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "995") self.assertRaises(ValueError, partial_date.from_partial_isoformat, "995")
self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "1995x") self.assertRaises(ValueError, partial_date.from_partial_isoformat, "1995x")
self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "1995-") self.assertRaises(ValueError, partial_date.from_partial_isoformat, "1995-")
def test_parse_month_seal(self): def test_parse_month_seal(self):
expected = datetime.date(1995, 5, 1) expected = datetime.date(1995, 5, 1)
@ -63,15 +63,15 @@ class PartialDateTest(unittest.TestCase):
] ]
for desc, value in test_cases: for desc, value in test_cases:
with self.subTest(desc): with self.subTest(desc):
parsed = sealed_date.from_partial_isoformat(value) parsed = partial_date.from_partial_isoformat(value)
self.assertEqual(expected, parsed.date()) self.assertEqual(expected, parsed.date())
self.assertFalse(parsed.has_day) self.assertFalse(parsed.has_day)
self.assertTrue(parsed.has_month) self.assertTrue(parsed.has_month)
def test_parse_month_dash_required(self): def test_parse_month_dash_required(self):
self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "20056") self.assertRaises(ValueError, partial_date.from_partial_isoformat, "20056")
self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "200506") self.assertRaises(ValueError, partial_date.from_partial_isoformat, "200506")
self.assertRaises(ValueError, sealed_date.from_partial_isoformat, "1995-7-") self.assertRaises(ValueError, partial_date.from_partial_isoformat, "1995-7-")
def test_parse_day_seal(self): def test_parse_day_seal(self):
expected = datetime.date(1995, 5, 6) expected = datetime.date(1995, 5, 6)
@ -82,19 +82,23 @@ class PartialDateTest(unittest.TestCase):
] ]
for desc, value in test_cases: for desc, value in test_cases:
with self.subTest(desc): with self.subTest(desc):
parsed = sealed_date.from_partial_isoformat(value) parsed = partial_date.from_partial_isoformat(value)
self.assertEqual(expected, parsed.date()) self.assertEqual(expected, parsed.date())
self.assertTrue(parsed.has_day) self.assertTrue(parsed.has_day)
self.assertTrue(parsed.has_month) self.assertTrue(parsed.has_month)
def test_partial_isoformat_no_time_allowed(self): 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( self.assertRaises(
ValueError, sealed_date.from_partial_isoformat, "2005-06-07T00:00:00" ValueError, partial_date.from_partial_isoformat, "2005-06-07 "
) )
self.assertRaises( 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): def setUp(self):
self._dt = datetime.datetime(2022, 11, 21, 17, 1, 0, tzinfo=timezone.utc) 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): 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)) self.assertEqual("2022-11-21", self.field.prepare_value(sealed))
def test_prepare_value_month(self): 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)) self.assertEqual("2022-11-0", self.field.prepare_value(sealed))
def test_prepare_value_year(self): 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)) self.assertEqual("2022-0-0", self.field.prepare_value(sealed))
def test_to_python(self): def test_to_python(self):
date = self.field.to_python("2022-11-21") 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()) self.assertEqual("2022-11-21", date.partial_isoformat())
def test_to_python_month(self): def test_to_python_month(self):
date = self.field.to_python("2022-11-0") 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()) self.assertEqual("2022-11", date.partial_isoformat())
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.field.to_python("2022-0-25") self.field.to_python("2022-0-25")
def test_to_python_year(self): def test_to_python_year(self):
date = self.field.to_python("2022-0-0") 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()) self.assertEqual("2022", date.partial_isoformat())
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.field.to_python("0-05-25") self.field.to_python("0-05-25")
@ -142,5 +146,5 @@ class PartialDateFormFieldTest(unittest.TestCase):
with translation.override("es"): with translation.override("es"):
# check super() is called # check super() is called
date = self.field.to_python("5/6/97") 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()) self.assertEqual("1997-06-05", date.partial_isoformat())