Ejemplo n.º 1
0
    def test_uniqueness(self):
        """
        Tests if the uniqueness of the model is handled properly
        :return: None
        """
        media_item, media_id = self.generate_sample_media_id()
        media_item_two = MediaItem(
            media_type=MediaType.MANGA,
            media_subtype=MediaSubType.MANGA,
            english_title="Don't Fly Me to the Moon",
            romaji_title="ATonikaku Cawaii",
            cover_url="https://s4.anilist.co/file/anilistcdn/media/manga/"
            "cover/medium/nx101177-FjjD5UWB3C3t.png",
            latest_release=None,
            releasing_state=ReleasingState.RELEASING)
        db.session.add(media_item_two)
        db.session.commit()

        standard_kwargs = media_id.__json__(False)
        standard_kwargs.pop("id")
        standard_kwargs["service"] = media_id.service
        standard_kwargs["media_type"] = media_id.media_type
        standard_kwargs["media_subtype"] = media_id.media_subtype

        try:
            duplicate = MediaId(**standard_kwargs)
            db.session.add(duplicate)
            db.session.commit()
            self.fail()
        except IntegrityError:
            db.session.rollback()

        for key, value, error_expected in [
            ("media_item_id", media_item_two.id, True),
            ("service", ListService.KITSU, False), ("service_id", "100", True)
        ]:
            kwargs = dict(standard_kwargs)
            kwargs[key] = value
            try:
                generated = MediaId(**kwargs)
                db.session.add(generated)
                db.session.commit()
                if error_expected:
                    self.fail()
            except IntegrityError as e:
                db.session.rollback()
                print(key)
                if not error_expected:
                    raise e
Ejemplo n.º 2
0
 def generate_sample_guess() \
         -> Tuple[MediaItem, MediaId, MangaChapterGuess]:
     """
     Generates a sample chapter guess
     :return: The media item, media id and chapter guess
     """
     media_item = MediaItem(
         media_type=MediaType.MANGA,
         media_subtype=MediaSubType.MANGA,
         english_title="Fly Me to the Moon",
         romaji_title="Tonikaku Cawaii",
         cover_url="https://s4.anilist.co/file/anilistcdn/media/manga/"
                   "cover/medium/nx101177-FjjD5UWB3C3t.png",
         latest_release=None,
         releasing_state=ReleasingState.RELEASING
     )
     media_id = MediaId(
         media_item=media_item,
         service_id="101177",
         service=ListService.ANILIST,
         media_type=media_item.media_type,
         media_subtype=media_item.media_subtype
     )
     chapter_guess = MangaChapterGuess(
         media_id=media_id,
         guess=None,
         last_update=0
     )
     db.session.add(media_item)
     db.session.add(media_id)
     db.session.add(chapter_guess)
     db.session.commit()
     return media_item, media_id, chapter_guess
Ejemplo n.º 3
0
    def test_hashing(self):
        """
        Tests using the model objects as keys in a dictionary
        :return: None
        """
        media_item, media_id, chapter_guess = self.generate_sample_guess()

        media_id_kwargs = media_id.__json__(False)
        media_id_kwargs.pop("id")
        media_id_kwargs["service"] = ListService.ANIMEPLANET
        media_id_kwargs["media_type"] = media_id.media_type
        media_id_kwargs["media_subtype"] = media_id.media_subtype
        new_media_id = MediaId(**media_id_kwargs)
        db.session.add(new_media_id)
        db.session.commit()

        chapter_guess_2 = MangaChapterGuess(
            media_id=new_media_id,
            guess=100,
            last_update=0
        )
        db.session.add(chapter_guess_2)
        db.session.commit()
        mapping = {
            chapter_guess: 100,
            chapter_guess_2: 200
        }
        self.assertEqual(mapping[chapter_guess], 100)
        self.assertEqual(mapping[chapter_guess_2], 200)
Ejemplo n.º 4
0
 def test_equality(self):
     """
     Tests checking equality for model objects
     :return: None
     """
     media_item, media_id = self.generate_sample_media_id()
     media_id_2 = MediaId(media_item=media_item,
                          service_id="101178",
                          service=ListService.KITSU)
     db.session.add(media_id_2)
     db.session.commit()
     self.assertEqual(media_id, media_id)
     self.assertNotEqual(media_id, media_id_2)
     self.assertNotEqual(media_id, 100)
Ejemplo n.º 5
0
 def test_hashing(self):
     """
     Tests using the model objects as keys in a dictionary
     :return: None
     """
     media_item, media_id = self.generate_sample_media_id()
     media_id_2 = MediaId(media_item=media_item,
                          service_id="101178",
                          service=ListService.KITSU)
     db.session.add(media_id_2)
     db.session.commit()
     mapping = {media_id: 100, media_id_2: 200}
     self.assertEqual(mapping[media_id], 100)
     self.assertEqual(mapping[media_id_2], 200)
Ejemplo n.º 6
0
 def generate_sample_media_list_item(
     self
 ) -> Tuple[MediaListItem, MediaList, MediaUserState, User, MediaItem,
            MediaId]:
     """
     Generates a media list item
     :return: The media list item, media list, media user state, user,
              media item and media id
     """
     user = self.generate_sample_user(True)[0]
     media_item = MediaItem(
         media_type=MediaType.MANGA,
         media_subtype=MediaSubType.MANGA,
         english_title="Fly Me to the Moon",
         romaji_title="Tonikaku Cawaii",
         cover_url="https://s4.anilist.co/file/anilistcdn/media/manga/"
         "cover/medium/nx101177-FjjD5UWB3C3t.png",
         latest_release=None,
         releasing_state=ReleasingState.RELEASING)
     media_id = MediaId(media_item=media_item,
                        service_id="101177",
                        service=ListService.ANILIST,
                        media_type=media_item.media_type,
                        media_subtype=media_item.media_subtype)
     media_user_state = MediaUserState(
         media_id=media_id,
         user=user,
         progress=10,
         score=75,
         consuming_state=ConsumingState.CURRENT)
     media_list = MediaList(name="ABC",
                            user=user,
                            service=ListService.ANILIST,
                            media_type=MediaType.MANGA)
     media_list_item = MediaListItem(media_list=media_list,
                                     media_user_state=media_user_state)
     db.session.add(media_item)
     db.session.add(media_id)
     db.session.add(media_user_state)
     db.session.add(media_list)
     db.session.add(media_list_item)
     db.session.commit()
     return media_list_item, media_list, media_user_state, \
         user, media_item, media_id
Ejemplo n.º 7
0
    def test_equality(self):
        """
        Tests checking equality for model objects
        :return: None
        """
        _, media_id, chapter_guess = self.generate_sample_guess()

        media_id_kwargs = media_id.__json__(False)
        media_id_kwargs.pop("id")
        media_id_kwargs["service"] = ListService.ANIMEPLANET
        media_id_kwargs["media_type"] = media_id.media_type
        media_id_kwargs["media_subtype"] = media_id.media_subtype
        new_media_id = MediaId(**media_id_kwargs)
        db.session.add(new_media_id)
        db.session.commit()

        chapter_guess_2 = MangaChapterGuess(
            media_id=new_media_id,
            guess=100,
            last_update=0
        )
        self.assertEqual(chapter_guess, chapter_guess)
        self.assertNotEqual(chapter_guess, chapter_guess_2)
        self.assertNotEqual(chapter_guess, 100)
Ejemplo n.º 8
0
def do_media_item_inserts():
    """
    Preforms media item insertions
    :return: None
    """
    # Prepare existing DB content
    media_items: Dict[Tuple, MediaItem] = {
        x.identifier_tuple: x for x in
        MediaItem.query.all()
    }
    media_ids: List[MediaId] = MediaId.query\
        .options(db.joinedload(MediaId.media_item)) \
        .options(db.joinedload(MediaId.media_user_states)) \
        .all()
    media_service_items: \
        Dict[ListService, Dict[MediaType, Dict[str, MediaId]]] = \
        {x: {y: {} for y in MediaType} for x in ListService}
    for media_id in media_ids:
        service = media_id.service
        media_type = media_id.media_item.media_type
        service_id = media_id.service_id
        media_service_items[service][media_type][service_id] = media_id
    media_lists: Dict[Tuple, MediaList] = {
        x.identifier_tuple: x for x in
        MediaList.query.options(db.joinedload(MediaList.media_list_items))
        .all()
    }

    # Add media list items, ids and user states
    while len(media_item_queue) > 0:
        queued = media_item_queue.pop(0)
        params, service, service_ids, user_state_params, list_params = queued

        service_id = service_ids.get(service)
        if service_id is None:
            app.logger.error(f"Missing service ID for service {service}")

        media_type = params["media_type"]

        app.logger.debug(f"Inserting {params['romaji_title']} into database")

        if service_id in media_service_items[service]:
            media_id = media_service_items[service][media_type][service_id]
            media_item = media_id.media_item
            media_item.update(MediaItem(**params))
        else:
            generated = MediaItem(**params)
            identifier = generated.identifier_tuple
            existing = media_items.get(identifier)
            if existing is None:
                media_item = generated
                db.session.add(media_item)
                media_items[identifier] = media_item
            else:
                media_item = existing
                media_item.update(generated)

        # Insert Media IDs
        for extra_service, extra_id in service_ids.items():

            associated = media_service_items[extra_service][media_type].get(
                extra_id
            )

            if associated is None:
                media_id = MediaId(
                    media_item=media_item, service=extra_service,
                    service_id=extra_id
                )
                db.session.add(media_id)
                db.session.commit()
                media_service_items[extra_service][media_type][extra_id] \
                    = media_id
            elif associated.media_item.id != media_item.id:
                associated.media_item_id = media_item.id

        # Insert User States
        if user_state_params is not None:
            media_id = media_service_items[service][media_type][service_id]
            user_state_params["media_id_id"] = media_id.id
            generated = MediaUserState(**user_state_params)
            existing_user_states = {
                x.user_id: x for x in media_id.media_user_states
            }
            user_id = user_state_params["user_id"]
            existing_user_state = existing_user_states.get(user_id)

            if existing_user_state is None:
                db.session.add(generated)
                user_state = generated
                db.session.commit()
            else:
                existing_user_state.update(generated)
                user_state = existing_user_state

            if list_params is not None:
                generated = MediaList(**list_params)
                identifier = generated.identifier_tuple
                existing_list = media_lists.get(identifier)

                if existing_list is None:
                    db.session.add(generated)
                    db.session.commit()
                    media_list = generated
                    media_lists[identifier] = media_list
                else:
                    existing_list.update(generated)
                    media_list = existing_list

                media_list_item_ids = [
                    x.media_user_state_id for x in media_list.media_list_items
                ]
                if user_state.id not in media_list_item_ids:
                    list_item = MediaListItem(
                        media_list_id=media_list.id,
                        media_user_state_id=user_state.id
                    )
                    db.session.add(list_item)