Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created validation function for validating samwat date #28

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions bikram/bikram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand All @@ -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:
'''
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions bikram/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
35 changes: 35 additions & 0 deletions tests/test_samwat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)