def test_using_different_channel_when_timezone_present(self, channel):
        with patch('helga_reminders.db') as db:
            with patch('helga_reminders.reactor'):
                args = ['13:00', 'EST', 'on', '#foo', 'this is a message']
                db.reminders.insert.return_value = 1

                # Account for UTC difference
                with freeze_time(self.now + datetime.timedelta(hours=5)):
                    reminders.at_reminder(self.client, '#bots', 'me', args)

                rec = db.reminders.insert.call_args[0][0]
                assert rec['channel'] == '#foo'
                assert rec['message'] == 'this is a message'
    def test_invalid_days_returns_warning(self, reactor, db):
        # Invalid chars
        args = ['6:00', 'US/Central', 'this is a message', 'repeat', 'XYZ']
        assert "I didn't understand" in reminders.at_reminder(
            self.client, '#bots', 'me', args)
        assert not db.reminders.insert.called
        assert not reactor.callLater.called

        # No chars
        args = ['6:00', 'US/Central', 'this is a message', 'repeat', '']
        assert "I didn't understand" in reminders.at_reminder(
            self.client, '#bots', 'me', args)
        assert not db.reminders.insert.called
        assert not reactor.callLater.called
    def test_no_tz_no_repeat_in_past(self, reactor, db):
        args = ['6:00', 'this is a message']
        db.reminders.insert.return_value = 1

        # Account for UTC difference
        with freeze_time(self.now + datetime.timedelta(hours=5)):
            reminders.at_reminder(self.client, '#bots', 'me', args)

        rec = db.reminders.insert.call_args[0][0]
        expect = self.now + datetime.timedelta(hours=18)

        # 'when' will be stored as UTC timestamp
        when = rec['when'].astimezone(self.tz).replace(tzinfo=None)

        assert 'repeat' not in rec
        assert when == expect
        assert rec['channel'] == '#bots'
        assert rec['message'] == 'this is a message'
        reactor.callLater.assert_called_with(18 * 3600, reminders._do_reminder,
                                             1, self.client)
    def test_tz_repeat_in_future(self, reactor, db):
        args = ['13:00', 'US/Central', 'this is a message', 'repeat', 'MWF']
        db.reminders.insert.return_value = 1

        # Account for UTC difference
        with freeze_time(self.now + datetime.timedelta(hours=6)):
            reminders.at_reminder(self.client, '#bots', 'me', args)

        rec = db.reminders.insert.call_args[0][0]
        expect = self.now + datetime.timedelta(hours=1)

        # 'when' will be stored as UTC timestamp
        when = rec['when'].astimezone(
            pytz.timezone('US/Central')).replace(tzinfo=None)

        assert when == expect
        assert rec['channel'] == '#bots'
        assert rec['message'] == 'this is a message'
        assert rec['repeat'] == [0, 2, 4]
        reactor.callLater.assert_called_with(1 * 3600, reminders._do_reminder,
                                             1, self.client)