<commit_before># Amara, universalsubtitles.org
#
# Copyright (C) 2015 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

"""utils.test_utils.monkeypatch -- Patch functions with the mock library

This module patches various functions that we don't want running during the
unittests.  We patch for a couple reasons

    - We don't want to make network requests during the tests
    - Operations related to search indexing are slow and not usually needed
      for the tests.

The objects used to patch the functions are available as module attributes.
They also have a run_original() method that can be used to run the original
function.  For example:

    > from utils.test_utils import monkeypatch
    > # run some code
    > monkeypatch.update_one_team_video.assert_called_with(...)
    > monkeypatch.update_one_team_video.run_original()
"""

import contextlib
import functools

from celery.task import Task
import mock

from subtitles.workflows import SaveDraft
import externalsites.google

save_thumbnail_in_s3 = mock.Mock()
video_changed_tasks = mock.Mock()
update_team_video = mock.Mock()
update_search_index = mock.Mock()

test_video_info = externalsites.google.VideoInfo(
    'test-channel-id', 'test-title', 'test-description', 60,
    'http://example.com/youtube-thumb.png')
youtube_get_video_info = mock.Mock(return_value=test_video_info)
youtube_get_user_info = mock.Mock(return_value=test_video_info)
youtube_get_new_access_token = mock.Mock(return_value='test-access-token')
youtube_revoke_auth_token = mock.Mock()
youtube_update_video_description = mock.Mock()
youtube_get_uploaded_video_ids = mock.Mock(return_value=[])
url_exists = mock.Mock(return_value=True)

current_locks = set()
acquire_lock = mock.Mock(
    side_effect=lambda c, name: current_locks.add(name))
release_lock = mock.Mock(
    side_effect=lambda c, name: current_locks.remove(name))
invalidate_widget_video_cache = mock.Mock()
update_subtitles = mock.Mock()
delete_subtitles = mock.Mock()
update_all_subtitles = mock.Mock()
fetch_subs_task = mock.Mock()
import_videos_from_feed = mock.Mock()
get_language_facet_counts = mock.Mock(return_value=([], []))<commit_msg>Fix unittest patching for applock<commit_after># Amara, universalsubtitles.org
#
# Copyright (C) 2015 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

"""utils.test_utils.monkeypatch -- Patch functions with the mock library

This module patches various functions that we don't want running during the
unittests.  We patch for a couple reasons

    - We don't want to make network requests during the tests
    - Operations related to search indexing are slow and not usually needed
      for the tests.

The objects used to patch the functions are available as module attributes.
They also have a run_original() method that can be used to run the original
function.  For example:

    > from utils.test_utils import monkeypatch
    > # run some code
    > monkeypatch.update_one_team_video.assert_called_with(...)
    > monkeypatch.update_one_team_video.run_original()
"""

import contextlib
import functools

from celery.task import Task
import mock

from subtitles.workflows import SaveDraft
import externalsites.google

save_thumbnail_in_s3 = mock.Mock()
video_changed_tasks = mock.Mock()
update_team_video = mock.Mock()
update_search_index = mock.Mock()

test_video_info = externalsites.google.VideoInfo(
    'test-channel-id', 'test-title', 'test-description', 60,
    'http://example.com/youtube-thumb.png')
youtube_get_video_info = mock.Mock(return_value=test_video_info)
youtube_get_user_info = mock.Mock(return_value=test_video_info)
youtube_get_new_access_token = mock.Mock(return_value='test-access-token')
youtube_revoke_auth_token = mock.Mock()
youtube_update_video_description = mock.Mock()
youtube_get_uploaded_video_ids = mock.Mock(return_value=[])
url_exists = mock.Mock(return_value=True)

current_locks = set()
acquire_lock = mock.Mock(
    side_effect=lambda c, name, timeout=None: current_locks.add(name))
release_lock = mock.Mock(
    side_effect=lambda c, name: current_locks.remove(name))
invalidate_widget_video_cache = mock.Mock()
update_subtitles = mock.Mock()
delete_subtitles = mock.Mock()
update_all_subtitles = mock.Mock()
fetch_subs_task = mock.Mock()
import_videos_from_feed = mock.Mock()
get_language_facet_counts = mock.Mock(return_value=([], []))
{'fraction_non_alphanumeric': 0.0823278921220724, 'fraction_numerical': 0.0035486160397444995, 'mean_word_length': 5.208149779735683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3495038', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1649, 'n_words': 549}