budget

Return the total budget for a given calculation period

Importing all of the required libraries:

The entry point of the budget console script:


source

main

 main (budget:float, start_date:str, end_date:str, days_in_week:int=6,
       weights:str=None, amounts:str=None)

Return the total budget for the calculation period from the start date to the end date (inclusive).

Type Default Details
budget float The budget amount per day
start_date str The start date of the calculation period; it is expected to be in the ISO 8601 format
end_date str The end date of the calculation period; it is expected to be in the ISO 8601 format
days_in_week int 6 The number of consecutive days in a week (starting from Monday) to include in the budget
weights str None The weight to apply for each day of a week, and specified as ‘[x0, .. , xn]’, where x0..xn are numbers; this will override –days_in_week
amounts str None The budget amount for each day of a week, and specified as ‘[x0, .. , xn]’, where x0..xn are numbers; the budget amount per day that was specified will be ignored

Unit Tests

The calendar for the month of August in 2023:

Screenshot 2023-08-14 at 12.26.09 AM.png

Testing the scenario where the start_date and end_date are of the expected ISO 8601 date format:

test_eq(main(budget=1, start_date='20230813', end_date='20230902'),
        18.0)

Testing the scenario where the start_date and end_date are of the expected ISO 8601 date format, and the days_in_week argument is a different value from the default value of 6:

test_eq(main(budget=1, start_date='20230813', end_date='20230902', days_in_week=7),
        21.0)

Testing a different variation of the date format in the ISO 8601 standard, which previously was the only format accepted by the datetime library (before version 3.11):

test_eq(main(budget=1, start_date='2023-08-13', end_date='2023-09-02'),
        18.0)

Testing a set of weights that is equivalent to days_in_weeks = 6:

test_eq(main(budget=1, start_date='20230813', end_date='20230902', weights='[1,1,1,1,1,1,0]'),
        18.0)

Testing a set of weights that is equivalent to days_in_weeks = 7:

test_eq(main(budget=1, start_date='20230813', end_date='20230902', weights='[1,1,1,1,1,1,1]'),
        21.0)

Testing a set of amounts in conjunction with the default value of days_in_week = 6:

test_eq(main(budget=1, start_date='20230813', end_date='20230902', amounts='[1,1,1,1,1,1,0]'),
        18.0)

Because days_in_week = 6, ws will take on the value of [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0], and total_budget will not take into consideration the 7th element of amts (which is also amounts):

test_eq(main(budget=1, start_date='20230813', end_date='20230902', amounts='[1,1,1,1,1,1,1]'),
        18.0)

The following test case will take into consideration the 7th element of amts (and amounts), as days_in_week = 7:

test_eq(main(budget=1, start_date='20230813', end_date='20230902', amounts='[1,1,1,1,1,1,1]',
             days_in_week=7), 21.0)

sys.exit() is invoked when the start_date is not of the ISO 8601 date format, and it raises a SystemExit exception:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='2023', end_date='20230902')

Similarly, sys.exit() is invoked when the end_date is not of the ISO 8601 date format, and it raises a SystemExit exception:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='2023')

The SystemExit exception is raised by sys.exit() when the end_date is before the start_date:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230808')

The SystemExit exception is raised by sys.exit() when the weights argument is not well-formed:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230902', weights='[1,a,1,1,1,1,0]')

The same exception is raised when the amounts argument is not well-formed:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230902', amounts='[1,a,1,1,1,1,0]')

Also, the SystemExit exception is raised when the weights argument has less than 7 elements:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230902', weights='[1,1,1]')

The same outcome when the amounts argument has less than 7 elements:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230902', amounts='[1,1,1]')

The same exception is raised when the days_in_week argument is not of the integer value from 1 to 7:

with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230902', days_in_week = 8)
with ExceptionExpected(ex=SystemExit): main(budget=1, start_date='20230813', end_date='20230902', days_in_week = 0)