예제 #1
0
    def test_get_reminders_ok(self) -> None:
        """ Test the function that obtains the list of reminders of a user, with success. """

        # Prepare the mocks
        reminder_1 = models.Reminder(10, 1, 1)
        reminder_1.id = 1

        reminder_2 = models.Reminder(50, 2, 1)
        reminder_2.id = 2

        db_calls_mock.get_reminders_user.return_value = [reminder_1, reminder_2]

        response_reminder: response_models.Reminder = unittest.mock.MagicMock()
        response_reminder.anticipation_minutes = 10

        show_session = models.ShowSession(None, None, datetime.datetime(2020, 1, 1), 5, 10)
        channel = models.Channel(None, 'Channel Name')

        show_data = models.ShowData('_Show_Name_', 'Show Name')
        show_data.is_movie = True

        complete_session = (show_session, channel, show_data)

        show_session_2 = models.ShowSession(None, None, datetime.datetime(2020, 2, 2), 15, 20)
        channel_2 = models.Channel(None, 'Channel Name 2')

        show_data_2 = models.ShowData('_Show_Name_2_', 'Show Name 2')
        show_data_2.is_movie = False

        complete_session_2 = (show_session_2, channel_2, show_data_2)

        db_calls_mock.get_show_session_complete.side_effect = [complete_session, complete_session_2]

        # Call the function
        actual_result = reminders.get_reminders(self.session, 1)

        # Verify the result
        self.assertEqual(2, len(actual_result))

        self.assertEqual(1, actual_result[0].id)
        self.assertEqual('Show Name', actual_result[0].title)
        self.assertEqual(datetime.datetime(2020, 1, 1), actual_result[0].date_time)
        self.assertEqual(10, actual_result[0].anticipation_minutes)

        self.assertEqual(2, actual_result[1].id)
        self.assertEqual('Show Name 2', actual_result[1].title)
        self.assertEqual(datetime.datetime(2020, 2, 2), actual_result[1].date_time)
        self.assertEqual(50, actual_result[1].anticipation_minutes)

        # Verify the calls to the mocks
        db_calls_mock.get_reminders_user.assert_called_with(self.session, 1)
        db_calls_mock.get_show_session_complete.assert_has_calls(
            [unittest.mock.call(self.session, 1), unittest.mock.call(self.session, 2)])
예제 #2
0
    def test_update_reminder_ok(self) -> None:
        """ Test the function update_reminder with success. """

        # The expected result
        expected_result = True, None

        # Prepare the mocks
        reminder = models.Reminder(60, 15, 1)
        reminder.id = 123

        db_calls_mock.get_reminder_id_user.return_value = reminder

        show_session = models.ShowSession(None, None, datetime.datetime.utcnow() + datetime.timedelta(hours=5), 1, 1)
        db_calls_mock.get_show_session.return_value = show_session

        db_calls_mock.update_reminder.return_value = True

        # Call the function
        actual_result = reminders.update_reminder(self.session, 1, 120, 1)

        # Verify the result
        self.assertEqual(expected_result, actual_result)

        # Verify the calls to the mocks
        db_calls_mock.get_reminder_id_user.assert_called_with(self.session, 1, 1)
        db_calls_mock.get_show_session.assert_called_with(self.session, 15)
        db_calls_mock.update_reminder.assert_called()
예제 #3
0
    def post(self):

        User = users.get_current_user()
        if User:
            student = db.GqlQuery("SELECT * FROM Student WHERE user = :1",
                                  User).get()

            #logging.warning("going to get credentials")
            #credentials = decorator.credentials
            #logging.warning("going to refresh credentials")
            #credentials.refresh(http=decorator.http())
            #logging.warning("refreshed credentials")

            postData = json.loads(self.request.body)
            logging.warning(postData)

            newReminder = models.Reminder(parent=models.student_key(User))

            newReminder.type = postData['type']
            newReminder.title = postData['title']
            newReminder.course_str = postData['course_str']
            newReminder.completed = False
            newReminder.date = postData['date']
            newReminder.start_time = postData['start_time']
            newReminder.end_time = postData['end_time']
            newReminder.course = postData['course']
            newReminder.note = postData['note']
            newReminder.id = int(time.time())  # do we need to rand here too?
            newReminder.eventid = ""
            newReminder.eventseq = -1
            newReminder.alert_min = int(postData['alert_min'])
            newReminder.deleted = False
            newReminder.semester_id = "SP13"  # eventually not hardcoded

            if postData['add_to_cal'] == True:
                newReminder.add_to_cal = True
                event = utils.createReminderEvent(postData)
                logging.warning(event)
                request = service.events().insert(calendarId=student.calID,
                                                  body=event)
                response = request.execute(http=decorator.http())

                if response is not None and response != "":
                    logging.warning(json.dumps(response))
                    newReminder.eventid = response["id"]
                    newReminder.eventseq = response["sequence"]
            else:
                newReminder.add_to_cal = False

            newReminder.put()
            logging.warning("added reminder to db")

            self.response.out.write(json.dumps(models.serialize(newReminder)))
        else:
            self.response.out.write("['auth':'fail']")
예제 #4
0
파일: fx.py 프로젝트: karimkohel/Kiwi
def setReminder(intent):
    reminders = models.Reminder.readReminders("reminder.p")
    speech.speak(intent)
    text = speech.takeCommand()
    speech.speak("when is this reminder due")
    dueDate = speech.takeCommand()
    reminder = models.Reminder(text, dueDate)
    speech.speak("Reminder set for " + reminder.dueDate)
    if speech.confirmCommand():
        reminders.append(reminder)
        models.Reminder.writeReminders(reminders, "reminder.p")
        speech.speak("Ok, done")
    else:
        speech.speak("You didn't confirm, canceling command")
예제 #5
0
def run2(db):
    events = get_all_users_new_events(db)
    p = db.session.query(models.Reminder).count() + 1
    for (user, new_events_list) in events:
        add_new_events_to_calendar(db, user, new_events_list)
        for new_event in new_events_list:
            newEvent = models.Event(new_event['name'],
                                    new_event['rsvp_status'], user.fb_id,
                                    new_event['id'], new_event['start_time'],
                                    new_event['end_time'])
            db.session.add(newEvent)
            db.session.commit()
            if user.remind_by_default == True:
                r = models.Reminder(get_reminder_time(user, newEvent), p,
                                    new_event['id'], user.fb_id,
                                    user.remind_type)
                db.session.add(r)
                db.session.commit()
예제 #6
0
    def test_update_reminder_error_02(self) -> None:
        """ Test the function update_reminder with anticipation in less than two hours from airing. """

        # The expected result
        expected_result = False, 'Invalid anticipation time'

        # Prepare the mocks
        reminder = models.Reminder(60, 15, 1)
        reminder.id = 123

        db_calls_mock.get_reminder_id_user.return_value = reminder

        show_session = models.ShowSession(None, None, datetime.datetime.utcnow() + datetime.timedelta(hours=1), 1, 1)
        db_calls_mock.get_show_session.return_value = show_session

        # Call the function
        actual_result = reminders.update_reminder(self.session, 1, 60, 1)

        # Verify the result
        self.assertEqual(expected_result, actual_result)

        # Verify the calls to the mocks
        db_calls_mock.get_reminder_id_user.assert_called_with(self.session, 1, 1)
        db_calls_mock.get_show_session.assert_called_with(self.session, 15)
예제 #7
0
    def test_register_reminder_ok(self) -> None:
        """ Test the function that register a reminder, with success. """

        # Prepare the mocks
        show_session = models.ShowSession(None, None, datetime.datetime.utcnow() + datetime.timedelta(hours=4), 1, 1)
        db_calls_mock.get_show_session.return_value = show_session

        reminder = models.Reminder(60, 1, 1)
        reminder.id = 123

        db_calls_mock.register_reminder.return_value = reminder

        # Call the function
        actual_result = reminders.register_reminder(self.session, 1, 60, 1)

        # Verify the result
        self.assertEqual(123, actual_result.id)
        self.assertEqual(60, actual_result.anticipation_minutes)
        self.assertEqual(1, actual_result.session_id)
        self.assertEqual(1, actual_result.user_id)

        # Verify the calls to the mocks
        db_calls_mock.get_show_session.assert_called_with(self.session, 1)
        db_calls_mock.register_reminder.assert_called_with(self.session, 1, 60, 1)
예제 #8
0
    def test_delete_old_sessions(self) -> None:
        """ Test the function delete_old_sessions. """

        # The expected result
        expected_result = 2

        # Prepare the mocks
        # Prepare the call to search_old_sessions
        show_session_1 = models.ShowSession(None, None,
                                            datetime.datetime(2020, 1, 1), 5,
                                            10)
        show_session_1.id = 1

        show_session_2 = models.ShowSession(1, 10,
                                            datetime.datetime(2020, 2,
                                                              2), 8, 25)
        show_session_2.id = 2

        db_calls_mock.search_old_sessions.return_value = [
            show_session_1, show_session_2
        ]

        # Prepare the call to get_reminders_session for session 1 and session 2
        reminder_1 = models.Reminder(10, 1, 7)
        reminder_1.id = 1

        reminder_2 = models.Reminder(50, 1, 4)
        reminder_2.id = 2

        reminders_session_1 = [reminder_1, reminder_2]
        reminders_session_2 = []

        db_calls_mock.get_reminders_session.side_effect = [
            reminders_session_1, reminders_session_2
        ]

        # Prepare the call to get_show_session_complete for session 1
        channel = models.Channel(None, 'Channel Name')

        show_data = models.ShowData('_Show_Name_', 'Show Name')
        show_data.is_movie = True

        complete_session = (show_session_1, channel, show_data)

        db_calls_mock.get_show_session_complete.return_value = complete_session

        # Prepare the call to get_user_id for reminder 1 and reminder 2
        user_7 = models.User('*****@*****.**', 'password', 'pt')
        user_7.id = 7

        user_4 = models.User('*****@*****.**', 'password', 'pt')
        user_4.id = 4

        db_calls_mock.get_user_id.side_effect = [user_7, user_4]

        # Prepare the call to send_deleted_sessions_email for user 7 and user 4
        process_emails_mock.send_deleted_sessions_email.return_value = True

        # Call the function
        start_datetime = datetime.datetime.utcnow() - datetime.timedelta(
            days=2)
        end_datetime = datetime.datetime.utcnow()
        channels = ['Odisseia']

        actual_result = get_file_data.delete_old_sessions(
            self.session, start_datetime, end_datetime, channels)

        # Verify the result
        self.assertEqual(expected_result, actual_result)

        # Verify the calls to the mocks
        db_calls_mock.search_old_sessions.assert_called_with(
            self.session, start_datetime, end_datetime, channels)
        db_calls_mock.get_reminders_session.assert_has_calls([
            unittest.mock.call(self.session, 1),
            unittest.mock.call(self.session, 2)
        ])
        db_calls_mock.get_show_session_complete.assert_called_with(
            self.session, 1)
        db_calls_mock.get_user_id.assert_has_calls([
            unittest.mock.call(self.session, 7),
            unittest.mock.call(self.session, 4)
        ])
        process_emails_mock.send_deleted_sessions_email.assert_has_calls([
            unittest.mock.call('*****@*****.**', unittest.mock.ANY),
            unittest.mock.call('*****@*****.**', unittest.mock.ANY)
        ])
        self.session.delete.assert_has_calls([
            unittest.mock.call(reminder_1),
            unittest.mock.call(reminder_2),
            unittest.mock.call(show_session_1),
            unittest.mock.call(show_session_2)
        ])
예제 #9
0
    def test_process_reminders_ok_02(self) -> None:
        """ Test the function that processes reminders, with success. """

        # Prepare the mocks
        now = datetime.datetime.utcnow()

        show_session_1 = models.ShowSession(None, None, now + datetime.timedelta(minutes=30), 5, 10)
        show_session_1.id = 1

        show_session_2 = models.ShowSession(2, 10, now + datetime.timedelta(minutes=100), 10, 15)
        show_session_2.id = 2

        show_session_3 = models.ShowSession(None, None, now + datetime.timedelta(days=15), 10, 10)
        show_session_3.id = 3

        channel_5 = models.Channel(None, 'Channel 5')
        channel_10 = models.Channel(None, 'Channel 10')

        show_data_10 = models.ShowData('Show 10', 'Show 10')
        show_data_10.is_movie = True

        show_data_15 = models.ShowData('Show 15', 'Show 15')
        show_data_15.is_movie = False

        reminder_1 = models.Reminder(60, 1, 1)
        reminder_session_1 = (reminder_1, show_session_1)

        reminder_2 = models.Reminder(120, 2, 2)
        reminder_session_2 = (reminder_2, show_session_2)

        reminder_3 = models.Reminder(60, 1, 2)
        reminder_session_3 = (reminder_3, show_session_1)

        # This session is still too far
        reminder_4 = models.Reminder(60, 3, 3)
        reminder_session_4 = (reminder_4, show_session_3)

        db_calls_mock.get_sessions_reminders.return_value = [reminder_session_1, reminder_session_2, reminder_session_3,
                                                             reminder_session_4]

        show_session_tuple_1 = (show_session_1, channel_5, show_data_10)
        show_session_tuple_2 = (show_session_2, channel_10, show_data_15)
        show_session_tuple_3 = (show_session_1, channel_5, show_data_10)

        db_calls_mock.get_show_session_complete.side_effect = [show_session_tuple_1, show_session_tuple_2,
                                                               show_session_tuple_3]

        db_calls_mock.get_user_id.side_effect = [models.User('*****@*****.**', 'password', 'pt'),
                                                 models.User('*****@*****.**', 'password', 'en'),
                                                 models.User('*****@*****.**', 'password', 'en')]

        process_emails_mock.send_alarms_email.side_effect = [True, True, True]

        # Call the function
        reminders.process_reminders(self.session)

        # Verify the calls to the mocks
        db_calls_mock.get_sessions_reminders.assert_has_calls(self.session)

        db_calls_mock.get_show_session_complete.assert_has_calls([unittest.mock.call(self.session, 1),
                                                                  unittest.mock.call(self.session, 2),
                                                                  unittest.mock.call(self.session, 1)])

        db_calls_mock.get_user_id.assert_has_calls([unittest.mock.call(self.session, 1),
                                                    unittest.mock.call(self.session, 2),
                                                    unittest.mock.call(self.session, 2)])

        process_emails_mock.set_language.assert_has_calls([unittest.mock.call('pt'), unittest.mock.call('en'),
                                                           unittest.mock.call('en')])

        # Given that it can't compare the object sent in the email
        self.assertEqual(3, process_emails_mock.send_reminders_email.call_count)