def _pack_subtitles_into_matches(self, subtitles) -> List[Match]:
        result_matches = []

        one_movie_subtitles = [subtitles[0]]
        for sub in subtitles:
            if sub == one_movie_subtitles[0]:
                continue

            if sub.movie.id == one_movie_subtitles[-1].movie.id:
                one_movie_subtitles.append(sub)
            else:
                match = Match(
                    movie=one_movie_subtitles[0].movie,
                    subtitles=one_movie_subtitles
                )
                result_matches.append(match)

                one_movie_subtitles = [sub]

        if len(one_movie_subtitles) != 0:
            match = Match(
                movie=one_movie_subtitles[0].movie,
                subtitles=one_movie_subtitles
            )

            result_matches.append(match)

        return result_matches
    def test__save__should_create_a_new_object(self):
        # Arrange
        match = Match(
            quote='test',
            user_profile=self.user_profile,
            movie=self.movie,
            subtitles=[
                self.sub_1,
                self.sub_2
            ]
        )

        expected_match_data = {
            'quote': 'test',
            'user_profile': self.user_profile,
            'movie': self.movie,
            'subtitles_ids': [self.sub_2, self.sub_1]
        }

        # Act
        match_repo = MatchRepo()
        actual_match = match_repo.save(match)
        actual_match_data = {
            'quote': actual_match.quote,
            'user_profile': actual_match.user_profile,
            'movie': actual_match.movie,
            'subtitles_ids': actual_match.subtitles
        }

        # Assert
        compare(expected_match_data, actual_match_data)
예제 #3
0
    def setUpTestData(cls):
        movie_orm_1 = MovieORM.objects.create(
            title='bomonka1', year=2001, director='me',
            poster_url='https://bmstu.ru',video_url='https://bmstu.ru/poster.png'
        )

        subtitle_orm1 = SubtitleORM.objects.create(quote = "Monday is a bad day!!!",
                                                   start_time = time(hour=0, minute=0, second=37, microsecond=673000),
                                                   end_time = time(hour=0, minute=0, second=39, microsecond=673000),
                                                   movie = movie_orm_1)
        subtitle_orm2 = SubtitleORM.objects.create(quote = "Also Monday is a good day",
                                                   start_time = time(hour=0, minute=1, second=37, microsecond=673000),
                                                   end_time = time(hour=0, minute=0, second=59, microsecond=673001),
                                                   movie = movie_orm_1)

        cls.movie_1 = MovieRepo().get(movie_orm_1.id)
        cls.sub_1 = SubtitleRepo().get(subtitle_orm1.id)
        cls.sub_2 = SubtitleRepo().get(subtitle_orm2.id)

        cls.match = Match(
            quote='test quote',
            movie=cls.movie_1,
            subtitles=[cls.sub_1, cls.sub_2]
        )

        user_orm = User.objects.create(username='******', email='*****@*****.**', password='******')
        cls.user_profile = UserProfileRepo().get(user_orm.id)
예제 #4
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)
    def test__execute__quote_Hello(self):
        # Arrange
        quote = "Hello"

        expected_matches = [
            Match(quote=quote,
                  movie=self.movie_1,
                  subtitles=[self.sub_1_movie_1, self.sub_3_movie_1]),
            Match(quote=quote,
                  movie=self.movie_2,
                  subtitles=[self.sub_5_movie_2])
        ]

        # Act
        match_repo = MatchRepo()
        subtitle_repo = SubtitleRepo()
        usecase = SearchByQuoteUsecase(match_repo, subtitle_repo, quote)
        actual_matches = usecase.execute()

        # Assert
        compare(expected_matches, actual_matches)
예제 #6
0
    def test__deserialize_match(self):
        # Arrange
        expected_match = Match(quote='Monday', movie=self.movie_1, subtitles=[self.sub_1, self.sub_2])

        match_json = f'{{"quote": "Monday", "movie_id": "{self.movie_1.id}", "subtitles": [\
{{ "id": "{self.sub_1.id}" }}, {{ "id": "{self.sub_2.id}" }} ]}}'

        # Act
        match_serializer = MatchSerializer()
        actual_match = match_serializer.deserialize(match_json)

        actual_match.movie = MovieRepo().get(actual_match.movie.id)
        actual_match.subtitles = [SubtitleRepo().get(sub.id) for sub in actual_match.subtitles]

        # Assert
        compare(expected_match, actual_match)
        def to_domain(match_orm: MatchORM) -> Match:
            movie_domain = MovieRepo.Mapper.to_domain(match_orm.movie)

            subtitles_domain = []
            for sub in match_orm.subtitles.all().order_by('-id'):
                subtitles_domain.append(SubtitleRepo.Mapper.to_domain(sub))

            user_profile = UserProfileRepo().get(
                match_orm.user_profile.user.id)

            match_domain = Match(id=match_orm.id,
                                 quote=match_orm.quote,
                                 user_profile=user_profile,
                                 movie=movie_domain,
                                 subtitles=subtitles_domain)

            return match_domain
    def test__execute__one_subtitle(self):
        # Arrange
        quote = "Joe Biden"

        expected_matches = [
            Match(quote=quote,
                  movie=self.movie_2,
                  subtitles=[self.sub_6_movie_2])
        ]

        # Act
        match_repo = MatchRepo()
        subtitle_repo = SubtitleRepo()
        usecase = SearchByQuoteUsecase(match_repo, subtitle_repo, quote)
        actual_matches = usecase.execute()

        # Assert
        compare(expected_matches, actual_matches)
    def deserialize(self, json_match) -> Match:
        dictionary = json.loads(json_match)

        movie_serializer = MovieSerializer()
        subtitle_serializer = SubtitleSerializer()

        movie = {'id': dictionary['movie_id']}

        movie = movie_serializer.deserialize(movie)

        subtitles = []
        for sub in dictionary['subtitles']:
            subtitle_entity = subtitle_serializer.deserealize(sub)
            subtitles.append(subtitle_entity)

        match = Match(quote=dictionary['quote'],
                      movie=movie,
                      subtitles=subtitles)

        return match
    def test__Mapper__from_domain(self):
        # Arrange
        expected_match_orm = MatchORM.objects.create(
            movie=self.movie_orm,
            user_profile=self.user_profile_orm
        )
        expected_match_orm.subtitles.set([self.sub_orm_1, self.sub_orm_2])
        expected_match_orm.save()

        match_domain = Match(
            id=1,
            user_profile=self.user_profile,
            movie=self.movie, 
            subtitles=[self.sub_1, self.sub_2]
        )

        # Act
        actual_match_orm = MatchRepo().Mapper.from_domain(match_domain)

        # Assert
        compare(expected_match_orm, actual_match_orm)
예제 #11
0
    def save(self, match: Match) -> Match:
        """
        Saves given match object if it exists in database,
        othervise creates a new
        """
        if match.id is None:
            saved_match = self._create(match)
        else:
            if MatchORM.objects.filter(pk=match.id).exists():
                match.movie = MovieRepo().save(match.movie)

                subtitle_repo = SubtitleRepo()
                for sub in match.subtitles:
                    sub = subtitle_repo.save(sub)

                match_orm = MatchORM.objects.get(pk=match.id)
                match_orm.quote = set_if_not_none(match_orm.quote, match.quote)
                match_orm.save()
            else:
                saved_match = self._create(match)

        return saved_match
    def test__Mapper__to_domain(self):
       # Arrange
        match_orm = MatchORM.objects.create(
            quote='test',
            movie=self.movie_orm,
            user_profile=self.user_profile_orm
        )
        match_orm.subtitles.set([self.sub_orm_2, self.sub_orm_1])
        match_orm.save()

        expected_match = Match(
            id=match_orm.id,
            quote='test',
            user_profile=self.user_profile,
            movie=self.movie,
            subtitles=[self.sub_2, self.sub_1]
        )

        # Act
        actual_match = MatchRepo.Mapper.to_domain(match_orm)

        # Assert
        compare(expected_match, actual_match)