Changeset - 51137815d304
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-21 13:07:14
brettcsmith@brettcsmith.org
config: fiscal_year_begin returns a FiscalYear.
2 files changed with 6 insertions and 3 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -30,12 +30,13 @@ from typing import (
 
    NamedTuple,
 
    Optional,
 
    Tuple,
 
    Type,
 
)
 

	
 
from . import books
 
from . import rtutil
 

	
 
class RTCredentials(NamedTuple):
 
    server: Optional[str] = None
 
    user: Optional[str] = None
 
    passwd: Optional[str] = None
...
 
@@ -127,13 +128,13 @@ class Config:
 
        )
 

	
 
    def config_file_path(self, name: str='conservancy_beancount') -> Path:
 
        config_root = self._path_from_environ('XDG_CONFIG_HOME')
 
        return Path(config_root, name, 'config.ini')
 

	
 
    def fiscal_year_begin(self) -> Tuple[int, int]:
 
    def fiscal_year_begin(self) -> books.FiscalYear:
 
        s = self.file_config.get('Beancount', 'fiscal year begin', fallback='3 1')
 
        match = re.match(r'([01]?[0-9])(?:\s*[-./ ]\s*([0-3]?[0-9]))?$', s.strip())
 
        if match is None:
 
            raise ValueError(f"fiscal year begin {s!r} has unknown format")
 
        try:
 
            month = int(match.group(1))
...
 
@@ -143,13 +144,13 @@ class Config:
 
            # 2. far enough in the past to not have books (pre-Unix epoch)
 
            # 3. not a leap year
 
            datetime.date(1959, month, day)
 
        except ValueError as e:
 
            raise ValueError(f"fiscal year begin {s!r} is invalid date: {e.args[0]}")
 
        else:
 
            return (month, day)
 
            return books.FiscalYear(month, day)
 

	
 
    def payment_threshold(self) -> decimal.Decimal:
 
        return decimal.Decimal(0)
 

	
 
    def repository_path(self) -> Optional[Path]:
 
        try:
tests/test_config.py
Show inline comments
...
 
@@ -375,7 +375,9 @@ def test_bad_fiscal_year_begin(value):
 
    config.load_string(f'[Beancount]\nfiscal year begin = {value}\n')
 
    with pytest.raises(ValueError):
 
        config.fiscal_year_begin()
 

	
 
def test_default_fiscal_year_begin():
 
    config = config_mod.Config()
 
    assert config.fiscal_year_begin() == (3, 1)
 
    actual = config.fiscal_year_begin()
 
    assert actual.month == 3
 
    assert actual.day == 1
0 comments (0 inline, 0 general)