class TestMymovieSearch(unittest.TestCase):

    movie_name = 'Casablanca'
    movie_id = '1234567890'

    def setUp(self):
        self.client = OMDB()

    @mock.patch('requests.get')
    def test_search(self, mock_get):
        response_data = dict(Search=[dict(
            Title=self.movie_name,
            imdbID=self.movie_id,
        )])
        mock_response = MockResponse(json_data=response_data, status_code=200)
        mock_get.return_value = mock_response
        results = self.client.search(self.movie_name)
        self.assertListEqual(results, response_data.get('Search'), 'unexpected results')

    @mock.patch('mymovie.search.OMDB.search')
    def test_search_call(self, mock_search):
        self.client.search(self.movie_name)
        mock_search.assert_called_with(self.movie_name)

    @mock.patch('mymovie.search.OMDB.search')
    def test_search_cmdline(self, mock_search):
        main(['-s', self.movie_name])
        mock_search.assert_called_with(self.movie_name)

    @mock.patch('requests.get')
    def test_get(self, mock_get):
        response_data = dict(
            Title=self.movie_name,
            imdbID=self.movie_id,
        )
        mock_response = MockResponse(json_data=response_data, status_code=200)
        mock_get.return_value = mock_response
        results = self.client.get(movie_name=self.movie_name)
        self.assertDictEqual(results, response_data, 'unexpected results')

    @mock.patch('mymovie.search.OMDB.get')
    def test_get_name_call(self, mock_search):
        self.client.get(movie_name=self.movie_name)
        mock_search.assert_called_with(movie_name=self.movie_name)

    @mock.patch('mymovie.search.OMDB.get')
    def test_get_id_call(self, mock_search):
        self.client.get(movie_id=self.movie_id)
        mock_search.assert_called_with(movie_id=self.movie_id)

    @mock.patch('mymovie.search.OMDB.get')
    def test_get_id_cmdline(self, mock_search):
        main(['-i', self.movie_id])
        mock_search.assert_called_with(movie_id=self.movie_id)
class TestMymovieSearch(unittest.TestCase):

    movie_name = 'Casablanca'
    movie_id = 'tt0034583'

    def setUp(self):
        self.client = OMDB()

    def test_search_success(self):
        results = self.client.search(self.movie_name)
        self.assertIsInstance(results, list, '{0} is not a list'.format(results))
        self.assertGreater(
            len(results), 0, 'did not get results for "{0}" search'.format(self.movie_name))
        self.assertIn('Title', results[0], 'result has no title')
        self.assertIn('imdbID', results[0], 'result has no imdb ID')

    def test_get_name(self):
        results = self.client.get(movie_name=self.movie_name)
        self.assertIn('Title', results, 'result has no title')
        self.assertEqual(self.movie_name, results.get(
            'Title'), '"{0}" title does not match "{1}"'.format(results.get('Title'), self.movie_name))
        self.assertIn('imdbID', results, 'result has no imdb ID')
        self.assertEqual(self.movie_id, results.get(
            'imdbID'), '"{0}" title does not match "{1}"'.format(results.get('imdbID'), self.movie_id))

    def test_get_id(self):
        results = self.client.get(movie_id=self.movie_id)
        self.assertIn('Title', results, 'result has no title')
        self.assertEqual(self.movie_name, results.get(
            'Title'), '"{0}" title does not match "{1}"'.format(results.get('Title'), self.movie_name))
        self.assertIn('imdbID', results, 'result has no imdb ID')
        self.assertEqual(self.movie_id, results.get(
            'imdbID'), '"{0}" title does not match "{1}"'.format(results.get('imdbID'), self.movie_id))
Esempio n. 3
0
def main(argv=None):
    '''Command line options.'''

    program_name = os.path.basename(sys.argv[0])
    program_version = __version__

    program_longdesc = ''''''  # optional - give further explanation about what the program does
    program_license = ["Copyright 2015 {0}".format(__author__),
                       "Licensed under the GPLv3.0",
                       "http://www.gnu.org/licenses/gpl.txt",
                       ]

    if argv is None:
        argv = sys.argv[1:]
    try:
        # setup option parser
        parser = argparse.ArgumentParser(
            description='\n'.join(program_license), epilog=program_longdesc)
        parser.add_argument('--version', action='version', version=program_version)
        parser.add_argument(
            "-s", "--search", dest="search_term", help="movie to search for", type=str)
        parser.add_argument(
            "-i", "--imdb_id", dest="imdb_id", help="IMDB id for the movie", type=str)
        parser.add_argument(
            "-v", "--verbose", dest="verbose", help="be more verbose", action="store_true")

        # process options
        args = parser.parse_args(argv)

        search_service = OMDB()
        if args.search_term:
            results = search_service.search(args.search_term)
            for result in results:
                print('"{0}" ({1} {2}) - id: {3}'.format(
                    result.get('Title'),
                    result.get('Year'),
                    result.get('Type'),
                    result.get('imdbID'),
                ))
                if args.verbose:
                    print('{0}\n'.format(result))
        if args.imdb_id:
            result = search_service.get(movie_id=args.imdb_id)
            if result:
                print('"{0}" ({1}) - id: {2}'.format(
                    result.get('Title'),
                    result.get('Year'),
                    result.get('imdbID'),
                ))
                if args.verbose:
                    print('{0}\n'.format(result))
            else:
                print('No result')

    except Exception as e:
        indent = len(program_name) * " "
        sys.stderr.write(program_name + ": " + repr(e) + "\n")
        sys.stderr.write(indent + "  for help use --help")
        return 2