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

Added few more Unit tests #2

Merged
merged 5 commits into from
Feb 10, 2024
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
24 changes: 16 additions & 8 deletions nepali_date_utils/date_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class DateConverter:
en_max_year = 2099
en_min_year = 1944
np_max_year = 2099
np_min_year = 1944
days_in_year = 365
reference_ad = {"year": 2017, "month": 2, "day": 11}
reference_bs = {"year": 2073, "month": 10, "day": 29}
Expand All @@ -13,11 +13,18 @@ def verify_nepali_date(self, date_obj):
year = date_obj["year"]
month = date_obj["month"]
day = date_obj["day"]
return (
self.en_min_year <= year <= self.en_max_year
and 1 <= month <= 12
and 1 <= day <= 31
)

if year < self.np_min_year or year > self.np_max_year:
return False
if month < 1 or month > 12:
return False
if (
day < 1
or day > calendar_data[year][month-1]
):
return False
return True


def difference_in_ad(self, date):
# Split the date string into year, month, and day components
Expand Down Expand Up @@ -117,14 +124,15 @@ def difference_in_bs(self, date):

return day_count * factor


def offset_ad_days(self, day_count):
base_date = datetime(
self.reference_ad["year"],
self.reference_ad["month"],
self.reference_ad["day"],
)
offset_date = base_date + timedelta(days=day_count)
return offset_date.date().strftime("%Y/%m/%d")
return offset_date.date().strftime("%Y/%-m/%-d")


@classmethod
Expand Down
41 changes: 35 additions & 6 deletions tests/test_date_converter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# python3 -m unittest tests/test_date_converter.py

import sys
import unittest
from os.path import dirname, join, abspath
Expand All @@ -10,17 +12,44 @@ class TestDateConverter(unittest.TestCase):

# Test data sets
test_data = [
{"bs_date": "2054/04/1", "ad_date": "1997/07/16"},
{"bs_date": "2055/09/15", "ad_date": "1998/12/30"},
{"bs_date": "2077/04/20", "ad_date": "2020/08/04"},
{"bs_date": "2054/4/1", "ad_date": "1997/7/16"},
{"bs_date": "2055/9/15", "ad_date": "1998/12/30"},
{"bs_date": "2077/4/20", "ad_date": "2020/8/4"},

{"bs_date": "2080/2/32", "ad_date": "2023/6/15"},
{"bs_date": "2080/1/31", "ad_date": "2023/5/14"},

{"bs_date": "2003/12/31", "ad_date": "1947/4/13"},
{"bs_date": "2004/1/1", "ad_date": "1947/4/14"},
{"bs_date": "2003/1/31", "ad_date": "1946/5/13"},
]

exception_test_data=[
{"bs_date": "2080/01/32", "ad_date": "2023/05/15"},
]

def test_bs_to_ad(self):
for data in self.test_data:
bs_date = data["bs_date"]
expected_ad_date = data["ad_date"]
actual_ad_date = converter.bs_to_ad(bs_date)
self.assertEqual(actual_ad_date, expected_ad_date)
ad_date = data["ad_date"]
converted_ad_date = converter.bs_to_ad(bs_date)
self.assertEqual(converted_ad_date, ad_date)


def test_ad_to_bs(self):
for data in self.test_data:
bs_date = data["bs_date"]
ad_date = data["ad_date"]
converted_bs_date = converter.ad_to_bs(ad_date)
self.assertEqual(converted_bs_date, bs_date)

# test for Date Out of Range exception
def test_bs_to_ad_exception(self):
for data in self.exception_test_data:
bs_date = data["bs_date"]
with self.assertRaises(ValueError):
converter.bs_to_ad(bs_date)


if __name__ == '__main__':
unittest.main()
Loading