示例#1
0
def test_remove_cakeday(database, reddit):
    cakeday1 = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2015-05-05 15:25:17"))
    cakeday2 = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2015-05-05 15:25:17"))
    database.add_cakeday(cakeday1)
    database.add_cakeday(cakeday2)

    message = reddit_test.RedditObject(body=f"Remove! cakeday",
                                       author="Watchful3")
    messages.process_message(message, reddit, database)
    assert "You don't have a cakeday reminder set." in message.get_first_child(
    ).body

    message = reddit_test.RedditObject(body=f"Remove! cakeday",
                                       author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "Cakeday reminder deleted." in message.get_first_child().body
    assert database.get_cakeday("Watchful1") is None
    assert database.get_cakeday("Watchful2") is not None
示例#2
0
def process_cakeday_message(message, database):
    log.info("Processing cakeday")

    if database.get_cakeday(message.author.name) is not None:
        log.info("Cakeday already exists")
        return ["It looks like you already have a cakeday reminder set."]

    account_created = utils.datetime_from_timestamp(message.author.created_utc)
    next_anniversary = utils.add_years(
        account_created,
        utils.datetime_now().year - account_created.year)
    if next_anniversary < utils.datetime_now():
        next_anniversary = utils.add_years(next_anniversary, 1)
    log.debug(
        f"u/{message.author.name} created {utils.get_datetime_string(account_created)}, "
        f"anniversary {utils.get_datetime_string(next_anniversary)}")

    cakeday = Cakeday(message.author.name, next_anniversary)
    database.add_cakeday(cakeday)

    return cakeday.render_confirmation(
        database.get_settings(message.author.name).timezone)
示例#3
0
def test_send_cakedays(database, reddit):
	cakedays = [
		Cakeday(
			user="******",
			date_time=utils.parse_datetime_string("2019-01-01 04:00:00")
		),
		Cakeday(
			user="******",
			date_time=utils.parse_datetime_string("2019-01-02 04:00:00")
		),
		Cakeday(
			user="******",
			date_time=utils.parse_datetime_string("2019-01-03 04:00:00")
		),
		Cakeday(
			user="******",
			date_time=utils.parse_datetime_string("2019-01-04 04:00:00")
		)
	]
	for cakeday in cakedays:
		database.add_cakeday(cakeday)

	utils.debug_time = utils.parse_datetime_string("2019-01-01 02:00:00")
	notifications.send_cakeday_notifications(reddit, database)
	assert len(reddit.sent_messages) == 0

	utils.debug_time = utils.parse_datetime_string("2019-01-01 06:00:00")
	notifications.send_cakeday_notifications(reddit, database)
	assert len(reddit.sent_messages) == 1
	assert database.get_cakeday("Watchful1").date_time == utils.parse_datetime_string("2020-01-01 04:00:00")

	utils.debug_time = utils.parse_datetime_string("2019-01-03 06:00:00")
	notifications.send_cakeday_notifications(reddit, database)
	assert len(reddit.sent_messages) == 3
	assert database.get_cakeday("Watchful1").date_time == utils.parse_datetime_string("2020-01-01 04:00:00")
	assert database.get_cakeday("Watchful2").date_time == utils.parse_datetime_string("2020-01-02 04:00:00")
	assert database.get_cakeday("Watchful4").date_time == utils.parse_datetime_string("2019-01-04 04:00:00")
示例#4
0
def test_get_reminders(database, reddit):
    utils.debug_time = utils.parse_datetime_string("2019-01-01 12:00:00")
    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body
    assert "You don't have any reminders." in result

    reminder1 = Reminder(
        source="https://www.reddit.com/message/messages/XXXXX",
        message="KKKKK",
        user="******",
        requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
        target_date=utils.parse_datetime_string("2019-01-04 05:00:00"))
    reminder2 = Reminder(
        source="https://www.reddit.com/message/messages/YYYYY",
        message="FFFFF",
        user="******",
        requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-02-05 07:00:00"))
    database.save_reminder(reminder1)
    database.save_reminder(reminder2)
    cakeday = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2019-05-05 15:25:17"))
    database.add_cakeday(cakeday)

    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "Click here to delete all your reminders" in result

    assert "Happy cakeday!" in result

    assert reminder1.source in result
    assert reminder1.message in result
    assert "01-04 05" in result

    assert reminder2.source in result
    assert reminder2.message in result
    assert "02-05 07" in result

    database.save_settings(
        UserSettings(user="******", timezone="America/Los_Angeles"))
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Your timezone is currently set to: `America/Los_Angeles`" in result
    assert "01-03 21" in result
    assert "02-04 23" in result
示例#5
0
    def get_pending_cakedays(self, count, timestamp):
        log.debug("Fetching pending cakedays")
        c = self.dbConn.cursor()
        results = []
        for row in c.execute(
                '''
			SELECT ID, CakedayDate, User
			FROM cakedays
			WHERE CakedayDate < ?
			ORDER BY CakedayDate ASC
			LIMIT ?
			''', (utils.get_datetime_string(timestamp), count)):
            cakeday = Cakeday(user=row[2],
                              date_time=utils.parse_datetime_string(row[1]),
                              db_id=row[0])
            results.append(cakeday)

        log.debug(f"Found cakedays: {len(results)}")
        return results
示例#6
0
    def get_cakeday(self, user):
        log.debug(f"Fetching cake by user: {user}")
        c = self.dbConn.cursor()
        c.execute(
            '''
			SELECT ID, CakedayDate, User
			FROM cakedays
			WHERE User = ?
			''', (user, ))

        result = c.fetchone()
        if result is None or len(result) == 0:
            log.debug("Cakeday not found")
            return None

        cakeday = Cakeday(user=result[2],
                          date_time=utils.parse_datetime_string(result[1]),
                          db_id=result[0])

        return cakeday
示例#7
0
def test_remove_all_reminders(database, reddit):
    utils.debug_time = utils.parse_datetime_string("2019-01-01 12:00:00")
    message = reddit_test.RedditObject(body=f"RemoveAll!", author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "Deleted" not in message.get_first_child().body

    reminder1 = Reminder(
        source="https://www.reddit.com/message/messages/XXXXX",
        message="KKKKK",
        user="******",
        requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
        target_date=utils.parse_datetime_string("2019-01-04 05:00:00"))
    reminder2 = Reminder(
        source="https://www.reddit.com/message/messages/YYYYY",
        message="FFFFF",
        user="******",
        requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-02-05 07:00:00"))
    reminder3 = Reminder(
        source="https://www.reddit.com/message/messages/ZZZZZ",
        message="JJJJJ",
        user="******",
        requested_date=utils.parse_datetime_string("2019-03-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-03-05 07:00:00"))
    database.save_reminder(reminder1)
    database.save_reminder(reminder2)
    database.save_reminder(reminder3)
    cakeday = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2019-05-05 15:25:17"))
    database.add_cakeday(cakeday)

    message = reddit_test.RedditObject(body=f"RemoveAll!", author="Watchful1")
    messages.process_message(message, reddit, database)
    body = message.get_first_child().body
    assert "Deleted **2** reminders." in body
    assert "Deleted cakeday reminder." in body

    assert len(database.get_user_reminders("Watchful1")) == 0
    assert len(database.get_user_reminders("Watchful2")) == 1