Files
@ 728db9983ec1
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_books_fiscal_year.py
728db9983ec1
5.7 KiB
text/x-python
fill: Use program logger over module logger to report problems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | """test_books_fiscal_year - Unit tests for books.FiscalYear"""
# Copyright © 2020 Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.
import datetime
import itertools
import pytest
from conservancy_beancount import books
@pytest.fixture(scope='module')
def conservancy_fy():
return books.FiscalYear(3, 1)
@pytest.fixture(scope='module')
def cy_fy():
return books.FiscalYear(1, 1)
# Not bothering because it's too much trouble to override __new__
# @pytest.mark.parametrize('month,day', [
# (0, 0),
# (2, 29),
# (4, 31),
# (13, 15),
# (-1, 1),
# (1, -5),
# ])
# def test_values_checked(month, day):
# with pytest.raises(ValueError):
# books.FiscalYear(month, day)
def test_attribute_access():
fy = books.FiscalYear(2, 15)
assert fy.month == 2
assert fy.day == 15
with pytest.raises(AttributeError):
fy.year
@pytest.mark.parametrize('month,day,expected', [
(1, 1, 2019),
(2, 15, 2019),
(2, 28, 2019),
(2, 29, 2019),
(3, 1, 2020),
(3, 9, 2020),
(6, 1, 2020),
(12, 31, 2020),
])
def test_for_date(conservancy_fy, month, day, expected):
date = datetime.date(2020, month, day)
assert conservancy_fy.for_date(date) == expected
def test_for_date_default_today(cy_fy):
assert cy_fy.for_date() == datetime.date.today().year
@pytest.mark.parametrize('begin_date,end_date,expected', [
((2020, 3, 1), (2021, 2, 28), [2020]),
((2020, 1, 1), (2020, 3, 1), [2019]),
((2020, 1, 1), (2020, 3, 5), [2019, 2020]),
((2019, 2, 1), (2020, 6, 1), [2018, 2019, 2020]),
])
def test_range_two_dates(conservancy_fy, begin_date, end_date, expected):
actual = list(conservancy_fy.range(datetime.date(*begin_date), datetime.date(*end_date)))
assert actual == expected
@pytest.mark.parametrize('year_offset', range(3))
def test_range_one_date(cy_fy, year_offset):
this_year = datetime.date.today().year
actual = list(cy_fy.range(datetime.date(this_year - year_offset, 1, 1)))
assert actual == list(range(this_year - year_offset, this_year + 1))
@pytest.mark.parametrize('begin_year,end_year', [
(2006, 2020),
(2019, 2020),
(2020, 2020),
])
def test_range_two_years(conservancy_fy, begin_year, end_year):
actual = list(conservancy_fy.range(begin_year, end_year))
assert actual == list(range(begin_year, end_year + 1))
@pytest.mark.parametrize('year_offset', range(3))
def test_range_one_year(cy_fy, year_offset):
this_year = datetime.date.today().year
actual = list(cy_fy.range(this_year - year_offset))
assert actual == list(range(this_year - year_offset, this_year + 1))
@pytest.mark.parametrize('year_offset,month_offset', itertools.product(
range(-3, 3),
[-1, 1]
))
def test_range_offset_and_date(conservancy_fy, year_offset, month_offset):
end_date = datetime.date(2020, conservancy_fy.month + month_offset, 10)
base_year = end_date.year
if month_offset < 0:
base_year -= 1
if year_offset < 0:
expected = range(base_year + year_offset, base_year + 1)
else:
expected = range(base_year, base_year + year_offset + 1)
actual = list(conservancy_fy.range(year_offset, end_date))
assert actual == list(expected)
@pytest.mark.parametrize('year_offset,year', itertools.product(
range(-3, 3),
[2010, 2015],
))
def test_range_offset_and_year(conservancy_fy, year_offset, year):
if year_offset < 0:
expected = range(year + year_offset, year + 1)
else:
expected = range(year, year + year_offset + 1)
actual = list(conservancy_fy.range(year_offset, year))
assert actual == list(expected)
@pytest.mark.parametrize('year_offset', range(-3, 3))
def test_range_offset_only(cy_fy, year_offset):
year = datetime.date.today().year
if year_offset < 0:
expected = range(year + year_offset, year + 1)
else:
expected = range(year, year + year_offset + 1)
actual = list(cy_fy.range(year_offset))
assert actual == list(expected)
@pytest.mark.parametrize('year', range(2016, 2022))
def test_first_date_year_arg(conservancy_fy, year):
assert conservancy_fy.first_date(year) == datetime.date(year, 3, 1)
@pytest.mark.parametrize('date', [
datetime.date(2019, 1, 1),
datetime.date(2019, 10, 10),
datetime.date(2020, 2, 2),
datetime.date(2020, 12, 12),
])
def test_first_date_date_arg(conservancy_fy, date):
year = date.year
if date.month < 3:
year -= 1
assert conservancy_fy.first_date(date) == datetime.date(year, 3, 1)
@pytest.mark.parametrize('year', range(2016, 2022))
def test_last_date_year_arg(conservancy_fy, year):
day = 28 if year % 4 else 29
assert conservancy_fy.last_date(year - 1) == datetime.date(year, 2, day)
@pytest.mark.parametrize('date', [
datetime.date(2019, 1, 1),
datetime.date(2019, 10, 10),
datetime.date(2020, 2, 2),
datetime.date(2020, 12, 12),
])
def test_last_date_date_arg(conservancy_fy, date):
year = date.year
if date.month >= 3:
year += 1
day = 28 if year % 4 else 29
assert conservancy_fy.last_date(date) == datetime.date(year, 2, day)
@pytest.mark.parametrize('year', range(2016, 2022))
def test_next_fy_date_year_arg(conservancy_fy, year):
assert conservancy_fy.next_fy_date(year) == datetime.date(year + 1, 3, 1)
@pytest.mark.parametrize('date', [
datetime.date(2019, 1, 1),
datetime.date(2019, 10, 10),
datetime.date(2020, 2, 29),
datetime.date(2020, 12, 12),
])
def test_next_fy_date_date_arg(conservancy_fy, date):
year = date.year
if date.month >= 3:
year += 1
assert conservancy_fy.next_fy_date(date) == datetime.date(year, 3, 1)
|