def message_schedule(person_name, message, ical, count, frequency, lexbot, interval, timezone, start_date, start_time, end_date, bot_names, ice_breaker, required_bots, introduction, end_time): try: print ice_breaker print required_bots click.echo("Scheduling message for person {}".format(person_name)) scheduler = Scheduler() if not timezone: timezone = tzlocal.get_localzone().zone click.echo('Timezone: {}'.format(timezone)) if start_time is None: start_time = arrow.now(timezone).format('HH:mm') if start_date is None: start_date = arrow.now(timezone).format('YYYY-MM-DD') start_datetime = arrow.get( '{} {}'.format(start_date, start_time)) \ .replace(tzinfo=tz.gettz(timezone)).to('UTC') if end_time is None: end_time = start_time.format('HH:mm') if end_date is None: end_date = start_datetime.replace(years=10).format('YYYY-MM-DD') end_datetime = arrow.get( '{} {}'.format(end_date, end_time)) \ .replace(tzinfo=tz.gettz(timezone)).to('UTC') message = ScheduledMessage(StartDateTimeInUtc=start_datetime, ical=ical, Body=message, PersonName=person_name, Frequency=frequency, Count=count, Lexbot=lexbot, TimeZone=timezone, Interval=interval, BotNames=bot_names, IceBreaker=ice_breaker, Introduction=introduction, RequiredBots=required_bots, EndDateTimeInUtc=end_datetime) scheduler.schedule_message(message) click.echo('Start Time: {}'.format(start_datetime)) click.echo('End Time: {}'.format(end_datetime)) if ical: click.echo('ical:\n{}'.format(ical)) print "Next: {}".format(message.next_occurrence_local) print message.to_ical() except Exception: click.echo(traceback.print_exc()) exit(2)
def test_get_next_occurence_with_hourly_recurrence(): compare_date = arrow.get('2012-01-05 03:00:00 UTC') m = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01:00 UTC'), ical="RRULE:FREQ=HOURLY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.utcnow().replace(days=10)) assert (m.is_message_ready(CompareDateTimeInUtc=compare_date))
def test_next_occurrence_after_compare_date_not_ready(): compare_date = arrow.get('2012-01-01 03:00:00 UTC') m = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01:00 UTC'), LastOccurrenceInUtc=arrow.get('2012-01-01 03:00:00 UTC'), ical="RRULE:FREQ=HOURLY;INTERVAL=2", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.utcnow().replace(days=10)) assert not m.is_message_ready(CompareDateTimeInUtc=compare_date)
def test_passing_startdate_adds_to_ical(): start_date = arrow.get('2012-01-01 01:01:00 UTC') ical = 'BEGIN:VEVENT\r\n' + \ 'DTSTART;VALUE=DATE-TIME:20120101T010100Z\r\n' + \ 'END:VEVENT\r\n' m = ScheduledMessage( StartDateTimeInUtc=start_date, Body="Test", PersonName="test") assert ical == m.to_ical()
def message_schedule(person_name, message, expiration_datetime, ical, start_datetime): try: click.echo("Scheduling message for person {}".format(person_name)) scheduler = Scheduler() start_datetime = arrow.utcnow() if start_datetime is None: click.echo("Setting start time to now") else: start_datetime = arrow.get(start_datetime) click.echo("Setting custom start time to %s" % start_datetime) if ical is None: ical = 'FREQ=DAILY;COUNT=1' if expiration_datetime is None: expiration_datetime = start_datetime.replace(hours=1) message = ScheduledMessage(StartDateTimeInUtc=start_datetime, ical=ical, Body=message, PersonName=person_name, EndDateTimeInUtc=expiration_datetime) scheduler.schedule_message(message) except Exception: click.echo(traceback.print_exc()) exit(2)
def test_passing_enddate_adds_to_ical(): start_date = arrow.get('2012-01-01 01:01:00 UTC') end_date = start_date.replace(days=5) ical = 'BEGIN:VEVENT\r\n' + \ 'DTSTART;VALUE=DATE-TIME:20120101T010100Z\r\n' + \ 'DTEND;VALUE=DATE-TIME:20120106T010100Z\r\n' + \ 'RRULE:FREQ=HOURLY\r\n' + \ 'END:VEVENT\r\n' m = ScheduledMessage( StartDateTimeInUtc=start_date, EndDateTimeInUtc=end_date, Body="Test", Frequency='HOURLY', PersonName="test") assert ical == m.to_ical()
def good_scheduled_message(): return ScheduledMessage( StartDateTimeInUtc=arrow.utcnow().replace(days=-7), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.utcnow().replace(days=5))
def test_expiration_is_end_of_interval(): start_date = arrow.get('2012-01-01 01:01:00 UTC') m = ScheduledMessage(StartDateTimeInUtc=start_date, ical="RRULE:FREQ=HOURLY;INTERVAL=2", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=start_date.replace(days=10)) assert m.next_expiration_utc == start_date.replace(hours=2)
def test_first_occurrence_is_start_date(): start_date = arrow.get('2012-01-01 01:01:00 UTC') m = ScheduledMessage(StartDateTimeInUtc=start_date, ical="RRULE:FREQ=HOURLY;INTERVAL=2", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=start_date.replace(days=10)) assert m.next_occurrence_utc == start_date
def test_next_occurrence_before_end_datetime_is_not_expired(): m = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01:00 UTC'), ical="RRULE:FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", LastOccurrenceInUtc=arrow.get('2012-01-02 01:01:00 UTC'), EndDateTimeInUtc=arrow.get('2027-01-03 01:01:01 UTC')) assert not m.is_expired
def test_get_next_occurence_with_bi_hourly_recurrence(): m = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01:00 UTC'), ical="RRULE:FREQ=HOURLY;INTERVAL=2", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.utcnow().replace(days=10)) assert m.next_occurrence_utc.year == 2012 and \ m.next_occurrence_utc.hour == 1
def test_adding_message_expiring_before_today_throws_error(): with pytest.raises(ValueError) as exception: ScheduledMessage(StartDateTimeInUtc=datetime.datetime.utcnow(), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=datetime.datetime.utcnow() - datetime.timedelta(days=10)) assert "Start datetime is after end datetime" in str(exception)
def test_adding_blank_messages_throws_error(): with pytest.raises(ValueError) as exception: ScheduledMessage(StartDateTimeInUtc=datetime.datetime.utcnow(), ical="FREQ=DAILY", Body="", PersonName="Testperson", EndDateTimeInUtc=datetime.datetime.utcnow() + datetime.timedelta(days=10)) assert "Message body is empty" in str(exception)
def test_endtime_before_expiration_becomes_expiration(): start_date = arrow.get('2012-01-01 01:01:00 UTC') end_date = arrow.get('2012-01-01 02:00:00 UTC') m = ScheduledMessage(StartDateTimeInUtc=start_date, ical="RRULE:FREQ=HOURLY;INTERVAL=2", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=end_date) assert m.next_expiration_utc == end_date
def test_last_datetime_in_utc_with_no_tz_throws_error(good_scheduled_message): with pytest.raises(ValueError) as exception: ScheduledMessage(StartDateTimeInUtc=arrow.utcnow(), LastOccurrenceInUtc=arrow.utcnow().naive, ical="RRULE:FREQ=HOURLY;INTERVAL=2", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.utcnow().replace(days=10)) assert 'datetime last_occurrence_in_utc has no timezone' in \ str(exception)
def test_only_get_ready_messages(good_scheduled_message): compare_date = arrow.utcnow().replace(hours=24) msg1 = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) msg2 = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) scheduler_under_test = Scheduler() scheduler_under_test.schedule_message(msg1) scheduler_under_test.schedule_message(msg2) m = scheduler_under_test.get_messages(compare_date, True) assert len(m) == 2
def test_new_message_is_ready(good_scheduled_message): msg = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) scheduler_under_test = Scheduler() scheduler_under_test.schedule_message(msg) m = scheduler_under_test.get_messages()[0] assert m.is_message_ready
def test_ignore_expired_messages(good_scheduled_message): msg = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2014-01-01 01:01 UTC')) compare_date = arrow.utcnow() scheduler_under_test = Scheduler() scheduler_under_test.schedule_message(msg) m = scheduler_under_test.get_messages(compare_date, True) assert len(m) == 0
def convert_to_scheduled_message(db_message): last = db_message.get("last_occurrence_in_utc", None) if db_message.get("last_occurrence_in_utc", None): last = arrow.get(last) return (ScheduledMessage( UUID=db_message["uuid"], StartDateTimeInUtc=arrow.get(db_message["start_datetime_in_utc"]), ical=db_message["ical"], Body=db_message["body"], IsQueued=db_message.get("in_queue", False), PersonName=db_message["person_name"], LastLocationIndex=db_message.get('last_location_index', 0), LastOccurrenceInUtc=last, EndDateTimeInUtc=arrow.get(db_message["end_datetime_in_utc"])))
def test_set_in_queue_sets_message_not_ready(good_scheduled_message): msg = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) test_person_name = msg.person_name scheduler_under_test = Scheduler() scheduler_under_test.schedule_message(msg) scheduler_under_test.update_queue_status(msg.uuid_key, test_person_name, True) m = scheduler_under_test.get_messages(None, False)[0] assert m.is_queued assert not m.is_message_ready()
def test_mark_update_last_occurrence_works(good_scheduled_message): msg = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) test_person_name = msg.person_name scheduler_under_test = Scheduler() scheduler_under_test.schedule_message(msg) m = scheduler_under_test.get_messages()[0] spoken_time = arrow.get('2014-01-01 11:11:11 UTC') m.mark_spoken(spoken_time) scheduler_under_test.update_last_occurrence(m.uuid_key, test_person_name, spoken_time) assert m.last_occurrence_in_utc.year == 2014
def test_mark_update_last_occurrence_doesnt_get_same(good_scheduled_message): say_date = arrow.get('2013-01-01 01:01:01 UTC') msg = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="Testperson", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) compare_date = say_date.replace(hours=1) test_person_name = msg.person_name scheduler_under_test = Scheduler() scheduler_under_test.schedule_message(msg) m = scheduler_under_test.get_messages(say_date, True)[0] m.mark_spoken(say_date) scheduler_under_test.update_last_occurrence(m.uuid_key, test_person_name) m = scheduler_under_test.get_messages(compare_date, True) assert len(m) == 0
from scheduler.scheduler import Scheduler from person.person import Person, PersonManager, PersonTimeWindow from tests.test_person import ical_event import arrow import logging import traceback class Config(): def __init__(self): self.value = 777 mock_message1 = ScheduledMessage( StartDateTimeInUtc=arrow.get('2012-01-01 01:01 UTC'), ical="FREQ=DAILY", Body="Test Message Body", PersonName="calvin", EndDateTimeInUtc=arrow.get('2027-01-01 01:01 UTC')) def chloe_three_rooms(now_dt): p = Person(Name='chloe') tw = PersonTimeWindow(LocationName='kitchen', Priority=100, ical=ical_event, CompareDateTime=now_dt) p.add_window(tw) tw = PersonTimeWindow(LocationName='dining_room', Priority=300, ical=ical_event, CompareDateTime=now_dt)