Skip to content

Commit

Permalink
moved helper functions to utils.py + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdckr committed May 22, 2019
1 parent c759b46 commit 5729d3e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
50 changes: 50 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
safe_save,
parse_tags,
PY2,
isTime,
isDateTime,
getDateTimeToday,
getMergedDateTime
)
from . import mock_datetime

Expand Down Expand Up @@ -228,3 +232,49 @@ def test_confirm_tags_reject_raises_abort(confirm):
watson_tags = ['a', 'b']
with pytest.raises(Abort):
confirm_project(tags, watson_tags)


@pytest.mark.parametrize("date_time_string, result", [
("2018-05-28 7:00", False),
("2018-05.28 7:00", False),
("7:00", True),
("17:00", True),
("117:00", False),
("117", False),
])
def test_isTime(date_time_string, result):
assert isTime(date_time_string) == result


@pytest.mark.parametrize("date_time_string, result", [
("2018-05-28 7:00", True),
("2018-05-28 07:00", True),
("2018-05-28 127:00", False),
("2018-05-f 7:00", False),
("2018-05-28 7.00", False),
("7:00", False),
])
def test_isDateTime(date_time_string, result):
assert isDateTime(date_time_string) == result


@pytest.mark.parametrize("date_time, time_string", [
("2014-04-01", "07:00"),
("2014-04-01", "17:00"),
])
def test_getMergedDateTime(date_time, time_string):
result = arrow.get("{} {}".format(date_time, time_string), 'YYYY-MM-DD HH:mm')
date = arrow.get(date_time, 'YYYY-MM-DD')
assert getMergedDateTime(date, time_string) == result


@pytest.mark.parametrize("time_string", [
("07:00"),
("17:00"),
("09:05"),
("13:37"),
])
def test_getDateTimeToday(time_string):
today = datetime.date.today().strftime("%Y-%m-%d")
result = arrow.get("{} {}".format(today, time_string), 'YYYY-MM-DD HH:mm').replace(tzinfo='local')
assert getDateTimeToday(time_string) == result
20 changes: 4 additions & 16 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
sorted_groupby,
style,
parse_tags,
isTime,
isDateTime,
getDateTimeToday,
getMergedDateTime
)


Expand Down Expand Up @@ -1013,22 +1017,6 @@ def frames(watson):
for frame in watson.frames:
click.echo(style('short_id', frame.id))

def isTime(date_time_string):
match = re.search("^\d{1,2}:\d{1,2}$", date_time_string)
return match != None

def isDateTime(date_time_string):
match = re.search("^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}$", date_time_string)
return match != None

def getDateTimeToday(time_string):
date_today = arrow.now().floor("day")
return getMergedDateTime(date_today, time_string)

def getMergedDateTime(date_time, time_string):
hours, minutes = time_string.split(":")
return date_time.shift(hours=int(hours), minutes=int(minutes))

@cli.command(context_settings={'ignore_unknown_options': True})
@click.argument('args', nargs=-1)
@click.option('-f', '--from', 'from_', required=True,
Expand Down
21 changes: 21 additions & 0 deletions watson/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import click
import arrow
import re

from .fullmoon import get_last_full_moon

Expand Down Expand Up @@ -282,3 +283,23 @@ def parse_tags(values_list):
))
for i, w in enumerate(values_list) if w.startswith('+')
)))) # pile of pancakes !


def isTime(date_time_string):
match = re.search("^\d{1,2}:\d{1,2}$", date_time_string)
return match != None


def isDateTime(date_time_string):
match = re.search("^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}$", date_time_string)
return match != None


def getDateTimeToday(time_string):
date_today = arrow.now().floor("day")
return getMergedDateTime(date_today, time_string)


def getMergedDateTime(date_time, time_string):
hours, minutes = time_string.split(":")
return date_time.shift(hours=int(hours), minutes=int(minutes))

0 comments on commit 5729d3e

Please sign in to comment.