Beispiel #1
0
    def post(self, request, username):
        """
        **post** - запрос на добавление матча в историю пользователя
        """

        try:
            token = request.COOKIES['token']
        except KeyError:
            # **Возвращаемый результат**
            """
            Пользователь не авторизован

            - код 401 

            """
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        try:
            data = dict(request.data)
            movie_id = request.data['movie_id']
            quote = request.data['quote']
            subtitle_ids = data['subtitle_ids']
        except KeyError:
            """
            Невалидные данные о структуре Match, присланные клиентом

            - код 400 

            """
            return Response(status=status.HTTP_400_BAD_REQUEST)

        user_profile = UserProfileRepo().find_by_token(token)
        if user_profile is None:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        subtitle_repo = SubtitleRepo()
        
        subtitle_ids = [int(sub_id) for sub_id in subtitle_ids]

        movie = MovieRepo().get(movie_id)
        subtitles = [subtitle_repo.get(id) for id in subtitle_ids]
        match = Match(
            quote=quote,
            movie=movie,
            subtitles=subtitles
        )

        usecase = UpdateUserHistoryUsecase(user_profile, match, MatchRepo())
        usecase.execute()

        """
        Успешное выполнение дополнения истории пользователя:

        - код 200

        """
        return Response(status=status.HTTP_200_OK)
Beispiel #2
0
    def test_save__try_to_update_with_none_values(self):
        # Arrange
        movie_name = "Black swan"
        year = 2011
        director = "Darren Aronofsky"
        poster_url = "https://blackswan/poster"
        video_url = "https://blackswan/video"

        movie_orm = MovieORM.objects.create(
            title=movie_name,
            year=year,
            director=director,
            poster_url=poster_url,
            video_url=video_url
        )

        quote = "I am gonna die"
        start_time = time(hour=0, minute=0, second=37, microsecond=673000)
        end_time = time(hour=0, minute=0, second=40, microsecond=673000)

        subtitle_orm = SubtitleORM.objects.create(
            quote=quote,
            start_time=start_time,
            end_time=end_time,
            movie=movie_orm
        )

        expected_subtitle_data = {
            'quote': quote,
            'start_time': start_time,
            'end_time': end_time,
            'movie_id': movie_orm.id
        }


        # Act
        subtitle_repo = SubtitleRepo()

        existing_subtitle = subtitle_repo.get(subtitle_orm.id)
        existing_subtitle.quote = None
        existing_subtitle.movie = None
        actual_subtitle = subtitle_repo.save(existing_subtitle)

        actual_subtitle_data = {
            'quote': actual_subtitle.quote,
            'start_time': actual_subtitle.start_time,
            'end_time': actual_subtitle.end_time,
            'movie_id': actual_subtitle.movie.id
        }

        # Assert
        self.assertDictEqual(expected_subtitle_data, actual_subtitle_data)
Beispiel #3
0
    def test_get_subtitle(self):
        # Arrange
        quote = "I am a new one"
        subtitle_orm = SubtitleORM.objects.create(quote = quote,
                                                  start_time = datetime.now(),
                                                  end_time = datetime.now(),
                                                  movie = self.movie_orm)
        id = subtitle_orm.id
        expected_subtitle = {'id': id, 'quote': quote}
        subtitle_repo = SubtitleRepo()

        # Act
        subtitle = subtitle_repo.get(id)
        actual_subtitle = {'id': subtitle.id, 'quote': subtitle.quote}

        # Assert
        self.assertDictEqual(expected_subtitle, actual_subtitle)