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)])
Example #2
0
    def test_search_tmdb_match_01(self, tmdb_calls_mock) -> None:
        """ Test the function search_tmdb_match with a match on a query with year. """

        # The expected result
        expected_result = response_models.TmdbShow()
        expected_result.year = 2020
        expected_result.is_movie = True
        expected_result.id = 2
        expected_result.original_title = 'Original Title'
        expected_result.vote_average = 8.5
        expected_result.popularity = 130

        # Prepare the mocks
        # Prepare the call to search_shows_by_text
        tmdb_show_1 = response_models.TmdbShow()
        tmdb_show_1.year = 2020
        tmdb_show_1.is_movie = True
        tmdb_show_1.id = 1
        tmdb_show_1.original_title = 'Similar Title'
        tmdb_show_1.vote_average = 8.5
        tmdb_show_1.popularity = 130

        tmdb_calls_mock.search_shows_by_text.return_value = (1, [
            tmdb_show_1, expected_result
        ])

        # Prepare the call to get_show_crew_members for the show 1
        tmdb_calls_mock.get_show_crew_members.return_value = []

        # Call the function
        show_data = models.ShowData('_search_title', 'Localized Title')
        show_data.director = 'Director 1,Director 2'
        show_data.year = 2020
        show_data.genre = 'Documentary'
        show_data.original_title = 'Original Title'
        show_data.is_movie = True

        actual_result = get_file_data.search_tmdb_match(self.session,
                                                        show_data,
                                                        use_year=True)

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

        # Verify the calls to the mocks
        tmdb_calls_mock.search_shows_by_text.assert_called_with(
            self.session, 'Original Title', is_movie=True, year=2020)

        tmdb_calls_mock.get_show_crew_members.assert_called_with(1, True)
    def test_process_alarms_ok_02(self) -> None:
        """ Test the function process_alarms with an alarm that gets two matches. """

        # 1 - Prepare the mocks
        # The db_calls.get_user_id in process_alarms
        user = models.User('email', 'pasword', 'pt')
        user.id = 933

        db_calls_mock.get_user_id.return_value = user

        # The db_calls.get_alarms in process_alarms
        alarm = models.Alarm(None, 123, True,
                             response_models.AlarmType.DB.value, None, None,
                             933)

        db_calls_mock.get_alarms.return_value = [alarm]

        # The db_calls.get_show_titles in get_show_titles
        show_titles = models.ShowTitles(123, True, 'Title 1|Title 2')
        show_titles.insertion_datetime = datetime.datetime.utcnow(
        ) - datetime.timedelta(hours=3)

        db_calls_mock.get_show_titles.return_value = show_titles

        # The db_calls.get_last_update in process_alarms -> search_sessions_db_with_tmdb_id ->
        # get_last_update_alarms_datetime
        original_datetime = datetime.datetime(2020, 8, 1, 9)
        last_update = models.LastUpdate(None, original_datetime)

        db_calls_mock.get_last_update.return_value = last_update

        # The db_calls.search_show_sessions_data_with_tmdb_id in process_alarms -> search_sessions_db_with_tmdb_id
        channel = models.Channel('CH', 'Channel')
        channel.id = 76

        show_data = models.ShowData('search title', 'Título')
        show_data.id = 27
        show_data.tmdb_id = 123

        show_session = models.ShowSession(
            None, None, original_datetime + datetime.timedelta(days=2), 76, 27)
        show_session.update_timestamp = datetime.datetime.utcnow(
        ) + datetime.timedelta(hours=38)

        db_calls_mock.search_show_sessions_data_with_tmdb_id.return_value = [
            (show_session, channel, show_data)
        ]

        # The db_calls.get_user_excluded_channels in process_alarms -> search_sessions_db_with_tmdb_id
        channel_2 = models.Channel('CH2', 'Channel 2')
        channel_2.id = 981

        user_excluded_channel = models.UserExcludedChannel(933, 981)

        db_calls_mock.get_user_excluded_channels.return_value = [
            user_excluded_channel
        ]

        # The db_calls.get_last_update in process_alarms -> search_sessions_db -> get_last_update_alarms_datetime
        # has been done with return_value

        # The db_calls.get_user_excluded_channels in process_alarms -> search_sessions_db
        db_calls_mock.get_user_excluded_channels.return_value = [
            user_excluded_channel
        ]

        # The db_calls.search_show_sessions_data in process_alarms -> search_sessions_db for 'Title 1'
        show_session_2 = models.ShowSession(
            None, None, original_datetime + datetime.timedelta(days=3), 76, 27)
        show_session_2.update_timestamp = datetime.datetime.utcnow(
        ) + datetime.timedelta(hours=38)

        db_calls_mock.search_show_sessions_data.side_effect = [[
            (show_session_2, channel, show_data)
        ], []]

        # The db_calls.search_show_sessions_data in process_alarms -> search_sessions_db
        # for 'Title 1' has been done with side_effect

        # The process_emails.set_language in process_alarms is void

        # The process_emails.send_alarms_email in process_alarms
        process_emails_mock.send_alarms_email.return_value = True

        # The db_calls.get_last_update in process_alarms has been done with return_value

        # The db_calls.commit in process_alarms
        db_calls_mock.commit.return_value = True

        # 2 - Call the function
        processing.process_alarms(self.session)

        # 3 - Verify the results
        self.assertTrue(last_update.alarms_datetime > original_datetime)

        # Verify the calls to the mocks
        db_calls_mock.get_user_id.assert_called_with(self.session, 933)

        db_calls_mock.get_alarms.assert_called_with(self.session)

        db_calls_mock.get_show_titles.assert_called_with(
            self.session, 123, True)

        db_calls_mock.get_last_update.assert_has_calls([
            unittest.mock.call(self.session),
            unittest.mock.call(self.session),
            unittest.mock.call(self.session)
        ])

        db_calls_mock.search_show_sessions_data_with_tmdb_id.assert_called_with(
            self.session,
            123,
            True,
            None,
            None,
            below_datetime=original_datetime)

        db_calls_mock.get_user_excluded_channels.assert_has_calls([
            unittest.mock.call(self.session, 933),
            unittest.mock.call(self.session, 933)
        ])

        db_calls_mock.search_show_sessions_data.assert_has_calls([
            unittest.mock.call(self.session,
                               '_Title_1_',
                               True,
                               None,
                               None,
                               False,
                               True,
                               below_datetime=original_datetime,
                               ignore_with_tmdb_id=True),
            unittest.mock.call(self.session,
                               '_Title_2_',
                               True,
                               None,
                               None,
                               False,
                               True,
                               below_datetime=original_datetime,
                               ignore_with_tmdb_id=True)
        ])

        process_emails_mock.set_language.assert_called_with('pt')

        send_alarms_email_calls = process_emails_mock.send_alarms_email.call_args_list

        self.assertEqual(1, len(send_alarms_email_calls))

        call_args, _ = send_alarms_email_calls[0]

        self.assertEqual('email', call_args[0])

        actual_results = call_args[1]

        self.assertEqual(2, len(actual_results))

        self.assertEqual(response_models.LocalShowResultType.TV,
                         actual_results[0].type)
        self.assertEqual('Título', actual_results[0].show_name)
        self.assertEqual('Channel', actual_results[0].service_name)
        self.assertEqual(original_datetime + datetime.timedelta(days=2),
                         actual_results[0].date_time)

        self.assertEqual(response_models.LocalShowResultType.TV,
                         actual_results[1].type)
        self.assertEqual('Título', actual_results[1].show_name)
        self.assertEqual('Channel', actual_results[1].service_name)
        self.assertEqual(original_datetime + datetime.timedelta(days=3),
                         actual_results[1].date_time)

        db_calls_mock.commit.assert_called_with(self.session)
    def test_get_response_highlights_week_ok(self) -> None:
        """ Test the function get_response_highlights_week. """

        # Prepare the mocks
        # Calls to get the highlights
        score_highlights = models.Highlights(models.HighlightsType.SCORE, 2021,
                                             2, [3], None)
        new_highlights = models.Highlights(models.HighlightsType.NEW, 2021, 2,
                                           [1, 2], [None, 10])

        db_calls_mock.get_week_highlights.side_effect = [
            score_highlights, new_highlights
        ]

        # Calls to get the shows
        show_3 = models.ShowData("_Programa_3_", "Programa 3")
        show_3.id = 3
        show_3.tmdb_id = 3792
        show_3.synopsis = "Synopsis 3"
        show_3.is_movie = True
        show_3.original_title = "Show 3"

        show_1 = models.ShowData("_Programa_1_", "Programa 1")
        show_1.id = 1
        show_1.tmdb_id = 84
        show_1.synopsis = "Synopsis 1"
        show_1.is_movie = False
        show_1.original_title = "Show 1"

        show_2 = models.ShowData("_Programa_2_", "Programa 2")
        show_2.id = 2
        show_2.tmdb_id = 1111
        show_2.synopsis = "Synopsis 2"
        show_2.is_movie = False
        show_2.original_title = "Show 2"

        db_calls_mock.get_show_data_id.side_effect = [show_3, show_1, show_2]

        # Calls to obtain the TMDB data for each of the shows
        tmdb_show_3 = response_models.TmdbShow()
        tmdb_show_3.id = 3792
        tmdb_show_3.vote_average = 5.5
        tmdb_show_3.vote_count = 25
        tmdb_show_3.popularity = 123
        tmdb_show_3.original_title = "Show 3"
        tmdb_show_3.title = "Show 3"
        tmdb_show_3.is_movie = True
        tmdb_show_3.overview = "Overview 3"
        tmdb_show_3.original_language = "en"
        tmdb_show_3.match_reason = "Name"

        tmdb_show = response_models.TmdbShow()
        tmdb_show.id = 84
        tmdb_show.vote_average = 7
        tmdb_show.vote_count = 50
        tmdb_show.popularity = 100
        tmdb_show.original_title = "Show 1"
        tmdb_show.title = "Show 1"
        tmdb_show.is_movie = False
        tmdb_show.overview = "Overview 1"
        tmdb_show.original_language = "en"
        tmdb_show.match_reason = "Name"

        tmdb_show_2 = response_models.TmdbShow()
        tmdb_show_2.id = 1111
        tmdb_show_2.vote_average = 5
        tmdb_show_2.vote_count = 33
        tmdb_show_2.popularity = 34
        tmdb_show_2.original_title = "Show 2"
        tmdb_show_2.title = "Show 2"
        tmdb_show_2.is_movie = False
        tmdb_show_2.overview = "Overview 2"
        tmdb_show_2.original_language = "en"
        tmdb_show_2.match_reason = "Name"

        tmdb_calls_mock.get_show_using_id.side_effect = [
            tmdb_show_3, tmdb_show, tmdb_show_2
        ]

        # Call the function
        actual_result = processing.get_response_highlights_week(
            self.session, 2021, 2)

        # Verify the calls to the mocks
        db_calls_mock.get_week_highlights.assert_has_calls([
            unittest.mock.call(self.session, models.HighlightsType.SCORE, 2021,
                               2),
            unittest.mock.call(self.session, models.HighlightsType.NEW, 2021,
                               2)
        ])

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

        tmdb_calls_mock.get_show_using_id.assert_has_calls([
            unittest.mock.call(self.session, 3792, True),
            unittest.mock.call(self.session, 84, False),
            unittest.mock.call(self.session, 1111, False)
        ])

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

        self.assertEqual("SCORE", actual_result[0].key)
        self.assertEqual(2021, actual_result[0].year)
        self.assertEqual(2, actual_result[0].week)

        expected_show_3 = {
            'is_movie': True,
            'show_title': 'Show 3',
            'show_year': None,
            'trakt_id': 3792,
            'show_overview': "Synopsis 3",
            'language': "en",
            'vote_average': 5.5,
            'vote_count': 25,
            'popularity': 123,
            'show_image': 'N/A',
            'match_reason': 'Name',
            'translated_title': 'Programa 3'
        }

        self.assertEqual(1, len(actual_result[0].show_list))
        self.assertEqual(expected_show_3, actual_result[0].show_list[0])

        self.assertEqual("NEW", actual_result[1].key)
        self.assertEqual(2021, actual_result[1].year)
        self.assertEqual(2, actual_result[1].week)

        self.assertEqual(2, len(actual_result[1].show_list))

        expected_show_1 = {
            'is_movie': False,
            'show_title': 'Show 1',
            'translated_title': 'Programa 1',
            'show_year': None,
            'trakt_id': 84,
            'show_overview': "Synopsis 1",
            'language': "en",
            'vote_average': 7,
            'vote_count': 50,
            'popularity': 100,
            'show_image': 'N/A',
            'match_reason': 'Name'
        }

        self.assertEqual(expected_show_1, actual_result[1].show_list[0])

        expected_show_2 = {
            'is_movie': False,
            'show_title': 'Show 2',
            'translated_title': 'Programa 2',
            'show_year': None,
            'trakt_id': 1111,
            'show_overview': "Synopsis 2",
            'language': "en",
            'vote_average': 5,
            'vote_count': 33,
            'popularity': 34,
            'show_image': 'N/A',
            'season_premiere': 10,
            'match_reason': 'Name'
        }

        self.assertEqual(expected_show_2, actual_result[1].show_list[1])
    def test_calculate_highlights_ok(self) -> None:
        """ Test the function calculate_highlights. """

        # Replace datetime class with a utility class with a fixed datetime
        datetime.date = NewDate

        # Prepare the mocks
        # Calls to check if the highlights already exist
        new_highlights_1 = models.Highlights(models.HighlightsType.NEW, 2021,
                                             10, [1, 2], [None, 10])

        score_highlights_2 = models.Highlights(models.HighlightsType.SCORE,
                                               2021, 11, [], None)

        db_calls_mock.get_week_highlights.side_effect = [
            None, new_highlights_1, score_highlights_2, None
        ]

        # Call to obtains the shows of the week, for updating
        show_data = models.ShowData('Show 1', 'Show 1')
        show_data.tmdb_id = 1234
        show_data.tmdb_vote_average = 3
        show_data.tmdb_vote_count = 25
        show_data.is_movie = True
        show_data.year = 2020
        show_data.id = 189

        show_data_2 = models.ShowData('Show 2', 'Show 2')
        show_data_2.tmdb_id = 6789
        show_data_2.tmdb_vote_average = 6
        show_data_2.tmdb_vote_count = 25
        show_data_2.is_movie = True
        show_data_2.year = 2011
        show_data_2.id = 2

        show_data_3 = models.ShowData('Show 3', 'Show 3')
        show_data_3.tmdb_id = 1271
        show_data_3.tmdb_vote_average = 6
        show_data_3.tmdb_vote_count = 25
        show_data_3.is_movie = True
        show_data_3.year = 2004
        show_data_3.id = 3

        show_data_4 = models.ShowData('Show 4', 'Show 4')
        show_data_4.tmdb_id = 1274
        show_data_4.tmdb_vote_average = 5.4
        show_data_4.tmdb_vote_count = 25
        show_data_4.is_movie = False
        show_data_4.year = 2004
        show_data_4.id = 46
        show_data_4.season_premiere = 3

        db_calls_mock.get_shows_interval.side_effect = [[
            show_data, show_data_2, show_data_3, show_data_4
        ]]

        # Calls to obtain the TMDB data for each of the shows
        tmdb_show = response_models.TmdbShow()
        tmdb_show.vote_average = 7
        tmdb_show.popularity = 100
        tmdb_show.vote_count = 25
        tmdb_show.id = 1234

        tmdb_show_2 = response_models.TmdbShow()
        tmdb_show_2.vote_average = 5
        tmdb_show_2.popularity = 34
        tmdb_show_2.vote_count = 25
        tmdb_show_2.id = 6789

        tmdb_show_3 = response_models.TmdbShow()
        tmdb_show_3.vote_average = 5.5
        tmdb_show_3.popularity = 123
        tmdb_show_3.vote_count = 25
        tmdb_show_3.id = 1274

        tmdb_calls_mock.get_show_using_id.side_effect = [
            tmdb_show, tmdb_show_2, tmdb_show_3
        ]

        # Calls to get the highest scored shows
        db_calls_mock.get_highest_scored_shows_interval.side_effect = [[
            (189, 1234, 7)
        ], [(46, 1274, 5.5)]]

        # Calls to get the new shows
        db_calls_mock.get_new_shows_interval.side_effect = [[
            (55, 42, datetime.date(2021, 3, 10), 5)
        ], []]

        # Calls to register highlights
        new_score_highlights = models.Highlights(models.HighlightsType.SCORE,
                                                 2021, 10, [189, 46], None)

        new_new_highlights = models.Highlights(models.HighlightsType.NEW, 2021,
                                               11, [55], [5])

        db_calls_mock.register_highlights.side_effect = [
            new_score_highlights, new_new_highlights
        ]

        # Call the function
        processing.calculate_highlights(self.session)

        # Verify the calls to the mocks
        db_calls_mock.get_week_highlights.assert_has_calls([
            unittest.mock.call(self.session, models.HighlightsType.SCORE, 2021,
                               10),
            unittest.mock.call(self.session, models.HighlightsType.NEW, 2021,
                               10),
            unittest.mock.call(self.session, models.HighlightsType.SCORE, 2021,
                               11),
            unittest.mock.call(self.session, models.HighlightsType.NEW, 2021,
                               11)
        ])

        db_calls_mock.get_shows_interval.assert_called_with(
            self.session, datetime.datetime(2021, 3, 8),
            datetime.datetime(2021, 3, 14, 23, 59, 59))

        tmdb_calls_mock.get_show_using_id.assert_has_calls([
            unittest.mock.call(self.session, 1234, True),
            unittest.mock.call(self.session, 6789, True),
            unittest.mock.call(self.session, 1274, False)
        ])

        db_calls_mock.get_highest_scored_shows_interval.assert_has_calls([
            unittest.mock.call(self.session, datetime.datetime(2021, 3, 8),
                               datetime.datetime(2021, 3, 14, 23, 59, 59),
                               True),
            unittest.mock.call(self.session, datetime.datetime(2021, 3, 8),
                               datetime.datetime(2021, 3, 14, 23, 59, 59),
                               False)
        ])

        db_calls_mock.get_new_shows_interval.assert_has_calls([
            unittest.mock.call(self.session, datetime.datetime(2021, 3, 15),
                               datetime.datetime(2021, 3, 21, 23, 59, 59),
                               True),
            unittest.mock.call(self.session, datetime.datetime(2021, 3, 15),
                               datetime.datetime(2021, 3, 21, 23, 59, 59),
                               False)
        ])

        db_calls_mock.register_highlights.assert_has_calls([
            unittest.mock.call(self.session, models.HighlightsType.SCORE, 2021,
                               10, [189, 46]),
            unittest.mock.call(self.session, models.HighlightsType.NEW, 2021,
                               11, [55], [5])
        ])
Example #6
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)
        ])
    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)
Example #8
0
    def test_Odisseia_add_file_data(self) -> None:
        """
        Test the function Odisseia.add_file_data with a new session of a show with a matching channel correction.
        An old event was added to show that nothing changes and it is ignored.
        """

        # Prepare the mocks
        # Replace datetime class with a utility class with a fixed datetime
        datetime.datetime = NewDatetime

        # Prepare the call to get_channel_name
        channel_data = models.Channel('Acronym', 'Channel Name')
        channel_data.id = 8373

        db_calls_mock.get_channel_name.return_value = channel_data

        # Prepare the call to search_channel_show_data
        channel_show_data = models.ChannelShowData(8373, 2, False,
                                                   'Attack and Defend',
                                                   'Ataque e Defesa')
        channel_show_data.show_id = 51474

        db_calls_mock.search_channel_show_data_correction.return_value = channel_show_data

        # Prepare the call to get_show_data_id
        show_data = models.ShowData('Search Title', 'Localized Title')
        show_data.id = 51474

        db_calls_mock.get_show_data_id.return_value = show_data

        # Prepare the call to search_existing_session
        db_calls_mock.search_existing_session.return_value = None

        # Prepare the call to register_show_session
        show_session = models.ShowSession(
            1, 5, datetime.datetime(2021, 3, 19, 5, 15, 16), 8373, 51474)

        db_calls_mock.register_show_session.return_value = show_session

        # Prepare the call to search_old_sessions
        db_calls_mock.search_old_sessions.return_value = []

        # Call the function
        actual_result = file_parsers.odisseia.Odisseia.add_file_data(
            self.session, base_path + 'data/odisseia_example.xml', 'Odisseia')

        # Get back the datetime.datetime
        datetime.datetime = self.datetime_backup

        # Verify the result
        self.assertEqual(datetime.datetime(2021, 3, 19, 5, 10, 16),
                         actual_result.start_datetime)
        self.assertEqual(datetime.datetime(2021, 3, 19, 5, 20, 16),
                         actual_result.end_datetime)
        self.assertEqual(1, actual_result.total_nb_sessions_in_file)
        self.assertEqual(0, actual_result.nb_updated_sessions)
        self.assertEqual(1, actual_result.nb_added_sessions)
        self.assertEqual(0, actual_result.nb_deleted_sessions)

        # Verify the calls to the mocks
        db_calls_mock.get_channel_name.assert_called_with(
            self.session, 'Odisseia')

        db_calls_mock.search_channel_show_data_correction.assert_called_with(
            self.session,
            8373,
            False,
            'Attack and Defend',
            'Ataque e Defesa',
            directors=['Seaton McLean'],
            year=2015,
            subgenre='Natureza',
            creators=None)

        db_calls_mock.get_show_data_id.assert_called_with(self.session, 51474)

        db_calls_mock.search_existing_session.assert_called_with(
            self.session, 1, 5, datetime.datetime(2021, 3, 19, 5, 15, 16),
            8373, 51474)

        db_calls_mock.register_show_session.assert_called_with(
            self.session,
            1,
            5,
            datetime.datetime(2021, 3, 19, 5, 15, 16),
            8373,
            51474,
            audio_language=None,
            extended_cut=False,
            should_commit=False)

        db_calls_mock.search_old_sessions.assert_called_with(
            self.session, datetime.datetime(2021, 3, 19, 5, 10, 16),
            datetime.datetime(2021, 3, 19, 5, 20, 16), ['Odisseia'])