From ff150e3da055c215304f29d1a76a4275128e8a3e Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Wed, 7 Jun 2017 17:45:24 -0400 Subject: [PATCH 1/4] Add additional test cases for add_year_dictionary function. --- bpfin/tests/test_lib/test_other.py | 44 ++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py index a589198..7f7191d 100644 --- a/bpfin/tests/test_lib/test_other.py +++ b/bpfin/tests/test_lib/test_other.py @@ -6,13 +6,45 @@ from bpfin.lib.other import average_nonzero_list 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_output = {2014: 200} + assert expected_output == 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_output = {2010: 100, 2014: 200, 2017: 400, 2020: 1000} + result = add_year_dictionary(dict1, dict2) + assert result == expected_output + 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_output = {2014: 300, 2016: 450} + result = add_year_dictionary(dict1, dict2) + assert result == expected_output def test_subtract_year_dictionary(): -- GitLab From 19a80f9728af8f3453c5aa9e5a6d1398c9d4d58b Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Thu, 8 Jun 2017 17:14:01 -0400 Subject: [PATCH 2/4] Add some tests for lib functions. --- bpfin/tests/test_lib/test_other.py | 264 +++++++++++++++++++++++++++-- 1 file changed, 246 insertions(+), 18 deletions(-) diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py index 7f7191d..0477765 100644 --- a/bpfin/tests/test_lib/test_other.py +++ b/bpfin/tests/test_lib/test_other.py @@ -1,8 +1,17 @@ +"""Tests for other.py in lib.""" +import pytest +from datetime import date + 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 +from bpfin.lib.other import min_none_list +from bpfin.lib.other import add_list +from bpfin.lib.other import cal_last_day +from bpfin.lib.other import form_bill_calendar +from bpfin.lib.other import form_date_calendar from bpfin.tests.testdata import sample_data as db @@ -27,41 +36,260 @@ class TestAddYear: dict1 = {2014: 200} dict2 = {} result = add_year_dictionary(dict1, dict2) - expected_output = {2014: 200} - assert expected_output == result + 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_output = {2010: 100, 2014: 200, 2017: 400, 2020: 1000} + expected_result = {2010: 100, 2014: 200, 2017: 400, 2020: 1000} result = add_year_dictionary(dict1, dict2) - assert result == expected_output + 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_output = {2014: 300, 2016: 450} + expected_result = {2014: 300, 2016: 450} result = add_year_dictionary(dict1, dict2) - assert result == expected_output + 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 = [] -- GitLab From 45c6c89824808ee1a60dd1073ea77c76db4dbd99 Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Mon, 12 Jun 2017 09:48:10 -0400 Subject: [PATCH 3/4] Reorder import statements. --- bpfin/tests/test_lib/test_other.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py index 0477765..9db5d86 100644 --- a/bpfin/tests/test_lib/test_other.py +++ b/bpfin/tests/test_lib/test_other.py @@ -1,16 +1,9 @@ """Tests for other.py in lib.""" -import pytest from datetime import date +import pytest -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 -from bpfin.lib.other import min_none_list -from bpfin.lib.other import add_list -from bpfin.lib.other import cal_last_day -from bpfin.lib.other import form_bill_calendar +from bpfin.lib.other import add_year_dictionary, subtract_year_dictionary, divide_dscr_dict, average_list_with_none +from bpfin.lib.other import average_nonzero_list, min_none_list, add_list, cal_last_day, form_bill_calendar from bpfin.lib.other import form_date_calendar from bpfin.tests.testdata import sample_data as db -- GitLab From 0663fa0de13597c0409b669bcf75a64b4ecb8f8a Mon Sep 17 00:00:00 2001 From: Adarsh Murthy Date: Mon, 19 Jun 2017 13:07:44 -0400 Subject: [PATCH 4/4] Fix indentation. Fix multi-import to single import. --- bpfin/tests/test_lib/test_other.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py index 9db5d86..2c3ad4d 100644 --- a/bpfin/tests/test_lib/test_other.py +++ b/bpfin/tests/test_lib/test_other.py @@ -2,9 +2,10 @@ from datetime import date import pytest -from bpfin.lib.other import add_year_dictionary, subtract_year_dictionary, divide_dscr_dict, average_list_with_none -from bpfin.lib.other import average_nonzero_list, min_none_list, add_list, cal_last_day, form_bill_calendar -from bpfin.lib.other import form_date_calendar +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 @@ -276,10 +277,10 @@ class TestFormDateCalendar: 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 -- GitLab