Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
import unittest
|
|
from datetime import date
|
|
|
|
from ledger import utils
|
|
|
|
|
|
class ChunkedTests(unittest.TestCase):
|
|
def test_chunks_of_three(self):
|
|
self.assertEqual(list(utils.chunked([1, 2, 3, 4, 5, 6, 7], 3)), [[1, 2, 3], [4, 5, 6], [7]])
|
|
|
|
def test_size_must_be_positive(self):
|
|
with self.assertRaises(ValueError):
|
|
list(utils.chunked([1], 0))
|
|
|
|
|
|
class GroupByTests(unittest.TestCase):
|
|
def test_keeps_first_seen_order(self):
|
|
groups = utils.group_by(["bb", "a", "cc", "d", "e"], len)
|
|
self.assertEqual(list(groups), [2, 1])
|
|
self.assertEqual(groups[1], ["a", "d", "e"])
|
|
|
|
|
|
class DateHelperTests(unittest.TestCase):
|
|
def test_ensure_date(self):
|
|
self.assertEqual(utils.ensure_date("2024-03-09"), date(2024, 3, 9))
|
|
self.assertEqual(utils.ensure_date(date(2024, 3, 9)), date(2024, 3, 9))
|
|
|
|
def test_add_months_clamps_day(self):
|
|
self.assertEqual(utils.add_months(date(2024, 1, 31), 1), date(2024, 2, 29))
|
|
self.assertEqual(utils.add_months(date(2023, 1, 31), 1), date(2023, 2, 28))
|
|
|
|
def test_add_months_crosses_years(self):
|
|
self.assertEqual(utils.add_months(date(2023, 11, 15), 3), date(2024, 2, 15))
|
|
self.assertEqual(utils.add_months(date(2024, 2, 15), -3), date(2023, 11, 15))
|
|
|
|
def test_month_boundaries(self):
|
|
self.assertEqual(utils.first_of_month(date(2024, 2, 17)), date(2024, 2, 1))
|
|
self.assertEqual(utils.last_of_month(date(2024, 2, 17)), date(2024, 2, 29))
|
|
self.assertEqual(utils.last_of_month(date(2023, 12, 3)), date(2023, 12, 31))
|
|
|
|
def test_daterange_is_inclusive(self):
|
|
days = list(utils.daterange(date(2024, 2, 27), date(2024, 3, 1)))
|
|
self.assertEqual(len(days), 4)
|
|
self.assertEqual(days[-1], date(2024, 3, 1))
|
|
|
|
def test_clamp(self):
|
|
self.assertEqual(utils.clamp(5, 1, 3), 3)
|
|
self.assertEqual(utils.clamp(-5, 1, 3), 1)
|
|
self.assertEqual(utils.clamp(2, 1, 3), 2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|