diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py index a589198cf21ae91c87607815ef6903f980f684bf..2c3ad4d70ac621625012e1cf9ab002644acb7c06 100644 --- a/bpfin/tests/test_lib/test_other.py +++ b/bpfin/tests/test_lib/test_other.py @@ -1,35 +1,289 @@ -from bpfin.lib.other import add_year_dictionary -from bpfin.lib.other import subtract_year_dictionary -from bpfin.lib.other import divide_dscr_dict -from bpfin.lib.other import average_list_with_none -from bpfin.lib.other import average_nonzero_list +"""Tests for other.py in lib.""" +from datetime import date +import pytest + +from bpfin.lib.other import ( + add_year_dictionary, subtract_year_dictionary, divide_dscr_dict, average_list_with_none, average_nonzero_list, + min_none_list, add_list, cal_last_day, form_bill_calendar, form_date_calendar +) from bpfin.tests.testdata import sample_data as db -def test_add_year_dictionary(): - year_1_input = db.dict_year_1 - year_2_input = db.dict_year_2 - output = db.dict_add - result = add_year_dictionary(year_1_input, year_2_input) - assert result == output +class TestAddYear: + """Test the add_year_dictionary function for different sets of inputs.""" + + def test_add_year_dictionary_1(self): + """Test for empty arguments.""" + result = add_year_dictionary({}, {}) + assert result == {} + + def test_add_year_dictionary_2(self): + """Test for some overlapping and non overlapping keys.""" + year_1_input = db.dict_year_1 + year_2_input = db.dict_year_2 + output = db.dict_add + result = add_year_dictionary(year_1_input, year_2_input) + assert result == output + + def test_add_year_dictionary_3(self): + """Test when one dictionary has key and the other doesn't.""" + dict1 = {2014: 200} + dict2 = {} + result = add_year_dictionary(dict1, dict2) + expected_result = {2014: 200} + assert expected_result == result + + def test_add_year_dictionary_4(self): + """Test when both dictionaries have non-overlapping keys.""" + dict1 = {2014: 200, 2017: 400} + dict2 = {2010: 100, 2020: 1000} + expected_result = {2010: 100, 2014: 200, 2017: 400, 2020: 1000} + result = add_year_dictionary(dict1, dict2) + assert result == expected_result + + def test_add_year_dictionary_5(self): + """Test when both dictionaries have all overlapping keys.""" + dict1 = {2014: 200, 2016: 300} + dict2 = {2014: 100, 2016: 150} + expected_result = {2014: 300, 2016: 450} + result = add_year_dictionary(dict1, dict2) + assert result == expected_result + + +class TestSubtractYear: + """Test the subtract_year_dictionary function for different input cases.""" + + def test_subtract_year_dictionary_1(self): + """Test for some overlapping and non overlapping keys.""" + year_1_input = db.dict_year_1 + year_2_input = db.dict_year_2 + output = db.dict_sub + result = subtract_year_dictionary(year_1_input, year_2_input) + assert result == output + + def test_subtract_year_dictionary_2(self): + """Test for empty inputs.""" + expected_result = {} + result = subtract_year_dictionary({}, {}) + assert result == expected_result + + def test_subtract_year_dictionary_3(self): + """Test when dict1 has inputs and dict2 is empty.""" + dict1 = {2014: 200, 2015: 250} + expected_result = {2014: 200, 2015: 250} + result = subtract_year_dictionary(dict1, {}) + assert result == expected_result + + def test_subtract_year_dictionary_4(self): + """Test when dict2 has inputs and dict1 is empty.""" + dict2 = {2014: 200, 2015: 240} + expected_result = {2014: -200, 2015: -240} + result = subtract_year_dictionary({}, dict2) + assert result == expected_result + +class TestDivideDSCR: + """"Test the divide_dscr_dict function for different input cases.""" + + def test_divide_dscr_dict_1(self): + """Test for empty inputs.""" + expected_result = {} + result = divide_dscr_dict({}, {}) + assert result == expected_result + + def test_divide_dscr_dict_2(self): + """Test for dict1 with values and empty dict2.""" + dict1 = {2014: 10, 2015: 20} + expected_result = {2014: None, 2015: None} + result = divide_dscr_dict(dict1, {}) + assert result == expected_result + + def test_divide_dscr_dict_3(self): + """Test when dict2 has a value 0.""" + dict1 = {2014: 100, 2015: 200} + dict2 = {2014: 0, 2015: 0} + expected_result = {2014: None, 2015: None} + result = divide_dscr_dict(dict1, dict2) + assert result == expected_result + + def test_divide_dscr_dict_4(self): + """Test for actual case inputs.""" + year_1_input = db.dict_year_1 + year_2_input = db.dict_year_2 + output = {2011: 0.0, 2012: 2.5, 2013: 0.3333333333333333, 2014: 2.0, 2015: None} + result = divide_dscr_dict(year_1_input, year_2_input) + assert result == output + + +class TestMinNoneList: + """Test the min_none_list function.""" + + def test_min_non_list_1(self): + """Test for empty list.""" + input_list = [] + expected_result = 0 + result = min_none_list(input_list) + assert result == expected_result + + def test_min_non_list_2(self): + """Test for list with one value.""" + input_list = [1] + expected_result = 1 + result = min_none_list(input_list) + assert result == expected_result + + def test_min_non_list_3(self): + """Test for input list with one None.""" + input_list = [None] + expected_result = 0 + result = min_none_list(input_list) + assert result == expected_result + + def test_min_non_list_4(self): + """Test with mix of positive and negative numbers, string and None.""" + input_list = [10, None, 0, '', -2] + with pytest.raises(ValueError): + min_none_list(input_list) + +class TestAddList: + """Test the add_list function.""" + + def test_add_list_1(self): + """Test to add 5 to an empty input list.""" + input_list = [] + expected_result = [] + result = add_list(input_list, 5) + assert result == expected_result + + def test_add_list_2(self): + """Test to add 5 to a list of numbers.""" + input_list = [10, 20, -2, 4, -8] + expected_result = [15, 25, 3, 9, -3] + result = add_list(input_list, 5) + assert result == expected_result + + def test_add_list_3(self): + """Test to add 5 to list of numbers and None and should raise ValueError.""" + input_list = [10, -3, None, 50] + with pytest.raises(ValueError): + add_list(input_list, 5) + + def test_add_list_4(self): + """Test to add 5 to a list with strings in them.""" + input_list = [20, 4, -10, 'a', -20] + with pytest.raises(ValueError): + add_list(input_list, 5) + + +class TestCalLastDay: + """Test cal_last_day function.""" + + def test_cal_last_day_1(self): + """Test when both arguments are 0.""" + with pytest.raises(ValueError): + cal_last_day(0, 0) + + def test_cal_last_day_2(self): + """Test when month is 0.""" + with pytest.raises(ValueError): + cal_last_day(2017, 0) + + def test_cal_last_day_3(self): + """Test when year is 0.""" + with pytest.raises(ValueError): + cal_last_day(0, 3) + + def test_cal_last_day_4(self): + """Test for february of leap year.""" + expected_result = 29 + result = cal_last_day(2012, 2) + assert result == expected_result + + def test_cal_last_day_5(self): + """Test for a June.""" + expected_result = 30 + result = cal_last_day(2017, 6) + assert result == expected_result + + +class TestFormBillCal: + """Test form_bill_calendar function.""" + + def test_form_bill_calendar_1(self): + """Test when year term is 0.""" + start_date = date(2017, 2, 10) + expected_result = [[], []] + result = form_bill_calendar(start_date, 0) + assert result == expected_result + + def test_form_bill_calendar_2(self): + """Test for year term of 1.""" + start_date = date(2017, 1, 23) + expected_result = [ + [ + date(2017, 1, 1), + date(2017, 2, 1), + date(2017, 3, 1), + date(2017, 4, 1), + date(2017, 5, 1), + date(2017, 6, 1), + date(2017, 7, 1), + date(2017, 8, 1), + date(2017, 9, 1), + date(2017, 10, 1), + date(2017, 11, 1), + date(2017, 12, 1), + ], + [ + date(2017, 1, 31), + date(2017, 2, 28), + date(2017, 3, 31), + date(2017, 4, 30), + date(2017, 5, 31), + date(2017, 6, 30), + date(2017, 7, 31), + date(2017, 8, 31), + date(2017, 9, 30), + date(2017, 10, 31), + date(2017, 11, 30), + date(2017, 12, 31), + ] + ] + result = form_bill_calendar(start_date, 1) + assert result == expected_result + def test_form_bill_calendar_3(self): + """Test when negative year term is entered.""" + with pytest.raises(ValueError): + form_bill_calendar(date(2017, 1, 1), -2) -def test_subtract_year_dictionary(): - year_1_input = db.dict_year_1 - year_2_input = db.dict_year_2 - output = db.dict_sub - result = subtract_year_dictionary(year_1_input, year_2_input) - assert result == output +class TestFormDateCalendar: + """Test form_date_calendar function.""" + def test_form_date_cal_1(self): + """Test when start and end date are equal.""" + expected_result = [[], []] + result = form_date_calendar(date(2017, 1, 1), date(2017, 1, 1)) + assert result == expected_result -def test_divide_dscr_dict(): - year_1_input = db.dict_year_1 - year_2_input = db.dict_year_2 - output = {2011: 0.0, 2012: 2.5, 2013: 0.3333333333333333, 2014: 2.0, 2015: None} - result = divide_dscr_dict(year_1_input, year_2_input) - assert result == output + def test_form_date_cal_2(self): + """Test when end date is within a month from start date.""" + expected_result = [[], []] + result = form_date_calendar(date(2017, 1, 1), date(2017, 1, 4)) + assert result == expected_result + def test_form_date_cal_3(self): + """Test when end date is exactly 1 month from start date.""" + expected_result = [ + [ + date(2017, 1, 1), + ], + [ + date(2017, 1, 31), + ] + ] + result = form_date_calendar(date(2017, 1, 1), date(2017, 2, 1)) + assert result == expected_result def test_average_list_with_none(): input_list = []