From f11e97e9719e7eedf290de11787fdb16c4a15d64 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Sat, 10 Feb 2024 18:07:35 +0545 Subject: [PATCH 1/5] added few more test data --- tests/test_date_converter.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/test_date_converter.py b/tests/test_date_converter.py index c832aa6..851a7bd 100644 --- a/tests/test_date_converter.py +++ b/tests/test_date_converter.py @@ -10,17 +10,30 @@ class TestDateConverter(unittest.TestCase): # Test data sets test_data = [ - {"bs_date": "2054/04/1", "ad_date": "1997/07/16"}, + {"bs_date": "2054/04/01", "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": "2080/02/32", "ad_date": "2023/06/15"}, + {"bs_date": "2080/01/31", "ad_date": "2023/05/14"}, + + {"bs_date": "2003/12/31", "ad_date": "1947/04/13"}, + {"bs_date": "2004/01/01", "ad_date": "1947/04/14"}, + {"bs_date": "2003/01/31", "ad_date": "1946/05/13"}, + + + # This should fail + # {"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"] + ad_date = data["ad_date"] actual_ad_date = converter.bs_to_ad(bs_date) - self.assertEqual(actual_ad_date, expected_ad_date) + self.assertEqual(actual_ad_date, ad_date) if __name__ == '__main__': unittest.main() \ No newline at end of file From 8a8698db382545cb6f48e4ee04cab31fe35440df Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Sat, 10 Feb 2024 18:13:33 +0545 Subject: [PATCH 2/5] tests for ValueError date types --- tests/test_date_converter.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/test_date_converter.py b/tests/test_date_converter.py index 851a7bd..6598834 100644 --- a/tests/test_date_converter.py +++ b/tests/test_date_converter.py @@ -20,12 +20,10 @@ class TestDateConverter(unittest.TestCase): {"bs_date": "2003/12/31", "ad_date": "1947/04/13"}, {"bs_date": "2004/01/01", "ad_date": "1947/04/14"}, {"bs_date": "2003/01/31", "ad_date": "1946/05/13"}, + ] - - # This should fail - # {"bs_date": "2080/01/32", "ad_date": "2023/05/15"}, - - + exception_test_data=[ + {"bs_date": "2080/01/32", "ad_date": "2023/05/15"}, ] def test_bs_to_ad(self): @@ -35,5 +33,13 @@ def test_bs_to_ad(self): actual_ad_date = converter.bs_to_ad(bs_date) self.assertEqual(actual_ad_date, ad_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() \ No newline at end of file From 48e0a83812cf84bcca5d56989d28e9d257ab9bc5 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Sat, 10 Feb 2024 20:15:43 +0545 Subject: [PATCH 3/5] update verify nepali date function --- nepali_date_utils/date_converter.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/nepali_date_utils/date_converter.py b/nepali_date_utils/date_converter.py index dbb7e81..63e9083 100644 --- a/nepali_date_utils/date_converter.py +++ b/nepali_date_utils/date_converter.py @@ -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} @@ -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 From 794570c2cd37fa1f721f99059fff24348b2f8c85 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Sat, 10 Feb 2024 20:33:36 +0545 Subject: [PATCH 4/5] updated return format for ad date --- nepali_date_utils/date_converter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nepali_date_utils/date_converter.py b/nepali_date_utils/date_converter.py index 63e9083..26cef08 100644 --- a/nepali_date_utils/date_converter.py +++ b/nepali_date_utils/date_converter.py @@ -124,6 +124,7 @@ def difference_in_bs(self, date): return day_count * factor + def offset_ad_days(self, day_count): base_date = datetime( self.reference_ad["year"], @@ -131,7 +132,7 @@ def offset_ad_days(self, day_count): 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 From 7d5c853b852085da20fe37be5e9325103708b8b3 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Sat, 10 Feb 2024 20:34:46 +0545 Subject: [PATCH 5/5] added test cases to test converting date from ad to bs --- tests/test_date_converter.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/test_date_converter.py b/tests/test_date_converter.py index 6598834..51314ae 100644 --- a/tests/test_date_converter.py +++ b/tests/test_date_converter.py @@ -1,3 +1,5 @@ +# python3 -m unittest tests/test_date_converter.py + import sys import unittest from os.path import dirname, join, abspath @@ -10,16 +12,16 @@ class TestDateConverter(unittest.TestCase): # Test data sets test_data = [ - {"bs_date": "2054/04/01", "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/02/32", "ad_date": "2023/06/15"}, - {"bs_date": "2080/01/31", "ad_date": "2023/05/14"}, + {"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/04/13"}, - {"bs_date": "2004/01/01", "ad_date": "1947/04/14"}, - {"bs_date": "2003/01/31", "ad_date": "1946/05/13"}, + {"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=[ @@ -30,8 +32,16 @@ def test_bs_to_ad(self): for data in self.test_data: bs_date = data["bs_date"] ad_date = data["ad_date"] - actual_ad_date = converter.bs_to_ad(bs_date) - self.assertEqual(actual_ad_date, 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):