<commit_before>from marvin.models import Movie
from marvin.tests import TestCaseWithTempDB

from mock import Mock, patch

class OMDBFetchTest(TestCaseWithTempDB):

    def setUp(self):
        # We can't import the tasks module until create_app has been called,
        # which is why we do it down here
        from marvin.tasks import external_search, find_cover_art, find_cover_art_for_movie
        self.external_search = external_search
        self.find_cover_art = find_cover_art
        self.find_cover_art_for_movie = find_cover_art_for_movie


    def test_query_omdb(self):
        # Supply a mock of requests
        attrs = {
            'json.return_value': {
                u'Search': [
                    {
                        u'imdbID': u'tt0109179',
                        u'Year': u'1998',
                        u'Type': u'movie',
                        u'Title': u"Ava's Magical Adventure"
                    },
                    {
                        u'imdbID': u'tt0798009',
                        u'Year': u'2004',
                        u'Type': u'episode',
                        u'Title': u'Ava Gardner: Another Touch of Venus'
                    },
                    {
                        u'imdbID': u'tt1548011',
                        u'Year': u'2008',
                        u'Type': u'movie',
                        u'Title': u'Stalking Ava Gardner'
                    },
                    {
                        u'imdbID': u'tt0363371',
                        u'Year': u'1975',
                        u'Type': u'series',
                        u'Title': u'Signora Ava'
                    }
                ]
            },
            'status_code': 200,
        }
        response = Mock(**attrs)
        requests = Mock(**{'get.return_value': response})

        # pylint: disable=multiple-statements
        with patch('marvin.tasks.requests', requests), self.app.test_request_context():
            self.external_search('ava')
        with self.app.test_request_context():
            # we expect the 'series'-type to be ignored
            self.assertEqual(len(Movie.query.all()), 3)


    def test_no_duplicate_movies(self):
        movie = Movie(
            title='The Hobbit: The Desolation of Smaug',
            external_id='imdb:tt1170358',
        )
        self.addItems(movie)
        attrs = {
            'json.return_value': {
                "Search": [
                    {
                        "Title": "The Hobbit: An Unexpected Journey",
                        "Type": "movie",
                        "Year": "2012",
                        "imdbID": "tt0903624"
                    },
                    {
                        "Title": "The Hobbit: The Desolation of Smaug",
                        "Type": "movie",
                        "Year": "2013",
                        "imdbID": "tt1170358"
                    }
                ]
            },
            'status_code': 200,
        }
        response = Mock(**attrs)
        requests = Mock(**{'get.return_value': response})

        # pylint: disable=multiple-statements
        with patch('marvin.tasks.requests', requests), self.app.test_request_context():
            self.external_search('the hobbit')
            self.assertEqual(len(Movie.query.all()), 2)


    def test_image_fetching(self):
        movie = Movie(
            title='The Hobbit: The Desolation of Smaug',
            external_id='imdb:tt1170358'
        )
        movie_id = self.addItems(movie)
        attrs = {
            'json.return_value': {
                "Actors": "<NAME>, <NAME>, <NAME>, <NAME>",
                "Director": "<NAME>",
                "Genre": "Adventure, Drama, Fantasy",
                "Plot": "The Dwarves, Bilbo and Gandalf have successfully escaped the Misty Mountains, and Bilbo " +
                    "has gained the One Ring. They all continue their journey to get their gold back from the " +
                    "Dragon, Smaug.",
                "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMjMzMzAxOV5BMl5BanBnXkFtZTcwNTU3NzU2OQ@@._V1_SX300.jpg", # pylint: disable=line-too-long
                "Rated": "N/A",
                "Released": "13 Dec 2013",
                "Response": "True",
                "Runtime": "N/A",
                "Title": "The Hobbit: The Desolation of Smaug",
                "Type": "N/A",
                "Writer": "<NAME>, <NAME>",<commit_msg>Patch cover image search when testing external_search<commit_after>from marvin.models import Movie
from marvin.tests import TestCaseWithTempDB

from mock import Mock, patch

class OMDBFetchTest(TestCaseWithTempDB):

    def setUp(self):
        # We can't import the tasks module until create_app has been called,
        # which is why we do it down here
        from marvin.tasks import external_search, find_cover_art, find_cover_art_for_movie
        self.external_search = external_search
        self.find_cover_art = find_cover_art
        self.find_cover_art_for_movie = find_cover_art_for_movie


    def test_query_omdb(self):
        # Supply a mock of requests
        attrs = {
            'json.return_value': {
                u'Search': [
                    {
                        u'imdbID': u'tt0109179',
                        u'Year': u'1998',
                        u'Type': u'movie',
                        u'Title': u"Ava's Magical Adventure"
                    },
                    {
                        u'imdbID': u'tt0798009',
                        u'Year': u'2004',
                        u'Type': u'episode',
                        u'Title': u'Ava Gardner: Another Touch of Venus'
                    },
                    {
                        u'imdbID': u'tt1548011',
                        u'Year': u'2008',
                        u'Type': u'movie',
                        u'Title': u'Stalking Ava Gardner'
                    },
                    {
                        u'imdbID': u'tt0363371',
                        u'Year': u'1975',
                        u'Type': u'series',
                        u'Title': u'<NAME>'
                    }
                ]
            },
            'status_code': 200,
        }
        response = Mock(**attrs)
        requests = Mock(**{'get.return_value': response})

        # pylint: disable=multiple-statements
        patch_requests = patch('marvin.tasks.requests', requests)
        patch_cover_art = patch('marvin.tasks.find_cover_art', Mock())
        with patch_requests, patch_cover_art, self.app.test_request_context():
            self.external_search('ava')
        with self.app.test_request_context():
            # we expect the 'series'-type to be ignored
            self.assertEqual(len(Movie.query.all()), 3)


    def test_no_duplicate_movies(self):
        movie = Movie(
            title='The Hobbit: The Desolation of Smaug',
            external_id='imdb:tt1170358',
        )
        self.addItems(movie)
        attrs = {
            'json.return_value': {
                "Search": [
                    {
                        "Title": "The Hobbit: An Unexpected Journey",
                        "Type": "movie",
                        "Year": "2012",
                        "imdbID": "tt0903624"
                    },
                    {
                        "Title": "The Hobbit: The Desolation of Smaug",
                        "Type": "movie",
                        "Year": "2013",
                        "imdbID": "tt1170358"
                    }
                ]
            },
            'status_code': 200,
        }
        response = Mock(**attrs)
        requests = Mock(**{'get.return_value': response})

        # pylint: disable=multiple-statements
        patch_requests = patch('marvin.tasks.requests', requests)
        patch_cover_art = patch('marvin.tasks.find_cover_art', Mock())
        with patch_requests, patch_cover_art, self.app.test_request_context():
            self.external_search('the hobbit')
            self.assertEqual(len(Movie.query.all()), 2)


    def test_image_fetching(self):
        movie = Movie(
            title='The Hobbit: The Desolation of Smaug',
            external_id='imdb:tt1170358'
        )
        movie_id = self.addItems(movie)
        attrs = {
            'json.return_value': {
                "Actors": "<NAME>, <NAME>, <NAME>, <NAME>",
                "Director": "<NAME>",
                "Genre": "Adventure, Drama, Fantasy",
                "Plot": "The Dwarves, Bilbo and Gandalf have successfully escaped the Misty Mountains, and Bilbo " +
                    "has gained the One Ring. They all continue their journey to get their gold back from the " +
                    "Dragon, Smaug.",
                "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMjMzMzAxOV5BMl5BanBnXkFtZTcwNTU3NzU2OQ@@._V1_SX300.jpg", # pylint: disable=line-too-long
                "Rated": "N/A",
                "Released": "13 Dec 2013",
                "Response": "True",
                "Runtime": "N/A",
                "Title": "The Hobbit: The Desolation of Smaug",
                "Type": "N/A",
                "Writer": "<NAME>, <NAME>",
{'fraction_non_alphanumeric': 0.11117130783400152, 'fraction_numerical': 0.022320944847762487, 'mean_word_length': 1.3727506426735219, 'pattern_counts': {'":': 42, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '210566', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2468, 'n_words': 550}