Changeset - 96a363633f5e
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-21 15:58:28
brettcsmith@brettcsmith.org
books: Add Loader.load_fy_range() method.
2 files changed with 28 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/books.py
Show inline comments
...
 
@@ -9,31 +9,37 @@
 
# 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
 

	
 
from pathlib import Path
 

	
 
from beancount import loader as bc_loader
 

	
 
from typing import (
 
    Any,
 
    Iterable,
 
    Mapping,
 
    NamedTuple,
 
    Optional,
 
    Union,
 
)
 
from .beancount_types import (
 
    LoadResult,
 
)
 

	
 
PathLike = Union[str, Path]
 
PluginsSpec = Mapping[str, Optional[str]]
 
Year = Union[int, datetime.date]
 

	
 
class FiscalYear(NamedTuple):
 
    month: int = 3
 
    day: int = 1
 

	
 
    def for_date(self, date: Optional[datetime.date]=None) -> int:
 
        if date is None:
 
            date = datetime.date.today()
...
 
@@ -138,12 +144,27 @@ class Loader:
 
        if plugins is None:
 
            plugins = self.plugins
 
        years = iter(self.fiscal_year.range(from_fy, to_fy))
 
        try:
 
            books_start = self._format_include(next(years), 'books')
 
        except StopIteration:
 
            return ''
 
        return '\n'.join([
 
            *(self._format_plugin(name, opts) for name, opts in plugins.items()),
 
            books_start,
 
            *(self._format_include(year) for year in years),
 
        ])
 

	
 
    def load_fy_range(self,
 
                      from_fy: Year,
 
                      to_fy: Optional[Year]=None,
 
                      plugins: Optional[PluginsSpec]=None,
 
    ) -> LoadResult:
 
        """Load books for a range of fiscal years
 

	
 
        This is a convenience wrapper to call
 
        self.fy_range_string(from_fy, to_fy, plugins)
 
        and load the result with beancount.loader.load_string.
 
        """
 
        return bc_loader.load_string(  # type:ignore[no-untyped-call, no-any-return]
 
            self.fy_range_string(from_fy, to_fy, plugins),
 
        )
tests/test_books_loader.py
Show inline comments
...
 
@@ -5,24 +5,26 @@
 
# 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 hashlib
 

	
 
from datetime import date
 
from pathlib import Path
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import books
 

	
 
books_path = testutil.test_path('booksroot')
 

	
 
@pytest.fixture(scope='module')
...
 
@@ -65,12 +67,17 @@ def test_fy_range_string_one_offset(conservancy_loader, year_offset):
 
    assert conservancy_loader.fy_range_string(year_offset) == expected
 

	
 
@pytest.mark.parametrize('plugins', [
 
    {},
 
    {'conservancy_beancount.plugin': '-all'},
 
])
 
def test_fy_range_string_plugins_override(conservancy_loader, plugins):
 
    expected = expect_string([2019, 2020], plugins)
 
    assert conservancy_loader.fy_range_string(2019, 2020, plugins) == expected
 

	
 
def test_fy_range_string_empty_range(conservancy_loader):
 
    assert conservancy_loader.fy_range_string(2020, 2019) == ''
 

	
 
def test_load_fy_range_empty(conservancy_loader):
 
    entries, errors, options_map = conservancy_loader.load_fy_range(2020, 2019)
 
    assert not entries
 
    assert options_map.get('input_hash') == hashlib.md5().hexdigest()
0 comments (0 inline, 0 general)