Beispiel #1
0
    def test_save__new(self):
        # Arrange
        expected_movie_data = {
            'title': self.title,
            'year': self.year,
            'director': self.director,
            'poster_url': self.poster_url,
            'video_url': self.video_url
        }

        # Act
        movie = Movie(title=self.title,
                      year=self.year,
                      director=self.director,
                      poster_url=self.poster_url,
                      video_url=self.video_url)

        movie_repo = MovieRepo()
        actual_movie = movie_repo.save(movie)
        actual_movie_data = {
            'title': actual_movie.title,
            'year': actual_movie.year,
            'director': actual_movie.director,
            'poster_url': actual_movie.poster_url,
            'video_url': actual_movie.video_url
        }

        # Assert
        self.assertDictEqual(expected_movie_data, actual_movie_data)
Beispiel #2
0
    def test_save__try_to_update_with_none_values(self):
        #
        #   In database fields with new values which are equal to None
        #   should not be overwritten because of the fact, that client tends to
        #   send back to server only a small part of object's data, such as ID.

        # Arrange
        movie_orm = MovieORM.objects.create(title=self.title,
                                            year=self.year,
                                            director=self.director,
                                            poster_url=self.poster_url,
                                            video_url=self.video_url)

        existed_movie = MovieRepo().get(movie_orm.id)

        expected_movie_data = {
            'title': self.title,
            'year': self.year,
            'director': self.director,
            'poster_url': self.poster_url,
            'video_url': self.video_url
        }

        # Act
        movie_repo = MovieRepo()

        existed_movie.title = None
        existed_movie.year = None
        existed_movie.director = None
        existed_movie.poster_url = None

        actual_movie = movie_repo.save(existed_movie)

        actual_movie_data = {
            'title': actual_movie.title,
            'year': actual_movie.year,
            'director': actual_movie.director,
            'poster_url': actual_movie.poster_url,
            'video_url': actual_movie.video_url
        }

        # Assert
        self.assertDictEqual(expected_movie_data, actual_movie_data)

        # Chech immutabilty direct in orm object
        actual_movie_orm = MovieORM.objects.get(pk=movie_orm.id)
        actual_movie_orm_data = {
            'title': actual_movie_orm.title,
            'year': actual_movie_orm.year,
            'director': actual_movie_orm.director,
            'poster_url': actual_movie_orm.poster_url,
            'video_url': actual_movie_orm.video_url
        }

        self.assertDictEqual(expected_movie_data, actual_movie_orm_data)
Beispiel #3
0
    def test__deserialize_movie_and_update_from_repo(self):
        # Arrange
        incomplete_movie_json = f'{{"id": "{self.movie_1.id}", \
"title": "{self.movie_1.title}", \
"director": "{self.movie_1.director}", \
"url": "{self.movie_1.video_url}"}}'

        # Act
        movie_serializer = MovieSerializer()
        incomplete_movie = movie_serializer.deserialize(incomplete_movie_json)

        movie_repo = MovieRepo()
        actual_movie = movie_repo.save(incomplete_movie)

        # Assert
        compare(self.movie_1, actual_movie)
Beispiel #4
0
    def test_save__update(self):
        # Arrange
        movie_orm = MovieORM.objects.create(title=self.title,
                                            year=self.year,
                                            director=self.director,
                                            poster_url=self.poster_url,
                                            video_url=self.video_url)

        existed_movie = MovieRepo().get(movie_orm.id)

        title_new_value = 'The Departed'
        year_new_value = 2006
        director_new_value = 'Martin Scorsese'

        expected_movie_data = {
            'title': title_new_value,
            'year': year_new_value,
            'director': director_new_value,
            'poster_url': self.poster_url,
            'video_url': self.video_url
        }

        # Act
        existed_movie.title = title_new_value
        existed_movie.year = year_new_value
        existed_movie.director = director_new_value

        movie_repo = MovieRepo()
        actual_movie = movie_repo.save(existed_movie)
        actual_movie_data = {
            'title': actual_movie.title,
            'year': actual_movie.year,
            'director': actual_movie.director,
            'poster_url': actual_movie.poster_url,
            'video_url': actual_movie.video_url
        }

        self.assertDictEqual(expected_movie_data, actual_movie_data)
class Command(BaseCommand):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # pylint: disable-msg=C0103
        self.HOST = 'http://movie-quotes.ru'
        self.STATIC_PATH = '/static/'

        self._movie_repo = MovieRepo()
        self._subtitle_repo = SubtitleRepo()

    def handle(self, *args, **options):
        # pylint: disable-msg=C0103
        # ---------------------- The Lord Of the Rings 2001 ------------------------
        movie_the_lord_of_the_rings_2001 = Movie(
            title='The Lord of The Rings',
            year=2001,
            director='Peter Jackson',
            poster_url=self.HOST + self.STATIC_PATH +
            'the_lord_of_the_rings_2001/poster.jpg',
            video_url=self.HOST + self.STATIC_PATH +
            'the_lord_of_the_rings_2001/movie.mp4')
        dir_name = 'the_lord_of_the_rings_2001'
        self._populate_movie(
            movie_the_lord_of_the_rings_2001, dir_name,
            [self.HOST + self.STATIC_PATH + dir_name + '/' + 'sub.rus.srt'])

        # --------------------------- Forrest Gump 1994 ----------------------------
        movie_forrest_gump_1994 = Movie(
            title='Forrest Gump',
            year=1994,
            director='Robert Zemeckis',
            poster_url=self.HOST + self.STATIC_PATH +
            'forrest_gump_1994/poster.jpg',
            video_url=self.HOST + self.STATIC_PATH +
            'forrest_gump_1994/movie.mp4')
        dir_name = 'forrest_gump_1994'
        self._populate_movie(
            movie_forrest_gump_1994, dir_name,
            [self.HOST + self.STATIC_PATH + dir_name + '/' + 'sub.rus.srt'])

        # --------------------------- The Godfather 1972 ----------------------------
        movie_the_godfather_1972 = Movie(
            title='The Godfather',
            year=1972,
            director='Francis Ford Coppola',
            poster_url=self.HOST + self.STATIC_PATH +
            'the_godfather_1972/poster.jpg',
            video_url=self.HOST + self.STATIC_PATH +
            'the_godfather_1972/movie.mp4')
        dir_name = 'the_godfather_1972'
        self._populate_movie(movie_the_godfather_1972, dir_name, [
            self.HOST + self.STATIC_PATH + dir_name + '/' + 'sub_pt1.rus.srt',
            self.HOST + self.STATIC_PATH + dir_name + '/' + 'sub_pt2.rus.srt'
        ])

    def _download_files(self, tmp_dirname, urls: list) -> List[str]:
        '''
        Downloads files from given urls into temporary directory tmp_dirname.
        Returns list of filenames, which were download succesfully.
        '''
        downloaded_filepaths = []

        os.mkdir(tmp_dirname)
        self.stdout.write(f'created temporary directory: {tmp_dirname}')

        for url in urls:
            self.stdout.write(f'downloading file: {url} ...')
            filepath = tmp_dirname + '/' + url.split('/')[-1]
            urllib.request.urlretrieve(url, filepath)
            downloaded_filepaths.append(filepath)

        return downloaded_filepaths

    def _read_subtitles(self, movie_dirname,
                        subtitle_files_urls: list) -> List[Subtitle]:
        downloaded_files = self._download_files(movie_dirname,
                                                subtitle_files_urls)

        subtitles = []
        for filepath in downloaded_files:
            self.stdout.write(f'reading file with subtitles: {filepath} ...')
            subtitles_part = SubtitleParser.parse(filepath)
            subtitles += subtitles_part

        shutil.rmtree(movie_dirname)
        self.stdout.write(f'deleted temporary directory: {movie_dirname}')

        return subtitles

    def _populate_movie(self, movie, movie_dir_name, subtitle_files_urls):
        movie = self._movie_repo.save(movie)

        subtitles = self._read_subtitles(movie_dir_name, subtitle_files_urls)

        for sub in subtitles:
            sub.movie = movie
            self._subtitle_repo.save(sub)

        self._log(movie, len(subtitles))

    def _log(self, movie, subtitles_count):
        self.stdout.write('Added Movie:')
        self.stdout.write(f'\t{movie.title}\n' + f'\t{movie.year}\n' +
                          f'\t{movie.director}\n' + f'\t{movie.poster_url}\n' +
                          f'\t{movie.video_url}\n')
        self.stdout.write(f'\tsubtitles count {subtitles_count}')
        self.stdout.write('\n')