Changeset - 1e09339b32f6
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-16 13:51:02
brettcsmith@brettcsmith.org
config: Make payment threshold configurable.
2 files changed with 17 insertions and 3 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -148,25 +148,28 @@ class Config:
 
            day = int(match.group(2) or 1)
 
            # To check date validity we use an arbitrary year that's
 
            # 1. definitely using the modern calendar
 
            # 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 books.FiscalYear(month, day)
 

	
 
    def payment_threshold(self) -> decimal.Decimal:
 
        return decimal.Decimal(0)
 
        try:
 
            return decimal.Decimal(self.file_config['Beancount']['payment threshold'])
 
        except (KeyError, ValueError):
 
            return decimal.Decimal(10)
 

	
 
    def repository_path(self) -> Optional[Path]:
 
        try:
 
            return Path(os.environ['CONSERVANCY_REPOSITORY'])
 
        except (KeyError, ValueError):
 
            return None
 

	
 
    def rt_credentials(self) -> RTCredentials:
 
        all_creds = zip(
 
            RTCredentials.from_env(),
 
            RTCredentials.from_rtrc(),
 
            RTCredentials(auth='rt'),
tests/test_config.py
Show inline comments
...
 
@@ -284,29 +284,40 @@ def test_cache_path_parent_conflict(tmp_path):
 
    (tmp_path / '.cache').touch()
 
    with update_environ(HOME=tmp_path, XDG_CACHE_HOME=None):
 
        config = config_mod.Config()
 
        assert config.cache_dir_path('TESTcache') is None
 

	
 
def test_relative_xdg_cache_home_ignored(tmp_path):
 
    with update_environ(HOME=tmp_path,
 
                        XDG_CACHE_HOME='nonexistent/test/cache/directory/tree'):
 
        config = config_mod.Config()
 
        cache_dir_path = config.cache_dir_path('TESTcache')
 
    assert cache_dir_path == tmp_path / '.cache/TESTcache'
 

	
 
def test_payment_threshold():
 
def test_default_payment_threshold():
 
    threshold = config_mod.Config().payment_threshold()
 
    assert threshold == 0
 
    assert isinstance(threshold, (int, decimal.Decimal))
 

	
 
@pytest.mark.parametrize('config_threshold', [
 
    '15',
 
    ' +15',
 
    '15. ',
 
    '15.0',
 
    '15.00',
 
])
 
def test_payment_threshold(config_threshold):
 
    config = config_mod.Config()
 
    config.load_string(f'[Beancount]\npayment threshold = {config_threshold}\n')
 
    assert config.payment_threshold() == decimal.Decimal(15)
 

	
 
@pytest.mark.parametrize('config_path', [
 
    None,
 
    '',
 
    'nonexistent/relative/path',
 
])
 
def test_config_file_path(config_path):
 
    expected = Path('~/.config/conservancy_beancount/config.ini').expanduser()
 
    with update_environ(XDG_CONFIG_HOME=config_path):
 
        config = config_mod.Config()
 
        assert config.config_file_path() == expected
 

	
 
def test_config_file_path_respects_xdg_config_home():
0 comments (0 inline, 0 general)