Files @ a2ee9c73fe38
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_ranges.py

Brett Smith
ranges: Start module.

The ledger report wants to use this functionality, so make it available in a
higher-level module.

I took the opportunity to clean up a lot of the surrounding type
declarations. It is less flexible, since it relies on the static list of
types in RangeT, but I don't think the other method actually worked at all
except by cheating with generic Any.
"""test_ranges.py - Unit tests for range classes"""
# Copyright © 2020  Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import datetime

import pytest

from conservancy_beancount import ranges

ONE_DAY = datetime.timedelta(days=1)

@pytest.mark.parametrize('start,stop', [
    # One month
    (datetime.date(2018, 3, 1), datetime.date(2018, 4, 1)),
    # Three months
    (datetime.date(2018, 6, 1), datetime.date(2018, 9, 1)),
    # Six months, spanning year
    (datetime.date(2018, 9, 1), datetime.date(2019, 3, 1)),
    # Nine months
    (datetime.date(2018, 2, 1), datetime.date(2018, 12, 1)),
    # Twelve months on Jan 1
    (datetime.date(2018, 1, 1), datetime.date(2019, 1, 1)),
    # Twelve months spanning year
    (datetime.date(2018, 3, 1), datetime.date(2019, 3, 1)),
    # Eighteen months spanning year
    (datetime.date(2018, 3, 1), datetime.date(2019, 9, 1)),
    # Wild
    (datetime.date(2018, 1, 1), datetime.date(2020, 4, 15)),
])
def test_date_range(start, stop):
    date_range = ranges.DateRange(start, stop)
    assert (start - ONE_DAY) not in date_range
    assert start in date_range
    assert (start + ONE_DAY) in date_range
    assert (stop - ONE_DAY) in date_range
    assert stop not in date_range
    assert (stop + ONE_DAY) not in date_range

def test_date_range_one_day():
    start = datetime.date(2018, 7, 1)
    date_range = ranges.DateRange(start, start + ONE_DAY)
    assert (start - ONE_DAY) not in date_range
    assert start in date_range
    assert (start + ONE_DAY) not in date_range

def test_date_range_empty():
    date = datetime.date(2018, 8, 10)
    date_range = ranges.DateRange(date, date)
    assert (date - ONE_DAY) not in date_range
    assert date not in date_range
    assert (date + ONE_DAY) not in date_range