From dd9040a2c02763e3b01e67854b516d044fca910d Mon Sep 17 00:00:00 2001 From: Dipendra Nath Date: Sun, 28 Jan 2024 14:42:25 -0500 Subject: [PATCH] created validation function for validting year, month and day value in samwat --- bikram/bikram.py | 43 +++++++++++++++++++++++++++++++++++++++++++ bikram/constants.py | 3 +++ tests/test_samwat.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/bikram/bikram.py b/bikram/bikram.py index 9d1499b..295a838 100644 --- a/bikram/bikram.py +++ b/bikram/bikram.py @@ -26,6 +26,8 @@ dev_digits_re_fragment, DEV_TO_ENG_DIGITS_TRANSTABLE as DEV_ENG_TRANS, ENG_TO_DEV_DIGITS_TRANSTABLE as ENG_DEV_TRANS, + FIRST_AD_YEAR, + LAST_AD_YEAR ) @@ -35,6 +37,46 @@ _PATTERNS_CACHE = {} +_FIRST_AD_DATE = {"year": FIRST_AD_YEAR, "month": 1, "day": 1} +_LAST_BS_DATE = {"year": LAST_AD_YEAR, "month": 12, "day": BS_YEAR_TO_MONTHS[LAST_AD_YEAR][-1]} + + +def _check_int_value(value): + if isinstance(value, int) and value > 0: + return value + else: + raise TypeError( + 'Invalid year, month or day Expected Natural Number "int" got {}: {}'.format(value, type(value).__name__)) + + +def _get_max_days_in_month(year, month): + try: + days = BS_YEAR_TO_MONTHS[year][month] + if not days: + raise ValueError('Invalid month value') + return days + except KeyError: + raise ValueError(f'Invalid year, supported range is {FIRST_AD_YEAR}-{LAST_AD_YEAR}') + except IndexError: + raise ValueError('Invalid month value') + + +def _check_date_values(year, month, day): + _check_int_value(year) + _check_int_value(month) + _check_int_value(day) + + if year < FIRST_AD_YEAR or year > LAST_AD_YEAR: + raise ValueError(f'Invalid year, supported range is {FIRST_AD_YEAR}-{LAST_AD_YEAR}') + + if month > 12: + raise ValueError('Invalid month, supported range is 1-12') + + if day > _get_max_days_in_month(year, month): + raise ValueError('Invalid day, supported range is 1-{}'.format(_get_max_days_in_month(year, month))) + return year, month, day + + @total_ordering class samwat: ''' @@ -107,6 +149,7 @@ class samwat: } def __init__(self, year, month, day, ad=None): + year, month, day = _check_date_values(year, month, day) self.year = year self.month = month self.day = day diff --git a/bikram/constants.py b/bikram/constants.py index e330fba..e9aa6d7 100644 --- a/bikram/constants.py +++ b/bikram/constants.py @@ -181,3 +181,6 @@ (2089, (None, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30)), (2090, (None, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30)), )) + +FIRST_AD_YEAR = next(iter(BS_YEAR_TO_MONTHS)) +LAST_AD_YEAR = next(reversed(BS_YEAR_TO_MONTHS)) \ No newline at end of file diff --git a/tests/test_samwat.py b/tests/test_samwat.py index d615c7c..a2aa3a7 100755 --- a/tests/test_samwat.py +++ b/tests/test_samwat.py @@ -233,3 +233,38 @@ def test_formatters(self): for formatstr, formatted_str in formats: self.assertEqual(self.bs_date.strftime(formatstr), formatted_str) print('PASSED', formatstr, formatted_str) + + +class TestSamwatDateValues(unittest.TestCase): + ''' + Test Year, Month and Day values for samwat class + ''' + + def setUp(self): + self.invalid_values = [ + {"year": 3001, "month": 1, "day": 1}, + {"year": 2000, "month": 13, "day": 1}, + {"year": 2000, "month": 1, "day": 32}, + ] + self.invalid_data_types = [ + + {"year": 2000, "month": 4, "day": 21.1}, + {"year": 2000, "month": 4.34, "day": 13}, + {"year": "two", "month": 4.34, "day": 13}, + {"year": 2000, "month": 4, "day": "thirteen"}, + {"year": 2000, "month": "three", "day": 20}, + {"year": 2000, "month": 4, "day": 0}, + {"year": 2000, "month": 0, "day": 13}, + ] + + def test_samwat_date(self): + for invalid_value in self.invalid_values: + with self.subTest(invalid_value=invalid_value): + with self.assertRaisesRegex(ValueError, 'Invalid'): + samwat(**invalid_value) + print(invalid_value) + for invalid_value in self.invalid_data_types: + with self.subTest(invalid_value=invalid_value): + with self.assertRaisesRegex(TypeError, 'Invalid'): + samwat(**invalid_value) + print('PASSED ', invalid_value)