Changeset - 7660700e6c8f
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-06-15 22:11:39
brettcsmith@brettcsmith.org
cliutil: Add date_arg() function.

Meant to be used as an argument type.
2 files changed with 25 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/cliutil.py
Show inline comments
...
 
@@ -19,2 +19,3 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>."""
 
import argparse
 
import datetime
 
import enum
...
 
@@ -233,2 +234,5 @@ def add_version_argument(parser: argparse.ArgumentParser) -> argparse.Action:
 

	
 
def date_arg(arg: str) -> datetime.date:
 
    return datetime.datetime.strptime(arg, '%Y-%m-%d').date()
 

	
 
def make_entry_point(mod_name: str, prog_name: str=sys.argv[0]) -> Callable[[], int]:
tests/test_cliutil.py
Show inline comments
...
 
@@ -17,2 +17,3 @@
 
import argparse
 
import datetime
 
import errno
...
 
@@ -76,2 +77,22 @@ def test_bytes_output_stream(path):
 

	
 
@pytest.mark.parametrize('year,month,day', [
 
    (2000, 1, 1),
 
    (2016, 2, 29),
 
    (2020, 12, 31),
 
])
 
def test_date_arg_valid(year, month, day):
 
    arg = f'{year}-{month}-{day}'
 
    expected = datetime.date(year, month, day)
 
    assert cliutil.date_arg(arg) == expected
 

	
 
@pytest.mark.parametrize('arg', [
 
    '2000',
 
    '20-02-12',
 
    '2019-02-29',
 
    'two thousand',
 
])
 
def test_date_arg_invalid(arg):
 
    with pytest.raises(ValueError):
 
        cliutil.date_arg(arg)
 

	
 
@pytest.mark.parametrize('func_name', [
0 comments (0 inline, 0 general)